(no commit message)
[atyndall/cits2231.git] / scene.c
diff --git a/scene.c b/scene.c
index e42c48c..f8dbc57 100644 (file)
--- a/scene.c
+++ b/scene.c
-/**\r
- * CITS2231 Graphics Scene Editor\r
- * @author Ashley Tyndall (20915779)\r
- */\r
 \r
-#include <stdlib.h>\r
-#include <stdio.h>\r
-#include <dirent.h>\r
-#include <string.h>\r
-#include <math.h>\r
-#include <GL/gl.h>\r
-#include <GL/glut.h>\r
-#include <time.h>\r
-\r
-#include "bitmap.h"\r
-\r
-// Type definitions for vertex-coordinates, normals, texture-coordinates, \r
-// and triangles (via the indices of 3 vertices).\r
-typedef GLfloat vertex[3];\r
-typedef GLfloat normal[3];\r
-typedef GLfloat texCoord[2];\r
-typedef GLint vertexIndex;\r
-typedef vertexIndex triangle[3];\r
-\r
-// A type for a mesh\r
-typedef struct {         \r
-    int nVertices;           //  The number of vertices in the mesh\r
-    vertex* vertices;        //  Array with coordinates of vertices\r
-    normal* normals;         //  Array with normals of vertices\r
-    texCoord* texCoords;     //  Array with texture-coordinates of vertices\r
-    int nTriangles;          //  The number of triangles in the mesh\r
-    triangle* triangles;     //  Array of trangles via 3 indices into "vertices"\r
-} mesh;\r
-\r
-#define NMESH 54       // The number of meshes (in the models-textures dir)\r
-mesh* meshes[NMESH];   // An array of pointers to the meshes - see getMesh\r
-\r
-// A type for a 2D texture, with height and width in pixels\r
-typedef struct {\r
-    int height;\r
-    int width;\r
-    GLubyte *rgbData;   // Array of bytes with the colour data for the texture\r
-} texture;\r
-\r
-#define NTEXTURE 30     // The number of textures (in the models-textures dir)\r
-texture* textures[NTEXTURE];   // An array of texture pointers - see getTexture\r
-\r
-typedef struct {  \r
-    // You'll need to add scale, rotation, material, mesh number, etc.,\r
-    // to this structure\r
-    float x,y,z;\r
-} SceneObject;\r
-\r
-// Menu enum\r
-enum menu {\r
-  // Main menu\r
-  ROTATE_MOVE_CAMERA,\r
-  POSITION_SCALE,\r
-  ROTATION_TEXTURE_SCALE,\r
-  EXIT,\r
-\r
-  // Material submenu\r
-  MATERIAL_ALL_RGB,\r
-  MATERIAL_AMBIENT_RGB,\r
-  MATERIAL_DIFFUSE_RGB,\r
-  MATERIAL_SPECULAR_RGB,\r
-  MATERIAL_ALL_ADSS,\r
-  MATERIAL_RED_ADSS,\r
-  MATERIAL_GREEN_ADSS,\r
-  MATERIAL_BLUE_ADSS,\r
-\r
-  // Light submenu\r
-  LIGHT_MOVE_LIGHT_1,\r
-  LIGHT_RGBALL_LIGHT_1,\r
-  LIGHT_MOVE_LIGHT_2,\r
-  LIGHT_RGBALL_LIGHT_2\r
-};\r
+/* Copyright (c) Mark J. Kilgard, 1994, 1997.  */\r
 \r
-// Menu arrays\r
-const char *textureMenuEntries[NTEXTURE] = {\r
-  "1 Plain", "2 Rust", "3 Concrete", "4 Carpet", "5 Beach Sand",\r
-  "6 Rocky", "7 Brick", "8 Water", "9 Paper", "10 Marble",\r
-  "11 Wood", "12 Scales", "13 Fur", "14 Denim", "15 Hessian",\r
-  "16 Orange Peel", "17 Ice Crystals", "18 Grass", "19 Corrugated Iron", "20 Styrofoam",\r
-  "21 Bubble Wrap", "22 Leather", "23 Camouflage", "24 Asphalt", "25 Scratched Ice",\r
-  "26 Rattan", "27 Snow", "28 Dry Mud", "29 Old Concrete", "30 Leopard Skin"\r
-};\r
+/* This program is freely distributable without licensing fees\r
+   and is provided without guarantee or warrantee expressed or\r
+   implied. This program is -not- in the public domain. */\r
 \r
-const char *objectMenuEntries[NMESH] = {\r
-  "1 Thin Dinosaur","2 Big Dog","3 Saddle Dinosaur", "4 Dragon", "5 Cleopatra",\r
-  "6 Bone I", "7 Bone II", "8 Rabbit", "9 Long Dragon", "10 Buddha",\r
-  "11 Sitting Rabbit", "12 Frog", "13 Cow", "14 Monster", "15 Sea Horse",\r
-  "16 Head", "17 Pelican", "18 Horse", "19 Kneeling Angel", "20 Porsche I",\r
-  "21 Truck", "22 Statue of Liberty", "23 Sitting Angel", "24 Metal Part", "25 Car",\r
-  "26 Apatosaurus", "27 Airliner", "28 Motorbike", "29 Dolphin", "30 Spaceman",\r
-  "31 Winnie the Pooh", "32 Shark", "33 Crocodile", "34 Toddler", "35 Fat Dinosaur",\r
-  "36 Chihuahua", "37 Sabre-toothed Tiger", "38 Lioness", "39 Fish", "40 Horse (head down)",\r
-  "41 Horse (head up)", "42 Skull", "43 Fighter Jet I", "44 Toad", "45 Convertible",\r
-  "46 Porsche II", "47 Hare", "48 Vintage Car", "49 Fighter Jet II", "50 Winged Monkey",\r
-  "51 Chef", "52 Parasaurolophus", "53 Rooster", "54 T-rex"\r
-};\r
+/* Example for PC game developers to show how to *combine* texturing,\r
+   reflections, and projected shadows all in real-time with OpenGL.\r
+   Robust reflections use stenciling.  Robust projected shadows\r
+   use both stenciling and polygon offset.  PC game programmers\r
+   should realize that neither stenciling nor polygon offset are\r
+   supported by Direct3D, so these real-time rendering algorithms\r
+   are only really viable with OpenGL.\r
 \r
-#define MAXOBJECTS 256\r
-SceneObject sceneObjs[MAXOBJECTS];  // An array with details of the objects in a scene\r
-int nObjects=0;                     // How many objects there are in the scene currently.\r
+   The program has modes for disabling the stenciling and polygon\r
+   offset uses.  It is worth running this example with these features\r
+   toggled off so you can see the sort of artifacts that result.\r
 \r
-// Directories containing models\r
-char *dirDefault1 = "models-textures";\r
-char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures";\r
+   Notice that the floor texturing, reflections, and shadowing\r
+   all co-exist properly. */\r
 \r
-char dataDir[200];  // Stores the directory name for the meshes and textures.\r
+/* When you run this program:  Left mouse button controls the\r
+   view.  Middle mouse button controls light position (left &\r
+   right rotates light around dino; up & down moves light\r
+   position up and down).  Right mouse button pops up menu. */\r
 \r
-static GLfloat lightColor[] = {1.0, 1.0, 1.0, 1.0}; // White light\r
-static GLfloat lightPosition[4];\r
+/* Check out the comments in the "redraw" routine to see how the\r
+   reflection blending and surface stenciling is done.  You can\r
+   also see in "redraw" how the projected shadows are rendered,\r
 \r
-int moving, startx, starty;\r
-int lightMoving = 0, lightStartX, lightStartY;\r
+   including the use of stenciling and polygon offset. */\r
+\r
+/* This program is derived from glutdino.c */\r
+\r
+/* Compile: cc -o dinoshade dinoshade.c -lglut -lGLU -lGL -lXmu -lXext -lX11 -lm */\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <math.h>       /* for cos(), sin(), and sqrt() */\r
+#include <GL/glut.h>    /* OpenGL Utility Toolkit header */\r
+\r
+/* Some <math.h> files do not define M_PI... */\r
+#ifndef M_PI\r
+#define M_PI 3.14159265\r
+#endif\r
+\r
+/* Variable controlling various rendering modes. */\r
+static int stencilReflection = 1, stencilShadow = 1, offsetShadow = 1;\r
+static int renderShadow = 1, renderDinosaur = 1, renderReflection = 1;\r
+static int linearFiltering = 0, useMipmaps = 0, useTexture = 1;\r
+static int reportSpeed = 0;\r
+static int animation = 1;\r
+static GLboolean lightSwitch = GL_TRUE;\r
+static int directionalLight = 1;\r
+static int forceExtension = 0;\r
 \r
 /* Time varying or user-controled variables. */\r
 static float jump = 0.0;\r
