【问题标题】:OpenGL Matrix scale then Translate is still scaling my positionOpenGL 矩阵缩放然后翻译仍在缩放我的位置
【发布时间】:2015-11-11 10:41:34
【问题描述】:

我正在尝试将我的文本模型网格定位在屏幕上。使用下面的代码,它按照代码建议绘制网格;网格的左侧位于屏幕的中心。但是,我想把它放在屏幕边缘的左边,这就是我卡住的地方。如果我取消注释 Matrix.translateM 行,我会认为该位置现在将位于屏幕的左侧,但似乎该位置正在被缩放(!?)

我尝试过的几个场景:

a.) 仅限 Matrix.scaleM(无 Matrix.translateM)= 网格左侧位于 0.0f(屏幕中心),具有正确的比例。

b.) Matrix.TranslateM only(无 Matrix.scaleM)= 网格左侧正确定位在屏幕左侧 -1.77f,但缩放不正确。

c.) Matrix.TranslateM 然后 Matrix.scaleM,或 Matrix.scaleM 然后 Matrix.TranslateM = 比例正确,但是位置不对。 似乎位置已缩放,并且比屏幕左侧更靠近中心。

我在 Java 中的 Android Studio 编程中使用 OpenGL ES 2.0。

屏幕边界(来自 Matrix.orthoM 的设置) 左:-1.77,右:1.77(中心为0.0),上:-1.0,下:1.0(中心为0.0)

网格高度为1.0f,所以如果没有Matrix.scaleM,网格占据整个屏幕高度。

float ratio = (float) 1920.0f / 1080.0f;

float scale = 64.0f / 1080.0f; // 64px height to projection matrix

Matrix.setIdentityM(modelMatrix, 0);
Matrix.scaleM(modelMatrix, 0, scale, scale, scale); // these two lines
//Matrix.translateM(modelMatrix, 0, -ratio, 0.0f, 0.0f); // these two lines

Matrix.setIdentityM(mMVPMatrix, 0);
Matrix.orthoM(mMVPMatrix, 0, -ratio, ratio, -1.0f, 1.0f, -1.0f, 1.0f);

Matrix.multiplyMM(mMVPMatrix, 0, mMVPMatrix, 0, modelMatrix, 0);

【问题讨论】:

  • 你试过颠倒订单吗? mMVPMatrix = modelMatrix * mMVPMatrix?
  • 在您的情况下,翻译绝对是在规模之前。正如已经提到的,乘法顺序似乎是错误的。正如 MVP 的名字所暗示的那样,它是 modelviewprojection,但你有 projection*model。当矩阵相乘时,顺序很重要。
  • 谢谢你们。 Matic,我将其更改为 Matrix.translateM,然后是 Matrix.scaleM,还颠倒了我的 MVP,现在效果很好!现在,如何给予信用,信用在哪里到期?

标签: matrix opengl-es opengl-es-2.0 matrix-transform


【解决方案1】:

谢谢,Ed Halferty 和 Matic Oblak,你们都是对的。正如 Matic 建议的那样,我现在将 Matrix.TranslateM 放在第一位,然后 Matrix.scaleM 放在第二位。我还确保 MVPMatrix 确实 modelviewprojection,而不是projectionviewmodel。

此外,现在将模型网格的 Matrix.translateM 设置为 -1.0f,它位于屏幕的左边缘,无论如何都优于 -1.77f。

Correct position + scale, thanks!

    float ratio = (float) 1920.0f / 1080.0f;

    float scale = 64.0f / 1080.0f;

    Matrix.setIdentityM(modelMatrix, 0);
    Matrix.translateM(modelMatrix, 0, -1.0f, 0.0f, 0.0f);
    Matrix.scaleM(modelMatrix, 0, scale, scale, scale);

    Matrix.setIdentityM(mMVPMatrix, 0);
    Matrix.orthoM(mMVPMatrix, 0, -ratio, ratio, -1.0f, 1.0f, -1.0f, 1.0f);

    Matrix.multiplyMM(mMVPMatrix, 0, modelMatrix, 0, mMVPMatrix, 0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    相关资源
    最近更新 更多