Using Vertex Shader to dynamically move objects?

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

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?

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:
[b] 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? [/b]

use glRotatef, glTranslatef, glScalef or other matrix operations.

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 TransformMatrix

and rather than

  
gl_Position = gl_ModelViewMatrix * gl_Vertex;

I would do

  
gl_Position = TransformMatrix * gl_Vertex;

CD

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.

The RedBook has all the matrices in the appendix.

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:
  3. 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);
  1. Render your object:
glDrawElements(...);
  1. 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 3glRotate + 1glTranslate 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 :slight_smile:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.