-static float lightAngle = 0.0, lightHeight = 5;\r
+static float lightAngle = 0.0, lightHeight = 20;\r
 GLfloat angle = -150;   /* in degrees */\r
 GLfloat angle2 = 30;   /* in degrees */\r
 \r
-/* Near and far parameters */\r
-GLfloat near = -100;\r
-GLfloat far = 100;\r
-\r
-/* Zoom factor for mouse movements */\r
-GLfloat zoomFactor = 1.0;\r
-\r
-/* Recursion level for floor drawing */\r
-int drawFloorRecurse = 5;\r
-\r
-/* Size of floor, from -n to n */\r
-int floorSize = 100;\r
-\r
-/* Light 0 parameters */\r
-GLfloat diffuse0[] = {1.0, 1.0, 1.0, 1.0};\r
-GLfloat ambient0[] = {0.0, 0.0, 0.0, 1.0};\r
-GLfloat specular0[] = {1.0, 1.0, 1.0, 1.0};\r
-GLfloat emission0[] = {0.0, 0.0, 0.0, 0.0};\r
-GLfloat light0_pos[] ={1.0, 1.0, 0,0, 1.0};\r
-GLfloat glightmodel[] = {0.2,0.2,0.2,1};\r
-\r
-\r
-/**\r
- * Prints out error message when file cannot be read\r
- * @param fileName Name of file that could not be read\r
- */\r
-void fileErr(char* fileName) {\r
-    printf("Error reading file: %s\n", fileName);\r
-    printf("If not in the CSSE labs, you will need to include the directory containing\n");\r
-    printf("the models on the command line, or put it in the same folder as the exectutable.");\r
-    exit(EXIT_FAILURE);\r
-}  \r
-\r
-/**\r
- * Reads .bmp texture files and converts them to a texture object\r
- * @param fileName .bmp texture file\r
- * @return texture object\r
- */\r
-texture* loadTexture(char *fileName) {\r
-    texture* t = malloc(sizeof (texture));\r
-    BITMAPINFO *info;\r
-\r
-    t->rgbData = LoadDIBitmap(fileName, &info);\r
-    t->height=info->bmiHeader.biHeight;\r
-    t->width=info->bmiHeader.biWidth;\r
-\r
-    return t;\r
-}\r
+int moving, startx, starty;\r
+int lightMoving = 0, lightStartX, lightStartY;\r
 \r
-/**\r
- * Reads .x files and converts them to a mesh object\r
- * @param fileName .x mesh file\r
- * @return mesh object\r
- */\r
-mesh* loadMesh(char* fileName) {\r
-    mesh* m = malloc(sizeof (mesh));\r
-    FILE* fp = fopen(fileName, "r");\r
-    char line[256] = "";\r
-    int lineBuffSize = 256;\r
-\r
-    if(fp == NULL) fileErr(fileName);\r
-\r
-    while(strcmp(line,"Mesh {\r\n") != 0 && strcmp(line,"Mesh {\n") != 0 )\r
-       fgets(line, lineBuffSize, fp);\r
-\r
-    fscanf(fp, "%d;\n", &(m->nVertices));\r
-    m->vertices = malloc(m->nVertices * sizeof(vertex));\r
-    for(int i=0; i < m->nVertices; i++)\r
-       fscanf(fp, "%f; %f; %f;%*[,;]\n", &(m->vertices[i][0]), &(m->vertices[i][1]), &(m->vertices[i][2]) );\r
-\r
-    fscanf(fp, "%d;\n", &(m->nTriangles));\r
-    m->triangles = malloc(m->nTriangles * sizeof(triangle));\r
-    for(int i=0; i < m->nTriangles; i++)\r
-       fscanf(fp, "%*d; %d, %d, %d;%*[;,]", m->triangles[i], m->triangles[i]+1, m->triangles[i]+2);\r
-\r
-    while(strcmp(line,"  MeshNormals {\r\n") != 0 && strcmp(line,"  MeshNormals {\n") != 0)\r
-       fgets(line, lineBuffSize, fp);\r
-\r
-    fgets(line, lineBuffSize, fp);\r
-    m->normals = malloc(m->nVertices * sizeof(normal));\r
-    for(int i=0; i < m->nVertices; i++)\r
-       fscanf(fp, "%f; %f; %f;%*[;,]\n",\r
-              &(m->normals[i][0]), &(m->normals[i][1]), &(m->normals[i][2]));\r
-\r
-    while(strcmp(line,"MeshTextureCoords {\r\n") != 0 && strcmp(line,"MeshTextureCoords {\n") != 0)\r
-       fgets(line, lineBuffSize, fp);\r
-\r
-    fgets(line, lineBuffSize, fp);\r
-    m->texCoords = malloc(m->nVertices * sizeof(texCoord));\r
-    for(int i=0; i < m->nVertices; i++)\r
-       fscanf(fp, "%f;%f;%*[,;]\n", &(m->texCoords[i][0]), &(m->texCoords[i][1]) );\r
-    fclose(fp);\r
-    \r
-    return m;\r
-}\r
+enum {\r
+  MISSING, EXTENSION, ONE_DOT_ONE\r
+};\r
+int polygonOffsetVersion;\r
+\r
+static GLdouble bodyWidth = 3.0;\r
+/* *INDENT-OFF* */\r
+static GLfloat body[][2] = { {0, 3}, {1, 1}, {5, 1}, {8, 4}, {10, 4}, {11, 5},\r
+  {11, 11.5}, {13, 12}, {13, 13}, {10, 13.5}, {13, 14}, {13, 15}, {11, 16},\r
+  {8, 16}, {7, 15}, {7, 13}, {8, 12}, {7, 11}, {6, 6}, {4, 3}, {3, 2},\r
+  {1, 2} };\r
+static GLfloat arm[][2] = { {8, 10}, {9, 9}, {10, 9}, {13, 8}, {14, 9}, {16, 9},\r
+  {15, 9.5}, {16, 10}, {15, 10}, {15.5, 11}, {14.5, 10}, {14, 11}, {14, 10},\r
+  {13, 9}, {11, 11}, {9, 11} };\r
+static GLfloat leg[][2] = { {8, 6}, {8, 4}, {9, 3}, {9, 2}, {8, 1}, {8, 0.5}, {9, 0},\r
+  {12, 0}, {10, 1}, {10, 2}, {12, 4}, {11, 6}, {10, 7}, {9, 7} };\r
+static GLfloat eye[][2] = { {8.75, 15}, {9, 14.7}, {9.6, 14.7}, {10.1, 15},\r
+  {9.6, 15.25}, {9, 15.25} };\r
+static GLfloat lightPosition[4];\r
+static GLfloat lightColor[] = {0.8, 1.0, 0.8, 1.0}; /* green-tinted */\r
+static GLfloat skinColor[] = {0.1, 1.0, 0.1, 1.0}, eyeColor[] = {1.0, 0.2, 0.2, 1.0};\r
+/* *INDENT-ON* */\r
+\r
+/* Nice floor texture tiling pattern. */\r
+static char *circles[] = {\r
+  "....xxxx........",\r
+  "..xxxxxxxx......",\r
+  ".xxxxxxxxxx.....",\r
+  ".xxx....xxx.....",\r
+  "xxx......xxx....",\r
+  "xxx......xxx....",\r
+  "xxx......xxx....",\r
+  "xxx......xxx....",\r
+  ".xxx....xxx.....",\r
+  ".xxxxxxxxxx.....",\r
+  "..xxxxxxxx......",\r
+  "....xxxx........",\r
+  "................",\r
+  "................",\r
+  "................",\r
+  "................",\r
+};\r
 \r
