首先,我想说明我在这里所做的只是试图测试我的OpenGL知识的能力,仅此而已。
以下代码表示无限循环,它可以在屏幕上渲染立方体,然后将其保存在静态投影中。无论出于何种原因,它拒绝展示。最重要的是,我得到的背景颜色是黑色,而不是背景颜色为白色。
代码
MainLoop语句
const int FIELD_OF_VIEW_Y = 60;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( FIELD_OF_VIEW_Y, 640.0 / 480.0, 1.0, 1028 );
glMatrixMode( GL_MODELVIEW );
glTranslatef( 0.0, 0.0, -2.0f );
Vector3f cam( 0, 0, 0 );
QPoint3F position( 3, 3, 3 );
const double CUBE_SIZE = 1;
while( true )
{
while( SDL_PollEvent( mEvent ) )
{
TestCubeRender( CUBE_SIZE, cam, position );
mControls->DoKeyHandling( mEvent );
mCamera->ReceiveInput( mEvent->key );
mCamera->UpdateCamera();
}
}
TestCubeRender
void TestCubeRender( const double size, const Vector3f& cameraPos, const QPoint3F& position)
{
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
glClearColor( 1, 1, 1, 1 );
int x1 = 0, x2 = 0;
int y1 = 0, y2 = 0;
int z1 = 0, z2 = 0;
x1 = ( position.x() - size );
x2 = position.x() + size;
y1 = ( position.y() - size );
y2 = position.y() + size;
z1 = ( position.z() - size );
z2 = position.z() + size;
qreal camZ = cameraPos.Z;
qreal camY = cameraPos.Y;
qreal camX = cameraPos.X;
glMatrixMode( GL_PROJECTION );
//glOrtho( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 );
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
//glTranslatef( 0.0, 0.0, -10.0 );
/*
* if axis > mCamera.axis; then normal3f -1.0 on the axis.
* axis point is axis1
* else if axis < mCamera.axis; then nomral3f 1.0 on the axis.
* axis point is axis2
*/
/* Pattern: one axis is written twice, with the other variant of the same axis written twice as well, then the
* order at which those axes are written is flipped, yet still following the same pattern.
* Pattern2: one type of axis is still written twice, and the other group of the same axis written twice as well,
* the only difference being that the first group is written once, with the next group written twice, and then that
* first group written once again. The third axis type is written using one type, depending on the camera comparisons.
*/
glBegin( GL_QUADS );
glColor4f( 1.0, 0, 1, 1 );
if ( z1 <= camZ )
{
glNormal3f( 0.0, 0.0, -1.0 );
glVertex3i( x1, y1, z1 ); //1
glVertex3i( x1, y2, z1 ); //1
glVertex3i( x2, y2, z1 ); //1
glVertex3i( x2, y1, z1 ); //1
}
if ( z2 >= camZ )
{
glNormal3f( 0.0, 0.0, 1.0 );
glVertex3i( x2, y1, z2 ); //2
glVertex3i( x2, y2, z2 ); //2
glVertex3i( x1, y2, z2 ); //2
glVertex3i( x1, y1, z2 ); //2
}
glColor4f( 0, 1, 0, 1 );
if ( y1 >= camY )
{
glNormal3f( 0.0, -1.0, 0.0 );
glVertex3i( x2, y1, z1 );
glVertex3i( x2, y1, z2 );
glVertex3i( x1, y1, z2 );
glVertex3i( x1, y1, z1 );
}
if ( y2 <= camY )
{
glNormal3f( 0.0, 1.0, 0.0 );
glVertex3i( x1, y2, z1 );
glVertex3i( x1, y2, z2 );
glVertex3i( x2, y2, z2 );
glVertex3i( x2, y2, z1 );
}
glColor4f( 1, 0, 0, 1 );
if ( x1 >= camX )
{
glNormal3f( -1.0, 0.0, 0.0 );
glVertex3i( x1, y1, z1 );
glVertex3i( x1, y1, z2 );
glVertex3i( x1, y2, z2 );
glVertex3i( x1, y2, z1 );
}
if ( x2 <= camX )
{
glNormal3f( 1.0, 0.0, 0.0 );
glVertex3i( x2, y2, z1 );
glVertex3i( x2, y2, z2 );
glVertex3i( x2, y1, z2 );
glVertex3i( x2, y1, z1 );
}
glEnd();
glPopMatrix();
}
我调试和调试了很多次。我知道这些值至少应该显示某些东西。如果我甚至可以看到我的背景颜色变化,我至少知道正在做的事情。 我该怎么办?
答案 0 :(得分:2)
缺少两件事:将视口(glViewport
)设置为窗口内部大小。渲染后交换缓冲区(SDL_GL_SwapBuffers
)。