glLoadIdentity() 在 OpenGL 中做什么? [英] What does glLoadIdentity() do in OpenGL?

查看:26
本文介绍了glLoadIdentity() 在 OpenGL 中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 OpenGL 的新手,我对代码中的所有随机函数有点不知所措.它们有效,我知道何时使用它们,但我不知道我为什么需要它们或它们实际做什么.

I'm new to OpenGL and I'm a little overwhelmed with all of the random functions that I have in my code. They work and I know when to use them, but I don't know why I need them or what they actually do.

我知道 glLoadIdentity() 将当前矩阵替换为单位矩阵,但是这究竟是做什么的?如果每个程序都需要它,为什么除非另有说明,否则默认情况下不使用单位矩阵?我不喜欢在我的代码中有函数,除非我知道它们是做什么的.我应该注意,我专门为富 2D 客户端使用 OpenGL,所以如果这对 3D 来说非常明显,请原谅我的无知.

I know that glLoadIdentity() replaces the current matrix with the identity matrix, but what exactly does that do? If every program requires it, why isn't the identity matrix by default unless otherwise specified? I don't like to have functions in my code unless I know what they do. I should note that I am using OpenGL exclusively for rich 2D clients so excuse my ignorance if this is something very obvious for 3D.

对于glMatrixMode(GL_PROJECTION) VS glMatrixMode(GL_MODELVIEW)也有点困惑.

Also a little confused about glMatrixMode(GL_PROJECTION) VS glMatrixMode(GL_MODELVIEW).

推荐答案

单位矩阵,就投影矩阵和模型视图矩阵而言,本质上是将矩阵重置为其默认状态.

The identity matrix, in terms of the projection and modelview matrices, essentially resets the matrix back to its default state.

如您所愿,glTranslateglRotate 始终与矩阵的当前状态相关.因此,例如,如果您调用 glTranslate,您是从矩阵的当前位置"而不是原点进行平移.但是如果你想从原点重新开始,那就是当你调用 glLoadIdentity(),然后你可以从现在位于原点的矩阵中 glTranslate,或者glRotate 来自现在面向默认方向的矩阵.

As you hopefully know, glTranslate and glRotate are always relative to the matrix's current state. So for instance, if you call glTranslate, you are translating from the matrix's current 'position', not from the origin. But if you want to start over at the origin, that's when you call glLoadIdentity(), and then you can glTranslate from the matrix which is now located at the origin, or glRotate from the matrix which is now oriented in the default direction.

我认为 Boon 的答案,它相当于 1,并不完全正确.矩阵实际上是这样的:

I think Boon's answer, that it is the equivalent of 1, is not exactly correct. The matrix actually looks like this:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

那是单位矩阵.Boon 在数学上是正确的,任何与该矩阵相乘的矩阵(或看起来像这样的矩阵;对角矩阵,其他都是 0)将产生原始矩阵,但我不相信他解释了为什么这很重要.

That is the identity matrix. Boon is correct, mathematically, that any matrix multiplied with that matrix (or a matrix that looks like that; diagonal ones, all else 0s) will result in the original matrix, but I don't believe he explained why this is important.

这很重要的原因是因为OpenGL通过每个矩阵乘以所有位置和旋转;因此,例如,当您绘制一个多边形(glBegin(GL_FACE), some points, glEnd())时,它会通过将其与 MODELVIEW 相乘来将其转换为世界空间",然后通过将其与 PROJECT 矩阵相乘将其从 3D 转换为 2D,这为其提供了屏幕上的 2D 点以及用于绘制像素的深度(来自屏幕相机").但是当这些矩阵之一是单位矩阵时,点与单位矩阵相乘,因此不会改变,所以矩阵没有影响;它不会平移点,也不会旋转它们,而是保持原样.

The reason why this is important is because OpenGL multiplies all positions and rotations through each matrix; so when for instance you draw a polygon (glBegin(GL_FACE), some points, glEnd()), it translates it to "world space" by multiplying it with the MODELVIEW, and then translates it from 3D to 2D by multiplying it with the PROJECT matrix, and that gives it the 2D points on screen, along with the depth (from the screen 'camera'), which it uses to draw pixels. But when one of these matrices are the identity matrix, the points are multiplied with the identity matrix and therefore are not changed, so the matrix has no effect; it does not translate the points, it does not rotate them, it leaves them as-is.

我希望这能澄清一点!

这篇关于glLoadIdentity() 在 OpenGL 中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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