Take the 2-minute tour ×

I am trying to follow this course about computer graphics, but I'm stuck in the homework 1. I don't understand what's the role of the vector eye and up. The descripcion of the homework can be found in this link, there's also the skeleton of the first assignment.

So far I have the following code:

// Transform.cpp: implementation of the Transform class.

#include "Transform.h"

//Please implement the following functions:

// Helper rotation function.  
mat3 Transform::rotate(const float degrees, const vec3& axis) {
  // Please implement this.
    float radians = degrees * M_PI / 180.0f;
    mat3 r1(cos(radians));
    mat3 r2(0, -axis.z, axis.y, axis.z, 0, -axis.x, -axis.y, axis.x, 0);
    mat3 r3(axis.x*axis.x, axis.x*axis.y, axis.x*axis.z,
            axis.x*axis.y, axis.y*axis.y, axis.y*axis.z,
            axis.x*axis.z, axis.z*axis.y, axis.z*axis.z);
            r2[i][j] = r2[i][j]*sin(radians);
            r3[i][j] = r3[i][j]*(1-cos(radians));
    return r1 + r2 + r3;

// Transforms the camera left around the "crystal ball" interface
void Transform::left(float degrees, vec3& eye, vec3& up) {
    eye = eye * rotate(degrees, up);

// Transforms the camera up around the "crystal ball" interface
void Transform::up(float degrees, vec3& eye, vec3& up) {
    vec3 newAxis = glm::cross(eye, up);

// Your implementation of the glm::lookAt matrix
mat4 Transform::lookAt(vec3 eye, vec3 up) {
    return lookAtMatrix;





for the left method it appears to be doing the right thing, which is, rotating the object around the y-axis (actually I'm not sure if the object is moving or what I'm moving is the camera, can someone clarify?).

for the up method I cannot make it work which will be rotating the object (or camera?) around the x-axis (at least that's what I think).

finally, I don't understand what should the lookAt method should do.

Can someone help me understand the actions to be performed? Can someone explain what are the roles of vectors eye and up?

share|improve this question
Presumably eye is a vector that defines the direction to look in, up is usually a vector that helps the "camera", or whatever it is being transformed stay upright. Otherwise the object is free to spin around the eye-axis which would not be natural for a lot of applications. –  Skurmedel Dec 30 '12 at 19:01
So the object once it's on a certain position, I have to move just the eye and up vectors? –  BRabbit27 Dec 30 '12 at 19:03

2 Answers 2

View transforms are often implemented using a "look-at" function. The idea being that you specify where the camera is, what direction it is looking in, and what direction represents "up" in your particular space, and you get a matrix back which represents that transform.

It looks like you're trying to implement some kind "rotating ball" navigation control. That's fairly simple - horizontal movement should rotate around some "Y" axis, and vertical movement should rotate around the "right" (or X) axis. Generally those rotations work around the current view axes, rather than globally, so that the movement is intuitive. I'm not sure exactly what you're looking for there.

A look-at function works as follows.

A 3x3 matrix representing a rotation can be viewed as being composed the 3 perpendicular unit axes of the space you are transforming into. So if you can supply those vectors, you can build the matrix.

The first axis is easy. A camera is typically oriented to look along "Z", so if you take the vector representing the direction of the thing being looked at from the camera's position, then normalise it, this is the Z axis.

Then you need to define a distinct 'up' vector - (0,1,0) is typical, but you will need to choose a different one in cases where the Z-axis is pointing in the same direction.

The cross product of this 'up' vector and the 'Z' axis gives the 'X' axis - this is because the cross product gives a perpendicular vector, and what constitutes horizontal will be perpendicular to both the 'forward' direction, and 'up'.

Then the cross product of the 'X' and 'Z' axes gives the 'Y' axis (which is not necessarily the same as the 'Y' axis - consider looking towards the ceiling or towards the floor).

These three axes, normalised, (x,y,z) directly form a rotation matrix.

The translation portion of the matrix is generally the position of the camera, transformed by the rotation's inverse (such that when transforming the camera position by the lookat matrix itself, it should end up back at the origin).

share|improve this answer
what do you mean by "current view axes" and "globally". Globally are the XYZ-axes with the origin at 0,0,0 ? –  BRabbit27 Dec 30 '12 at 19:19
What I mean, is that you are probably looking to rotate the camera around its own Y axis, rather than the world Y axis. If the camera was on its side, then rotating around the world Y axis would be unintuitive. –  JasonD Dec 30 '12 at 19:21
Still I'm missing something. The rotate method is simply apply the formula for a 3D rotation, isn't it? Then, for the left method, when i write eye = eye * rotate(degrees, up) means that I want to rotate the vector eye about the up-axis? is that correct, I mean should I do that? or should I first create new xyz-axes? or are eye and up actually my xy-axes? –  BRabbit27 Dec 30 '12 at 19:59

1) Your course is using the OpenGL library, and your homework assignment is to fill in the skeleton module "Transform.cpp".

2) The method you're asking about is "mat4 Transform::lookAt(vec3 eye, vec3 up)":

lookAt: Finally, you need to code in the transformation matrix, given the eye and up vectors. You will likely need to refer to the class notes to do this. It is likely to help to dene a uvw coordinate frame (as 3 vectors), and to build up an auxiliary 4 4 matrix M which is returned as the result of this function. Consult class notes and lectures for this part.

3) A hint for what these two arguments "eye" and "up" mean should be in your class notes and lectures.

4) Another hint is to "define a uvw coordinate frame (as three vectors), and build up an auxiliary 4x4 matrix ... which is returned as a result...".

5) A final hint:

Q: What's the difference between an OpenGL mat3 and mat4?

A: What extractly mat3(a mat4 matrix) statement in glsl do?

mat3(MVI) * normal

Returns the upper 3x3 matrix from the 4x4 matrix and multiplies the normal by that. This matrix is called the 'normal matrix'. You use this to bring your normals from world space to eye space.

The reason why the original matrix is 4x4 and not 3x3 is because 4x4 matrices let you do affine transformations and contain useful information for perspective rendering. But to take a normal from world space to eye space, you just need the 3x3 model view matrix.

share|improve this answer

Your Answer


