--- /dev/null
+/**
+ * Global Variables File
+ * Contains all of the project's global variables
+ * @author Ashley Tyndall (20915779), Jenna de la Harpe (20367932)
+ */
+
+#include "types.h"
+#include "globals.h"
+
+mesh* meshes[NMESH]; // An array of pointers to the meshes - see getMesh
+texture* textures[NTEXTURE]; // An array of texture pointers - see getTexture
+
+// Menu arrays
+const char *textureMenuEntries[NTEXTURE] = {
+ "1 Plain", "2 Rust", "3 Concrete", "4 Carpet", "5 Beach Sand",
+ "6 Rocky", "7 Brick", "8 Water", "9 Paper", "10 Marble",
+ "11 Wood", "12 Scales", "13 Fur", "14 Denim", "15 Hessian",
+ "16 Orange Peel", "17 Ice Crystals", "18 Grass", "19 Corrugated Iron", "20 Styrofoam",
+ "21 Bubble Wrap", "22 Leather", "23 Camouflage", "24 Asphalt", "25 Scratched Ice",
+ "26 Rattan", "27 Snow", "28 Dry Mud", "29 Old Concrete", "30 Leopard Skin"
+};
+
+const char *objectMenuEntries[NMESH] = {
+ "1 Thin Dinosaur","2 Big Dog","3 Saddle Dinosaur", "4 Dragon", "5 Cleopatra",
+ "6 Bone I", "7 Bone II", "8 Rabbit", "9 Long Dragon", "10 Buddha",
+ "11 Sitting Rabbit", "12 Frog", "13 Cow", "14 Monster", "15 Sea Horse",
+ "16 Head", "17 Pelican", "18 Horse", "19 Kneeling Angel", "20 Porsche I",
+ "21 Truck", "22 Statue of Liberty", "23 Sitting Angel", "24 Metal Part", "25 Car",
+ "26 Apatosaurus", "27 Airliner", "28 Motorbike", "29 Dolphin", "30 Spaceman",
+ "31 Winnie the Pooh", "32 Shark", "33 Crocodile", "34 Toddler", "35 Fat Dinosaur",
+ "36 Chihuahua", "37 Sabre-toothed Tiger", "38 Lioness", "39 Fish", "40 Horse (head down)",
+ "41 Horse (head up)", "42 Skull", "43 Fighter Jet I", "44 Toad", "45 Convertible",
+ "46 Porsche II", "47 Hare", "48 Vintage Car", "49 Fighter Jet II", "50 Winged Monkey",
+ "51 Chef", "52 Parasaurolophus", "53 Rooster", "54 T-rex"
+};
+
+SceneObject sceneObjs[MAXOBJECTS]; // An array with details of the objects in a scene
+int nObjects=0; // How many objects there are in the scene currently.
+
+// Directories containing models
+char *dirDefault1 = "models-textures";
+char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures";
+
+char dataDir[200]; // Stores the directory name for the meshes and textures.
+
+GLfloat lightColor[] = {1.0, 1.0, 1.0, 1.0}; // White light
+GLfloat lightPosition[4];
+
+int moving, startx, starty;
+int lightMoving = 0, lightStartX, lightStartY;
+
+/* Time varying or user-controled variables. */
+float jump = 0.0;
+float lightAngle = 0.0, lightHeight = 5;
+GLfloat angle = -150; /* in degrees */
+GLfloat angle2 = 30; /* in degrees */
+
+/* Near and far parameters */
+GLfloat near = -100;
+GLfloat far = 100;
+
+/* Zoom factor for mouse movements */
+GLfloat zoomFactor = 1.0;
+
+/* Recursion level for floor drawing */
+int drawFloorRecurse = 5;
+
+/* Size of floor, from -n to n */
+int floorSize = 200;
+
+/* Light 0 parameters */
+GLfloat diffuse0[] = {1.0, 1.0, 1.0, 1.0};
+GLfloat ambient0[] = {0.0, 0.0, 0.0, 1.0};
+GLfloat specular0[] = {1.0, 1.0, 1.0, 1.0};
+GLfloat emission0[] = {0.0, 0.0, 0.0, 0.0};
+GLfloat light0_pos[] ={1.0, 1.0, 0,0, 1.0};
+GLfloat glightmodel[] = {0.2,0.2,0.2,1};
\ No newline at end of file
--- /dev/null
+/**
+ * Helper Function File
+ * Contains all of the project's helper functions
+ * @author Ashley Tyndall (20915779), Jenna de la Harpe (20367932)
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <dirent.h>
+#include <string.h>
+#include <math.h>
+#include <GL/gl.h>
+#include <GL/glut.h>
+#include <time.h>
+
+#include "bitmap.h"
+#include "types.h"
+#include "globals.h"
+#include "helper.h"
+
+/**
+ * Prints out error message when file cannot be read
+ * @param fileName Name of file that could not be read
+ */
+void fileErr(char* fileName) {
+ printf("Error reading file: %s\n", fileName);
+ printf("If not in the CSSE labs, you will need to include the directory containing\n");
+ printf("the models on the command line, or put it in the same folder as the exectutable.");
+ exit(EXIT_FAILURE);
+}
+
+/**
+ * Reads .bmp texture files and converts them to a texture object
+ * @param fileName .bmp texture file
+ * @return texture object
+ */
+texture* loadTexture(char *fileName) {
+ texture* t = malloc(sizeof (texture));
+ BITMAPINFO *info;
+
+ t->rgbData = LoadDIBitmap(fileName, &info);
+ t->height=info->bmiHeader.biHeight;
+ t->width=info->bmiHeader.biWidth;
+
+ return t;
+}
+
+/**
+ * Reads .x files and converts them to a mesh object
+ * @param fileName .x mesh file
+ * @return mesh object
+ */
+mesh* loadMesh(char* fileName) {
+ mesh* m = malloc(sizeof (mesh));
+ FILE* fp = fopen(fileName, "r");
+ char line[256] = "";
+ int lineBuffSize = 256;
+
+ if(fp == NULL) fileErr(fileName);
+
+ while(strcmp(line,"Mesh {\r\n") != 0 && strcmp(line,"Mesh {\n") != 0 )
+ fgets(line, lineBuffSize, fp);
+
+ fscanf(fp, "%d;\n", &(m->nVertices));
+ m->vertices = malloc(m->nVertices * sizeof(vertex));
+ for(int i=0; i < m->nVertices; i++)
+ fscanf(fp, "%f; %f; %f;%*[,;]\n", &(m->vertices[i][0]), &(m->vertices[i][1]), &(m->vertices[i][2]) );
+
+ fscanf(fp, "%d;\n", &(m->nTriangles));
+ m->triangles = malloc(m->nTriangles * sizeof(triangle));
+ for(int i=0; i < m->nTriangles; i++)
+ fscanf(fp, "%*d; %d, %d, %d;%*[;,]", m->triangles[i], m->triangles[i]+1, m->triangles[i]+2);
+
+ while(strcmp(line," MeshNormals {\r\n") != 0 && strcmp(line," MeshNormals {\n") != 0)
+ fgets(line, lineBuffSize, fp);
+
+ fgets(line, lineBuffSize, fp);
+ m->normals = malloc(m->nVertices * sizeof(normal));
+ for(int i=0; i < m->nVertices; i++)
+ fscanf(fp, "%f; %f; %f;%*[;,]\n",
+ &(m->normals[i][0]), &(m->normals[i][1]), &(m->normals[i][2]));
+
+ while(strcmp(line,"MeshTextureCoords {\r\n") != 0 && strcmp(line,"MeshTextureCoords {\n") != 0)
+ fgets(line, lineBuffSize, fp);
+
+ fgets(line, lineBuffSize, fp);
+ m->texCoords = malloc(m->nVertices * sizeof(texCoord));
+ for(int i=0; i < m->nVertices; i++)
+ fscanf(fp, "%f;%f;%*[,;]\n", &(m->texCoords[i][0]), &(m->texCoords[i][1]) );
+ fclose(fp);
+
+ return m;
+}
+
+// [You may want to add to this function.]
+/**
+ * Loads mesh[i] if it isn't already loaded.
+ * You must call getMesh(i) at least once before using mesh[i].
+ *
+ * @param i Mesh ID
+ */
+void getMesh(int i) { // getMesh(i) loads mesh[i] if it isn't already loaded.
+ char fileName[220];
+ if(i>=NMESH || i<0) {
+ printf("Error in getMesh - wrong model number");
+ exit(1);
+ }
+ if(meshes[i] != NULL)
+ return;
+ sprintf(fileName, "%s/model%d.x", dataDir, i+1);
+ meshes[i] = loadMesh(fileName);
+}
+
+/**
+ * Loads texture i if it isn't already loaded
+ *
+ * After calling getTexture(i), you can make texture i the current texture using
+ * glBindTexture(GL_TEXTURE_2D, i);
+ * Use i=0 to return to the default plain texture.
+ *
+ * You can then scale the texture via:
+ * glMatrixMode(GL_TEXTURE);
+ * See the textbook, section 8.8.3.
+ *
+ * You must call getTexture(i) at least once before using texture i.
+ * @param i Texture ID
+ */
+void getTexture(int i) {
+ char fileName[220];
+ if(i<1 || i>NTEXTURE) {
+ printf("Error in getTexture - wrong texture number");
+ exit(1);
+ }
+ if(textures[i-1] != NULL)
+ return;
+ sprintf(fileName, "%s/texture%d.bmp", dataDir, i);
+
+ textures[i-1] = loadTexture(fileName);
+
+ glBindTexture(GL_TEXTURE_2D, i);
+
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textures[i-1]->width, textures[i-1]->height,
+ 0, GL_RGB, GL_UNSIGNED_BYTE, textures[i-1]->rgbData);
+ gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, textures[i-1]->width, textures[i-1]->height, GL_RGB,
+ GL_UNSIGNED_BYTE, textures[i-1]->rgbData);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+
+ glBindTexture(GL_TEXTURE_2D, 0); // Back to default texture
+}
+
+/**
+ * Rounds up numbers, from http://stackoverflow.com/questions/3407012/c-rounding-up-to-the-nearest-multiple-of-a-number
+ * @param numToRound Number to round
+ * @param multiple Multiple to round up to
+ * @return Rounded number
+ */
+int roundUp(int numToRound, int multiple) {
+ if(multiple == 0) {
+ return numToRound;
+ }
+
+ int remainder = numToRound % multiple;
+ if (remainder == 0)
+ return numToRound;
+ return numToRound + multiple - remainder;
+}
+
+/**
+ * Makes a submenu from an array of items, splitting the list into subsubmenus
+ * of only 10 items.
+ * @param menuEntries Array of menu items
+ * @param menuEntriesSize Size of menuEntries
+ * @param callback Callback function for this array of menu items
+ * @return Reference to menu created
+ */
+int makeSubmenuFromArray( const char *menuEntries[], unsigned int menuEntriesSize, void *callback ) {
+ if ( menuEntriesSize == 0 ) return -1;
+
+ int menuNumber = roundUp(menuEntriesSize, 10) / 10;
+ int submenuObjects[menuNumber-1];
+
+ for( int i = 0; i < menuNumber; i++ ) {
+ submenuObjects[i] = glutCreateMenu(callback);
+ int startNum = i*11 - (i-1);
+ for ( int j = startNum - 1; j < (startNum+9); j++ ) {
+ if ( j == menuEntriesSize ) break; // Detect if we've reached the end of the array
+ glutAddMenuEntry( menuEntries[j], j + 1 );
+ }
+ }
+
+ int mainMenu = glutCreateMenu(callback);
+ for ( int i = 0; i < menuNumber; i++ ) {
+ char name[10]; // buffer to hold name
+ int startNum = i*11 - (i-1);
+ int endNum = startNum + 9;
+ if ( i == menuNumber - 1 ) { // We're on the last one
+ endNum = startNum + (menuEntriesSize - startNum); // Work out final number
+ }
+ sprintf(name, "%d-%d", startNum, endNum);
+ glutAddSubMenu( name, submenuObjects[i] );
+ }
+
+ return mainMenu;
+}
+
+/**
+ * Recursive function to draw a square by drawing smaller and smaller
+ * divisions of the square, determined by drawFloorRecurse.
+ * @param recurseLevel Current level of recursion, only pass 0
+ * @param x1 top-left x
+ * @param z1 top-left z
+ * @param x2 bottom-left x
+ * @param z2 bottom-left z
+ */
+void drawSquare(int recurseLevel, float x1, float z1, float x2, float z2) {
+
+ if ( drawFloorRecurse != recurseLevel ) {
+ // Calculate middle points
+ float xm = (x1 + x2) / 2.0;
+ float zm = (z1 + z2) / 2.0;
+
+ // Increment recursion level
+ int rnew = recurseLevel + 1;
+
+ // Split into four sub-quads
+ drawSquare(rnew, x1, z1, xm, zm);
+ drawSquare(rnew, x1, zm, xm, z2);
+ drawSquare(rnew, xm, zm, x2, z2);
+ drawSquare(rnew, xm, z1, x2, zm);
+
+ } else {
+ // Draw square.
+ // **NOTE: Is the polygon facing in the right direction?
+ glBegin(GL_QUADS);
+ glVertex3f(x1, 0.0, z1);
+ glVertex3f(x1, 0.0, z2);
+ glVertex3f(x2, 0.0, z2);
+ glVertex3f(x2, 0.0, z1);
+ glEnd();
+ }
+
+}
+
+/**
+ * Draw a floor by calling the drawSquare recursion
+ */
+void drawFloor() {
+ drawSquare(0, -floorSize, -floorSize, floorSize, floorSize);
+}
+
+/**
+ * Draw x, z axis on floor
+ */
+void drawLine() {
+ // **NOTE: fix function
+ glDisable(GL_TEXTURE_2D);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glColor4ub( 0.0, 0.0, 0.0, 0.5 );
+
+ glBegin(GL_LINES);
+ glVertex3i( 10.0, 0.1, 10.0);
+ glVertex3i( -10.0, 0.1, -10.0);
+ glEnd();
+
+ glDisable(GL_BLEND);
+ glEnable(GL_TEXTURE_2D);
+}
\ No newline at end of file
#include <time.h>\r
\r
#include "bitmap.h"\r
+#include "globals.h"\r
+#include "helper.h"\r
+#include "types.h"\r
+#include "scene.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
-\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 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 = 5;\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 = 200;\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
-\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
\r
/**\r
* Event hander for main menu events\r
*/\r
void processMainEvents(int id) {\r
switch (id) {\r
- case ROTATE_MOVE_CAMERA:\r
+ case M_ROTATE_MOVE_CAMERA:\r
// Do stuff\r
break;\r
\r
- case POSITION_SCALE:\r
+ case M_POSITION_SCALE:\r
// Do stuff\r
break;\r
\r
- case ROTATION_TEXTURE_SCALE:\r
+ case M_ROTATION_TEXTURE_SCALE:\r
// Do stuff\r
break;\r
\r
- case EXIT:\r
+ case M_EXIT:\r
exit(EXIT_SUCCESS);\r
\r
}\r
*/\r
void processMaterialEvents(int id) {\r
switch (id) {\r
- case MATERIAL_ALL_RGB:\r
+ case M_MATERIAL_ALL_RGB:\r
// Do stuff\r
break;\r
\r
- case MATERIAL_AMBIENT_RGB:\r
+ case M_MATERIAL_AMBIENT_RGB:\r
// Do stuff\r
break;\r
\r
- case MATERIAL_DIFFUSE_RGB:\r
+ case M_MATERIAL_DIFFUSE_RGB:\r
// Do stuff\r
break;\r
\r
- case MATERIAL_SPECULAR_RGB:\r
+ case M_MATERIAL_SPECULAR_RGB:\r
// Do stuff\r
break;\r
\r
- case MATERIAL_ALL_ADSS:\r
+ case M_MATERIAL_ALL_ADSS:\r
// Do stuff\r
break;\r
\r
- case MATERIAL_RED_ADSS:\r
+ case M_MATERIAL_RED_ADSS:\r
// Do stuff\r
break;\r
\r
- case MATERIAL_GREEN_ADSS:\r
+ case M_MATERIAL_GREEN_ADSS:\r
// Do stuff\r
break;\r
\r
- case MATERIAL_BLUE_ADSS:\r
+ case M_MATERIAL_BLUE_ADSS:\r
// Do stuff\r
break;\r
\r
*/\r
void processLightEvents(int id) {\r
switch (id) {\r
- case LIGHT_MOVE_LIGHT_1:\r
+ case M_LIGHT_MOVE_LIGHT_1:\r
// Do stuff\r
break;\r
\r
- case LIGHT_RGBALL_LIGHT_1:\r
+ case M_LIGHT_RGBALL_LIGHT_1:\r
// Do stuff\r
break;\r
\r
- case LIGHT_MOVE_LIGHT_2:\r
+ case M_LIGHT_MOVE_LIGHT_2:\r
// Do stuff\r
break;\r
\r
- case LIGHT_RGBALL_LIGHT_2:\r
+ case M_LIGHT_RGBALL_LIGHT_2:\r
// Do stuff\r
break;\r
\r
\r
}\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
-\r
- int remainder = numToRound % multiple;\r
- if (remainder == 0)\r
- return numToRound;\r
- return numToRound + multiple - remainder;\r
-}\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
- }\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
- sprintf(name, "%d-%d", startNum, endNum);\r
- glutAddSubMenu( name, submenuObjects[i] );\r
- }\r
-\r
- return mainMenu;\r
-}\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
+ glutAddMenuEntry("All R/G/B", M_MATERIAL_ALL_RGB);\r
+ glutAddMenuEntry("Ambient R/G/B", M_MATERIAL_AMBIENT_RGB);\r
+ glutAddMenuEntry("Diffuse R/G/B", M_MATERIAL_DIFFUSE_RGB);\r
+ glutAddMenuEntry("Specular R/G/B", M_MATERIAL_SPECULAR_RGB);\r
+ glutAddMenuEntry("All Amb/Diff/Spec/Shine", M_MATERIAL_ALL_ADSS);\r
+ glutAddMenuEntry("Red Amb/Diff/Spec/Shine", M_MATERIAL_RED_ADSS);\r
+ glutAddMenuEntry("Green Amb/Diff/Spec/Shine", M_MATERIAL_GREEN_ADSS);\r
+ glutAddMenuEntry("Blue Amb/Diff/Spec/Shine", M_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
+ glutAddMenuEntry("Move Light 1", M_LIGHT_MOVE_LIGHT_1);\r
+ glutAddMenuEntry("R/G/B/All Light 1", M_LIGHT_RGBALL_LIGHT_1);\r
+ glutAddMenuEntry("Move Light 2", M_LIGHT_MOVE_LIGHT_2);\r
+ glutAddMenuEntry("R/G/B/All Light 2", M_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
+ int objectMenu = makeSubmenuFromArray( objectMenuEntries, NMESH, 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
+ int textureMenu = makeSubmenuFromArray( textureMenuEntries, NTEXTURE, processTextureEvents );\r
+ int gTextureMenu = makeSubmenuFromArray( textureMenuEntries, NTEXTURE, processGTextureEvents );\r
\r
// Construct main menu\r
glutCreateMenu(processMainEvents);\r
- //glutAddMenuEntry("Rotate/Move Camera", ROTATE_MOVE_CAMERA);\r
+ //glutAddMenuEntry("Rotate/Move Camera", M_ROTATE_MOVE_CAMERA);\r
//glutAddSubMenu("Add object", objectMenu);\r
- //glutAddMenuEntry("Position/Scale", POSITION_SCALE);\r
- //glutAddMenuEntry("Rotation/Texture Scale", ROTATION_TEXTURE_SCALE);\r
+ //glutAddMenuEntry("Position/Scale", M_POSITION_SCALE);\r
+ //glutAddMenuEntry("Rotation/Texture Scale", M_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
+ glutAddMenuEntry("Exit", M_EXIT);\r
\r
// Bind to right mouse button\r
glutAttachMenu(GLUT_RIGHT_BUTTON);\r
}\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
-\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
-\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
-\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
-\r
- glBegin(GL_LINES);\r
- glVertex3i( 10.0, 0.1, 10.0);\r
- glVertex3i( -10.0, 0.1, -10.0);\r
- glEnd();\r
-\r
- glDisable(GL_BLEND);\r
- glEnable(GL_TEXTURE_2D);\r
-}\r
-\r
/**\r
* Display function\r
*/\r