0

I'm wondering what the best way is to store the positions of objects in a 3D scene. In my case, the scenes are actually levels of a game.

Each Scene has:

  • It's own projection matrix.
  • A list of models to render

Each Model has:

  • The vertex list
  • A GLSL program to render itself
  • A texture
  • A "model" matrix

I'm using GLES 2. So there is no glPushMatrix, glPopMatrix, glTranslate, etc. They aren't available. Instead, I have my own functions which operate on my own Matrix objects.

1) Setup the projection matrix (using an orthographic matrix)

_math_matrix_ortho(&this->ProjectionMatrix, 0.0, 1.0, 0.0, (float)height / (float)width, -1.0f, 1.0f);

2) Load each model

3) Scale each model to make them the right side in proportion to one another. The scaling function adjusts the Model's "model" matrix.

model->modelMatrix.Scale(0.25, 0.25, 0.25);

4) Move each model into place

model->modelMatrix.Translate(2.0, 0.0, 0.0);

7) Render each model

What seems odd to me right off: Is it appropriate to be translating the Model's matrix in order to position the models?

Should I store the model ID along with rotation, scale and translation factors in a file somewhere?

genpfault
  • 51,148
  • 11
  • 85
  • 139
101010
  • 14,866
  • 30
  • 95
  • 172
  • BTW, I asked a related question here. I realized that my question has multiple questions within it, so I'm asking for an answer here to a smaller part of it. http://stackoverflow.com/questions/5687205/positioning-objects-in-a-3d-scene-and-then-figuring-out-what-the-user-clicked-on – 101010 Apr 17 '11 at 02:32

1 Answers1

1

In general, you want as few matrix calculations as possible per frame, as those are pretty expensive. So in your entity classes, store their position, rotation, scaling absolut values in seperate 3D vectors (the rotation maybe even in a quaternion), and only during rendering connect those to a matrix.

Xeo
  • 129,499
  • 52
  • 291
  • 397
  • That makes a lot of sense. Now which matrix should these scaling, rotation and position vectors be relative to? – 101010 Apr 17 '11 at 12:03
  • 1
    Depends. If you implement a scene graph, then these matrices should of course be relative to the parent. If you have no scene graph, just plain models, make them relative to the world. If you have no scene graph, but parent/child relationships, make them relative to the parent again. – Xeo Apr 17 '11 at 12:06
  • Thanks again. I just read up on Scene Graphs. That method makes sense. About using the world... in what cases would the world not have coordinates of the identity matrix or 0,0,0 as its origin? – 101010 Apr 17 '11 at 16:48
  • Uhm... maybe if the world itself is a child of something? ;) One example I can think or though, is when you want to move/rotate and especially scale everything in the world at once. – Xeo Apr 18 '11 at 12:09