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

查看:219
本文介绍了如何在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天全站免登陆