newbie question : mouse interaction & point drawing

Hi all,
I'm really newbie to openGL.I tried to write a very simple piece of code but have huge problems.The code has to do the following: I create a window, when the user left clicks , a point is immediately drawn under the mouse cursor.Here is the code :


[code]
#include
#include
#include
using namespace std;

void display (void){
glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0,1.0,1.0);
glFlush();

}



void drawSquare(int x, int y) {
y = 250-y;
x = x-250;

glPointSize(10);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POINTS);
glVertex2f(x , y);
glEnd();
glutSwapBuffers();
//glFlush();
}





void mouse(int bin, int state , int x , int y) {
if(bin == GLUT_LEFT_BUTTON && state == GLUT_DOWN) drawSquare(x,y);
}


void init (void)
{
/* select clearing (background) color */
glClearColor (1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

glViewport( 0,0, 500, 500 );
glMatrixMode( GL_PROJECTION );
glOrtho( 0.0, 500.0, 0.0, 500.0, 1.0, -1.0 );

/* initialize viewing values */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

/*Main*/

int main (int argc,char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(500,500);
/*Set the posotion of window*/
glutInitWindowPosition(0,0);
glutCreateWindow("My Vindow");
glutDisplayFunc(display);
glutMouseFunc(mouse);
init();
glutMainLoop();


}

[/code]


But I have strange problems .When I click I get the coordinates , but the clicked point is not drawn immediately.I clcik 8 or 9 time to draw a point .I think it's due to glFlush() or glutSwapBuffers().I read their manual pages but I think I'm doing sth wrong.
I also have one more question : How can write a code to do this by calling glutPostRedisplay() inside mouse function?? I tried it , but it was no good. Any help is appreciated.:))
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories