Anyone knows why this happens when trying to generate a simple square in OpenGL?
I am using the following source code from the book Computer Graphics Through
Try adding a call to glutSwapBuffers();
at the end of drawScene
and removing GLUT_SINGLE
from glutInitDisplayMode
. Single-buffer mode has all sorts of compatibility issues. On Windows it uses the software rasterizer which is VERY slow and has even more issues.
If that doesn't work, try clearing the depth buffer by changing glClear
to glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
and glClearcolor
to glClearColor(1.0, 1.0, 1.0, 1.0);
.
Also, this example uses legacy OpenGL, which is significantly slower than modern OpenGL and has many other problems. Unless you are using a GPU that's more than a decade old, you have zero reason to use it. Technically, modern GPUs aren't even required to support it. If you need a good modern OpenGL tutorial, http://open.gl is a good one. Just look for an OpenGL 2.1 (or later) tutorial.
On Windows Vista and newer Windows operating systems, there is a component known as the Desktop Window Manager
(DWM) which has a special mode called "Desktop Composition" that draws windows into offscreen buffers and then composites them. It does this to provide new visual effects such as live window previews in the Alt+Tab screen.
A consequence of this new architecture is that you cannot draw single buffered applications (in windowed mode anyway) the same way you could in Windows XP (or in Windows Vista+ with Desktop Composition disabled). In a nutshell, the DWM uses a copy of your render context's back buffer for composition. You should switch to double buffered drawing.
To use double buffered drawing in GLUT, you would use GLUT_DOUBLE
instead of GLUT_SINGLE
in your call to glutInitDisplayMode (...)
. Additionally, you need to replace your calls to glFlush (...)
with glutSwapBuffers (...)
.