Sign in to follow this  

OpenGL Rotating submodel

Recommended Posts

I load an OBJ model into OpenGL. The model is a car. It consists of 5 submodels (groups) : frame, wheel1, wheel2, wheel3, wheel4. I use a rotation matrix to rotate the entire object around the 0 point (0,0,0). For each coordinate in the model I multiple with a rotation matrix, depending on the input. I made the functions so that the submodels can rotate around their midpoint (origin). The rotation of models around the point (0,0,0) or around their midpoint/origin works fine. However, I want to rotate the wheels forward, while the model/car is rotating around its origin. I thought to use rotation matrix rY. This is fine if the model is in position of Image A. However in Image B: the rotation matrix the Z coordinate does not change. So the positions aren't right anymore, if the model matrix is rotated. (See Image B.) I'm guessing I should use all 3 rotation matrixes somehow. The rotation matrix would depend on the 3 angles of rotation of the original model, but I dont know how. How would you rotate the wheels forward? (Could I also use a standard function of OpenGL?)

Share this post

Link to post
Share on other sites
Are you performing all of your rotations as 3x3 matrices? In 3d graphics transformation matrices are typically represented as 4x4 matrices, so that each matrix can store a rotation and a translation. If you only have 3x3 matrices I think that might be a problem for you, because you don't always want a rotation about an object's midpoint.

To do this properly you'd want to first rotate the wheel forward around its own midpoint, translate it to the proper position in the car, and then rotate it again around the center of the car.

(Could I also use a standard function of OpenGL?)

You can do this with OpenGL fixed function transformations. (These are depreciated, and you should learn to handle all of your matrix rotations yourself for forward compatibility, but they still work for now.) However you have to apply the transformations in reverse order due to the way the matrix multiplication stack works.

Assuming that your wheels rotate around the X axis by 45 degrees, and that the wheels are located at (+/- 1, 0, +/- 1) XYZ coordinates. Lets say you want to rotate the car by 30 degrees around the Y axis (up vector). This is what it would look like:

glLoadIdentity() //Reset everything
glRotatef(30,0,1,0)//Rotate the car
Draw the Car frame

For Each Wheel(
glPushMatrix() //Save the original rotation matrix
glTranslatef(+/-1, 0, +/-1) //Move the wheel into position
glRotatef(45,1,0,0)//Rotate your wheel around its midpoint
Draw a wheel
glPopMatrix() //Load the original rotation matrix

[I think I've got the rotations in the right order, but it has been a while since I used the fixed function equations, so someone can correct me if I've misspoke]

Share this post

Link to post
Share on other sites
The matrix is 3x3 (or 3xPolygonSize). I've the matrix stored in this way

It now works when I rotate the model around one axis (X,Y or Z) including the wheels. So Yaw, Roll or Pitch the object:
When using Yaw, Roll or Pitch the model goes back to its original position.
(inside: not drawn)

However when I do 2 or 3 rotations on the total model (for example coordinates*Ry and coordinates*Rz) it doesn't work. In other words, with for example Yaw and Pitch together the model doesn't get back in its original position.

I stored the total rotation of the angles, and wanted to rotate back with it.

So the problem is to get it back in it's original positions, when the model itself is rotated using 2 or 3 rotation matrixes.
I thought to do the rotations undone. (substracted), but this seems to work only in the first case.

I found out that if I do this in the opposite order that it was done, I get back into the intial position.

pitch -92
yaw -92

However, if I do it in a different order it gets rotated, why is this?
Should I keep a list of each rotation that is done, or the order that the rotations of the total model is done?

[Edited by - walkaways on March 11, 2010 12:11:33 PM]

Share this post

Link to post
Share on other sites
Yes the order of rotations is very important, and if you change the order you will get different behavior.

You should take a look at the OpenGL Red Book, it will help you learn some fundamental concepts of 3d graphics, which should help you understand all of the questions you are asking.

There is a free online version (it is quite old, but the transformation concepts are still the same). Specifically in your case read through chapter 3: "Viewing"

Red Book Online Version

Share this post

Link to post
Share on other sites
I'll look into the book.
Because the order of rotation is important, should I store all rotations of the model in an array?
This would make it easy to reverse them all.

For example

orignal state -> pitch 30* -> roll 30* -> pitch 40* -> roll 30*

To get back to the original matrix position, one would go trough an array that contains this rotation in opposite direction, right?

curent_state -> roll -30* -> pitch -40* -> roll -30* -> pitch -30* -> original state

So to rotate the wheels in this situation from the original state:

roll -30* - pitch -40* - roll -30* - pitch -30* -> original state
rotate wheels
original state + rotated wheels -> pitch 30* - roll 30* - pitch 40* - roll 30*

Is it the 'right' way to keep an array of all rotations you've done with the model?

Share this post

Link to post
Share on other sites
For what reason do you feel the need to reverse your rotations? If you needed to get a matrix that "undid" all of your operations, then you would need to find the inverse of the original rotation matrix. You can do this by remembering all of the operations in reverse order, or there are other techniques for inverting a matrix that you can use. However I can't see why you would need to do this in your situation.

If you want to undo all your rotations (to draw another object for example), just reload the identity matrix, or better yet just use glPushMatrix/glPopMatrix (more appropriate for hierarchies).

For example if you have two cars you want to draw in different orientations:

//Do anything else in your scene you want.

glPushMatrix() //Store the current matrix on the stack
glPopMatrix() //we throw away the current matrix, and are back to what we had when we called pushmatrix
glPushMatrix() //save the matrix again

Share this post

Link to post
Share on other sites
I want to rotate the matrix back to the original position, so that I can rotate the wheels forward.

If I have the car in the following position:

I could Roll, Yaw or Pitch it around the midpoint, but neither of these will cause the wheel to roll forward.

If I rotate back to the original state, its easy to use Roll to move the wheel forward. Then I could rotate back to position in picture 1 and draw screen again.

Should I do it differently?

Side note: Im applying rotation matrixes directly to the original matrix, so the original matrix is not stored anywhere.

Matrix[i] = ( cos( Theta * (PI/180)) * Matrix[i]) + (sin(Theta * (PI/180)))*Matrix[i+2];

// z
Matrix[i+2] = (-sin( Theta * (PI/180)) * Matrix[i]) + (cos(Theta * (PI/180)))*Matrix[i+2];

Share this post

Link to post
Share on other sites
You should not have to translate the wheel back to 0,0,0 to rotate it. If you rotate the wheel last, it will rotate on its local axis (assuming you perform the matrix operation in the right order). It should not matter if the car (and thus wheel) is upside down, flipped over, whatever. Put the wheel wherever you want it, and if you call glRotate last it will rotate on its own axis.

If you look at the sequence that I put in my first post, this should be all you need.

It looks like you are using your own matrix solution, which might be confusing you. Do you understand the difference between pre-multiplying or post-multiplying with your rotation matrix? It is not commutative. I will suggest that you try to get it working with OpenGL transformations until you get a better grasp for transformations, then you can go back and implement your own solution again if you prefer.

Anyway this is all covered in the Red Book chapter I mentioned, specifically pay attention to the section "Thinking about Transformations". Go read that and think about it for a few hours, and see if it makes more sense to you. If after studying that you're still having trouble then come back and we'll see what we can do.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Partner Spotlight

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
    • By cebugdev
      hi guys, 
      are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
      Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic 
      let me know if you guys have recommendations.
      Thank you in advance!
    • By dud3
      How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below? 
      Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
      Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.
      The video shows the difference between blender and my rotation:
    • By Defend
      I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
      My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
      * make lots of VAO/VBO pairs and flip through them to render different objects, or
      * make one big VBO and jump around its memory to render different objects. 
      I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
      If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?
    • By test opty
      Hello all,
      On my Windows 7 x64 machine I wrote the code below on VS 2017 and ran it.
      #include <glad/glad.h>  #include <GLFW/glfw3.h> #include <std_lib_facilities_4.h> using namespace std; void framebuffer_size_callback(GLFWwindow* window , int width, int height) {     glViewport(0, 0, width, height); } //****************************** void processInput(GLFWwindow* window) {     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)         glfwSetWindowShouldClose(window, true); } //********************************* int main() {     glfwInit();     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);     //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);     GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);     if (window == nullptr)     {         cout << "Failed to create GLFW window" << endl;         glfwTerminate();         return -1;     }     glfwMakeContextCurrent(window);     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))     {         cout << "Failed to initialize GLAD" << endl;         return -1;     }     glViewport(0, 0, 600, 480);     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);     glClear(GL_COLOR_BUFFER_BIT);     while (!glfwWindowShouldClose(window))     {         processInput(window);         glfwSwapBuffers(window);         glfwPollEvents();     }     glfwTerminate();     return 0; }  
      The result should be a fixed dark green-blueish color as the end of here. But the color of my window turns from black to green-blueish repeatedly in high speed! I thought it might be a problem with my Graphics card driver but I've updated it and it's: NVIDIA GeForce GTX 750 Ti.
      What is the problem and how to solve it please?
  • Popular Now