Home > database >  drawing program with glut/openGL not working
drawing program with glut/openGL not working

Time:09-27

This program is able to create a window and locate my mouse when it is on the window, however the display function does seem to work. What am I missing here?

float pointSize = 10.0f;
bool leftBottState;
float xvalue;
float yvalue;


void init()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
}

void dots()
{
    glBegin(GL_POINTS);
    glVertex3f(xvalue, yvalue, 0.0);

}


void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1, 0, 1);
    dots();
    glEnd();
    glFlush();
    

}
void mouse(int button, int state, int x, int y)
{
    y = 600 - y;
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        leftBottState = true;
    }
    else
    {
        leftBottState = false;
    }
}

void motion(int x, int y) 
{   
    if (leftBottState) 
    {
        std::cout << "LEFt bUTTON " << std::endl;
        xvalue = x;
        yvalue = y;
        glutPostRedisplay();
    }
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize(600, 600);
    glutCreateWindow("test");   
    glutMotionFunc(motion);
    glutDisplayFunc(display);   
    glutMouseFunc(mouse);
    glutMainLoop();

    return(0);                  
}

CodePudding user response:

You're using a double buffered window (see glutInitDisplayMode).

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

Therefore you must call glutSwapBuffers to swaps the buffers after rendering the scene:

void dots()
{
    glBegin(GL_POINTS);
    glVertex3f(xvalue, yvalue, 0.0);
    glEnd();
}
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1, 0, 1);
    dots();
 
    // glFlush();
    glutSwapBuffers();
}

Note, a glBegin \ glEnd sequence delimits the vertices of a primitive. I recommend to call this instructions in the same function, immediately before and after specifying the vertices.

glClear clears the buffers and thus the entire scene. If you want to draw the points permanently, you have to remove the glClear instruction from the display function.

glClear(GL_COLOR_BUFFER_BIT);

  • Related