PDA

View Full Version : Object not rendering after rotation



Crosswar
04-23-2015, 05:27 PM
Hello, I have recently started working on a project where I trying to simulate a rocket taking off, the problem I am currently experiencing is regarding the rendering of the object, I translate it on it's "local axis" and it moves fine however when I rotate it, it only displays the 1 degree rotation based on

glRotatef(deg, 1, 0, 0);
where if the degree value would be 1 it would rotate on the said axis by 1 and only render the first rotation as in if I were to press the left arrow it rotates it by 1 degree, it shows the rotation and any other rotations done afterwards are not rendered however are still applied.
If I were to press the left key lets say 45 times it and the degree would still be 1 it will render the first rotation of 1 degree then rotate the coord system by another 44 degrees without actually showing the rotation of the object.
Moving coordinate system and allowing translation on said system.
Here is the code I am using for the rotation and translation and how I am calling it in the main function.

Rocket.cpp


#include "Rocket.h"

Rocket::Rocket(float x, float y, float z)
{
Transform[0] = 1.0f;
Transform[5] = 1.0f;
Transform[10] = -1.0f;
Transform[15] = 1.0f;
Transform[12] = x; Transform[13] = y; Transform[14] = z;

Right=&Transform[0];
Up=&Transform[4];
Forward=&Transform[8];
Position=&Transform[12];
}

Rocket::~Rocket(){}

void Rocket::locMove(float x, float y, float z, float distance)
{
float dx=x*Transform[0] + y*Transform[4] + z*Transform[8];
float dy=x*Transform[1] + y*Transform[5] + z*Transform[9];
float dz=x*Transform[2] + y*Transform[6] + z*Transform[10];
Transform[12] += dx * distance;
Transform[13] += dy * distance;
Transform[14] += dz * distance;
}

void Rocket::locRot(float deg, float x, float y, float z)
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadMatrixf(Transform);
glRotatef(deg, x,y,z);
glGetFloatv(GL_MODELVIEW_MATRIX, Transform);
glPopMatrix();

degG = deg;
xG = x;
yG = y;
zG = z;
}

void Rocket::drawRocket()
{
glPushMatrix();
glColor3ub(229,228,226);
glTranslatef(Transform[12], Transform[13], Transform[14]);
glRotatef(degG, xG,yG,zG);
glutSolidCube(15);
glPopMatrix();
}



main.cpp


Rocket rock(rockX, rockY, rockZ);

draw3d()
{
rock.drawRocket();
}


The main class is quite large and the only bits of code related is the instancing of a new rocket and calling the draw rocket function, alongside various rock.locRotate and rock.locMove calls within the keypress function.
My best guess as to how this could be fixed would be an update function for the rocket however I do not know what would be inside said function.
Any help would be appreciated as this has been an issue of mine for a few weeks now, getting into this stage just a few days ago.
Thanks again.

GClements
04-23-2015, 05:49 PM
I suggest that you don't use any OpenGL matrix functions other than glLoadMatrix() or possibly glMultMatrix(). If you need to access (or even just store) the matrices in the application, it's easier (and more efficient) to just generate them yourself.

You can find the actual matrices generated by functions such as glRotate() in the online reference pages (https://www.opengl.org/sdk/docs/man2/). Or you can use GLM instead.

As for your specific problem, the Transform matrix contains the cumulative result of many translation and rotation operations, but degG,xG,yG,zG contain only the most recent rotation. I'm not sure why you're using glTranslate() and glRotate() in drawRocket() when (so far as I can tell) you could just use glMultMatrix(Transform) instead.

Unless xG,yG,zG are constant (i.e. you're always rotating about the same axis), the only feasible approach for calculating the cumulative rotation in axis-and-angle form is to calculate it in either matrix or quaternion form then convert that to axis-and-angle (which is pointless if you're just going to use glRotate() to convert it straight back to matrix form).

Crosswar
04-24-2015, 08:32 AM
That did actually work and yes I was told about storing matrices and calling them if needed with the glm library, had a look at it and it does seem really useful especially if I will deal with quaternions in the future, but for now this simple implementation is all I needed, and plus I am currently using version 1.2 since that is what I was taught so far, have been also learning directx and it seems like the logical step to use the latest version of opengl since it's quite similar and seems like it may not have that big of a learning curve.
Thank you so much for the help.