【问题标题】:OpenGL ES 2.0 2D projection matrix, error converting GLfloat (*)[4] to GLfloatOpenGL ES 2.0 2D 投影矩阵,将 GLfloat (*)[4] 转换为 GLfloat 时出错
【发布时间】:2013-07-28 04:27:00
【问题描述】:

我在 OpenGL ES 2.0 中创建一个应用程序,并想设置一个 2D 投影矩阵来整理由屏幕纵横比引起的拉伸和失真。我使用函数创建了一个具有所需值的矩阵。该函数的结果是一个 4x4 矩阵,然后将其上传到顶点着色器。当我运行该程序时,我收到一个构建错误消息:“无法转换 GLfloat (*)[4]' toGLfloat' 作为回报”。我的 C++ 知识有限,不知道如何解决这个问题,下面是我调用的函数的代码。

GLfloat ortho_matrix(float left, float right, float bottom, float top, float near,
float  far)
{
GLfloat result[4][4];

result[0][0] = 2.0 / (right - left);
result[1][0] = 0.0;
result[2][0] = 0.0;
result[3][0] = 0.0;

//Second Column
result[0][1] = 0.0;
result[1][1] = 2.0 / (top - bottom);
result[2][1] = 0.0;
result[3][1] = 0.0;

//Third Column
result[0][2] = 0.0;
result[1][2] = 0.0;
result[2][2] = -2.0 / (far - near);
result[3][2] = 0.0;

//Fourth Column
result[0][3] = -(right + left) / (right - left);
result[1][3] = -(top + bottom) / (top - bottom);
result[2][3] = -(far + near) / (far - near);
result[3][3] = 1;

return result;
}

【问题讨论】:

    标签: c++ opengl opengl-es


    【解决方案1】:

    您正试图从您的函数中返回一个浮点数,而您需要在该函数中返回一个二维数组。以这种方式声明你的函数:

    void ortho_matrix(float left, float right, float bottom, float top, float near,
    float  far, GLfloat result[4][4])
    

    【讨论】:

    • 我需要返回数组 'result' 而不是 void 所暗示的任何内容。我应该用什么替换 void 以使数组返回?
    • 在我的原型中,您将通过函数的result 参数返回结果。您不需要在函数中使用return 语句和result[4][4] 声明。
    • 我解决了。最后,我将其存储为 1D 矩阵,因为我使用的统一函数 (glUniformMatrix4fv) 在将其导入着色器之前自动将其转换为 2D 矩阵。这解决了从 Glfloat 4x4 转换为 GLfloat 的问题。感谢您的建议和帮助,让我得到正确的答案。
    【解决方案2】:

    看看GLM。它具有适当的矩阵类型和对矩阵运算的适当支持。它也是 OpenGL 友好的。

    【讨论】:

      猜你喜欢
      • 2019-03-05
      • 1970-01-01
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多