PDA

View Full Version : Using Vertex Shader to dynamically move objects?



CelticDaddio
10-17-2006, 11:50 AM
I have an object file which specifies the initial vertex locations for an object.

I also have a dynamics file which specifies the subsequent positions of the object at various time steps.

i.e.

Time x y z yaw pitch roll
0 0 0 0 0.0 0.0 0.0
0.1 0 2 0 0.0 0.0 0.0
0.2 0 5 0 0.0 0.0 0.0
0.3 0 20 0 0.0 0.0 0.0
etc

What I want to do is use a VBO to download the vertex data to the GPU, then move the vertices via a vertex shader.

What I am attempting to do is avoid downloading updated vertex data each time through my sim loop. It seems that if I only have to send a new matrix which the shader uses to translate/rotate/etc each vertex, I can speed things up. Does this make sense?

CD

Korval
10-17-2006, 04:55 PM
Yes, it does make sense. In fact, it makes so much sense that everybody does it. And has done it since the earliest days of OpenGL, back before there were even vertex arrays, let alone VBOs and shaders.

Since you already know what it is you want to do, what specifically is your question?

CelticDaddio
10-17-2006, 05:35 PM
I suppose my question is how to do that... or more specifically, given sequential positions and orientations of the object, how to form the matrix which I would use to transform the verts.

CD


Originally posted by Korval:
Yes, it does make sense. In fact, it makes so much sense that everybody does it. And has done it since the earliest days of OpenGL, back before there were even vertex arrays, let alone VBOs and shaders.

Since you already know what it is you want to do, what specifically is your question?

glAren
10-18-2006, 01:44 AM
use glRotatef, glTranslatef, glScalef or other matrix operations.

CelticDaddio
10-18-2006, 06:03 AM
More specifically, what I want to do, is use my input file of pos and ori to calculate a series of transformation matrices. Then store them and at each time step, send them down the next matrix to the vertex shader

In my shader, I would have



mat4 TransformMatrixand rather than



gl_Position = gl_ModelViewMatrix * gl_Vertex;I would do



gl_Position = TransformMatrix * gl_Vertex;CD

Korval
10-18-2006, 10:52 AM
Is you question, "How do I generate the matrix in question?"

If so, that's not really an OpenGL question. What kind of algorithm you use to craft your matrices is beyond the bounds of GL.

Overmind
10-19-2006, 12:28 AM
The RedBook has all the matrices in the appendix.

k_szczech
10-19-2006, 06:06 AM
I don't know if you're aware of this, but when you render your geometry, all vertices will be transformed by CURRENT modelview and projection matrix, but such transformed vertices are NOT written back to VBO.

Init:
1. Load vertices to VBO
2. Load positions/orientations to arrays
Rander:
1. Setup modelview matrix:

glPushMatrix();
glRotatef(roll, 0, 0, 1);
glRotatef(pitch, 1, 0, 0);
glRotatef(heading, 0, 1, 0);
glTranslatef(position.x, position.y, position.z);2. Render your object:

glDrawElements(...);3. Restore previous matrix state

glPopMatrix();As you can see you only have to upload data to VBO only once. Perhaps by precalculating all matrices and calling glMultMatrix instead of 3*glRotate + 1*glTranslate you will be able to save a few nanoseconds per frame, but is it worth memory consumption?

Conclusion:
-no need to upload anything to VBO every frame
-no need to use vertex shader
-no need to calculate or store matrix contents by yourself
-no need to do things the hard way if easy way is equally fast and more flexible :)