Was just trying to do some rendering with OpenGL and I happened to think about that: how would you run transformations in a fixed coordinate system. In other words, if you rotate an object around one axis you will see that the others are going to rotate too? Therefore, following rotations will be done on the newly construct axis.
I read this on the documentation (see part 9.070 ) https://www.opengl.org/archives/resources/faq/technical/transformations.htm, but I have no idea: 1. if that works 2. how to implement it cause I don't really understand what I am supposed to do.
I think I won't be the only one to want to do that and it's also I guess an interesting question.
Edit: Here is an implementation that doesn't work and that I would like to fix
evoid display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
glMatrixMode(GL_MODELVIEW); // To operate on model-view matrix
// Render a color-cube consisting of 6 quads with different colors
glLoadIdentity(); // Reset the model-view matrix
glTranslatef(1.5f, 0.0f, -7.0f); // Move right and into the screen
//glRotatef(angleCube, 1.0f, 1.0f, 1.0f); // Rotate about (1,1,1)-axis [NEW]
//Matrix a transformation matrix obtained from a Translation, Scale and Rotation composition
// In an other piece of the code I wanted to try
// The matrix is a float[16] and the values are correct and updated every now and then to make the cube rotate
glMultMatrixf(matrix);
glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads
// Top face (y = 1.0f)
// Define vertices in counter-clockwise (CCW) order with normal pointing out
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
// Bottom face (y = -1.0f)
glColor3f(1.0f, 0.5f, 0.0f); // Orange
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
// Front face (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
// Back face (z = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd(); // End of drawing color-cube
The OpenGL modelview matrix works as a stack. Every transformation is performed by post-multiplying the transformation with the current MVM matrix. In other words, it looks like this:
New_MVM = Old_MVM * Transformation
This causes the transformations to always occur in the local coordinate system. As the article states, you need the transformation to work in the fixed-coordinates system ("global coordinates"), so you need to pre-multiply like this:
New_MVM = Transformation * Old_MVM
Note that instead of retrieving the MVM from OpenGL like the article specifies, you can store the transformation matrix for the object as a data member. Each time you want to transform your object, just pre-multiply the new transformation with the old one.
Once you have computed the final transformation matrix for your object by pre-multiplying, you can pass it to OpenGL by calling glMultMatrix.
EDIT: Since you need to calculate the matrix yourself, you're going to want a math library. If you're using GLM, you can do it like:
When rotating your object:
glm::vec3 axis(x, y, z);
glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), angle_in_degrees, axis);
totalTransform = rotation * totalTransform;
Then in your 'display' method:
float *matPtr = glm::value_ptr(totalTransform);
glMultMatrixf(matPtr);
Disclaimer: All code is untested.