开发者

How is the right way to do that in OpenGL 3.x?

开发者 https://www.devze.com 2023-04-01 11:01 出处:网络
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

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, glT开发者_如何学Cranslatef, 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号