Why glClear doesn't clear my screen?
Solution 1
Where's your display callback? You shouldn't use the idle function for drawing.
All drawing needs to take place in the appropriate callbacks, the GL context might not be active until glutMainLoop
starts running, and with no active context, your commands simply get ignored (without a context, there might not even be a place to store errors for retrieval with glGetError
).
NOTE: Usually you want to clear the buffer at the beginning of every frame. You might get away with clearing just once with single-buffering, but double-buffering is better and requires you to somehow render the entire area between each swap.
Solution 2
Your problem is, that you do clear the screen in your initialization code. But you need to clear it every frame, so right at the start of your display (or in your case idle) function.
xzhu
Updated on June 04, 2022Comments
-
xzhu almost 2 years
Here is a simple opengl program by me. I'm trying to clear the screen before I draw a triangle. I've called glClear() in my init() function, however, it seemed that it failed to clear the screen.
#include <stdio.h> #include <stdlib.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> void myIdleFunc() { glBegin(GL_TRIANGLES); { glColor3f(1.0f, 1.0f, 1.0f); glVertex2f(0.0f, 1.0f); glVertex2f(-1.0f, -1.0f); glVertex2f(1.0f, -1.0f); } glEnd(); glFlush(); usleep(1000000); } void init() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glFlush(); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE); glutCreateWindow("Hello, World!"); init(); glutIdleFunc(myIdleFunc); glutMainLoop(); return 1; }
Here is a screen-shot, the text is from the gnome terminal in the back ground.
-
xzhu almost 13 yearsThanks. Why not use the idle function for drawing? I mean I need to repeatedly draw this triangle. Isn't the display function not drawing when nothing particular happens?
-
Ben Voigt almost 13 years@tr: I dunno, I don't use GLUT. But the code that's failing isn't in any callback, it's before GLUT has started its loop, so GL may not be ready to use.
-
Christian Rau almost 13 yearsThe context gets created and actived in glutCreateWindow and not in glutMainLoop. So his code works (but not as he intended).
-
Ben Voigt almost 13 years@Christian: You're right that the context is active, the docs say "State changes to a window's associated OpenGL context can be done immediately after the window is created." But it also says "rendering to a created window is ineffective because the window can not yet be displayed.", so perhaps the window is resized (and the context's framebuffer reallocated) on entry to
glMainLoop
. -
datenwolf almost 13 years@trVoldemort: Just call glutPostRedisplay() from your idle function. The glClear belongs into the display function; OpenGL does not maintain some kind of scene representation, it just executes drawing commands. OpenGL init() functions like yours are usually not a good OpenGL coding style, either.