files/monkey2.png files/monkey3.png files/monkey4.png

Overview

In this assignment, you will be introduced to OpenGL and the 3D rendering pipeline. You are required to do the following.

  1. load a monkey head into your program from .raw format data.
  2. render it using opengl. (be sure to color it in such a way that its features are visible)

Background

A monkey head has been created in a modelling program, such as Maya, 3D Studio Max, or Blender.

files/monkey1.png

Keep in mind that this head consists of nothing but polygons (specifically, triangles).

files/monkey_tri.png

In this example I'm using the RAW file format mostly because its so simple. Each line consists of nine numbers representing a triangle. Every three numbers form the x-y-z coordinate of a vertex. Together three vertices form a triangle. So our monkey.raw is nothing more than a list of triangles. There are of course more complicated file formats which can store color, texture, normal information, per vertex or are structured in better ways. But we will use this for its simplicity.

files/monkey_exp.png files/monkey3.png

You will now load the file into a program (skeleton code provided) and then use some OpenGL calls to send those vertices down the graphics pipeline. The result will be similar to the following, albeit in wireframe:

files/monkey4.png

Triangles

The only thing we need to worry about right now is how to send triangles down the pipeline. Fortunately, this is as simple as specifying the coordinates for the triangle:

glBegin(GL_TRIANGLES); //tell OpenGL we'll be sending it a bunch of vertices
glVertex3f(-1,0,0);
glVertex3f(1,0,0);
glVertex3f(0,1,0);
glEnd(); //tell OpenGL we are done

likewise we can also specify a color per vertex. This is specifying a specific vertex attribute. There are others such as glTexCoord2f() for texturing or glNormal3f() for normal information for lighting.

glBegin(GL_TRIANGLES); //tell OpenGL we'll be sending it a bunch of vertices
glColor3f(0,0,0); //rgb color value
glVertex3f(-1,0,0);
glColor3f(0,0,1); //rgb color value
glVertex3f(1,0,0);
glColor3f(1,1,1); //rgb color value
glVertex3f(0,1,0);
glEnd(); //tell OpenGL we are done

files/triangles.png

And of course we are only showing you the easiest(and depreciated) way to pass vertex data into OpenGL. There are of course faster ways that don't involve an entire function call per vertex you are passing in:
http://www.opengl.org/wiki/Vertex_Arrays
With vertex arrays you are essentially passing in pointers to your data instead of using an entire function call per vertex. You also pass in the stride, which tells OpenGL how many bytes to jump to get to the next part of the data. I'll lecture more on this if you're curious.

Also there are ways which involve storing the data inside the graphics card for future use. The benefit is that you don't have to stream in the data from the client each time you want to draw something.
http://www.opengl.org/wiki/Vertex_Buffer_Object

Code

Sample code is given here. Note: I stripped away all of the structure for the code to make it easier to parse. Later on in the course you will be given better organized code but for now I'm going to err on the side of simplicity.

In the viewer, hold the left mouse to change the camera's position, and the right mouse to zoom in and out. Examine the code to find the simple camera implementation.

Once you have the basics of monkey-head rendering working, take some time to play around with different vertex coloring, and solid vs. wireframe triangle rendering (what happens if you comment out the line saying "this will enable wireframe mode"). You can also play with the camera code itself; try making it more or less sensitive, changing the default orientation, or whatever else comes to mind.

Submission Instructions

Please save your labs. I will check off the labs when you are done.

External Resources

The following resources should prove to be useful for this and future labs.