Sign in to follow this  
lordmonkey

Draw a point on mouse click

Recommended Posts

Hello everyone,
I have (I think) an easy problem with drawing a point while left mouse button is being pressed. My code looks like this :
my glutDrawFunc is draw
[code]void draw(void)
{
//clear the current window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//make changes to the modelview matrix
glMatrixMode(GL_MODELVIEW);
//initialise the modelview matrix to the identity matrix
glLoadIdentity();

// ........... draw something

//flush what we've drawn to the buffer
glFlush();
//swap the back buffer with the front buffer
glutSwapBuffers();
}[/code]

and my glutMouseFunc is button
[code]void button(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
if(button == GLUT_LEFT_BUTTON)
{
glColor3f( 1, 0, 0 );

glMatrixMode(GL_MODELVIEW);
glBegin(GL_POINTS);
glVertex2f( x, y);
glEnd();

//flush what we've drawn to the buffer
//glFlush();

//swap the back buffer with the front buffer
//glutSwapBuffers();
}
}
}[/code]

but obviously this does not work. If I uncomment glFlush() and glutSwapBuffers() I have some kind of 2 buffers and even clicks points are on one and odd mouse clicks are on the second one.
SHould I put some code in idle() function ?

Share this post


Link to post
Share on other sites
I assume you came from DOS programming and ega/vga's putpixel stuff.

openGL works differently: you should decouple the logic from rendering.

You have to store the info in the main memory: have an array that stores the coordinates, that's clicked. And just store them! Don't display there. Display the points in 'draw' by reading the coordinates from the said array.[size="1"]

a quick solution would be to use single buffering and glFlush, but as i stated, it's better to learn decoupling right away[/size][size="1"]. And I assume you would want to use the vertex data in the future anyway[/size]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this