#include <GL/gl.h>
#include <GL/glut.h>
void display();
void init();
int main(int argc, char* argv[])
{
init();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(320, 240);
glutCreateWindow("Main Window");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init()
{
glDisable(GL_DEPTH_TEST);
}
void display()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_QUADS);
glColor3i(255,255,255);
glVertex2f(10, 10);
glVertex2f(100, 10);
glVertex2f(100, 100);
glVertex2f(10, 100);
glEnd();
glutSwapBuffers();
}
从理论上讲,此代码应绘制一个白色矩形。但是我所看到的只是一个黑屏。怎么了?
最佳答案
这是我的工作示例,其中有我通过注释注明的更改:
#include <gl/glut.h>
#include <gl/gl.h>
#define WINDOW_WIDTH 320
#define WINDOW_HEIGHT 240
void display();
void init();
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Main Window");
init(); // changed the init function to come directly before display function
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init()
{
glClearColor(0, 0, 0, 0); // moved this line to be in the init function
glDisable(GL_DEPTH_TEST);
// next four lines are new
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, WINDOW_WIDTH-1, WINDOW_HEIGHT-1, 0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_QUADS);
glColor3ub(255,255,255); // changed glColor3i to glColor3ub (see below)
glVertex2f(10, 10);
glVertex2f(100, 10);
glVertex2f(100, 100);
glVertex2f(10, 100);
glEnd();
glFlush(); // added this line
//glutSwapBuffers(); // removed this line
}
glColor3ub
是要提供0-255范围内的颜色时要使用的功能。希望这可以帮助。
关于c++ - 带有GLUT的OpenGL。没有画图?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11606609/