For my final art project, I’m working on programmatically generating scenes to be rendered in Sunflow. Lot of the work is placing objects where I want them. The following is just a way to use OpenGL to do your matrix multiplications and transformations.

The returned array is a column-major representation of a 4×4 matrix. This section of the OpenGL guide is a good reference for remembering in which order to apply transformations.

For example, if I want to translate an object by (3,4,5) and then rotate it 45 degrees around the z-axis, I would use the following code to retrieve the transformation matrix. Notice the order of the transformation functions.

## Using OpenGL to calculate transformations

For my final art project, I’m working on programmatically generating scenes to be rendered in Sunflow. Lot of the work is placing objects where I want them. The following is just a way to use OpenGL to do your matrix multiplications and transformations.

glPushMatrix();

glLoadIdentity();

// transformations go here

GLfloat modelMatrix[16];

glGetFloatv (GL_MODELVIEW_MATRIX, modelMatrix);

glPopMatrix();

Transformation functions: glTranslate, glRotate, glScale, glMultMatrix

The returned array is a column-major representation of a 4×4 matrix. This section of the OpenGL guide is a good reference for remembering in which order to apply transformations.

For example, if I want to translate an object by (3,4,5) and then rotate it 45 degrees around the z-axis, I would use the following code to retrieve the transformation matrix. Notice the order of the transformation functions.

glPushMatrix();

glLoadIdentity();

glRotatef(45, 0, 0, 1);

glTranslatef(3, 4, 5);

GLfloat modelMatrix[16];

glGetFloatv (GL_MODELVIEW_MATRIX, modelMatrix);

glPopMatrix();

for (int i = 0; i < 4; i++) {

for (int j = 0; j < 4; j++) {

printf("%f ", modelMatrix[j * 4 + i]);

}

printf("\n");

}

The code to the right outputs the following: