Author: Romain Vergne (website)
Please cite my name and add a link to my web page if you use this course

Image synthesis and OpenGL: exercices 02

Setup

We will use Linux and the following libraries:

Installing sources. In a terminal, do:
  1. Download the sources at http://romain.vergne.free.fr/teaching/IS/data02/TP02.tgz
  2. If needed, edit the file main.pro to change paths
  3. To compile: qmake && make
  4. To run: ./tp02
  5. To edit: use either your prefered text editor or qtcreator

A simple planetary system

The goal of these exercices is to familiarize yourself with OpenGL transformation and projection matrices. You should obtain something like this:



Exercice 1: Have a look at the code, understand matrices



// identity matrix
glm::mat4(1.0f);

// rotation matrix (will apply the following transformation: M*R
glm::rotate(glm::mat4 M,float angleInDegree, glm::vec3 rotationAxis);

// which is equivalent to:
M*glm::rotate(glm::mat4(1.0f),float angleInDegree, glm::vec3 rotationAxis);

// scaling matrix: M*S
glm::scale(glm::mat4 M,glm::vec3 scalingFactors);

// translation matrix: M*T
glm::translate(
glm::mat4 M,glm::vec3 translateValues);

// view matrix V
glm::lookAt(glm::vec3 pos,glm::vec3 center,glm::vec3 up);

// orthogographic projection matrix P
glm::ortho(float left,float right,float bottom,float top,float near,float far);


// perspective projection matrix P
glm::perspective(float fovy,float aspect,float near,float far)


Be careful with the order of transformations. The following example shows how to apply a scaling, followed by a translation, followed by a rotation, before computing the model matrix:

// Lets consider an initial model matrix, at the top of the stack: _modelMatrices.top() = glm::mat4(1.0f);

// the following code...
glm::mat4 s = glm::scale(glm::mat4(1.0f),scalingFactors);
glm::mat4 r = glm::rotate(glm::mat4(1.0f),spin,axis);
glm::mat4 t = glm::translate(glm::mat4(1.0f),translateValues);
_modelMatrices.top() = _modelMatrices.top()*r*t*s;

// ...is equivalent to
_modelMatrices.top() = glm::rotate(_modelMatrices.top(),spin, axis);  
_modelMatrices.top() = glm::translate(_modelMatrices.top(),translateValues);
_modelMatrices.top() = glm::scale(_modelMatrices.top(),scalingFactors);

Exercice 2: Looking at the sphere

All the exercises will be done either in paintGL (for initializing the view and projection matrices) and drawScene (for applying transformations and drawing multiple spheres).
  1. Initialize the view matrix so that the camera is located on the z-axis and look at the center (0,0,0).
  2. Initialize the projection matrix so that
    1. the fovy is equal to 45
    2. the aspect is equal to the aspect of the viewport
    3. the near value is equal to 0.1 and the far value to 100
Try different camera positions until you are satisfied with the result. Pressing "w" will display the scene as wireframes.

Exercice 3: Rotating the sphere

Check the result in wireframes mode. Pressing "a" will stop/play the animation.

Exercice 4: Adding a planet

Exercice 5: Adding satellites

Exercice 5: Viewports

Exercice 6: Bonus


PREVIOUS: 02 - TRANSFORMATIONS
NEXT: 03 - PIPELINE