Right now I’m rotating an object, lets call it ‘mainBody’ but attached to the mainBody are several attachments and when I rotate my mainBody they are supposed to rotate along as well, but right now they don’t. I’m not using a parent/child system. The main body has an array of it’s attachments and draws them in the mainBody draw function.
The glPopMatrix() from the main body is done after the equipment items are drawn.
I know I managed to do this in the past with pushMatrix(); and popMatrix(); But now it doesn’t seem to work.
I’m using c++, opengl, and glm.
Here is some code that shows you what I have right now:
{
setupStartModelMatrix(); //<---- Has glPushMatrix();
setupModelviewMatrix(); //<--- has all the gml stuff
drawMainBody();
}
if(mNumberOfEquipementOwned != 0)
{
for(int i = 0; i < mNumberOfEquipementOwned; i++)
{
//obj is retrieved with a function
obj->render();
}
}
setupEndModelMatrix(); // <--- Has glPopMatrix();
}
And the glm code
void GameObject::setupModelviewMatrix()
{
glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.f);
glm::mat4 Model = glm::scale( glm::mat4(1.0f), glm::vec3(1.0f));
glm::mat4 ViewTranslate = glm::translate( glm::mat4(1.0f), glm::vec3(mPosition.x, mPosition.y, mPosition.z));
glm::mat4 ViewRotateX = glm::rotate( ViewTranslate, mRotation.x, glm::vec3(1.0f, 0.0f, 0.0f));
glm::mat4 ViewRotateY = glm::rotate( ViewRotateX, mRotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 View = glm::rotate( ViewRotateY, mRotation.z, glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 MVP = View * Model;
glUniformMatrix4fv( 0, 1, GL_FALSE, glm::value_ptr(MVP));
glLoadMatrixf(glm::value_ptr(MVP));
}
And the draw equipement code
void Gun::render()
{
glPushMatrix();
setupModelviewMatrix();
mMesh->render();
glPopMatrix();
}
Because of Tim I noticed my matrix wasn’t passed trough too the others. When I finally understood what he was saying it’s a easy fix so 100% of the props go to him.
Now the code for anyone who might need it.
And the glm code
And the draw equipement code
And thats about it.
I’m not 100% sure if this code works as I had to fix it for my self in a different way because of my object construction but this should at least give a good head start for anyone with the same problem.
Also no glPopMatrix() or glPushMatrix() is required to make this work.