Sign in to follow this  
walkaways

OpenGL Rotating submodel

Recommended Posts

walkaways    100
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
karwosts    840
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.

Quote:
(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
walkaways    100
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.

so:
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
karwosts    840
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
walkaways    100
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
karwosts    840
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
doCarRotations()
drawCar()
glPopMatrix() //we throw away the current matrix, and are back to what we had when we called pushmatrix
glPushMatrix() //save the matrix again
doCar2Rotations()
drawCar2()
glPopMatrix()




Share this post


Link to post
Share on other sites
walkaways    100
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.


//x
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
karwosts    840
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  

  • Similar Content

    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
  • Popular Now