- Globally define relevant data structures. Refer to Lecture #19 for details of the specification.
/*
** manage polygonal mesh
*/
#define EDGES 12
#define FACES 6
typedef GLfloat vertexType[3];
vertexType box[8]={
{0.0,0.0,0.0},
/* specify remaining 7 vertices */
};
GLint boxFace[FACES][4]={
/* Identify each face using subscripts to the array of vertices.
** Each face has 4 vertices. Specify them in a right-handed
** (anti-clockwise) direction.
*/
};
enum {begin,end};
GLint boxEdge[EDGES][2]={
/* Identify each edge using subscripts to the array of vertices.
** Each edge has 2 vertices - the begin vertex and the end vertex.
*/
};
- Write a function to render a wireframe model of the box by joining the edges.
void wireBox(){
int i;
glLineWidth(3);
for (i=0;i<EDGES;i++){
glBegin(GL_LINES);
glVertex3fv(box[boxEdge[i][begin]]);
glVertex3fv(box[boxEdge[i][end]]);
glEnd();
}
}
- Render the wireframe model in magenta by adding (in the appropriate place) the following code to
display()
.
glColor3fv(colour[magenta]);
wireBox();
- Write a function to render the outer faces of the box by traversing the vertices for each face in an anticlockwise direction.
void outerBox(){
int i,j;
for(i=0;i<FACES;i++){
glBegin(GL_POLYGON);
for(j=0;j<4;j++)
glVertex3fv(box[boxFace[i][j]]);
glEnd();
}
}
- Render the outer faces in cyan. Add the following code to
display()
, before rendering the wireframe model.
glColor3fv(colour[cyan]);
outerBox();
- Write a function,
innerBox()
to render the inner faces of the box by traversing the vertices in a clockwise direction.
- Render the inner faces in cyan. Add the following code to
display()
, AFTER rendering the outer faces.
glColor3fv(colour[red]);
innerBox();
- Note that the box now appears to be red. There is no depth testing and the inner faces are rendered after the outer.
Toggle depth testing by pressing the
- Set up appropriate global data structures, such as those below.
/*
** manage model transforms
*/
enum {boxMesh};
GLdouble t[][3]={
{0,0,0}
};
GLdouble ra[]={
0,0
};
GLdouble r[][3]={
{0,1,0}
};
- Place the following code in the appropriate position in the
display()
function.
glTranslated(t[boxMesh][x],t[boxMesh][y],t[boxMesh][z]);
glRotated(ra[boxMesh],r[boxMesh][x],r[boxMesh][y],r[boxMesh][z]);
- Write a special key function. The one below is partially complete.
void specialKeys(int key,int xMouse,int yMouse){
int i;
switch(key){
case GLUT_KEY_PAGE_UP : t[boxMesh][z]+= 0.1; break;
case GLUT_KEY_PAGE_DOWN: t[boxMesh][z]+=-0.1; break;
}
glutPostRedisplay();
}
- Register the callback in
setUpGLUT
.
glutSpecialFunc(specialKeys);
- Edit the
keyboard
function to respond to the r key. Suggestion below.
case 'r': ra[boxMesh]+=1.0;
if(ra[boxMesh]>=360)ra[boxMesh]=0;
break;
- Move the box around so that the camera passes inside the box.
- Is there a clear distinction between inner and outer faces? Or does OpenGL find it difficult to render them consistently?
We need back face culling.