-// [You may want to add to this function.]\r
-/**\r
- * Loads mesh[i] if it isn't already loaded.\r
- * You must call getMesh(i) at least once before using mesh[i].\r
- *\r
- * @param i Mesh ID\r
- */\r
-void getMesh(int i) { // getMesh(i) loads mesh[i] if it isn't already loaded.  \r
-    char fileName[220];\r
-    if(i>=NMESH || i<0) {\r
-       printf("Error in getMesh - wrong model number");\r
-       exit(1);\r
+static void\r
+makeFloorTexture(void)\r
+{\r
+  GLubyte floorTexture[16][16][3];\r
+  GLubyte *loc;\r
+  int s, t;\r
+\r
+  /* Setup RGB image for the texture. */\r
+  loc = (GLubyte*) floorTexture;\r
+  for (t = 0; t < 16; t++) {\r
+    for (s = 0; s < 16; s++) {\r
+      if (circles[t][s] == 'x') {\r
+       /* Nice green. */\r
+        loc[0] = 0x1f;\r
+        loc[1] = 0x8f;\r
+        loc[2] = 0x1f;\r
+      } else {\r
+       /* Light gray. */\r
+        loc[0] = 0xaa;\r
+        loc[1] = 0xaa;\r
+        loc[2] = 0xaa;\r
+      }\r
+      loc += 3;\r
     }\r
-    if(meshes[i] != NULL)\r
-       return;\r
-    sprintf(fileName, "%s/model%d.x", dataDir, i+1);\r
-    meshes[i] = loadMesh(fileName);\r
-}\r
+  }\r
 \r
-/**\r
- * Loads texture i if it isn't already loaded\r
- *\r
- * After calling getTexture(i), you can make texture i the current texture using\r
- *      glBindTexture(GL_TEXTURE_2D, i);\r
- * Use i=0 to return to the default plain texture.\r
- *\r
- * You can then scale the texture via:\r
- *      glMatrixMode(GL_TEXTURE);\r
- * See the textbook, section 8.8.3.\r
- *\r
- * You must call getTexture(i) at least once before using texture i.\r
- * @param i Texture ID\r
- */\r
-void getTexture(int i) {\r
-    char fileName[220];\r
-    if(i<1 || i>NTEXTURE) {\r
-       printf("Error in getTexture - wrong texture number");\r
-       exit(1);\r
-    }\r
-    if(textures[i-1] != NULL)\r
-       return;\r
-    sprintf(fileName, "%s/texture%d.bmp", dataDir, i);\r
+  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);\r
 \r
-    textures[i-1] = loadTexture(fileName);\r
+  if (useMipmaps) {\r
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,\r
+      GL_LINEAR_MIPMAP_LINEAR);\r
+    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 16, 16,\r
+      GL_RGB, GL_UNSIGNED_BYTE, floorTexture);\r
+  } else {\r
+    if (linearFiltering) {\r
+      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);\r
+    } else {\r
+      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);\r
+    }\r
+    glTexImage2D(GL_TEXTURE_2D, 0, 3, 16, 16, 0,\r
+      GL_RGB, GL_UNSIGNED_BYTE, floorTexture);\r
+  }\r
+}\r
 \r
-    glBindTexture(GL_TEXTURE_2D, i);\r
+enum {\r
+  X, Y, Z, W\r
+};\r
+enum {\r
+  A, B, C, D\r
+};\r
 \r
-    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textures[i-1]->width, textures[i-1]->height,\r
-                0, GL_RGB, GL_UNSIGNED_BYTE, textures[i-1]->rgbData);\r
-    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, textures[i-1]->width, textures[i-1]->height, GL_RGB, \r
-                     GL_UNSIGNED_BYTE, textures[i-1]->rgbData);\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);\r
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);\r
+/* Create a matrix that will project the desired shadow. */\r
+void\r
+shadowMatrix(GLfloat shadowMat[4][4],\r
+  GLfloat groundplane[4],\r
+  GLfloat lightpos[4])\r
+{\r
+  GLfloat dot;\r
+\r
+  /* Find dot product between light position vector and ground plane normal. */\r
+  dot = groundplane[X] * lightpos[X] +\r
+    groundplane[Y] * lightpos[Y] +\r
+    groundplane[Z] * lightpos[Z] +\r
+    groundplane[W] * lightpos[W];\r
+\r
+  shadowMat[0][0] = dot - lightpos[X] * groundplane[X];\r
+  shadowMat[1][0] = 0.f - lightpos[X] * groundplane[Y];\r
+  shadowMat[2][0] = 0.f - lightpos[X] * groundplane[Z];\r
+  shadowMat[3][0] = 0.f - lightpos[X] * groundplane[W];\r
+\r
+  shadowMat[X][1] = 0.f - lightpos[Y] * groundplane[X];\r
+  shadowMat[1][1] = dot - lightpos[Y] * groundplane[Y];\r
+  shadowMat[2][1] = 0.f - lightpos[Y] * groundplane[Z];\r
+  shadowMat[3][1] = 0.f - lightpos[Y] * groundplane[W];\r
+\r
+  shadowMat[X][2] = 0.f - lightpos[Z] * groundplane[X];\r
+  shadowMat[1][2] = 0.f - lightpos[Z] * groundplane[Y];\r
+  shadowMat[2][2] = dot - lightpos[Z] * groundplane[Z];\r
+  shadowMat[3][2] = 0.f - lightpos[Z] * groundplane[W];\r
+\r
+  shadowMat[X][3] = 0.f - lightpos[W] * groundplane[X];\r
+  shadowMat[1][3] = 0.f - lightpos[W] * groundplane[Y];\r
+  shadowMat[2][3] = 0.f - lightpos[W] * groundplane[Z];\r
+  shadowMat[3][3] = dot - lightpos[W] * groundplane[W];\r
 \r
-    glBindTexture(GL_TEXTURE_2D, 0);  // Back to default texture\r
 }\r
 \r
