【问题标题】:OpenGL ES 2.0 Projection to map as positive Y?OpenGL ES 2.0 投影映射为正 Y?
【发布时间】:2012-08-29 05:41:04
【问题描述】:

我在 OpenGL ES 2.0 中有一个投影,但不幸的是我希望 Y 为正值,这样我更容易使用 TouchEvents。

现在这是投影矩阵:

private void setOrthographicMatrix(int w, int h)
{
    orthographicMatrix[0] = (float) (2.0f / w ); //a
    orthographicMatrix[3] = -1f;                 //tx
    orthographicMatrix[5] = (float) (2.0f / h);  //b
    orthographicMatrix[7] = 1f;                  //ty
    orthographicMatrix[10] = -1f;                //c
    orthographicMatrix[15] = 1f;                 //1

//  a, 0, 0, tx,
//  0, b, 0, ty,
//  0, 0, c, tz,
//  0, 0, 0, 1
}

顶点着色器:

attribute vec4 a_position;
attribute vec2 a_texCoord;

varying vec2 v_texCoord;

uniform mat4 u_MVPMatrix;

void main()
{
    //Pass Values to Fragment Shader
    v_texCoord = a_texCoord; 

    //Set Position
    gl_Position = a_position * u_MVPMatrix;
}

片段着色器:

precision mediump float;

varying vec2 v_texCoord;

uniform vec4 u_color;
uniform sampler2D u_s_texture;

void main()
{
    gl_FragColor = texture2D(u_s_texture, v_texCoord) * u_color;
}

结果是:(0,0) - 左上角

0,0----------> X++
|
|
|
↓
Y--

我希望投影映射,因此 Y 在屏幕下方变为正数。

编辑 - 解决方案:

我已将 OrthographicMatrix 函数的设置替换为使用内置的 Android Matrix 库:

Matrix.orthoM(orthographicMatrix, 0, 0, w, h, 0, -10f, 10f);

并将我的顶点着色器更改为:

gl_Position = u_MVPMatrix * a_position;

【问题讨论】:

    标签: android opengl-es opengl-es-2.0


    【解决方案1】:

    我建议只使用 OpenGL 的标准正交投影功能,它允许您定义顶部、底部、左侧、右侧,而不仅仅是宽度和高度,因此您可以随意翻转 Y 轴:

    OpenGLES 2.0 中不存在此函数,但您可以将方程复制到自己的正交函数中:

    http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xml

    另外,如果这是针对 Android 的(您的其他帖子是针对 android 的,所以我假设是这样),已经存在您可以使用的正交矩阵函数 (android.opengl.Matrix.orthoM)

    【讨论】:

    • 我将如何设置 orthoM?这个Matrix.orthoM(orthographicMatrix, 0, 0, w, h, 0, -10f, 10f);好像没有给我想要的效果?
    • @dnxviral 这对我来说看起来很合适,你能确切地说它是怎么不工作的吗?
    • @dnxviral :另外,也许考虑将您的着色器和统一代码添加到您的原始帖子中。您生成的矩阵看起来与 OpenGL 通常使用的矩阵不同(tx、ty、tz 通常是元素 [12]、[13] 和 [14],而不是 [3]、[7]、[11] ])
    • 没有任何东西呈现到屏幕上。 - 添加着色器
    • @dnxviral :我建议您在着色器中交换顶点乘法的顺序。这将是更标准的gl_Position = u_MVPMatrix * a_position;。它现在不起作用,因为您曾经使用转置矩阵,因此您必须向后进行乘法运算。我向您展示的方式在 OpenGL 中更加“标准”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多