繁体   English   中英

C-OpenGL中的深度效果

[英]C - Depth effect in OpenGL

我在线阅读了本教程: http : //www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/ 更确切地说,关于投影矩阵的章节。

我试图将我阅读的内容应用于所画内容的特定部分。 但是我看不出有什么不同。 我是否误解了所有内容或只是错误地使用了它?

我实际上是在画2所房子。 我想创建这种效果: http : //www.opengl-tutorial.org/assets/images/tuto-3-matrix/homogeneous.png

我的代码:

       void rescale()
       {
         glViewport(0,0,500,500);
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glOrtho(-6.500, 6.500, -6.500, 6.500, -6.00, 12.00);
        }

        void setup()
        {
        glClearColor(1.0, 1.0, 1.0, 1.0); 
    glEnable(GL_DEPTH_TEST);
         }

       void draw()
        {

    glPushMatrix();
          //draw something in the orthogonal projection
    glPopMatrix();

    glPushMatrix();
    gluPerspective(0.0, 1, -12, 30);  //perspective projection
    glPushMatrix();
        glScalef(0.2,0.2,0.2);
        glTranslatef(-1, 0.5, -5);
        glColor3f(1.0, 0.0, 0.0);
        drawVertexCube();
    glPopMatrix();

    glPushMatrix();
        glScalef(0.2,0.2,0.2);
        glTranslatef(-1, 0.5, -40);
        glColor3f(0, 1.0, 0.0);
            drawVertexCube();
    glPopMatrix();
   glPopMatrix();

    glFlush(); 
        }

结果如下:

在此处输入图片说明

如您所见,没有(可见)结果。 我想进一步强调效果。

您的gluPerspective呼叫无效:

gluPerspective(0.0, 1, -12, 30);

nearfar参数都必须为正,否则该命令将仅生成GL错误,并使当前矩阵保持不变,因此正射投影仍在使用中。 您的glPushMatrix() / glPopMatrix()调用将无济于事,因为您从未将glOrtho()调用包含在这样的块中,因此您永远不会恢复到尚未应用该矩阵的状态。 此外。 glLoadIdentity()glOrthoglMatrixMode()似乎也很奇怪。

您还应该知道,除glLoad*函数外,所有GL矩阵函数都会当前矩阵乘以一个新矩阵。 由于您仍然应用了正交矩阵,因此将得到两个矩阵的乘积,这将使您的结果完全不正确。

最后,您应该意识到, 所有 GL矩阵strack功能在现代OpenGL的核心配置文件中弃用完全删除 如果您现在正在学习OpenGL,则应该真正考虑学习OpenGL中的“现代”方式(基本上已经有十年的历史了)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM