C ++ OpenGL Window only tracks background image

Does anyone know why this happens when trying to create a simple square in OpenGL?

I use the following source code from the book Computer graphics through OpenGL : from theory to experiment.

//////////////////////////////////////////////////// // square.cpp // // Stripped down OpenGL program that draws a square. // // Sumanta Guha. //////////////////////////////////////////////////// #include <iostream> #ifdef __APPLE__ # include <GLUT/glut.h> #else # include <GL/glut.h> #endif using namespace std; // Drawing (display) routine. void drawScene(void) { // Clear screen to background color. glClear(GL_COLOR_BUFFER_BIT); // Set foreground (or drawing) color. glColor3f(0.0, 0.0, 0.0); // Draw a polygon with specified vertices. glBegin(GL_POLYGON); glVertex3f(20.0, 20.0, 0.0); glVertex3f(80.0, 20.0, 0.0); glVertex3f(80.0, 80.0, 0.0); glVertex3f(20.0, 80.0, 0.0); glEnd(); // Flush created objects to the screen, ie, force rendering. glFlush(); } // Initialization routine. void setup(void) { // Set background (or clearing) color. glClearColor(1.0, 1.0, 1.0, 0.0); } // OpenGL window reshape routine. void resize(int w, int h) { // Set viewport size to be entire OpenGL window. glViewport(0, 0, (GLsizei)w, (GLsizei)h); // Set matrix mode to projection. glMatrixMode(GL_PROJECTION); // Clear current projection matrix to identity. glLoadIdentity(); // Specify the orthographic (or perpendicular) projection, // ie, define the viewing box. glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0); // Set matrix mode to modelview. glMatrixMode(GL_MODELVIEW); // Clear current modelview matrix to identity. glLoadIdentity(); } // Keyboard input processing routine. void keyInput(unsigned char key, int x, int y) { switch(key) { // Press escape to exit. case 27: exit(0); break; default: break; } } // Main routine: defines window properties, creates window, // registers callback routines and begins processing. int main(int argc, char **argv) { // Initialize GLUT. glutInit(&argc, argv); // Set display mode as single-buffered and RGB color. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set OpenGL window size. glutInitWindowSize(500, 500); // Set position of OpenGL window upper-left corner. glutInitWindowPosition(100, 100); // Create OpenGL window with title. glutCreateWindow("square.cpp"); // Initialize. setup(); // Register display routine. glutDisplayFunc(drawScene); // Register reshape routine. glutReshapeFunc(resize); // Register keyboard routine. glutKeyboardFunc(keyInput); // Begin processing. glutMainLoop(); return 0; } 

I try for a long time ...

More details here:

A screen will open displaying only the background, if you drag it, it will continue to track the background, and this is the result of moving the window to the bottom and then moving it to its original position. I tested the same source code on a Linux machine and it works fine ... :(

EDIT: I tried using glutSwapBuffers (), and that didn't work either.

enter image description here

+4
source share
2 answers

Windows Vista and newer Windows operating systems have a component known as Desktop Window Manager (DWM), which has a special "Desktop Composition" mode, which draws windows into off buffers and then composes them. He does this to create new visual effects, such as real-time previews on the Alt + Tab screen.

The consequence of this new architecture is that you cannot draw separate buffered applications (in windowed mode anyway) in the same way as in Windows XP (or in Windows Vista + with disabling Desktop Composition). In a nutshell, DWM uses a copy of the back buffer rendering context for composition. You must switch to double buffer drawing.

To use a double buffered drawing in GLUT, use GLUT_SINGLE instead of GLUT_SINGLE in glutInitDisplayMode (...) . In addition, you need to replace your calls with glFlush (...) with glutSwapBuffers (...) .

+4
source

Try adding a call to glutSwapBuffers(); at the end of drawScene and remove GLUT_SINGLE from glutInitDisplayMode . Single-buffer mode has all sorts of compatibility issues. On Windows, it uses a software rasterizer, which is VERY slow and has even more problems.

If this does not work, try clearing the depth buffer by changing glClear to glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); and glClearcolor on glClearColor(1.0, 1.0, 1.0, 1.0); .

In addition, this example uses legacy OpenGL, which is significantly slower than modern OpenGL, and has many other problems. If you have not used the GPU for more than ten years, you have no reason to use it. Technically, modern GPUs are not even required to support it. If you need a good modern OpenGL tutorial, http://open.gl is a good one. Just find the OpenGL 2.1 tutorial (or later).

+2
source

Source: https://habr.com/ru/post/1499269/


All Articles