• Advertisement
Sign in to follow this  

Draw a point on mouse click

This topic is 2564 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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();
}


and my glutMouseFunc is button
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();
}
}
}


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
Advertisement
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="1"]. And I assume you would want to use the vertex data in the future anyway

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement