<html> <head> </head><body><pre><html> <head> </head><body><pre> /* ** File: dancer1.c ** Description: Uses GLU quadric objects. ** First step in creating a hierarchical ** model of a dancer. ** Rev: 1.0 ** Created: 22 September 2002 ** Last Update: 22 September 2002 ** Author: Fran Soddell ** Email: F.Soddell@bendigo.latrobe.edu.au */ #include &lt;GL/glut.h&gt; #include &lt;stdio.h&gt; #define TRUE 1 #define FALSE 0 /* ** manage 3D */ enum {x,y,z}; /* ** manage window */ int width =400; int height=400; int xPosition=50; int yPosition=70; /* ** manage world projection */ enum {xLeft,xRight,yBottom,yTop,zNear,zFar}; GLdouble world[]={ -6.0,6.0,-6.0,6.0,0.1,1000 }; GLdouble fovy =90.0; GLdouble aspect =1.0; int perspective =TRUE; /* ** manage camera */ GLdouble eye[]={ 0,1,3 }; GLdouble centre[]={ 0.0,1.0,0.0 }; GLdouble up[]={ 0.0,1.0,0.0 }; /* ** manage colour */ #define NUM_COLOURS 8 #define ALPHA 0.0 enum {red,green,blue, yellow,magenta,cyan, white,black,colours}; typedef GLfloat colourType[4]; colourType colour[NUM_COLOURS]={ {1.0,0.0,0.0,1.0},{0.0,1.0,0.0,1.0},{0.0,0.0,1.0,1.0}, {1.0,1.0,0.0,1.0},{1.0,0.0,1.0,1.0},{0.0,1.0,1.0,1.0}, {1.0,1.0,1.0,1.0},{0.0,0.0,0.0,1.0} }; /* ** manage model */ GLUquadricObj * wireTool; /* ** manage model transforms */ enum {dancer,head}; GLdouble t[][3]={ {0,0,0},{0,0,1.925} }; GLdouble ra[]={ -90,0 }; GLdouble r[][3]={ {1,0,0},{0,0,1} }; /* ** *********************************************************** ** code ** *********************************************************** */ void renderBody(){ glColor3fv(colour[green]); gluCylinder(wireTool,1.0,0.25,1.7,9,2); } void renderHead(){ glRotated(ra[head],r[head][x],r[head][y],r[head][z]); glColor3fv(colour[magenta]); gluSphere(wireTool,0.45,15,15); } void renderDancer(){ glRotated(ra[dancer],r[dancer][x],r[dancer][y],r[dancer][z]); renderBody(); glTranslated(t[head][x],t[head][y],t[head][z]); renderHead(); } void renderView(){ glTranslated(t[dancer][x],t[dancer][y],t[dancer][z]); renderDancer(); } void setCamera(){ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(eye[x], eye[y],eye[z], centre[x],centre[y],centre[z], up[x],up[y],up[z]); } void display(){ setCamera(); /* ** Clear window with background colour before (re)rendering. */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); wireTool=gluNewQuadric(); gluQuadricDrawStyle(wireTool,GLU_LINE); renderView(); glFlush(); gluDeleteQuadric(wireTool); } void setProjection(){ glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(!perspective) glOrtho(world[xLeft], world[xRight], world[yBottom],world[yTop], world[zNear], world[zFar]); else gluPerspective(fovy,1,world[zNear],world[zFar]); } void reshapeWindow(int w, int h){ int yOrigin=0, xOrigin=0; if(w&gt;h){ yOrigin=(h-w)/2; h=w; } else{ xOrigin=(w-h)/2; w=h; } glViewport(xOrigin,yOrigin,(GLsizei)w,(GLsizei)h); setProjection(); } void specialKeys(int key,int xMouse,int yMouse){ int i; switch(key){ case GLUT_KEY_PAGE_UP : t[dancer][z]+= 0.1; break; case GLUT_KEY_PAGE_DOWN: t[dancer][z]+=-0.1; break; } glutPostRedisplay(); } void moveHead(){ static int right=TRUE; if(right){ ra[head]-=1.0; if(ra[head]&lt;=-55.0) right=FALSE; } else{ ra[head]+=1.0; if(ra[head]&gt;=55.0) right=TRUE; } } void keyboard(unsigned char key,int xMouse,int yMouse){ switch(key){ case 27 : exit(0); case &#39;p&#39;: perspective=!perspective; setProjection(); break; case &#39;f&#39;: if(fovy&lt;180) fovy+=0.5; setProjection(); break; case &#39;F&#39;: if(fovy&gt;0) fovy-=0.5; setProjection(); break; case &#39;h&#39;: moveHead(); break; } glutPostRedisplay(); } void setUpGLUT(int argc,char ** argv){ glutInit(&amp;argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(width,height); glutInitWindowPosition(xPosition,yPosition); glutCreateWindow(&quot;Hierarchical Model - Dancer&quot;); /* ** Register callbacks. */ glutDisplayFunc(display); glutReshapeFunc(reshapeWindow); glutKeyboardFunc(keyboard); glutSpecialFunc(specialKeys); } void initialiseGL(){ int i; glClearColor(0,0,0,ALPHA); glEnable(GL_DEPTH_TEST); } int main(int argc,char ** argv){ setUpGLUT(argc,argv); initialiseGL(); /* ** Display window and enter the GLUT event ** processing loop. */ glutMainLoop(); return 0; } </pre></body></html></pre></body></html>