PDA

View Full Version : matrixes for objects in a 3D world?



ColacX
06-22-2009, 10:08 AM
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

MaxH
06-22-2009, 10:24 AM
Look at the 4th NeHe Tutorial, titled 'Rotation'.

ColacX
06-22-2009, 01:11 PM
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?

ColacX
06-23-2009, 02:04 AM
hmm i just found out about Quaternion's ill look into that.

ColacX
06-23-2009, 06:36 AM
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.



void moveX(float v){ // any object x axis movement
glPushMatrix();
glLoadMatrixf(positionMatrix);
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();
glLoadMatrixf(rotationMatrix);
glRotatef(v,rotationMatrix[0],rotationMatrix[4],rotationMatrix[8]);
glGetFloatv(GL_MODELVIEW_MATRIX,rotationMatrix);
glPopMatrix();
}

void setView(){// camera
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadMatrixf(rotationMatrix);
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]);

glBegin(GL_QUADS);
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.

_NK47
06-23-2009, 08:58 AM
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.