在 OpenGL 3.x 中如何正确执行此操作?
在谷歌搜索了很多次之后,我只剩下这个空间来问你下一个问题。
我正在尝试编写一个简单的 OpenGL 3.x 示例来了解新的可编程管道(着色器)的工作原理。本教程 确实很有帮助(并且使用 glut 来使事情变得简单,正如您所看到的)并且作为一个很好的起点。但是当我尝试使用预定义的过剩对象(即茶壶)并尝试以本地方式(如旧的和已弃用的方式)移动或旋转时,噩梦和问题就开始了(glScalef< /code>、
glTranslatef
、glRotatef
、glLoadIdentity
、glMultMatrixf
、 glPushMatrix
和 glPopMatrix
...),但现在对我来说这是不可能的。
如果我尝试使用带有平移的方便的变换矩阵来做到这一点,它会全局移动整个场景(两个或多个对象旋转,不仅仅是一个,即),但不是局部的。我在这里找到了类似的问题 ,但仍然一团糟...(仅适用于 vbos?场景中的每个对象都必须有一个唯一的着色器?,...)
我不知道我是否解释清楚了。我发现的关于该主题的每个教程都始终使用单个对象。如果有人知道任何编写良好的教程或示例代码来解释这一点,我将非常感谢您的帮助。
After googling a lot, I only have this space to ask you the next question.
I'm trying to writing a simple OpenGL 3.x sample to learn how works the new programmable pipeline (shaders). This tutorial is really helpful (and uses glut to keep things simple, as you can see) and great as a starting point. But the nightmare and questions starts when I'm trying to use the predefined glut objects (teapots i.e) and trying to move or rotate in a local way like the old and deprecated way (glScalef
, glTranslatef
, glRotatef
, glLoadIdentity
, glMultMatrixf
, glPushMatrix
and glPopMatrix
...), but for now it's impossible for me.
If I'm trying to do that using a handy transformation matrix with a translation, it moves the whole scene globally (the two or more objects rotates, not only one, i.e.), but not local. I've found this similar question here, but still in a mess... (only works with vbos? every object in the scene has to have a unique shader?,...)
I don't know if I've explained clearly. Every tutorial I've found about this topic always uses a single object. If someone knows any well written tutorial or sample code that explains this, I'll much appreciate your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设,当您说“OpenGL 3.x”时,您的意思是 核心 OpenGL 3.1或更大。也就是说,您没有使用兼容性上下文。
首先,您不能再使用GLUT的预定义对象。您也不能使用 glu 的预定义对象。如果这对您来说限制太大,那么我建议您创建一个兼容性上下文。
所有对象移动的原因是因为您没有在绘制两个对象之间重置制服。统一是从 OpenGL 设置的着色器中的数据,但不会在单个
glDraw*
调用中多次执行着色器时发生更改。以前的 GL 版本中的矩阵函数有效地设置了制服的等价物。因此只需将这些功能转换为统一设置即可。如果你想看使用GL 3.x核心的教程系列,那么你可以看看我的教程系列< /a>.
I will assume that, when you say "OpenGL 3.x", what you mean is core OpenGL 3.1 or greater. That is, you are not using a compatibility context.
First, you cannot use GLUT's predefined objects anymore. Nor can you use glu's predefined objects. If that's too much of a limitation for you, then I suggest you create a compatibility context.
The reason all of your objects move is because you didn't reset the uniforms between drawing the two objects. Uniforms are data in shaders that are set from OpenGL, but will not change over multiple executions of a shader within a single
glDraw*
call. The matrix functions in previous GL versions effectively set the equivalent of uniforms. So simply convert those functions into uniform setting.If you want to see a tutorial series that uses GL 3.x core, then you can look at my tutorial series.
这里的关键是,你需要维持自己的转型等级。 glPushMatrix 在活动堆栈上创建当前矩阵的副本,然后应用一些应用于堆栈的变换。然后画东西,他们就会得到这种转变。 glPopMatrix 在层次结构中上升了一步。
对于制服,您不再有矩阵堆栈。因此,您可以创建当前变换级别矩阵的副本,而不是 glPushMatrix,应用子变换并将该新矩阵加载到统一中。
The key here is, that you need to maintain your own transfomration heirachy. glPushMatrix creates a copy of the current matrix on the active stack, then you apply some transform that's applied to the stack. Then drawing things they will recieve that transformation. glPopMatrix goes one step up in the hierachy.
In the case of Uniforms you no longer have matrix stacks. So instead of glPushMatrix you create a copy of the current transformation level matrix, apply the sub-transform and load that new matrix into the uniform.