Why does glClear not clear the screen?

Here is a simple opengl program. I am trying to clear the screen before drawing a triangle. I called glClear () in my init () function, however, it seemed that it could not 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 screenshot, text from the gnome's terminal in the background.

enter image description here

+6
source share
4 answers

Where is the callback of your display? You should not use the idle function for drawing.

All drawings must be executed in the appropriate callbacks, the GL context may not be active until glutMainLoop begins to work, and without an active context, your commands will simply be ignored (without a context, there might not even be a place to store errors to extract using glGetError ).

NOTE. Usually you want to clear the buffer at the beginning of each frame. You can leave with cleaning only once with one buffering, but double buffering is better and requires that you somehow display the entire area between each change.

+4
source

Your problem is that you clear the screen in the initialization code. But you need to clear every frame, so immediately at the beginning of your display (or in your standby mode).

+1
source

I do not use GLUT, but a very simple way to show something like this:

 while( !done ) { /* Loop until done. Do drawing. */ glClear( GL_COLOR_BUFFER_BIT); glLoadIdentity(); 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(); SwapBuffers( ); etc... event handling.... } 
0
source

Run this code and you will probably get a solution.

 #include<GL/gl.h> #include<GL/glut.h> #include<stdio.h> double x_0 = -100; double y_0 = -25; double x_1 = 100; double y_1 = -25; double x_2 = 100; double y_2 = 25; double x_3 = -100; double y_3 = 25; void init(void) { /*initialize the xy co-ordinate*/ glClearColor(0,0,0,0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-320, 319,-240, 239); glClear(GL_COLOR_BUFFER_BIT); glFlush(); } void drawRectangle() { glBegin(GL_LINES); glVertex2d(x_0, y_0); glVertex2d(x_1, y_1); glVertex2d(x_1, y_1); glVertex2d(x_2, y_2); glVertex2d(x_2, y_2); glVertex2d(x_3, y_3); glVertex2d(x_3, y_3); glVertex2d(x_0, y_0); glEnd(); glFlush(); } int main(int argc, char *argv[]) { double x_0, y_0, x_1, y_1; glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(640, 480); glutInitWindowPosition(400, 400); glutCreateWindow("Clear Screen"); init(); drawRectangle(); /* clear the screen. You can uncomment following three lines to view the effect of clearing */ // glClearColor(0, 0, 0, 0); // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // /* don't forget to flush */ // glFlush(); /***/ glutMainLoop(); } 

Compile and run -

 gcc file.c -lglut -lGLU -lGL ./a.out 
0
source

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


All Articles