-/**\r
- * Event hander for main menu events\r
- * @param id ID of menu item selected\r
- */\r
-void processMainEvents(int id) {\r
-  switch (id) {\r
-    case ROTATE_MOVE_CAMERA:\r
-      // Do stuff\r
-      break;\r
+/* Find the plane equation given 3 points. */\r
+void\r
+findPlane(GLfloat plane[4],\r
+  GLfloat v0[3], GLfloat v1[3], GLfloat v2[3])\r
+{\r
+  GLfloat vec0[3], vec1[3];\r
 \r
-    case POSITION_SCALE:\r
-      // Do stuff\r
-      break;\r
+  /* Need 2 vectors to find cross product. */\r
+  vec0[X] = v1[X] - v0[X];\r
+  vec0[Y] = v1[Y] - v0[Y];\r
+  vec0[Z] = v1[Z] - v0[Z];\r
 \r
-    case ROTATION_TEXTURE_SCALE:\r
-      // Do stuff\r
-      break;\r
+  vec1[X] = v2[X] - v0[X];\r
+  vec1[Y] = v2[Y] - v0[Y];\r
+  vec1[Z] = v2[Z] - v0[Z];\r
 \r
-    case EXIT:\r
-      exit(EXIT_SUCCESS);\r
+  /* find cross product to get A, B, and C of plane equation */\r
+  plane[A] = vec0[Y] * vec1[Z] - vec0[Z] * vec1[Y];\r
+  plane[B] = -(vec0[X] * vec1[Z] - vec0[Z] * vec1[X]);\r
+  plane[C] = vec0[X] * vec1[Y] - vec0[Y] * vec1[X];\r
 \r
-  }\r
+  plane[D] = -(plane[A] * v0[X] + plane[B] * v0[Y] + plane[C] * v0[Z]);\r
 }\r
 \r
-/**\r
- * Event hander for materials menu events\r
- * @param id ID of menu item selected\r
- */\r
-void processMaterialEvents(int id) {\r
-  switch (id) {\r
-    case MATERIAL_ALL_RGB:\r
-      // Do stuff\r
-      break;\r
-\r
-    case MATERIAL_AMBIENT_RGB:\r
-      // Do stuff\r
-      break;\r
+void\r
+extrudeSolidFromPolygon(GLfloat data[][2], unsigned int dataSize,\r
+  GLdouble thickness, GLuint side, GLuint edge, GLuint whole)\r
+{\r
+  static GLUtriangulatorObj *tobj = NULL;\r
+  GLdouble vertex[3], dx, dy, len;\r
+  int i;\r
+  int count = (int) (dataSize / (2 * sizeof(GLfloat)));\r
+\r
+  if (tobj == NULL) {\r
+    tobj = gluNewTess();  /* create and initialize a GLU\r
+                             polygon tesselation object */\r
+    gluTessCallback(tobj, GLU_BEGIN, glBegin);\r
+    gluTessCallback(tobj, GLU_VERTEX, glVertex2fv);  /* semi-tricky */\r
+    gluTessCallback(tobj, GLU_END, glEnd);\r
+  }\r
+  glNewList(side, GL_COMPILE);\r
+  glShadeModel(GL_SMOOTH);  /* smooth minimizes seeing\r
+                               tessellation */\r
+  gluBeginPolygon(tobj);\r
+  for (i = 0; i < count; i++) {\r
+    vertex[0] = data[i][0];\r
+    vertex[1] = data[i][1];\r
+    vertex[2] = 0;\r
+    gluTessVertex(tobj, vertex, data[i]);\r
+  }\r
+  gluEndPolygon(tobj);\r
+  glEndList();\r
+  glNewList(edge, GL_COMPILE);\r
+  glShadeModel(GL_FLAT);  /* flat shade keeps angular hands\r
+                             from being "smoothed" */\r
+  glBegin(GL_QUAD_STRIP);\r
+  for (i = 0; i <= count; i++) {\r
+    /* mod function handles closing the edge */\r
+    glVertex3f(data[i % count][0], data[i % count][1], 0.0);\r
+    glVertex3f(data[i % count][0], data[i % count][1], thickness);\r
+    /* Calculate a unit normal by dividing by Euclidean\r
+       distance. We * could be lazy and use\r
+       glEnable(GL_NORMALIZE) so we could pass in * arbitrary\r
+       normals for a very slight performance hit. */\r
+    dx = data[(i + 1) % count][1] - data[i % count][1];\r
+    dy = data[i % count][0] - data[(i + 1) % count][0];\r
+    len = sqrt(dx * dx + dy * dy);\r
+    glNormal3f(dx / len, dy / len, 0.0);\r
+  }\r
+  glEnd();\r
+  glEndList();\r
+  glNewList(whole, GL_COMPILE);\r
+  glFrontFace(GL_CW);\r
+  glCallList(edge);\r
+  glNormal3f(0.0, 0.0, -1.0);  /* constant normal for side */\r
+  glCallList(side);\r
+  glPushMatrix();\r
+  glTranslatef(0.0, 0.0, thickness);\r
+  glFrontFace(GL_CCW);\r
+  glNormal3f(0.0, 0.0, 1.0);  /* opposite normal for other side */\r
+  glCallList(side);\r
+  glPopMatrix();\r
+  glEndList();\r
+}\r
 \r
-    case MATERIAL_DIFFUSE_RGB:\r
-      // Do stuff\r
-      break;\r
+/* Enumerants for refering to display lists. */\r
+typedef enum {\r
+  RESERVED, BODY_SIDE, BODY_EDGE, BODY_WHOLE, ARM_SIDE, ARM_EDGE, ARM_WHOLE,\r
+  LEG_SIDE, LEG_EDGE, LEG_WHOLE, EYE_SIDE, EYE_EDGE, EYE_WHOLE\r
+} displayLists;\r
+\r
+static void\r
+makeDinosaur(void)\r
+{\r
+  extrudeSolidFromPolygon(body, sizeof(body), bodyWidth,\r
+    BODY_SIDE, BODY_EDGE, BODY_WHOLE);\r
+  extrudeSolidFromPolygon(arm, sizeof(arm), bodyWidth / 4,\r
+    ARM_SIDE, ARM_EDGE, ARM_WHOLE);\r
+  extrudeSolidFromPolygon(leg, sizeof(leg), bodyWidth / 2,\r
+    LEG_SIDE, LEG_EDGE, LEG_WHOLE);\r
+  extrudeSolidFromPolygon(eye, sizeof(eye), bodyWidth + 0.2,\r
+    EYE_SIDE, EYE_EDGE, EYE_WHOLE);\r
+}\r
 \r
-    case MATERIAL_SPECULAR_RGB:\r
-      // Do stuff\r
-      break;\r
+static void\r
+drawDinosaur(void)\r
 \r
-    case MATERIAL_ALL_ADSS:\r
-      // Do stuff\r
-      break;\r
+{\r
+  glPushMatrix();\r
+  /* Translate the dinosaur to be at (0,8,0). */\r
+  glTranslatef(-8, 0, -bodyWidth / 2);\r
+  glTranslatef(0.0, jump, 0.0);\r
+  glMaterialfv(GL_FRONT, GL_DIFFUSE, skinColor);\r
+  glCallList(BODY_WHOLE);\r
+  glTranslatef(0.0, 0.0, bodyWidth);\r
+  glCallList(ARM_WHOLE);\r
+  glCallList(LEG_WHOLE);\r
+  glTranslatef(0.0, 0.0, -bodyWidth - bodyWidth / 4);\r
+  glCallList(ARM_WHOLE);\r
+  glTranslatef(0.0, 0.0, -bodyWidth / 4);\r
+  glCallList(LEG_WHOLE);\r
+  glTranslatef(0.0, 0.0, bodyWidth / 2 - 0.1);\r
+  glMaterialfv(GL_FRONT, GL_DIFFUSE, eyeColor);\r
+  glCallList(EYE_WHOLE);\r
+  glPopMatrix();\r
+}\r
 \r
-    case MATERIAL_RED_ADSS:\r
-      // Do stuff\r
-      break;\r
+static GLfloat floorVertices[4][3] = {\r
+  { -20.0, 0.0, 20.0 },\r
+  { 20.0, 0.0, 20.0 },\r
+  { 20.0, 0.0, -20.0 },\r
+  { -20.0, 0.0, -20.0 },\r
+};\r
 \r
-    case MATERIAL_GREEN_ADSS:\r
-      // Do stuff\r
-      break;\r
+/* Draw a floor (possibly textured). */\r
+static void\r
+drawFloor(void)\r
+{\r
+  glDisable(GL_LIGHTING);\r
 \r
-    case MATERIAL_BLUE_ADSS:\r
-      // Do stuff\r
-      break;\r
+  if (useTexture) {\r
+    glEnable(GL_TEXTURE_2D);\r
+  }\r
 \r
+  glBegin(GL_QUADS);\r
+    glTexCoord2f(0.0, 0.0);\r
+    glVertex3fv(floorVertices[0]);\r
+    glTexCoord2f(0.0, 16.0);\r
+    glVertex3fv(floorVertices[1]);\r
+    glTexCoord2f(16.0, 16.0);\r
+    glVertex3fv(floorVertices[2]);\r
+    glTexCoord2f(16.0, 0.0);\r
+    glVertex3fv(floorVertices[3]);\r
+  glEnd();\r
+\r
+  if (useTexture) {\r
+    glDisable(GL_TEXTURE_2D);\r
   }\r
+\r
+  glEnable(GL_LIGHTING);\r
 }\r
 \r
-/**\r
- * Event hander for light menu events\r
- * @param id ID of menu item selected\r
- */\r
-void processLightEvents(int id) {\r
-  switch (id) {\r
-    case LIGHT_MOVE_LIGHT_1:\r
-      // Do stuff\r
-      break;\r
+static GLfloat floorPlane[4];\r
+static GLfloat floorShadow[4][4];\r
 \r
-    case LIGHT_RGBALL_LIGHT_1:\r
-      // Do stuff\r
-      break;\r
+static void\r
+redraw(void)\r
+{\r
+  int start, end;\r
 \r
-    case LIGHT_MOVE_LIGHT_2:\r
-      // Do stuff\r
-      break;\r
+  if (reportSpeed) {\r
+    start = glutGet(GLUT_ELAPSED_TIME);\r
+  }\r
 \r
-    case LIGHT_RGBALL_LIGHT_2:\r
-      // Do stuff\r
-      break;\r
+  /* Clear; default stencil clears to zero. */\r
+  if ((stencilReflection && renderReflection) || (stencilShadow && renderShadow)) {\r
+    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);\r
+  } else {\r
+    /* Avoid clearing stencil when not using it. */\r
+    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);\r
+  }\r
 \r
+  /* Reposition the light source. */\r
+  lightPosition[0] = 12*cos(lightAngle);\r
+  lightPosition[1] = lightHeight;\r
+  lightPosition[2] = 12*sin(lightAngle);\r
+  if (directionalLight) {\r
+    lightPosition[3] = 0.0;\r
+  } else {\r
+    lightPosition[3] = 1.0;\r
   }\r
-}\r
 \r
-/**\r
- * Event hander for object menu events\r
- * @param id ID of object selected\r
- */\r
-void processObjectEvents(int id) {\r
+  shadowMatrix(floorShadow, floorPlane, lightPosition);\r
 \r
-}\r
+  glPushMatrix();\r
+    /* Perform scene rotations based on user mouse input. */\r
+    glRotatef(angle2, 1.0, 0.0, 0.0);\r
+    glRotatef(angle, 0.0, 1.0, 0.0);\r
 \r
-/**\r
- * Event hander for texture menu events\r
- * @param id ID of texutre selected\r
- */\r
-void processTextureEvents(int id) {\r
+    /* Tell GL new light source position. */\r
+    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);\r
 \r
-}\r
+    if (renderReflection) {\r
+      if (stencilReflection) {\r
+        /* We can eliminate the visual "artifact" of seeing the "flipped"\r
+          dinosaur underneath the floor by using stencil.  The idea is\r
+          draw the floor without color or depth update but so that\r
+          a stencil value of one is where the floor will be.  Later when\r
+          rendering the dinosaur reflection, we will only update pixels\r
+          with a stencil value of 1 to make sure the reflection only\r
+          lives on the floor, not below the floor. */\r
 \r
-/**\r
- * Event hander for ground texture menu events\r
- * @param id ID of ground texture selected\r
- */\r
-void processGTextureEvents(int id) {\r
+        /* Don't update color or depth. */\r
+        glDisable(GL_DEPTH_TEST);\r
+        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);\r
 \r
-}\r
+        /* Draw 1 into the stencil buffer. */\r
+        glEnable(GL_STENCIL_TEST);\r
+        glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);\r
+        glStencilFunc(GL_ALWAYS, 1, 0xffffffff);\r
 \r
-/**\r
- * Rounds up numbers, from http://stackoverflow.com/questions/3407012/c-rounding-up-to-the-nearest-multiple-of-a-number\r
- * @param numToRound Number to round\r
- * @param multiple Multiple to round up to\r
- * @return Rounded number\r
- */\r
-int roundUp(int numToRound, int multiple) {\r
-  if(multiple == 0) {\r
-    return numToRound;\r
-  }\r
+        /* Now render floor; floor pixels just get their stencil set to 1. */\r
+        drawFloor();\r
 \r
-  int remainder = numToRound % multiple;\r
-  if (remainder == 0)\r
-    return numToRound;\r
-  return numToRound + multiple - remainder;\r
-}\r
+        /* Re-enable update of color and depth. */\r
+        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);\r
+        glEnable(GL_DEPTH_TEST);\r
+\r
+        /* Now, only render where stencil is set to 1. */\r
+        glStencilFunc(GL_EQUAL, 1, 0xffffffff);  /* draw if ==1 */\r
+        glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);\r
+      }\r
+\r
+      glPushMatrix();\r
+\r
+        /* The critical reflection step: Reflect dinosaur through the floor\r
+           (the Y=0 plane) to make a relection. */\r
+        glScalef(1.0, -1.0, 1.0);\r
+\r
+       /* Reflect the light position. */\r
+        glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);\r
+\r
+        /* To avoid our normals getting reversed and hence botched lighting\r
+          on the reflection, turn on normalize.  */\r
+        glEnable(GL_NORMALIZE);\r
+        glCullFace(GL_FRONT);\r
 \r
