OpenGL Translate Rotate Scale Transformation
I am trying to create user firendly transformation class (Unity as
example) that only holds (or only visible to user) position, rotation and
translation vectors.
Applying the transformation is easy for OpenGL by using glTranslate,
glRotate and glScale functions. I am calling Transform method for each
object before it is about to be drawn. But I am having a trouble with
changing position related by a rotation. Here is my code.
// A sample Rendering method for an object
void Render()
{
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
transform->Transform();
glEnable(GL_COLOR_MATERIAL);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, 0, m_colors->constData());
glVertexPointer(3, GL_FLOAT, 0, m_positions->constData());
glDrawArrays(GL_LINES, 0, 6);
glDisable(GL_COLOR_MATERIAL);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glPopMatrix();
}
// Transformation class
class Transformation
{
public:
QVector3D Position;
QVector3D Rotation;
QVector3D Scale;
Transformation()
{
Position = QVector3D(0.0f, 0.0f, 0.0f);
Rotation = QVector3D(0.0f, 0.0f, 0.0f);
Scale = QVector3D(1.0f, 1.0f, 1.0f);
}
~Transformation()
{
}
void Translate(const QVector3D& amount)
{
}
void Rotate(const QVector3D& amount)
{
Rotation += amount;
Rotation.setX(AdjustDegree(Rotation.x()));
Rotation.setY(AdjustDegree(Rotation.y()));
Rotation.setZ(AdjustDegree(Rotation.z()));
}
void Transform()
{
// Rotation
glRotatef(Rotation.x(), 1.0f, 0.0f, 0.0f);
glRotatef(Rotation.y(), 0.0f, 1.0f, 0.0f);
glRotatef(Rotation.z(), 0.0f, 0.0f, 1.0f);
// Translation
glTranslatef(Position.x(), Position.y(), Position.z());
// Scale
glScalef(Scale.x(), Scale.y(), Scale.z());
}
};
How can I Translate?
No comments:
Post a Comment