你如何在 OpenGL 中获得模型视图和投影矩阵? [英] How do you get the modelview and projection matrices in OpenGL?

查看:37
本文介绍了你如何在 OpenGL 中获得模型视图和投影矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 OpenGL 着色语言 (GLSL) 1.5 版来制作顶点和几何着色器.

I am trying to use the OpenGL Shading Language (GLSL) version 1.5 to make vertex and geometry shaders.

我了解到,在 GLSL 1.5 版中,诸如 gl_ModelViewProjectionMatrix 之类的内置变量已被弃用,因此您必须手动传入它们.如果我已经设置了模型视图和投影矩阵(例如使用 gluLookAtgluPerspective),那么我如何让矩阵传递到顶点和几何着色器中?我已经做了一些搜索,有些网站似乎提到了一个函数 glGetMatrix(),但我在任何官方文档中都找不到该函数,而且它似乎不存在于我的实现中正在使用(当我尝试使用该函数进行编译时,出现编译错误 unknown identifier: glGetMatrix).

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).

推荐答案

嘿,让我们在这里放慢一点 :) 是的,您确实通过 glGetFloatv(GL_MODELVIEW_MATRIX, ptr) 接收矩阵... 但这绝对不是你应该在这里做的事情!

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!

让我解释一下:

在 GLSL 中,不推荐使用 gl_ModelViewProjectionMatrix 等内置变量或 ftransform() 等函数 - 没错,但这只是因为 整个矩阵堆栈是在 GL 3.x 中已弃用,您应该使用自己的矩阵堆栈(或使用任何其他解决方案,矩阵堆栈很有帮助,但不是必须的!).

In GLSL, built-in variables like gl_ModelViewProjectionMatrix or functions like ftransform() 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!).

如果您仍在使用矩阵堆栈,那么您将依赖 OpenGL 2.x 或 1.x 的功能.没关系,由于 GL 兼容性配置文件,现代显卡仍然支持所有这些 - 切换到新的 GL 版本很好,但您现在可以继续使用.

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.

但是如果您使用的是较旧版本的 OpenGL(带有矩阵堆栈),也请使用较旧版本的 GLSL. 尝试 1.2,因为更高版本(包括您的 1.5)旨在兼容使用 OpenGL3,其中 OpenGL 中不再存在诸如投影或模型视图矩阵之类的东西,如果需要,预计将作为自定义、用户定义的 uniform 变量显式传递.

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.

OpenGL 和 GLSL 版本之间的对应关系曾经有点棘手(在他们清理版本编号以匹配之前),但应该或多或少类似于:

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:

GL    GLSL

4.1 - 4.1
4.0 - 4.0
3.3 - 3.3
3.2 - 1.5
3.1 - 1.4
3.0 - 1.3
2.x and lower - 1.2 and lower

所以,长话短说 - 着色器内置制服已被弃用,因为 OpenGL 中的相应功能也已被弃用;要么选择更高版本的 OpenGL,要么选择更低版本的 GLSL.

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.

这篇关于你如何在 OpenGL 中获得模型视图和投影矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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