Archive Pages Design$type=blogging

Cursor Position OpenGL Code Blocks Example

In the previous post in our blog, we have posted about OpenGL mouse position , in similar way today...

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 OpenGL.org. 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);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0, WindowWidth, 0, WindowHight);
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(20);
    glFlush();
}

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);
        glBegin(GL_POINTS);
        glVertex2i(x, y);
        glEnd();
        glFlush();
        // Clear display window
        glClear(GL_COLOR_BUFFER_BIT);
    }
}

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!

COMMENTS

Name

3D Bi-Cycle 3D Game 3D Laptop 3D Objects 3D Projects 3D Zoo Algorithm Android Aquarium Bellman-Ford Algorithm Block Breaker Blockshooting Bucket Sort C# Chess Clock Code Blocks Colors Complex Project Complex Projects Crab Pong Cross Zero DSAV Escapa First Come First Serve Fluids Font Demo Games Gear Motion GlutDino Glutplane Graphics Editor iOS iPhone Java Java OpenGL Graphics Programming Light Torus Linked List Ludo Board Game Mancala board games Memory blocks game Memory Game Menu Mickey Mouse Minesweeper Miniature Steam Engine moth Motion Blur MoveLight moving car Moving Ship Multiplex Networking Based Project Nuclear Power Plant Olympic opengl c++ examples OpenGL ES OpenGL Tutorial Origami OS Based Projects Paint Paper Folding Particles Drop Path Finding ping pong Pong Game Project Report Projects Report Puzzles qix like Random Flowing lines Rigid Body Rings Robot sample c++ opengl code Screen Saver Screen Saver Ship Shadow Cube Shadowfun Ship Iceberg Simple Drawing Simple Haloed Lined Wireframe Simple Move Light Simple Project Simulation SNAKE XENZIA GAME Solar system Sorting Sphere Spot Light Swing Stars Taj Mahal The Edge Game Tic Tac Toe Tower of Hanoi Traffic Signal Transformation Triangular Animation Tutorial water ripple effects Whirlpool
false
ltr
item
OpenGL Projects: Cursor Position OpenGL Code Blocks Example
Cursor Position OpenGL Code Blocks Example
http://3.bp.blogspot.com/-xkv66c-AoUA/VoVh-G0P33I/AAAAAAAAHiE/HWF9FSycktI/s320/Cursor%2BPosition%2BOpenGL%2BCode%2BBlocks%2BExample.png
http://3.bp.blogspot.com/-xkv66c-AoUA/VoVh-G0P33I/AAAAAAAAHiE/HWF9FSycktI/s72-c/Cursor%2BPosition%2BOpenGL%2BCode%2BBlocks%2BExample.png
OpenGL Projects
http://www.openglprojects.in/2015/12/cursor-position-opengl-code-blocks-example.html
http://www.openglprojects.in/
http://www.openglprojects.in/
http://www.openglprojects.in/2015/12/cursor-position-opengl-code-blocks-example.html
true
2020194800836807725
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago