在 OpenGL ES 2.0 中实现固定功能流水线高效吗?

发布于 2024-10-15 16:27:26 字数 989 浏览 2 评论 0原文

我想在我的 openGL 2.0 应用程序中使用固定函数方法,例如 glTranslate()、glRotate()、glScale()。我知道,我需要实现一个矩阵类 - 并且已经做到了。我现在的问题是关于效率。为了能够使用类似的东西:

glLoadIdentity();
glRotatef(2.0f, 0.0f, 0.0f, 1.0f);
glScalef(2.0f, 2.0f, 2.0f);

我认为,我需要至少进行 3 次矩阵乘法(假设我们有一个投影和一个模型视图矩阵,这是针对模型视图的)。 第一个是:Identity-Matrix*Rotation-Matrix - 第二个是:ActualMatrix*ScaleMatrix,最后一个是:projectionMatrix*ActualMatrix(我将其作为统一值传递给我的着色器)。

glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEW_PROJECTION_MATRIX], 1, GL_FALSE, matrix->getProjectionModelviewMatrix());

所以我的 Vertexshader 看起来像:

attribute vec4 position;
attribute vec4 color;

varying vec4 colorVarying;

uniform mat4 modelViewProjectionMatrix;

void main()
{
    gl_Position = modelViewProjectionMatrix * position;
    colorVarying = color;
}

在 OpenGL ES 1.1 中是否以相同的方式完成?看起来,我需要一次矩阵乘法:glRotate、glScale、glTranslate...调用 - 这对我来说似乎非常重要。或者有更好的方法吗? (也许矩阵乘法更少?)

有关此主题的任何帮助将不胜感激!感谢您的阅读

I want to use fixed function methods like glTranslate(), glRotate(), glScale() in my openGL 2.0 App. I know, that I need to implement an matrix class - and have done this. My question now is about efficiency. To be able to use something like:

glLoadIdentity();
glRotatef(2.0f, 0.0f, 0.0f, 1.0f);
glScalef(2.0f, 2.0f, 2.0f);

I think, I need to do at least 3 matrix multiplications (assuming we have a projection and a modelview matrizes and this is for the modelview).
First would be: Identity-Matrix*Rotation-Matrix - Second is: ActualMatrix*ScaleMatrix and the last would be: projectionMatrix*ActualMatrix (and this Im passing as uniform value to my shader).

glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEW_PROJECTION_MATRIX], 1, GL_FALSE, matrix->getProjectionModelviewMatrix());

So my Vertexshader looks like:

attribute vec4 position;
attribute vec4 color;

varying vec4 colorVarying;

uniform mat4 modelViewProjectionMatrix;

void main()
{
    gl_Position = modelViewProjectionMatrix * position;
    colorVarying = color;
}

Is it done the same way in OpenGL ES 1.1? It seems like, I need one matrix multiplication vor every: glRotate, glScale, glTranslate... Call - that seems very much for me. Or is there a better way? (maybe with less matrix multiplications?)

Any help on this topic would be highly appreciated! Thank you for reading

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒废 2024-10-22 16:27:26

恒等、平移、旋转和缩放矩阵不需要应用完整的矩阵乘法,因为许多项始终为 0.0 或 1.0。如果您逐个写出其他矩阵与这些矩阵相乘的结果,您会发现许多元素可能只有少数项影响其最终值。两个简单的例子:

  • 给定单位矩阵 I 和任意矩阵 MI × M = M × I = M
  • 缩放矩阵 S 中唯一的非零元素是沿对角线的四个元素,我们将其称为 S0 S1S2S3。 (对于像 glScalef 这样的东西,S3 始终是 1.0。)S × MM 的第 nth 行缩放 SnM × S 而是逐列工作。您还可以将单位矩阵视为特别无聊的缩放矩阵。

生成的平移和旋转的逐元素表达式比这些示例稍微复杂一些,但仍然比完整的矩阵乘法简单得多。 (如果旋转轴与 X、Y 或 Z 轴完全对齐,旋转也会变得更加简单。)当要求平移、缩放或旋转时,您可能希望直接修改矩阵,而不是构造另一个矩阵并相乘。

Identity, translation, rotation, and scaling matrices don’t require a full matrix multiplication to apply, because many of the terms are always 0.0 or 1.0. If you write out the element-by-element results of multiplying other matrices by these matrices, you’ll see that many elements may only have a few terms contributing to their final values. Two simple examples:

  • Given the identity matrix I and an arbitrary matrix M, I × M = M × I = M.
  • The only non-zero elements in a scaling matrix S are the four along the diagonal, which we’ll call S0, S1, S2, and S3. (S3 is always 1.0 for something like glScalef.) S × M scales the nth row of M by Sn. M × S works column-by-column instead. You can also think of the identity matrix as a particularly boring scaling matrix.

The resulting element-by-element expressions for translations and rotations are a bit more complicated than these examples, but still greatly simpler than a full matrix multiplication. (Rotations also get a great deal simpler if the axis of rotation is exactly aligned with the X, Y, or Z axis.) You’ll probably want to look into modifying matrices directly when asked to translate, scale, or rotate, rather than constructing another matrix and multiplying.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文