copenglmouseeventscreenonmouseclick

Mouse click changing the screen face in OpenGL


I'm programming a code that when the user click on the screen It marks a point and when he continues clicking on other points on the screen It'll be linking the points with lines.

I don't know why but I'm working with two different screen plan. When I click with the mouse It marks a point in the screen that I only see when I'm with the left mouse button clicked and when the button isn't clicked I see a clean screen.

My code that marks the points and link them with lines:

void exerciseThree(int x, int y){
 write("Exercise Three", -5, 18);

 float cx = 0, cy = 0;

 if(x != 0 && y != 0 && isFinished == false){
     glPushMatrix(); 
        glPointSize(6.0f);
        //Draw the points
        glBegin(GL_POINTS);
            cx = ((x * (maxx - minx)) / width) + minx;
            cy = (((height - y) * (maxy - miny)) / height) + miny;
            glVertex2f(cx , cy);
        glEnd();

        if(lastCx != 0 && lastCy != 0){
            glBegin(GL_LINE_STRIP);
            glVertex2f(lastCx , lastCy);
            glVertex2f(cx , cy);
            glEnd();      
        }

        lastCx = cx;
        lastCy = cy;    
     glPopMatrix();
 }

 write("Press 0 (zero) to come back.", -10, -18);
}

The mouse function:

void mouse(int button, int state, int x, int y){
 switch(button){
       case GLUT_LEFT_BUTTON:
            if( option == 3){
                 exerciseThree(x, y);
                 projecao();
                 glutSwapBuffers();
            }

       break;         
 }    
}

I know that I should handle the GLUT_DOWN and GLUT_UP of the mouse, but does exist a way of work with only one screen face?


Solution

  • You're only seeing something on the screen when you click because that's the only time you are drawing to and updating the buffer. You should just update the list of x/y coordinates when the mouse is clicked. However, you should draw the points and call glutSwapBuffers() every time in your main loop so that they are always on screen regardless of a button press.

    The flow should be something like the following pseudo code:

    ArrayOfPoints[];
    
    while(Running)
    {
        CheckForMouseInput(); // Update array of points to draw if pressed
    
        DrawPoints(); // Use same code draw code from exerciseThree()
    
        glutSwapBuffers(); // Update video buffer
    }