Quick OpenGL Question

Started by
1 comment, last by aleks_1661 19 years, 6 months ago
I'm researching and learning openGL and so far the concepts and structure seems pretty understandable. I do have a question about placing objects in the world (rendering them translated and rotated) in regards to the matrices. To be efficient, do you reset the matrix after you draw each object? Do you push the matrix before you draw, do translating and rotating, and then pop the old one afterwards? I'm just a little confused about this. Like for example, if I'm making a 3D tank game, where there are 5 tanks, each tank being comprised of a main body, a rotating "head" on top, and a vertically raising/lowering turret that comes off the rotating "head". How do you deal with all the matrix stuff in drawing each tank, in each location, facing each direction, with each head facing another direction, and finally the turret being raised to a certain angle? - Jeremiah
Advertisement
basicly the easiest way to draw a tank, as you described is

glLoadIdentity();glPushMatrix();glTranslate();glRotate(); etc  Draw Tank Body  glPushMatrix();  glTranslate();  glRotate(); etc    Draw Tank Turret        glPushmatrix()    glTranslate();    glRotate(); etc      Draw Tank Barrel    glPopMatrix();  glPopMatrix();glPopMatrix();


Basicly the best way to make this usable is to think of all the objects as nodes of a tree, or 'scene graph' as some people like to call them.

World | |-- Tank 1 |    |-- Turret |         |-- Barrel | |-- Tank 2 ... etc

basicly, say you represent each object as a class, which stores some reference to its children,

the basic render function should be something like (peusdo code)

Object::Render(){   glPushMatrix();   glTranslate( pos.x, pos.y, pos.z );   glRotate(    foreach child       child.Render();   glPushMatrix();}


basicly when its all hooked up it looks after itself, you can also dynamically add and remove child nodes and all the transform info is done for you.

if you want to rotate a tank, get the node for tank 1 and call somethong like tank1.rotate( 30 ) that will alter that nodes rotation, be it stored as a matrix, an angle, quaternion etc when you render all the child nodes of tank1 will also be rotated by 30 degrees around the local axes of tank1.

Hope that helps.

Alex

have a class, on the house...

class CObject{CObject();~CObject();void SetPosition( point3D pos )void SetRotation( flaot xrot, float yrot, float zrot );void Move( flaot x, float y, flaot z );void Rotate( flaot x, float y, flaot z );void Render();void AddChildNode( CObject* obj ){  obj->next = children;  children = obj;}private:  Point3D pos;  float   rotation[3]  CObject* nextNode; // the next node at this level  CObject* children; // linked list of child nodes};


no complete but will get you started if your new to C++..

Twitter: [twitter]CaffinePwrdAl[/twitter]

Website: (Closed for maintainance and god knows what else)

Oh, just one other thing, this is all well and good using pushMatrix and glTranslate but if your after top performance its worth doing the matrix math your self when you update an object, then when it comes to rendering the object, push the matrix, glMultMatrix( modelMatrix ), render and pop.

Saves glTranselate etc recalculating the same stuff each frame..

Twitter: [twitter]CaffinePwrdAl[/twitter]

Website: (Closed for maintainance and god knows what else)

This topic is closed to new replies.

Advertisement