Search Projects

Thursday, December 31, 2015

Cursor Position OpenGL Code Blocks Example

In the previous post in our blog, we have posted about OpenGL mouse position, in similar way today's post we are going to have tutorial about Cursor Position in OpenGL.

Cursor Position OpenGL Code Blocks Example

Do you Know about Code::Blocks?

There are many IDE for C/C+, Like same Code::Blocks is one of the IDE for C/C++/FORTRAN. Built around a plugin framework, Code::Blocks can be extended with plugins. Any kind of functionality can be added by installing/coding a plugin. For instance, compiling and debugging functionality is already provided by plugins!

Cursor Position OpenGL

The coordinate of screen in the OpenGL window starts from upper left corner and we determined the mouse position in same we determine the cursor position in OpenGL. In this post wear going to learn how to get cursor position in screen coordinates. This example is done with Code::Blocks, but can be run on different platforms with little/no modification.

In this post we are going to learn an OpenGL Code Blocks Example of determining the cursor position.

Objective of Program  : Show the position of cursor in c++ using the OpenGL graphics library.

Step By Step Process

Step 1: Create variable that will determine the both X, Y Coordinate of cursor as well as the windows. The code for this as given below - 

GLint x = 10;
GLint y = 20;
GLint WindowWidth = 800;
GLint WindowHight = 600; 

You may change the values!

Step 2 : In each of the OpenGL program we have used the init and Display functions. Hence also we are going to use the same OpenGL Code Blocks Example, with minimal code. These codes are needed in almost all the programs. Find out the use of each functions at In our display function we are going to fix the size of the point as well. The Display function will enable us to draw a small square where the cursor position id determined. 

void init()
    glClearColor(1, 1, 1, 0);
    gluOrtho2D(0, WindowWidth, 0, WindowHight);
void display()

Create a Mouse Function

Step 3 : In our mouse function we will determine the cursor position with the left mouse button.  We will check the status of left mouse button, whether it is pressed down or not. Hence we will draw a point when the left mouse button have the status of it's state=down. This determine the coordinate of screen and we know the position of cursor.

As we know that in opengl the coordinate are from upper left corner, so we need to anti alias the y coordinate. Rest of the logic is simple take the position of x and y and create the point there with specified color.

void mouseHandler(int button, int state, int mouse_x, int mouse_y)
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
        x = mouse_x;
        y = WindowHight - mouse_y;
        // Set line segment color as glColor3f(R,G,B)
        glColor3f(0.5, 0.5, 0.9);
        glVertex2i(x, y);
        // Clear display window

Step 4 : Main is to be code which is same for the program as all other programs have.

We hope you liked this OpenGL Code Blocks Example, put your suggestion, opinions in the comment below. Watch video below!

No comments:

Post a Comment