-\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
-\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
-\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
-\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
-\r
-// Directories containing models\r
-char *dirDefault1 = "models-textures";\r
-char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures";\r
-\r
-char dataDir[200]; // Stores the directory name for the meshes and textures.\r
-\r
-static GLfloat floorVertices[4][3] = {\r
- { -1000.0, 0.0, 1000.0 },\r
- { 1000.0, 0.0, 1000.0 },\r
- { 1000.0, 0.0, -1000.0 },\r
- { -1000.0, 0.0, -1000.0 },\r
-};\r
-\r
-static GLfloat lightColor[] = {1.0, 1.0, 1.0, 1.0}; // White light\r
-static GLfloat lightPosition[4];\r
-\r
-int moving, startx, starty;\r
-int lightMoving = 0, lightStartX, lightStartY;\r
-\r
-/* Time varying or user-controled variables. */\r
-static float jump = 0.0;\r
-static float lightAngle = 0.0, lightHeight = 20;\r
-GLfloat angle = -150; /* in degrees */\r
-GLfloat angle2 = 30; /* in degrees */\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
-\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
-\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
- }\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
- * 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
-\r
- textures[i-1] = loadTexture(fileName);\r
-\r
- glBindTexture(GL_TEXTURE_2D, i);\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
-\r
- glBindTexture(GL_TEXTURE_2D, 0); // Back to default texture\r
-}\r