问题
Here is the code, there aren't any errors, what is wrong with it?
I compile it, the command prompt opens, the window opens, the window is all white, I recolored it to grey.., and it also does not draw my shape, so what is the problem?
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
void ProcessSpecialKeys(int key, int x, int y){
switch(key){
case GLUT_KEY_RIGHT:
exit(0);
case GLUT_KEY_LEFT:
exit(0);
case GLUT_KEY_UP:
exit(0);
case GLUT_KEY_DOWN:
exit(0);
default:
exit(0);
}
}
void renderScene(void) {
glClearColor(0.3f, 0.3f, 0.3f, 0.3f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
void renderPrimitive(void){
glBegin(GL_QUADS);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(-1.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glEnd();
}
void display(void){
glTranslatef(0.0f,0.0f,-0.5f);
renderPrimitive();
glFlush();
glClearColor(0.3f,0.3f,0.3f,0.3f);
glClear(GL_COLOR_BUFFER_BIT);
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("Dimension");
glutDisplayFunc(display);
glLoadIdentity();
glutSpecialFunc(ProcessSpecialKeys);
glutMainLoop();
return 1;
}
回答1:
The problem is that you are clearing the display after you draw your scene in the display function. Also, there is a useless glFlush()
call. Remove these lines:
glFlush();
glClearColor(0.3f,0.3f,0.3f,0.3f);
glClear(GL_COLOR_BUFFER_BIT);
Add a glutSwapBuffers()
instead of them.
glFlush()
is used in single buffered mode.
Remember, you are using double buffered mode, so there are two buffers. Your scene is drawn to the back buffer, and to display it, you have to copy it to the front buffer by calling glutSwapBuffers()
at the end of the display function.
回答2:
Try this:
#include <GL/glut.h>
#include <stdlib.h>
void ProcessSpecialKeys(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_RIGHT:
case GLUT_KEY_LEFT:
case GLUT_KEY_UP:
case GLUT_KEY_DOWN:
exit(0);
break;
default:
exit(0);
break;
}
}
void renderPrimitive()
{
glBegin(GL_QUADS);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(-1.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glEnd();
}
void display()
{
glClearColor(0.3f,0.3f,0.3f,0.3f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -2, 2, -2, 2, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0f,0.0f,-0.5f);
renderPrimitive();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("Dimension");
glutDisplayFunc(display);
glutSpecialFunc(ProcessSpecialKeys);
glutMainLoop();
return 1;
}
来源:https://stackoverflow.com/questions/17730333/glut-is-just-rendering-a-blank-white-screen