Reputation: 21966
Consider this code:
#include <stdlib.h>
#include <stdarg.h>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
double width=600;
double height=600;
void processMouse(int button, int state, int x, int y)
{
glColor4f(1.0,0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glFlush();
}
static void render()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glutMouseFunc(processMouse);
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutCreateWindow("Board");
glutDisplayFunc(render);
glutMainLoop();
}
The render function is executed, and every time a click is performed, it should start the function processMouse. So if the mouse is clicked, all the window should become red with the instructions:
glColor4f(1.0,0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glFlush();
But when I click the mouse I notice a strange behaviour: only a part of the window gets colored, the part at bottom left (instead of all the screen). The window remains in this state, until I open a google chrome window.If I open a google chrome (or another graphical application), all the window become colored of red. Why this? I also have problems with more complex programs, it seems like sometime the glVertex instructions are ignored.If I try debugging the program with fprintf it appears that everything is ok, and everything seems to go like expected (for example I tried to print mouse coordinates in the processMouse function, they were ok), except for the fact that what I draw is ignored.
Edit: I have modified this code but it still has the same problem:
#include <stdlib.h>
#include <stdarg.h>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
double width=600;
double height=600;
bool down=false;;
// http://elleestcrimi.me/2010/10/06/mouseevents-opengl/
static void render()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
if(down)
{
glColor4f(1.0,0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glFlush();
}
}
void processMouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN)
{
down=true;
glutPostRedisplay();
}
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutCreateWindow("Board");
glutMouseFunc(processMouse);
glutDisplayFunc(render);
glutMainLoop();
}
Still getting only a part of the screen red.
PS: Solved using glutSwapBuffers(), thanks.
Upvotes: 0
Views: 1361
Reputation: 13877
When you are using double buffering with GLUT, you need to call glutSwapBuffers()
to see the result of the draw.
Add this to the end of your render()
function and it will work fine.
Upvotes: 1