矩阵变换的Opengl阶 [英] Opengl order of matrix transformations

查看:115
本文介绍了矩阵变换的Opengl阶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个蓝色的3D框(顶部是红色).

Let's say I have a blue 3D box (where the top side is red colored).

  1. 现在我叫glScalef(1,10,1).
  2. 然后我将其称为glRotatef(90,0,1,0).
  3. 然后我渲染立方体.

我期望看到的是红色的一面朝向屏幕并沿着屏幕延伸(沿着模型的Y轴).

What i expected is to see the the red side facing and stretched towards the screen (along the Y axis of the model).

但是我看到的是: 红色的一面正对着屏幕(如预期的那样). 但是拉伸发生在视图空间(而不是模型)的Y轴上.

But what im seeing is this: The red side is facing the screen (as expected). But the stretch is occurring on the Y axis of the view space (not the model).

我确实知道,如果我沿Z轴设置比例,那么我会得到正确的结果. 但是我的困惑是我认为在Y轴上按比例放大,然后旋转盒子,会给我正确的结果.

I do know that if I set the scale along the Z axis, then i'll get the right result. But my confusion is that i thought scaling up on the Y axis, then rotating the box, would give me the right result.

我想念什么?

推荐答案

OpenGL在称为

OpenGL reads its matrices in what is known as column major order. The result is that each subsequent operation is a pre-multiplication of all the operations before it, not a post-multiplication. Thus, each subsequent call to an OpenGL operation (glScale, glTranslate, glRotate) is the first thing that affects an object, and it goes in reverse order from how you write it.

因此,您需要更改乘法顺序:

So, you need to change your multiplication order:

glRotatef(90, 0, 1, 0); // Notice how this is the first operation ...
// ... but will be applied after
glScalef(1, 10, 1); // This will be applied first, but is the last operation
// Zany, right?

说实话,当您编写程序并使用计算机必须按相反顺序指定内容时,感觉会不太直观.但是要决定的Graphics Gods,是他们希望OpenGL的矩阵堆栈表现出来的方式(以及HLSL,GLSL和其他系统:请参见上面有关列优先顺序的链接).

Truth be told, it feels less intuitive when you're writing programs and using a computer to have to specify things in reverse order. But the Graphics Gods That Be decided that's how they wanted OpenGL's matrix stack to behave (along with HLSL, GLSL, and other systems: see the link above about column-major ordering).

这篇关于矩阵变换的Opengl阶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