-/**\r
- * Makes a submenu from an array of items, splitting the list into subsubmenus\r
- * of only 10 items.\r
- * @param menuEntries Array of menu items\r
- * @param menuEntriesSize Size of menuEntries\r
- * @param callback Callback function for this array of menu items\r
- * @return Reference to menu created\r
- */\r
-int makeSubmenuFromArray( const char *menuEntries[], unsigned int menuEntriesSize, void *callback ) {\r
-  if ( menuEntriesSize == 0 ) return -1;\r
-\r
-  int menuNumber = roundUp(menuEntriesSize, 10) / 10;\r
-  int submenuObjects[menuNumber-1];\r
-\r
-  for( int i = 0; i < menuNumber; i++ ) {\r
-    submenuObjects[i] = glutCreateMenu(callback);\r
-    int startNum = i*11 - (i-1);\r
-    for ( int j = startNum - 1; j < (startNum+9); j++ ) {\r
-      if ( j == menuEntriesSize ) break; // Detect if we've reached the end of the array\r
-      glutAddMenuEntry( menuEntries[j], j + 1 );\r
+        /* Draw the reflected dinosaur. */\r
+        drawDinosaur();\r
+\r
+        /* Disable noramlize again and re-enable back face culling. */\r
+        glDisable(GL_NORMALIZE);\r
+        glCullFace(GL_BACK);\r
+\r
+      glPopMatrix();\r
+\r
+      /* Switch back to the unreflected light position. */\r
+      glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);\r
+\r
+      if (stencilReflection) {\r
+        glDisable(GL_STENCIL_TEST);\r
+      }\r
     }\r
-  } \r
-\r
-  int mainMenu = glutCreateMenu(callback);\r
-  for ( int i = 0; i < menuNumber; i++ ) {\r
-    char name[10]; // buffer to hold name\r
-    int startNum = i*11 - (i-1);\r
-    int endNum = startNum + 9;\r
-    if ( i == menuNumber - 1 ) { // We're on the last one\r
-      endNum = startNum + (menuEntriesSize - startNum); // Work out final number\r
+\r
+    /* Back face culling will get used to only draw either the top or the\r
+       bottom floor.  This let's us get a floor with two distinct\r
+       appearances.  The top floor surface is reflective and kind of red.\r
+       The bottom floor surface is not reflective and blue. */\r
+\r
+    /* Draw "bottom" of floor in blue. */\r
+    glFrontFace(GL_CW);  /* Switch face orientation. */\r
+    glColor4f(0.1, 0.1, 0.7, 1.0);\r
+    drawFloor();\r
+    glFrontFace(GL_CCW);\r
+\r
+    if (renderShadow) {\r
+      if (stencilShadow) {\r
+       /* Draw the floor with stencil value 3.  This helps us only\r
+          draw the shadow once per floor pixel (and only on the\r
+          floor pixels). */\r
+        glEnable(GL_STENCIL_TEST);\r
+        glStencilFunc(GL_ALWAYS, 3, 0xffffffff);\r
+        glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);\r
+      }\r
     }\r
-    sprintf(name, "%d-%d", startNum, endNum);\r
-    glutAddSubMenu( name, submenuObjects[i] );\r
-  }\r
 \r
-  return mainMenu;\r
-}\r
+    /* Draw "top" of floor.  Use blending to blend in reflection. */\r
+    glEnable(GL_BLEND);\r
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
+    glColor4f(0.7, 0.0, 0.0, 0.3);\r
+    glColor4f(1.0, 1.0, 1.0, 0.3);\r
+    drawFloor();\r
+    glDisable(GL_BLEND);\r
 \r
-/**\r
- * Creates menu for program\r
- */\r
-void makeMenu() {\r
-  // Construct material menu\r
-  int materialMenu = glutCreateMenu(processMaterialEvents);\r
-  glutAddMenuEntry("All R/G/B", MATERIAL_ALL_RGB);\r
-  glutAddMenuEntry("Ambient R/G/B", MATERIAL_AMBIENT_RGB);\r
-  glutAddMenuEntry("Diffuse R/G/B", MATERIAL_DIFFUSE_RGB);\r
-  glutAddMenuEntry("Specular R/G/B", MATERIAL_SPECULAR_RGB);\r
-  glutAddMenuEntry("All Amb/Diff/Spec/Shine", MATERIAL_ALL_ADSS);\r
-  glutAddMenuEntry("Red Amb/Diff/Spec/Shine", MATERIAL_RED_ADSS);\r
-  glutAddMenuEntry("Green Amb/Diff/Spec/Shine", MATERIAL_GREEN_ADSS);\r
-  glutAddMenuEntry("Blue Amb/Diff/Spec/Shine", MATERIAL_BLUE_ADSS);\r
-\r
-  // Construct light menu\r
-  int lightMenu = glutCreateMenu(processLightEvents);\r
-  glutAddMenuEntry("Move Light 1", LIGHT_MOVE_LIGHT_1);\r
-  glutAddMenuEntry("R/G/B/All Light 1", LIGHT_RGBALL_LIGHT_1);\r
-  glutAddMenuEntry("Move Light 2", LIGHT_MOVE_LIGHT_2);\r
-  glutAddMenuEntry("R/G/B/All Light 2", LIGHT_RGBALL_LIGHT_2);\r
-\r
-  // Construct object menu\r
-  int objectMenuEntriesSize = sizeof(objectMenuEntries) / sizeof(objectMenuEntries[0]);\r
-  int objectMenu = makeSubmenuFromArray( objectMenuEntries, objectMenuEntriesSize, processObjectEvents );\r
-\r
-  // Construct texture / ground texture menus\r
-  int textureMenuEntriesSize = sizeof(textureMenuEntries) / sizeof(textureMenuEntries[0]);\r
-  int textureMenu = makeSubmenuFromArray( textureMenuEntries, textureMenuEntriesSize, processTextureEvents );\r
-  int gTextureMenu = makeSubmenuFromArray( textureMenuEntries, textureMenuEntriesSize, processGTextureEvents );\r
-\r
-  // Construct main menu\r
-  glutCreateMenu(processMainEvents);\r
-  //glutAddMenuEntry("Rotate/Move Camera", ROTATE_MOVE_CAMERA);\r
-  //glutAddSubMenu("Add object", objectMenu);\r
-  //glutAddMenuEntry("Position/Scale", POSITION_SCALE);\r
-  //glutAddMenuEntry("Rotation/Texture Scale", ROTATION_TEXTURE_SCALE);\r
-  //glutAddSubMenu("Material", materialMenu);\r
-  //glutAddSubMenu("Texture", textureMenu);\r
-  //glutAddSubMenu("Ground texture", gTextureMenu);\r
-  //glutAddSubMenu("Lights", lightMenu);\r
-  glutAddMenuEntry("Exit", EXIT);\r
-\r
-  // Bind to right mouse button\r
-  glutAttachMenu(GLUT_RIGHT_BUTTON);\r
-}\r
+    if (renderDinosaur) {\r
+      /* Draw "actual" dinosaur, not its reflection. */\r
+      drawDinosaur();\r
+    }\r
 \r
-/**\r
- * Called when window is resized\r
- * @param w New width\r
- * @param h New height\r
- */\r
-void windowReshape(int w, int h) {\r
-  glViewport(0, 0, (GLsizei) w, (GLsizei) h);\r
-  glMatrixMode(GL_PROJECTION);\r
-  glLoadIdentity();\r
-  if (w <= h) \r
-    glOrtho(near, far, near*(GLfloat)h/(GLfloat)w,\r
-             far*(GLfloat)h/(GLfloat)w, near, far);\r
-  else\r
-    glOrtho(near*(GLfloat)w/(GLfloat)h,\r
-             far*(GLfloat)w/(GLfloat)h, near, far, near, far);\r
-   glMatrixMode(GL_MODELVIEW); \r
-   glLoadIdentity();\r
+    if (renderShadow) {\r
+\r
+      /* Render the projected shadow. */\r
+\r
+      if (stencilShadow) {\r
+\r
+        /* Now, only render where stencil is set above 2 (ie, 3 where\r
+          the top floor is).  Update stencil with 2 where the shadow\r
+          gets drawn so we don't redraw (and accidently reblend) the\r
+          shadow). */\r
+        glStencilFunc(GL_LESS, 2, 0xffffffff);  /* draw if ==1 */\r
+        glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);\r
+      }\r
+\r
+      /* To eliminate depth buffer artifacts, we use polygon offset\r
+        to raise the depth of the projected shadow slightly so\r
+        that it does not depth buffer alias with the floor. */\r
+      if (offsetShadow) {\r
+       switch (polygonOffsetVersion) {\r
+       case EXTENSION:\r
+#ifdef GL_EXT_polygon_offset\r
+         glEnable(GL_POLYGON_OFFSET_EXT);\r
+         break;\r
+#endif\r
+#ifdef GL_VERSION_1_1\r
+       case ONE_DOT_ONE:\r
+          glEnable(GL_POLYGON_OFFSET_FILL);\r
+         break;\r
+#endif\r
+       case MISSING:\r
+         /* Oh well. */\r
+         break;\r
+       }\r
+      }\r
+\r
+      /* Render 50% black shadow color on top of whatever the\r
+         floor appareance is. */\r
+      glEnable(GL_BLEND);\r
+      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
+      glDisable(GL_LIGHTING);  /* Force the 50% black. */\r
+      glColor4f(0.0, 0.0, 0.0, 0.5);\r
+\r
+      glPushMatrix();\r
+       /* Project the shadow. */\r
+        glMultMatrixf((GLfloat *) floorShadow);\r
+        drawDinosaur();\r
+      glPopMatrix();\r
+\r
+      glDisable(GL_BLEND);\r
+      glEnable(GL_LIGHTING);\r
+\r
+      if (offsetShadow) {\r
+       switch (polygonOffsetVersion) {\r
+#ifdef GL_EXT_polygon_offset\r
+       case EXTENSION:\r
+         glDisable(GL_POLYGON_OFFSET_EXT);\r
+         break;\r
+#endif\r
+#ifdef GL_VERSION_1_1\r
+       case ONE_DOT_ONE:\r
+          glDisable(GL_POLYGON_OFFSET_FILL);\r
+         break;\r
+#endif\r
+       case MISSING:\r
+         /* Oh well. */\r
+         break;\r
+       }\r
+      }\r
+      if (stencilShadow) {\r
+        glDisable(GL_STENCIL_TEST);\r
+      }\r
+    }\r
+\r
+    glPushMatrix();\r
+    glDisable(GL_LIGHTING);\r
+    glColor3f(1.0, 1.0, 0.0);\r
+    if (directionalLight) {\r
+      /* Draw an arrowhead. */\r
+      glDisable(GL_CULL_FACE);\r
+      glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);\r
+      glRotatef(lightAngle * -180.0 / M_PI, 0, 1, 0);\r
+      glRotatef(atan(lightHeight/12) * 180.0 / M_PI, 0, 0, 1);\r
+      glBegin(GL_TRIANGLE_FAN);\r
+       glVertex3f(0, 0, 0);\r
+       glVertex3f(2, 1, 1);\r
+       glVertex3f(2, -1, 1);\r
+       glVertex3f(2, -1, -1);\r
+       glVertex3f(2, 1, -1);\r
+       glVertex3f(2, 1, 1);\r
+      glEnd();\r
+      /* Draw a white line from light direction. */\r
+      glColor3f(1.0, 1.0, 1.0);\r
+      glBegin(GL_LINES);\r
+       glVertex3f(0, 0, 0);\r
+       glVertex3f(5, 0, 0);\r
+      glEnd();\r
+      glEnable(GL_CULL_FACE);\r
+    } else {\r
+      /* Draw a yellow ball at the light source. */\r
+      glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);\r
+      glutSolidSphere(1.0, 5, 5);\r
+    }\r
+    glEnable(GL_LIGHTING);\r
+    glPopMatrix();\r
+\r
+  glPopMatrix();\r
+\r
+  if (reportSpeed) {\r
+    glFinish();\r
+    end = glutGet(GLUT_ELAPSED_TIME);\r
+    printf("Speed %.3g frames/sec (%d ms)\n", 1000.0/(end-start), end-start);\r
+  }\r
+\r
+  glutSwapBuffers();\r
 }\r
 \r
