【问题标题】:glFrustrum variant in Opengl ES 2.0 - how fix blank screen?Opengl ES 2.0 中的 glFrustrum 变体 - 如何修复空白屏幕?
【发布时间】:2012-03-15 04:42:30
【问题描述】:

这个问题与我之前关于 glOrtho 变体的问题类似
glOrtho OpenGL es 2.0 variant how fix blank screen?

下面的三角形在正交投影上完美绘制(没有投影它是压扁的三角形,而不是矩形视口上的三个等边三角形)

GLfloat triangle_vertices[] =
                 {
                -0.5, -0.25, 0.0,
                 0.5, -0.25, 0.0,
                 0.0, 0.559016994, 0.0
                 };

正交矩阵代码:

typedef float[16] matrix;

void ortho_matrix(float right, float left, float bottom, float top, float near, float far, matrix result)
  {
// First Column
    result[0] = 2.0 / (right - left);
    result[1] = 0.0;
    result[2] = 0.0;
    result[3] = 0.0;

// Second Column
    result[4] = 0.0;
    result[5] = 2.0 / (top - bottom);
    result[6] = 0.0;
    result[7] = 0.0;

// Third Column
   result[8] = 0.0;
   result[9] = 0.0;
   result[10] = -2.0 / (far - near);
   result[11] = 0.0;

// Fourth Column
    result[12] = -(right + left) / (right - left);
    result[13] = -(top + bottom) / (top - bottom);
    result[14] = -(far + near) / (far - near);
    result[15] = 1;
  }

将我的投影矩阵设置为正交,其中 aspect_ratio = screen_width/screen_heigth

  ortho_matrix(-aspect_ratio, aspect_ratio, -1.0, 1.0, -1.0, 1.0, PROJECTION_MATRIX);

任务是将正射投影更改为透视,因此我为此编写函数
UPD:更改为 col-major

void frustum_matrix(float right, float left, float bottom, float top, float near, float far, matrix result)
{
        // First Column
    result[0] = 2 * near / (right - left);
    result[1] = 0.0;
    result[2] = 0.0;
    result[3] = 0.0;

    // Second Column
    result[4] = 0.0;
    result[5] = 2 * near / (top - bottom);
    result[6] = 0.0;
    result[7] = 0.0;

    // Third Column
    result[8] = (right + left) / (right - left);
    result[9] = (top + bottom) / (top - bottom);
    result[10] = -(far + near) / (far - near);
    result[11] = -1;

    // Fourth Column
    result[12] = 0.0;
    result[13] = 0.0;
    result[14] = -(2 * far * near) / (far - near);
    result[15] = 0.0;
 }

将我的投影设置为平截头体矩阵,其中 aspect_ratio = screen_width/screen_heigth

  frustum_matrix(-aspect_ratio, aspect_ratio, -1.0, 1.0, 0.1, 1.0, PROJECTION_MATRIX);

好吧,我在 glFrustrum 页面http://www.opengl.org/sdk/docs/man/xhtml/glFrustum.xml 上查看了矩阵,但正交函数的矩阵来自相同的来源并且工作正常。无论如何,我在https://stackoverflow.com/a/5812983/1039175 frustum 函数等各个地方都看到了类似的截锥矩阵。

我得到的只是空白屏幕、视口和其他与绘图相关的东西都设置好了。

【问题讨论】:

    标签: c opengl-es opengl-es-2.0


    【解决方案1】:

    看起来您的矩阵索引是从 glFrustum 的文档页面转置的。你在上传之前转置矩阵吗? OpenGL 通常指的是列向量矩阵,因此如果您从 glFrustum 复制方程,索引应如下所示:

    [0]  [4]  [ 8]  [12]
    [1]  [5]  [ 9]  [13]
    [2]  [6]  [10]  [14] 
    [3]  [7]  [11]  [15]
    

    【讨论】:

      【解决方案2】:

      我不得不承认我懒得看你的代码,但是......假设你的清除颜色设置为白色,可能是你没有设置视口:

      确保在渲染之前至少调用一次:

      GLint viewport[4];
      glGetIntegerv(GL_VIEWPORT, viewport);
      GLsizei width = viewport[2];
      GLsizei height = viewport[3];
      glViewport(0, 0, width, height);
      

      至于一些一般性建议:不要试图通过重写矩阵代码来重新发明轮子(除非可能出于某种学术目的)。如果您考虑切换到 c++,值得查看 glm 库:http://glm.g-truc.net/

      它可以替换您正在尝试实现的那些矩阵函数,然后是一些......我自己使用它,它是一个很棒的数学库,因为它专门针对 opengl-es 2.0 和 glsl。

      【讨论】:

      • 视口已设置,我当然知道 GLM,你明白了 - 学术目的,无论如何 + 1
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多