# Thread: matrixes for objects in a 3D world?

1. ## matrixes for objects in a 3D world?

If i want to make a 3d world with objects that can rotate around their own axises do each of the objects have to have their own private rotation-matrix if i want them to be displayed correctly?

or is there a simpler way using glRotate?

correct me if i'm wrong but glRotate only rotates the object around the camera-axises

and do they all maybee need a position-matrix too?

i want all my objects to be able to move and rotate around their own x-axis y-axis and z-axis

2. ## Re: matrixes for objects in a 3D world?

Look at the 4th NeHe Tutorial, titled 'Rotation'.

3. ## Re: matrixes for objects in a 3D world?

and in his tutorial they rotate the object around the world axis.

his tutorials doesn't really cover object space orientations.

and he has only one object and a camera there

i want to make 3d world with multiple objects and multiple cameras.

so whats the easiest way to make an object rotate and move along its own axises?

4. ## Re: matrixes for objects in a 3D world?

hmm i just found out about Quaternion's ill look into that.

5. ## Re: matrixes for objects in a 3D world?

Okay so i made every object with a rotation matrix and a position matrix and it works, without using quarternions. Here some code if anyone is interested. Took me a while to figure this out.

Code :
```void moveX(float v){ // any object x axis movement
glPushMatrix();
glTranslatef(rotationMatrix[0]*v,rotationMatrix[4]*v,rotationMatrix[8]*v);
glGetFloatv(GL_MODELVIEW_MATRIX,positionMatrix);
glPopMatrix();

}
void rotateX(float v){ //any object x axis rotation
glPushMatrix();
glRotatef(v,rotationMatrix[0],rotationMatrix[4],rotationMatrix[8]);
glGetFloatv(GL_MODELVIEW_MATRIX,rotationMatrix);
glPopMatrix();
}

void setView(){// camera
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glMultMatrixf(positionMatrix);
//wrong implementation might have the inverse effect
//rotate first move second will cause the fps effect
//move first rotate second will cause the fixed camera effect
//all other models will/should draw with the camera matrix defaults added to them
}

void draw(){ // model
glPushMatrix();
glMultMatrixf(positionMatrix);
glMultMatrixf(rotationMatrix);

glColor4f(red,green,blue,alpha);
glBindTexture(GL_TEXTURE_2D, texture[0]);

glTexCoord2f(0.0f, 0.0f); glVertex3f(-scale,-scale,-scale);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-scale,-scale,+scale);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-scale,+scale,+scale);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-scale,+scale,-scale);

glTexCoord2f(0.0f, 0.0f); glVertex3f(+scale,-scale,-scale);
glTexCoord2f(0.0f, 1.0f); glVertex3f(+scale,-scale,+scale);
glTexCoord2f(1.0f, 1.0f); glVertex3f(+scale,+scale,+scale);
glTexCoord2f(1.0f, 0.0f); glVertex3f(+scale,+scale,-scale);

glTexCoord2f(0.0f, 0.0f); glVertex3f(-scale,-scale,-scale);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-scale,-scale,+scale);
glTexCoord2f(1.0f, 1.0f); glVertex3f(+scale,-scale,+scale);
glTexCoord2f(1.0f, 0.0f); glVertex3f(+scale,-scale,-scale);

glTexCoord2f(0.0f, 0.0f); glVertex3f(-scale,+scale,-scale);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-scale,+scale,+scale);
glTexCoord2f(1.0f, 1.0f); glVertex3f(+scale,+scale,+scale);
glTexCoord2f(1.0f, 0.0f); glVertex3f(+scale,+scale,-scale);

glVertex3f(-scale,-scale,-scale);
glVertex3f(-scale,+scale,-scale);
glVertex3f(+scale,+scale,-scale);
glVertex3f(+scale,-scale,-scale);

glVertex3f(-scale,-scale,+scale);
glVertex3f(-scale,+scale,+scale);
glVertex3f(+scale,+scale,+scale);
glVertex3f(+scale,-scale,+scale);
glEnd();

glPopMatrix();
}```

I used opengl built in matrix operations which gives a really bad cpu performance. I'll fix my own matrix operation classes later.
glGetFloatv(); is the one causing heavy cpu drain.

6. ## Re: matrixes for objects in a 3D world?

you could concatenate them directly without glGet. all query functions should be avoided and called only at startup/loading times. best to you use own matrix classes and glLoadMatrix them, which is also the only way for OpenGL 3.0 afaik.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•