【问题标题】:OpenGL ortho, perspective and frustum projectionsOpenGL 正交、透视和平截头体投影
【发布时间】:2014-02-12 16:10:46
【问题描述】:

45我正在尝试理解 OpenGL 对单点的投影。我使用 QGLWidget 渲染上下文和 QMatrix4x4 投影矩阵。这里是绘制函数

    attribute vec4 vPosition;      
        uniform mat4 projection;  
        uniform mat4 modelView; 
        void main()
        {
          gl_Position = projection* vPosition;
        }       

        void OpenGLView::Draw()
        {
           glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glUseProgram(programObject);
    glViewport(0, 0, width(), height());

    qreal aspect = (qreal)800 / ((qreal)600);
    const qreal zNear = 3.0f, zFar = 7.0f, fov = 45.0f;

    QMatrix4x4 projection;
    projection.setToIdentity();
    projection.ortho(-1.0f,1.0f,-1.0f,1.0f,-20.0f,20.0f);
   // projection.frustum(-1.0f,1.0f,-1.0f,1.0f,-20.0f,20.0f);
   // projection.perspective(fov,aspect,zNear, zFar);

   position.setToIdentity();
   position.translate(0.0f, 0.0f, -5.0f);
   position.rotate(0,0,0, 0);

    QMatrix4x4 mvpMatrix =   projection * position;

    for (int r=0; r<4; r++)
        for (int c=0; c<4; c++)
            tempMat[r][c] = mvpMatrix.constData()[ r*4 + c ];

    glUniformMatrix4fv(projection, 1, GL_FALSE, (float*)&tempMat[0][0]);

    //Draw point at 0,0
    GLfloat f_RefPoint[2];
    glUniform4f(color,1, 0,1,1);
    glPointSize(15);
    f_RefPoint[0] = 0;
    f_RefPoint[1] = 0;
    glEnableVertexAttribArray(vertexLoc);
    glVertexAttribPointer(vertexLoc, 2, GL_FLOAT, 0, 0, f_RefPoint);
    glDrawArrays (GL_POINTS, 0, 1);            
        }

观察:

1)projection.ortho:窗口上渲染的点,平移不同z轴值的点没有效果

2) projection.frustum:点在窗口上绘制,只有点被翻译为 translate(0.0f, 0.0f, -20.0f)

3) projection.perspective:该点永远不会呈现在屏幕上。

有人可以帮我理解这种行为吗?

【问题讨论】:

    标签: opengl-es qglwidget


    【解决方案1】:
    1. 正射投影以这种方式工作。我建议你搜索一些关于不同投影之间差异的图片或视频。
    2. 我不知道你如何看待 Z 坐标中的点平移,但如果你有一个正方形,它会通过将它平移得更远而变得更小(使用 ortho 会保持不变)。当您在this value should be positive 时对 zNear 使用 -20.0f 时,这里有一个问题。在大多数情况下,插入此方法的值应使用field of view, aspect ratio 生成...无论如何,您将无法看到比 zNear 更近的任何东西以及比 zFar 更远的任何东西。
    3. 这与平截头体相同,但已将参数作为视野、纵横比。您看不到任何东西的原因是您的 zNear 在 3.0f 处,而该点距离 .0f 长度。通过翻译该点,您将能够看到它,但请尝试将其翻译为从 3.0f 到 7.0f 的任何值(3.0f 是您的 zNear,7.0f 是您的 zFar)。替代方案是增加 zFar 或向后平移投影矩阵。或者主要是在你的情况下,我建议在投影矩阵上添加一些“查看”系统,因为它会给你一些易于使用的工具来操作你的“相机”,在大多数情况下,你可以设置一个你正在查看的点,您正在查看的点和向上向量。

    【讨论】:

      猜你喜欢
      • 2016-02-11
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      相关资源
      最近更新 更多