我正在尝试在 OpenGL 中渲染一个球体。不幸的是,屏幕总是只显示背景。我尝试重新定位相机无济于事(尽管我可能做错了)。任何见解将不胜感激。
这是 OpenGL 调用来更新屏幕的函数:
//The draw function - I have confirmed that this is called periodically.
void draw()
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glEnable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK, (globals.wireframe == true) ? GL_LINE : GL_FILL);
glViewport(0, 0, globals.window_size.x, globals.window_size.y);
mat4 prj = perspective(globals.fov, float(globals.window_size.x) / float(globals.window_size.y), globals.hither, globals.yon);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(value_ptr(prj));
glMatrixMode(GL_MODELVIEW);
mat4 context = mat4(1.0f);
globals.ship.draw(context); //the ship only draws a sphere for now
mat4 mv = lookAt(vec3(0.0f, 0.0f, -5.5f), vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f));
glLoadMatrixf(value_ptr(mv));
glutSwapBuffers();
}
以及 Ship 的定义:
GLuint Ship::sphere_handle; //static
bool Ship::is_initialized; //static
mat4 Ship::draw(mat4 context) const {
glLoadMatrixf(value_ptr(context));
glCallList(sphere_handle);
return context;
}
//called when program starts, after window created
bool Ship::initialize() {
if (sphere_handle == BAD_GL_VALUE)
{
GLUquadric *q = gluNewQuadric();
if (q != NULL)
{
if ((sphere_handle = glGenLists(1)) == 0)
{
cout << "Model::Initialize() - Failed to GenLists()" << endl;
return false;
}
glNewList(sphere_handle, GL_COMPILE);
gluSphere(q, 1.0, 10, 10);
glEndList();
gluDeleteQuadric(q);
is_initialized = true;
}
else
{
return false;
}
}
return true;
}