Where: Room 1.05 in CSSE
and online (active in consultation hour)
You should try to complete this lab during the lab session. If you are behind, then
try to catch up the lab exercises in your own time.
lab4/LINUX_VERSIONS
or lab4/MAC_VERSIONS
and copy the Makefile
from lab3. E.g., via:
cd ~/cits3003/labs-examples
mkdir lab4; cd lab4;
Then, depending on what platform you use, type:
mkdir LINUX_VERSIONS; cd LINUX_VERSIONS
cp ../../lab3/LINUX_VERSIONS/Makefile .
or
mkdir MAC_VERSIONS; cd MAC_VERSIONS
cp ../../lab3/MAC_VERSIONS/Makefile .
Hint: you may find it easier to start with the C++ code from lab 2, then
alter it to use the vertex shader from the end of lab 3.
Sample for rotating a coloured square in the x and z directions.
glUniformMatrix3fv
with the appropriate matrix, followed
by a second call to glDrawArrays
.
glDrawArrays
simply overwrites the appropriate parts of the framebuffer.
Samples for rotating three coloured squares without hidden surface removal.
GLUT_DEPTH
to the display mode requested in the call to glutInitDisplayMode
, e.g.:
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
init
function
(or anywhere after the window is created):
glEnable( GL_DEPTH_TEST );
GL_DEPTH_BUFFER_BIT
to the call to glClear
at the start of the display function:
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
Samples for rotating three coloured squares with hidden surface removal.
vec3 points[NumVertices] = { vec3( -0.5, -0.5, 0.5 ), vec3( 0.5, -0.5, 0.5 ), vec3( -0.5, 0.5, 0.5 ), vec3( 0.5, 0.5, 0.5 ), vec3( -0.5, 0.5, 0.5 ), vec3( 0.5, -0.5, 0.5 ), vec3( -0.5, -0.5, -0.5 ), vec3( 0.5, -0.5, -0.5 ), vec3( -0.5, 0.5, -0.5 ), vec3( 0.5, 0.5, -0.5 ), vec3( -0.5, 0.5, -0.5 ), vec3( 0.5, -0.5, -0.5 ), vec3( 0.5, -0.5, -0.5 ), vec3( 0.5, 0.5, -0.5 ), vec3( 0.5, -0.5, 0.5 ), vec3( 0.5, 0.5, 0.5 ), vec3( 0.5, -0.5, 0.5 ), vec3( 0.5, 0.5, -0.5 ), vec3( -0.5, -0.5, -0.5 ), vec3( -0.5, 0.5, -0.5 ), vec3( -0.5, -0.5, 0.5 ), vec3( -0.5, 0.5, 0.5 ), vec3( -0.5, -0.5, 0.5 ), vec3( -0.5, 0.5, -0.5 ), vec3( -0.5, 0.5, -0.5 ), vec3( -0.5, 0.5, 0.5 ), vec3( 0.5, 0.5, -0.5 ), vec3( 0.5, 0.5, 0.5 ), vec3( 0.5, 0.5, -0.5 ), vec3( -0.5, 0.5, 0.5 ), vec3( -0.5, -0.5, -0.5 ), vec3( -0.5, -0.5, 0.5 ), vec3( 0.5, -0.5, -0.5 ), vec3( 0.5, -0.5, 0.5 ), vec3( 0.5, -0.5, -0.5 ), vec3( -0.5, -0.5, 0.5 ), };
NumTriangles
to 12.
Samples for rotating a coloured cube and the x and z directions with hidden surface removal.
One way to allow more general rotations is to combine the three rotations we know together:
first rotate around the x-axis, then rotate the result around the y-axis and finally
rotate around the z-axis. (This in fact allows us to form any rotation.)
We could create the three rotation matrices in our C++ program and pass all three to
the vertex shader so that it can multiply each vertex by the three matrices, one after the
other with code like:
gl_Position.xyz = zRot * (yRot * (xRot * vPosition));
But, by the magic of linear algebra/matrices this is equivalent to:
gl_Position.xyz = (zRot * yRot * xRot) * vPosition;
So, we can just multiply the three matrices together and then pass them to the same vertex shader as previously.
glDrawArrays
.
Samples for general 3D rotations via composition.
rotY * rotX
Since the whole calculation for a vertex will be rotY * rotX * vPosition
therotY
happens second, causing the front face to consistently rotate towards
the left, with the rotX
adding some rotation towards the top or bottom
depending on the current y-axis rotation.
rotX * rotY
Since the rotX
happens second, the front face consistently rotates towards the
bottom, with the rotY
adding some rotation towards the left or right depending
on the current x-axis rotation.
./q6rotYrotXcube & ./q6rotXrotYcube &
See the following video. The first version should generally have the
front face moving towards the right, but also varying between moving upwards and
downwards, while the second one
should generally have the front face moving towards the top, but also varying between moving leftwards
and rightwards.
mat3 shrinkX = mat3( vec3(0.25, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0) );
shrinkX
to the left of the two rotations.
shrinkX
to the right of the two rotations.
See the following video. The first version should directly reduce the final x-coordinates so
everything ends up looking "squeezed" in the centre. The second version should instead reduce
the original x-coordinates so that instead of a cube we get a box with one dimension a quarter of
the other two.
As in the previous question, the point here is that the
order that we put the matrices matters when we multiply them. The general rule is that each
matrix is given the coordinates after they've been transformed by the matrices to the right of it.
Unzip this so that you have the lab4-soln
under your labs-examples
folder, then build using make as usual.