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

查看:45
本文介绍了矩阵变换的 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 操作的每个后续调用(glScale、glTranslate、glRotate)都是影响对象的第一个事情,它会进入与你的写作顺序相反.

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 That Be 决定这就是他们希望 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天全站免登陆