如何在 OpenGL 中获取模型视图和投影矩阵?
我正在尝试使用 OpenGL 着色语言 (GLSL) 1.5 版来制作顶点和几何着色器。
我了解到,在 GLSL 1.5 版本中,像 gl_ModelViewProjectionMatrix
这样的内置变量已被弃用,因此您必须手动传递它们。如果我已经设置了模型视图和投影矩阵(例如使用 gluLookAt 和 gluPerspective ),那么如何将矩阵传递到顶点和几何着色器中?我做了一些搜索,有些网站似乎提到了一个函数 glGetMatrix()
,但我在任何官方文档中都找不到该函数,并且它似乎不存在于我的实现中正在使用(当我尝试使用该函数编译它时,出现编译错误未知标识符:glGetMatrix
)。
I am trying to use the OpenGL Shading Language (GLSL) version 1.5 to make vertex and geometry shaders.
I have learned that in GLSL version 1.5, the built-in variables like gl_ModelViewProjectionMatrix
are deprecated so you have to pass them in manually. If I have already set the modelview and projection matrices (using gluLookAt
and gluPerspective
for example) then how do I get the matrices to pass into the vertex and geometry shaders? I've done some searching and some sites seem to mention a function glGetMatrix()
, but I can't find that function in any official documentation, and it doesn't seem to exist in the implementation I am using (I get a compilation error unknown identifier: glGetMatrix
when I try to compile it with that function).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嘿,让我们放慢一点:) 是的,您确实通过
glGetFloatv(GL_MODELVIEW_MATRIX, ptr)
接收了矩阵... 但这绝对不是您应该在这里做的事情!让我解释一下:
在 GLSL 中,像
gl_ModelViewProjectionMatrix
这样的内置变量或像ftransform()
这样的函数已被弃用 - 这是正确的,但这只是因为 >整个矩阵堆栈在 GL 3.x 中已被弃用,您应该使用自己的矩阵堆栈(或使用任何其他解决方案,矩阵堆栈很有帮助,但不是必需的!)。如果您仍在使用矩阵堆栈,那么您将依赖 OpenGL 2.x 或 1.x 的功能。没关系,因为由于 GL 兼容性配置文件,现代显卡仍然支持所有这些 - 切换到新的 GL 版本很好,但您现在可以继续使用它。
但是如果您使用旧版本的OpenGL(带有矩阵堆栈),也请使用旧版本的GLSL。尝试1.2,因为更高版本(包括您的1.5)被设计为与OpenGL3兼容,其中投影或模型视图矩阵等内容不再存在于 OpenGL 中,并且如果需要,预计将作为自定义、用户定义的统一变量显式传递。
OpenGL 和 GLSL 版本之间的对应关系曾经有点棘手(在他们清理版本编号以匹配之前),但它应该或多或少类似于:
所以,长话短说 - 着色器内置制服已被弃用,因为相应的OpenGL 中的功能也已被弃用;要么选择更高版本的 OpenGL,要么选择更低版本的 GLSL。
Hey, let's slow down a bit here :) Yes, that's true that you receive the matrix by
glGetFloatv(GL_MODELVIEW_MATRIX, ptr)
... But that's definitely not the thing you should do here!Let me explain:
In GLSL, built-in variables like
gl_ModelViewProjectionMatrix
or functions likeftransform()
are deprecated - that's right, but that's only because the whole matrix stack is deprecated in GL 3.x and you're supposed to use your own matrix stack (or use any other solution, a matrix stack is helpful but isn't obligatory!).If you're still using the matrix stack, then you're relying on functionality from OpenGL 2.x or 1.x. That's okay since all of this is still supported on modern graphics cards because of the GL compatibility profile - it's good to switch to a new GL version, but you can stay with this for now.
But if you are using an older version of OpenGL (with matrix stack), also use an older version of GLSL. Try 1.2, because higher versions (including your 1.5) are designed to be compatible with OpenGL3, where things such as projection or modelview matrices no longer exist in OpenGL and are expected to be passed explicitly as custom, user-defined
uniform
variables if needed.The correspondence between OpenGL and GLSL versions used to be a bit tricky (before they cleaned up the version numbering to match), but it should be more or less similar to:
So, long story short - the shader builtin uniforms are deprecated because the corresponding functionality in OpenGL is also deprecated; either go for a higher version of OpenGL or a lower version of GLSL.
要获取任一矩阵,请使用常量 GL_MODELVIEW_MATRIX 或 GL_PROJECTION_MATRIX 与 glGetxxxx:
To get either matrix you use the constants GL_MODELVIEW_MATRIX or GL_PROJECTION_MATRIX with glGetxxxx: