Jump to content
  • Advertisement
Sign in to follow this  
cutovoi

OpenGL Rotating a 3ds model with OpenGL

This topic is 4351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi fellows I'm developing an app that rotates an 3DS model. My problem is that the app isn't rotate the correct way. My app instead of rotating the object in it's ownd axis, it rotates around a axis. If I want to rotate in the y-axis, the object is TRANSLATED in the y-axis. I don't know why if I will be very happy if anyone helps. I put the code below Fellows thanks a lot
void DrawScene()
{
   
   glClearColor(0.0, 0.0, 1.0, 0.0); // Limpa o buffer com a cor que está setada
   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   glPushMatrix();
      RotateObject(fRotate);
      theModel.Draw();
   glPopMatrix();
   fRotate+=2.0;
   fTest+=2.0;
   glutSwapBuffers(); //Swapa os buffers, o mesmo que SwapBuffers(HDC)   
}

void ResizeWindow(int iWidth,  int iHeight)
{
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glViewport(0, 0, iWidth, iHeight); //isso aqui parece as dimesões de um RECT misturado com um HDC
   params.fRatio = 1.0*iWidth/iHeight;
   gluPerspective(45, params.fRatio, 0, 1000);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   //Aqui defino a posição da minha câmera, do meu olho e o vetor up(???)
   gluLookAt(params.fX, params.fY, params.fZ, params.fX+params.lfX, params.fY+params.lfY, params.fZ+params.lfZ, 0.0f, 1.0f, 0.0f);
}

void MoveCamera(float fAngle)
{
   params.lfX = sin(fAngle);
   params.lfZ = -cos(fAngle);
   glLoadIdentity();
   gluLookAt(params.fX, params.fY, params.fZ, params.fX+params.lfX, params.fY+params.lfY, params.fZ+params.lfZ, 0.0f, 1.0f, 0.0f);
}

P.S: params is just a struct that contains 9 float values.

Share this post


Link to post
Share on other sites
Advertisement
Where's the rotate object function? Can't say anything about the code without seeing it. The draw scene-function seems OK otherwise.

-Riku

Share this post


Link to post
Share on other sites
Ok fellows, the code for my RotateObject is this:

void RotateObject(float fAngle)
{
glRotatef(fAngle, 0.0, 1.0, 0.0);
}

It only this. One more question: how can I multiply a glIdentity() matrix with the object matrix?

Share this post


Link to post
Share on other sites
im new to OpenGL but try this:

// clear buffers etc

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();
// rotate
// draw
glPopMatrix();

If you want to multiply with an Identity matrix use glLoadIdentity. this will identify the current matrix and multiply it with the object matrix.
something like:

glPushMatrix();
glLoadIdentity();
glMultMatrixf(objectmtx);
glPopMatrix();

hope this helps

Share this post


Link to post
Share on other sites
Did you set up your matrix mode correctly (GL_MODELVIEW)?
Your RotateObject-function seems correct, so it should work.

Why would you want to multiply your object matrix with the identity as this wouldn't change your matrix?

Try this in your render scene:

//set up camera orientation and position, note that camera position is inverse (cam.pos = (1,0,0) would mean translating the world by (-1,0,0))
glLoadMatrixf(cameramatrix) //will set the matrix to the cameramatrix, no need for glLoadIdentity()

glPushMatrix()
//scale
//rotate
//translate
//or: keep your object matrix externally e.g. in your object instance and just use glMultMatrixf(objecmatrix)
//draw
glPopMatrix()

Share this post


Link to post
Share on other sites
Your problem seems to be post-mutiply and pre-multiply of matrices.
i.e. transform-matrix * Object is not equal to Object * transform-matrix.
read this

Picked this up from the OpenGL FAQ.


9.070 How do I transform my objects around a fixed coordinate system rather than the object's local coordinate system?


If you rotate an object around its Y-axis, you'll find that the X- and Z-axes rotate with the object. A subsequent rotation around one of these axes rotates around the newly transformed axis and not the original axis. It's often desirable to perform transformations in a fixed coordinate system rather than the object’s local coordinate system.

The OpenGL Game Developer’s FAQ contains information on using quaternions to store rotations, which may be useful in solving this problem.

The root cause of the problem is that OpenGL matrix operations postmultiply onto the matrix stack, thus causing transformations to occur in object space. To affect screen space transformations, you need to premultiply. OpenGL doesn't provide a mode switch for the order of matrix multiplication, so you need to premultiply by hand. An application might implement this by retrieving the current matrix after each frame. The application multiplies new transformations for the next frame on top of an identity matrix and multiplies the accumulated current transformations (from the last frame) onto those transformations using glMultMatrix().

You need to be aware that retrieving the ModelView matrix once per frame might have a detrimental impact on your application’s performance. However, you need to benchmark this operation, because the performance will vary from one implementation to the next.

Share this post


Link to post
Share on other sites
Tks for all for the support, but how can I do this:
"To affect screen space transformations, you need to premultiply. OpenGL doesn't provide a mode switch for the order of matrix multiplication, so you need to premultiply by hand. "
What it means "premultiply"? Do I need to create a 4x4 vertex to make all this things? Sorry but it doesn't clear enough for me.

this is the source that I draw the model:

void DrawModel()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-600);
glRotatef(rot_x, 1.0, 0.0, 0.0);
glRotatef(rot_y, 0.0, 1.0, 0.0);
glRotatef(rot_z, 0.0, 0.0, 1.0);
//glTranslatef(-theVector.x,-theVector.y,-theVector.z);
//glLoadIdentity();
theModel.Draw();
glFlush();
}


And this is my code for rederization:
void DrawScene()
{
glClearColor(0.0, 0.0, 1.0, 0.0); // Limpa o buffer com a cor que está setada
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
DrawModel();
glutSwapBuffers(); //Swapa os buffers, o mesmo que SwapBuffers(HDC)
}

Share this post


Link to post
Share on other sites
Tks for all for the support, but how can I do this:
"To affect screen space transformations, you need to premultiply. OpenGL doesn't provide a mode switch for the order of matrix multiplication, so you need to premultiply by hand. "
What it means "premultiply"? Do I need to create a 4x4 vertex to make all this things? Sorry but it doesn't clear enough for me.

this is the source that I draw the model:

void DrawModel()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-600);
glRotatef(rot_x, 1.0, 0.0, 0.0);
glRotatef(rot_y, 0.0, 1.0, 0.0);
glRotatef(rot_z, 0.0, 0.0, 1.0);
//glTranslatef(-theVector.x,-theVector.y,-theVector.z);
//glLoadIdentity();
theModel.Draw();
glFlush();
}


And this is my code for rederization:

void DrawScene()
{
glClearColor(0.0, 0.0, 1.0, 0.0); // Limpa o buffer com a cor que está setada
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
DrawModel();
glutSwapBuffers(); //Swapa os buffers, o mesmo que SwapBuffers(HDC)
}

Share this post


Link to post
Share on other sites
Quote:
Original post by cutovoi
What it means "premultiply"? Do I need to create a 4x4 vertex to make all this things? Sorry but it doesn't clear enough for me.


1) Premultiply means -
New ModelView Matrix = (Your Transformation matrix) * Current ModelView matrix.

2) Postmultiply means-
New ModelView Matrix = Current ModelView matrix * (Your Transformation matrix).

When you do glRotatef case 2 happens. You need to do case 1 to achive the result you are looking for.

Yes you will have to make or use a pre written matrix library. When they say "by hand" they mean you have to write code to do premultiplication. OpenGL will not do premultiplication. You will need a matrix library/class to do it. There are a lot of articles about matrix math on gamedev and all over the web.
Browse this.
And read this

If you want more info... this!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!