引擎测试:在屏幕上渲染立方体 (OpenGL)

Posted

技术标签:

【中文标题】引擎测试:在屏幕上渲染立方体 (OpenGL)【英文标题】:Engine Testing: Rendering a Cube on the Screen (OpenGL) 【发布时间】:2012-02-06 05:23:21 【问题描述】:

首先,我想说的是,我在这里所做的只是尝试测试我的 OpenGL 知识的能力,仅此而已。

以下代码表示一个无限循环,它尽其所能在屏幕上渲染一个立方体,然后将其保持在静态投影中。无论出于何种原因,它拒绝显示。最重要的是,我得到了黑色的背景色,而不是白色的背景色。

代码

主循环

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();
    

我已经调试过很多次了。我知道这些值至少应该显示 something。如果我什至可以看到我的背景颜色发生了变化,我至少会知道某些事情正在正确地完成。 我该怎么办?

【问题讨论】:

您确定要交换窗口上的缓冲区吗? 您的SDL_GL_SwapBuffers() 电话在哪里? 【参考方案1】:

缺少两件事:将视口 (glViewport) 设置为窗口内部大小。渲染后交换缓冲区 (SDL_GL_SwapBuffers)。

【讨论】:

glViewport 默认覆盖所有已创建 OpenGL 上下文的窗口区域。 仅在第一次调整大小之前。当 OpenGL 上下文第一次(并且仅在那时)附加到可绘制对象时,会设置初始视口尺寸。窗口大小的后续变化必须通过适当调用 glViewport 来反映。 当然。但这并不能解释为什么显示器上什么也没有显示。

以上是关于引擎测试:在屏幕上渲染立方体 (OpenGL)的主要内容,如果未能解决你的问题,请参考以下文章

倒置 X 轴 OpenGL

OpenGL 问题:无法在屏幕上渲染几何图形

opengl 立方体不会渲染

渲染时 3D 立方体出现拉伸

SDL 3d OpenGL:立方体上的纹理产生空白屏幕

在 OpenGL 中渲染一个*填充的*半透明立方体