-/**\r
- * Called when mouse event occurs\r
- * @param btn Mouse button\r
- * @param state State of mouse button\r
- * @param x Mouse x position\r
- * @param y Mouse y position\r
- */\r
-void mouse(int button, int state, int x, int y) {\r
+/* ARGSUSED2 */\r
+static void\r
+mouse(int button, int state, int x, int y)\r
+{\r
   if (button == GLUT_LEFT_BUTTON) {\r
     if (state == GLUT_DOWN) {\r
       moving = 1;\r
@@ -542,12 +636,10 @@ void mouse(int button, int state, int x, int y) {
   }\r
 }\r
 \r
-/**\r
- * Called when motion event occurs\r
- * @param x Mouse x position\r
- * @param y Mouse y position\r
- */\r
-void motion(int x, int y) {\r
+/* ARGSUSED1 */\r
+static void\r
+motion(int x, int y)\r
+{\r
   if (moving) {\r
     angle = angle + (x - startx);\r
     angle2 = angle2 + (y - starty);\r
@@ -564,203 +656,237 @@ void motion(int x, int y) {
   }\r
 }\r
 \r
-/**\r
- * Recursive function to draw a square by drawing smaller and smaller\r
- * divisions of the square, determined by drawFloorRecurse.\r
- * @param recurseLevel Current level of recursion, only pass 0\r
- * @param x1 top-left x\r
- * @param z1 top-left z\r
- * @param x2 bottom-left x\r
- * @param z2 bottom-left z\r
- */\r
-void drawSquare(int recurseLevel, float x1, float z1, float x2, float z2) {\r
-\r
-  if ( drawFloorRecurse != recurseLevel ) {\r
-    // Calculate middle points\r
-    float xm = (x1 + x2) / 2.0;\r
-    float zm = (z1 + z2) / 2.0;\r
-\r
-    // Increment recursion level\r
-    int rnew = recurseLevel + 1;\r
-\r
-    // Split into four sub-quads\r
-    drawSquare(rnew, x1, z1, xm, zm);\r
-    drawSquare(rnew, x1, zm, xm, z2);\r
-    drawSquare(rnew, xm, zm, x2, z2);\r
-    drawSquare(rnew, xm, z1, x2, zm);\r
+/* Advance time varying state when idle callback registered. */\r
+static void\r
+idle(void)\r
+{\r
+  static float time = 0.0;\r
 \r
-  } else {\r
-    // Draw square.\r
-    // **NOTE: Is the polygon facing in the right direction?\r
-    glBegin(GL_QUADS);\r
-      glVertex3f(x1, 0.0, z1);\r
-      glVertex3f(x1, 0.0, z2);\r
-      glVertex3f(x2, 0.0, z2);\r
-      glVertex3f(x2, 0.0, z1);\r
-    glEnd();\r
-  }\r
+  time = glutGet(GLUT_ELAPSED_TIME) / 500.0;\r
 \r
+  jump = 4.0 * fabs(sin(time)*0.5);\r
+  if (!lightMoving) {\r
+    lightAngle += 0.03;\r
+  }\r
+  glutPostRedisplay();\r
 }\r
 \r
-/**\r
- * Draw a floor by calling the drawSquare recursion\r
- */\r
-void drawFloor() {\r
-  drawSquare(0, -floorSize, -floorSize, floorSize, floorSize);\r
-}\r
+enum {\r
+  M_NONE, M_MOTION, M_LIGHT, M_TEXTURE, M_SHADOWS, M_REFLECTION, M_DINOSAUR,\r
+  M_STENCIL_REFLECTION, M_STENCIL_SHADOW, M_OFFSET_SHADOW,\r
+  M_POSITIONAL, M_DIRECTIONAL, M_PERFORMANCE\r
+};\r
 \r
-/**\r
- * Draw x, z axis on floor\r
- */\r
-void drawLine() {\r
-    // **NOTE: fix function\r
-    glDisable(GL_TEXTURE_2D);\r
-    glEnable(GL_BLEND);\r
-    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
-    glColor4ub( 0.0, 0.0, 0.0, 0.5 );\r
+static void\r
+controlLights(int value)\r
+{\r
+  switch (value) {\r
+  case M_NONE:\r
+    return;\r
+  case M_MOTION:\r
+    animation = 1 - animation;\r
+    if (animation) {\r
+      glutIdleFunc(idle);\r
+    } else {\r
+      glutIdleFunc(NULL);\r
+    }\r
+    break;\r
+  case M_LIGHT:\r
+    lightSwitch = !lightSwitch;\r
+    if (lightSwitch) {\r
+      glEnable(GL_LIGHT0);\r
+    } else {\r
+      glDisable(GL_LIGHT0);\r
+    }\r
+    break;\r
+  case M_TEXTURE:\r
+    useTexture = !useTexture;\r
+    break;\r
+  case M_SHADOWS:\r
+    renderShadow = 1 - renderShadow;\r
+    break;\r
+  case M_REFLECTION:\r
+    renderReflection = 1 - renderReflection;\r
+    break;\r
+  case M_DINOSAUR:\r
+    renderDinosaur = 1 - renderDinosaur;\r
+    break;\r
+  case M_STENCIL_REFLECTION:\r
+    stencilReflection = 1 - stencilReflection;\r
+    break;\r
+  case M_STENCIL_SHADOW:\r
+    stencilShadow = 1 - stencilShadow;\r
+    break;\r
+  case M_OFFSET_SHADOW:\r
+    offsetShadow = 1 - offsetShadow;\r
+    break;\r
+  case M_POSITIONAL:\r
+    directionalLight = 0;\r
+    break;\r
+  case M_DIRECTIONAL:\r
+    directionalLight = 1;\r
+    break;\r
+  case M_PERFORMANCE:\r
+    reportSpeed = 1 - reportSpeed;\r
+    break;\r
+  }\r
+  glutPostRedisplay();\r
+}\r
 \r
-    glBegin(GL_LINES);\r
-    glVertex3i( 10.0, 0.1, 10.0);\r
-    glVertex3i( -10.0, 0.1, -10.0);\r
-    glEnd();\r
+/* When not visible, stop animating.  Restart when visible again. */\r
+static void\r
+visible(int vis)\r
+{\r
+  if (vis == GLUT_VISIBLE) {\r
+    if (animation)\r
+      glutIdleFunc(idle);\r
+  } else {\r
+    if (!animation)\r
+      glutIdleFunc(NULL);\r
+  }\r
+}\r
 \r
-    glDisable(GL_BLEND);\r
-    glEnable(GL_TEXTURE_2D);\r
+/* Press any key to redraw; good when motion stopped and\r
+   performance reporting on. */\r
+/* ARGSUSED */\r
+static void\r
+key(unsigned char c, int x, int y)\r
+{\r
+  if (c == 27) {\r
+    exit(0);  /* IRIS GLism, Escape quits. */\r
+  }\r
+  glutPostRedisplay();\r
 }\r
 \r
-/**\r
- * Display function\r
- */\r
-void display() {\r
-  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);\r
-  glLoadIdentity();\r
-  gluLookAt(\r
-    0.0, 0.0, 10.0,  /* eye is at (x,y,z) */\r
-    0.0, 0.0,  0.0,  /* center is at (x,y,z) */\r
-    0.0, 1.0,  0.0   /* up is in postivie Y direction */\r
-    );\r
+/* Press any key to redraw; good when motion stopped and\r
+   performance reporting on. */\r
+/* ARGSUSED */\r
+static void\r
+special(int k, int x, int y)\r
+{\r
+  glutPostRedisplay();\r
+}\r
 \r
+static int\r
+supportsOneDotOne(void)\r
+{\r
+  const char *version;\r
+  int major, minor;\r
 \r
-  // **NOTE: Currently this rotation function is all that moves the camera off\r
-  //         the flat surface. Need to integrate function into gluLookAt\r
-  glRotatef(30.0, 1.0, 0.0, 0.0);\r
+  version = (char *) glGetString(GL_VERSION);\r
+  if (sscanf(version, "%d.%d", &major, &minor) == 2)\r
+    return major >= 1 && minor >= 1;\r
+  return 0;            /* OpenGL version string malformed! */\r
+}\r
 \r
-  /* Reposition the light source. */\r
-  lightPosition[0] = 12*cos(lightAngle);\r
-  lightPosition[1] = lightHeight;\r
-  lightPosition[2] = 12*sin(lightAngle);\r
-  lightPosition[3] = 0.0;\r
+int\r
+main(int argc, char **argv)\r
+{\r
+  int i;\r
 \r
-  glPushMatrix();\r
+  glutInit(&argc, argv);\r
 \r
-    /* Perform scene rotations based on user mouse input. */\r
-    glRotatef(angle, 0.0, 1.0, 0.0);\r
-    //glRotatef(angle2, 1.0, 0.0, 0.0); **NOTE: Only one degree of freedom\r
+  for (i=1; i<argc; i++) {\r
+    if (!strcmp("-linear", argv[i])) {\r
+      linearFiltering = 1;\r
+    } else if (!strcmp("-mipmap", argv[i])) {\r
+      useMipmaps = 1;\r
+    } else if (!strcmp("-ext", argv[i])) {\r
+      forceExtension = 1;\r
+    }\r
+  }\r
 \r
-    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);\r
+  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL | GLUT_MULTISAMPLE);\r
 \r
-    drawFloor();\r
+#if 1\r
+  /* In GLUT 4.0, you'll be able to do this an be sure to\r
+     get 2 bits of stencil if the machine has it for you. */\r
+  glutInitDisplayString("samples stencil>=2 rgb double depth");\r
+#endif\r
 \r
-    drawLine();\r
-    \r
-    // Draw teapot for a test object\r
-    glPushMatrix();\r
-      glTranslatef(0.0, 0.5, 0.0); // **NOTE: Teapot does not rest on surface\r
-      glColor3f(0.5, 0.5, 0.5);\r
-      glutSolidTeapot(1);\r
-    glPopMatrix();\r
+  glutCreateWindow("Shadowy Leapin' Lizards");\r
 \r
-    // Draw a white ball over the light source\r
-    glPushMatrix();\r
-      glDisable(GL_LIGHTING);\r
-      glColor3f(1.0, 1.0, 1.0);\r
-      glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);\r
-      glutSolidSphere(1.0, 50, 50);\r
-      glEnable(GL_LIGHTING);\r
-    glPopMatrix();\r
+  if (glutGet(GLUT_WINDOW_STENCIL_SIZE) <= 1) {\r
+    printf("dinoshade: Sorry, I need at least 2 bits of stencil.\n");\r
+    exit(1);\r
+  }\r
 \r
-  glPopMatrix();\r
+  /* Register GLUT callbacks. */\r
+  glutDisplayFunc(redraw);\r
+  glutMouseFunc(mouse);\r
+  glutMotionFunc(motion);\r
+  glutVisibilityFunc(visible);\r
+  glutKeyboardFunc(key);\r
+  glutSpecialFunc(special);\r
+\r
+  glutCreateMenu(controlLights);\r
+\r
+  glutAddMenuEntry("Toggle motion", M_MOTION);\r
+  glutAddMenuEntry("-----------------------", M_NONE);\r
+  glutAddMenuEntry("Toggle light", M_LIGHT);\r
+  glutAddMenuEntry("Toggle texture", M_TEXTURE);\r
+  glutAddMenuEntry("Toggle shadows", M_SHADOWS);\r
+  glutAddMenuEntry("Toggle reflection", M_REFLECTION);\r
+  glutAddMenuEntry("Toggle dinosaur", M_DINOSAUR);\r
+  glutAddMenuEntry("-----------------------", M_NONE);\r
+  glutAddMenuEntry("Toggle reflection stenciling", M_STENCIL_REFLECTION);\r
+  glutAddMenuEntry("Toggle shadow stenciling", M_STENCIL_SHADOW);\r
+  glutAddMenuEntry("Toggle shadow offset", M_OFFSET_SHADOW);\r
+  glutAddMenuEntry("----------------------", M_NONE);\r
+  glutAddMenuEntry("Positional light", M_POSITIONAL);\r
+  glutAddMenuEntry("Directional light", M_DIRECTIONAL);\r
+  glutAddMenuEntry("-----------------------", M_NONE);\r
+  glutAddMenuEntry("Toggle performance", M_PERFORMANCE);\r
+  glutAttachMenu(GLUT_RIGHT_BUTTON);\r
+  makeDinosaur();\r
+\r
+#ifdef GL_VERSION_1_1\r
+  if (supportsOneDotOne() && !forceExtension) {\r
+    polygonOffsetVersion = ONE_DOT_ONE;\r
+    glPolygonOffset(-2.0, -1.0);\r
+  } else\r
+#endif\r
+  {\r
+#ifdef GL_EXT_polygon_offset\r
+  /* check for the polygon offset extension */\r
+  if (glutExtensionSupported("GL_EXT_polygon_offset")) {\r
+    polygonOffsetVersion = EXTENSION;\r
+    glPolygonOffsetEXT(-0.1, -0.002);\r
+  } else\r
+#endif\r
+    {\r
+      polygonOffsetVersion = MISSING;\r
+      printf("\ndinoshine: Missing polygon offset.\n");\r
+      printf("           Expect shadow depth aliasing artifacts.\n\n");\r
+    }\r
+  }\r
 \r
-  glutSwapBuffers();\r
-}\r
+  glEnable(GL_CULL_FACE);\r
+  glEnable(GL_DEPTH_TEST);\r
+  glEnable(GL_TEXTURE_2D);\r
+  glLineWidth(3.0);\r
 \r
-/**\r
- * init function; sets initial OpenGL state\r
- */\r
-void init() {\r
   glMatrixMode(GL_PROJECTION);\r
-  glLoadIdentity();\r
-\r
-  gluPerspective(\r
-    60.0,  /* field of view in degree */\r
-     1.0,  /* aspect ratio */\r
-    -10,  /* Z near */\r
-     10   /* Z far */\r
-    );    \r
-\r
-  \r
-  glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);\r
-  glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);\r
-  glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);\r
-  glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);\r
-  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glightmodel);\r
+  gluPerspective( /* field of view in degree */ 40.0,\r
+  /* aspect ratio */ 1.0,\r
+    /* Z near */ 20.0, /* Z far */ 100.0);\r
+  glMatrixMode(GL_MODELVIEW);\r
+  gluLookAt(0.0, 8.0, 60.0,  /* eye is at (0,8,60) */\r
+    0.0, 8.0, 0.0,      /* center is at (0,8,0) */\r
+    0.0, 1.0, 0.);      /* up is in postivie Y direction */\r
+\r
+  glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);\r
+  glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);\r
+  glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1);\r
+  glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05);\r
   glEnable(GL_LIGHT0);\r
   glEnable(GL_LIGHTING);\r
