这是代码,没有任何错误,有什么问题吗?
我编译它,命令提示符打开,窗口打开,窗口全是白色,我把它重新变成灰色..,它也没有画出我的形状,所以有什么问题?
#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;
}
答案 0 :(得分:1)
问题是在显示功能中绘制场景后清除显示。此外,还有一个无用的glFlush()
电话。删除这些行:
glFlush();
glClearColor(0.3f,0.3f,0.3f,0.3f);
glClear(GL_COLOR_BUFFER_BIT);
添加glutSwapBuffers()
代替它们。
glFlush()
用于单缓冲模式。
请记住,您使用的是双缓冲模式,因此有两个缓冲区。您的场景被绘制到后台缓冲区,要显示它,您必须通过调用显示功能结束时的glutSwapBuffers()
将其复制到前台缓冲区。
答案 1 :(得分:-2)
试试这个:
#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;
}