-  glEnable(GL_COLOR_MATERIAL);\r
-  glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );\r
-  glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, specular0);\r
-  glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, emission0);\r
-\r
-  \r
-\r
-  glMatrixMode(GL_MODELVIEW);\r
-  glLoadIdentity();\r
-}\r
-\r
-/**\r
- * Main function\r
- * @param argc Number of arguments\r
- * @param argv Array of arguments\r
- * @return Program exit code\r
- */\r
-int main(int argc, char **argv) {\r
-  if(argc>1)\r
-    strcpy(dataDir, argv[1]);\r
-  else if(opendir(dirDefault1))\r
-    strcpy(dataDir, dirDefault1);\r
-  else if(opendir(dirDefault2))\r
-    strcpy(dataDir, dirDefault2);\r
-  else fileErr(dirDefault1);\r
-\r
-  for(int i=0; i<NMESH; i++) meshes[i]=NULL;\r
-  for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;\r
-\r
-  glutInit(&argc, argv);\r
 \r
-  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);\r
+  makeFloorTexture();\r
 \r
-  glutInitWindowSize(500, 500);\r
-  glutCreateWindow("Scene Editor");\r
-\r
-  glShadeModel(GL_SMOOTH); // Enables Smooth Shading\r
-  glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background\r
-  glClearDepth(1.0f); // Depth Buffer Setup\r
-  glEnable(GL_DEPTH_TEST); // Enables Depth Testing\r
-  glDepthFunc(GL_LEQUAL);  // the type\r
-  glEnable(GL_CULL_FACE);\r
-  glEnable(GL_TEXTURE_2D);\r
-  glLineWidth(1.0);\r
-\r
-  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);\r
-\r
-  glutReshapeFunc(windowReshape);\r
-  glutDisplayFunc(display);\r
-  glutMouseFunc(mouse);\r
-  glutMotionFunc(motion);\r
-\r
-  makeMenu();\r
-\r
-  init();\r
+  /* Setup floor plane for projected shadow calculations. */\r
+  findPlane(floorPlane, floorVertices[1], floorVertices[2], floorVertices[3]);\r
 \r
   glutMainLoop();\r
-}\r
+  return 0;             /* ANSI C requires main to return int. */\r
+}
\ No newline at end of file

UCC git Repository :: git.ucc.asn.au