(no commit message)
[atyndall/cits2231.git] / scene.c
1 /**\r
2  * CITS2231 Graphics Scene Editor\r
3  * @author Ashley Tyndall (20915779)\r
4  */\r
5 \r
6 #include <stdlib.h>\r
7 #include <stdio.h>\r
8 #include <dirent.h>\r
9 #include <string.h>\r
10 #include <math.h>\r
11 #include <GL/gl.h>\r
12 #include <GL/glut.h>\r
13 #include <time.h>\r
14 \r
15 #include "bitmap.h"\r
16 \r
17 // Type definitions for vertex-coordinates, normals, texture-coordinates, \r
18 // and triangles (via the indices of 3 vertices).\r
19 typedef GLfloat vertex[3];\r
20 typedef GLfloat normal[3];\r
21 typedef GLfloat texCoord[2];\r
22 typedef GLint vertexIndex;\r
23 typedef vertexIndex triangle[3];\r
24 \r
25 // A type for a mesh\r
26 typedef struct {         \r
27     int nVertices;           //  The number of vertices in the mesh\r
28     vertex* vertices;        //  Array with coordinates of vertices\r
29     normal* normals;         //  Array with normals of vertices\r
30     texCoord* texCoords;     //  Array with texture-coordinates of vertices\r
31     int nTriangles;          //  The number of triangles in the mesh\r
32     triangle* triangles;     //  Array of trangles via 3 indices into "vertices"\r
33 } mesh;\r
34 \r
35 #define NMESH 54       // The number of meshes (in the models-textures dir)\r
36 mesh* meshes[NMESH];   // An array of pointers to the meshes - see getMesh\r
37 \r
38 // A type for a 2D texture, with height and width in pixels\r
39 typedef struct {\r
40     int height;\r
41     int width;\r
42     GLubyte *rgbData;   // Array of bytes with the colour data for the texture\r
43 } texture;\r
44 \r
45 #define NTEXTURE 30     // The number of textures (in the models-textures dir)\r
46 texture* textures[NTEXTURE];   // An array of texture pointers - see getTexture\r
47 \r
48 typedef struct {  \r
49     // You'll need to add scale, rotation, material, mesh number, etc.,\r
50     // to this structure\r
51     float x,y,z;\r
52 } SceneObject;\r
53 \r
54 // Menu enum\r
55 enum menu {\r
56   // Main menu\r
57   ROTATE_MOVE_CAMERA,\r
58   POSITION_SCALE,\r
59   ROTATION_TEXTURE_SCALE,\r
60   EXIT,\r
61 \r
62   // Material submenu\r
63   MATERIAL_ALL_RGB,\r
64   MATERIAL_AMBIENT_RGB,\r
65   MATERIAL_DIFFUSE_RGB,\r
66   MATERIAL_SPECULAR_RGB,\r
67   MATERIAL_ALL_ADSS,\r
68   MATERIAL_RED_ADSS,\r
69   MATERIAL_GREEN_ADSS,\r
70   MATERIAL_BLUE_ADSS,\r
71 \r
72   // Light submenu\r
73   LIGHT_MOVE_LIGHT_1,\r
74   LIGHT_RGBALL_LIGHT_1,\r
75   LIGHT_MOVE_LIGHT_2,\r
76   LIGHT_RGBALL_LIGHT_2\r
77 };\r
78 \r
79 // Menu arrays\r
80 const char *textureMenuEntries[NTEXTURE] = {\r
81   "1 Plain", "2 Rust", "3 Concrete", "4 Carpet", "5 Beach Sand",\r
82   "6 Rocky", "7 Brick", "8 Water", "9 Paper", "10 Marble",\r
83   "11 Wood", "12 Scales", "13 Fur", "14 Denim", "15 Hessian",\r
84   "16 Orange Peel", "17 Ice Crystals", "18 Grass", "19 Corrugated Iron", "20 Styrofoam",\r
85   "21 Bubble Wrap", "22 Leather", "23 Camouflage", "24 Asphalt", "25 Scratched Ice",\r
86   "26 Rattan", "27 Snow", "28 Dry Mud", "29 Old Concrete", "30 Leopard Skin"\r
87 };\r
88 \r
89 const char *objectMenuEntries[NMESH] = {\r
90   "1 Thin Dinosaur","2 Big Dog","3 Saddle Dinosaur", "4 Dragon", "5 Cleopatra",\r
91   "6 Bone I", "7 Bone II", "8 Rabbit", "9 Long Dragon", "10 Buddha",\r
92   "11 Sitting Rabbit", "12 Frog", "13 Cow", "14 Monster", "15 Sea Horse",\r
93   "16 Head", "17 Pelican", "18 Horse", "19 Kneeling Angel", "20 Porsche I",\r
94   "21 Truck", "22 Statue of Liberty", "23 Sitting Angel", "24 Metal Part", "25 Car",\r
95   "26 Apatosaurus", "27 Airliner", "28 Motorbike", "29 Dolphin", "30 Spaceman",\r
96   "31 Winnie the Pooh", "32 Shark", "33 Crocodile", "34 Toddler", "35 Fat Dinosaur",\r
97   "36 Chihuahua", "37 Sabre-toothed Tiger", "38 Lioness", "39 Fish", "40 Horse (head down)",\r
98   "41 Horse (head up)", "42 Skull", "43 Fighter Jet I", "44 Toad", "45 Convertible",\r
99   "46 Porsche II", "47 Hare", "48 Vintage Car", "49 Fighter Jet II", "50 Winged Monkey",\r
100   "51 Chef", "52 Parasaurolophus", "53 Rooster", "54 T-rex"\r
101 };\r
102 \r
103 #define MAXOBJECTS 256\r
104 SceneObject sceneObjs[MAXOBJECTS];  // An array with details of the objects in a scene\r
105 int nObjects=0;                     // How many objects there are in the scene currently.\r
106 \r
107 // Directories containing models\r
108 char *dirDefault1 = "models-textures";\r
109 char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures";\r
110 \r
111 char dataDir[200];  // Stores the directory name for the meshes and textures.\r
112 \r
113 static GLfloat floorVertices[4][3] = {\r
114   { -1000.0, 0.0, 1000.0 },\r
115   { 1000.0, 0.0, 1000.0 },\r
116   { 1000.0, 0.0, -1000.0 },\r
117   { -1000.0, 0.0, -1000.0 },\r
118 };\r
119 \r
120 static GLfloat lightColor[] = {1.0, 1.0, 1.0, 1.0}; // White light\r
121 static GLfloat lightPosition[4];\r
122 \r
123 int moving, startx, starty;\r
124 int lightMoving = 0, lightStartX, lightStartY;\r
125 \r
126 /* Time varying or user-controled variables. */\r
127 static float jump = 0.0;\r
128 static float lightAngle = 0.0, lightHeight = 5;\r
129 GLfloat angle = -150;   /* in degrees */\r
130 GLfloat angle2 = 30;   /* in degrees */\r
131 \r
132 GLfloat near = -10;\r
133 GLfloat far = 10;\r
134 \r
135 static float zoomFactor = 1.0;\r
136 \r
137 /**\r
138  * Prints out error message when file cannot be read\r
139  * @param fileName Name of file that could not be read\r
140  */\r
141 void fileErr(char* fileName) {\r
142     printf("Error reading file: %s\n", fileName);\r
143     printf("If not in the CSSE labs, you will need to include the directory containing\n");\r
144     printf("the models on the command line, or put it in the same folder as the exectutable.");\r
145     exit(EXIT_FAILURE);\r
146 }  \r
147 \r
148 /**\r
149  * Reads .bmp texture files and converts them to a texture object\r
150  * @param fileName .bmp texture file\r
151  * @return texture object\r
152  */\r
153 texture* loadTexture(char *fileName) {\r
154     texture* t = malloc(sizeof (texture));\r
155     BITMAPINFO *info;\r
156 \r
157     t->rgbData = LoadDIBitmap(fileName, &info);\r
158     t->height=info->bmiHeader.biHeight;\r
159     t->width=info->bmiHeader.biWidth;\r
160 \r
161     return t;\r
162 }\r
163 \r
164 /**\r
165  * Reads .x files and converts them to a mesh object\r
166  * @param fileName .x mesh file\r
167  * @return mesh object\r
168  */\r
169 mesh* loadMesh(char* fileName) {\r
170     mesh* m = malloc(sizeof (mesh));\r
171     FILE* fp = fopen(fileName, "r");\r
172     char line[256] = "";\r
173     int lineBuffSize = 256;\r
174 \r
175     if(fp == NULL) fileErr(fileName);\r
176 \r
177     while(strcmp(line,"Mesh {\r\n") != 0 && strcmp(line,"Mesh {\n") != 0 )\r
178         fgets(line, lineBuffSize, fp);\r
179 \r
180     fscanf(fp, "%d;\n", &(m->nVertices));\r
181     m->vertices = malloc(m->nVertices * sizeof(vertex));\r
182     for(int i=0; i < m->nVertices; i++)\r
183         fscanf(fp, "%f; %f; %f;%*[,;]\n", &(m->vertices[i][0]), &(m->vertices[i][1]), &(m->vertices[i][2]) );\r
184 \r
185     fscanf(fp, "%d;\n", &(m->nTriangles));\r
186     m->triangles = malloc(m->nTriangles * sizeof(triangle));\r
187     for(int i=0; i < m->nTriangles; i++)\r
188         fscanf(fp, "%*d; %d, %d, %d;%*[;,]", m->triangles[i], m->triangles[i]+1, m->triangles[i]+2);\r
189 \r
190     while(strcmp(line,"  MeshNormals {\r\n") != 0 && strcmp(line,"  MeshNormals {\n") != 0)\r
191         fgets(line, lineBuffSize, fp);\r
192 \r
193     fgets(line, lineBuffSize, fp);\r
194     m->normals = malloc(m->nVertices * sizeof(normal));\r
195     for(int i=0; i < m->nVertices; i++)\r
196         fscanf(fp, "%f; %f; %f;%*[;,]\n",\r
197                &(m->normals[i][0]), &(m->normals[i][1]), &(m->normals[i][2]));\r
198 \r
199     while(strcmp(line,"MeshTextureCoords {\r\n") != 0 && strcmp(line,"MeshTextureCoords {\n") != 0)\r
200         fgets(line, lineBuffSize, fp);\r
201 \r
202     fgets(line, lineBuffSize, fp);\r
203     m->texCoords = malloc(m->nVertices * sizeof(texCoord));\r
204     for(int i=0; i < m->nVertices; i++)\r
205         fscanf(fp, "%f;%f;%*[,;]\n", &(m->texCoords[i][0]), &(m->texCoords[i][1]) );\r
206     fclose(fp);\r
207     \r
208     return m;\r
209 }\r
210 \r
211 // [You may want to add to this function.]\r
212 /**\r
213  * Loads mesh[i] if it isn't already loaded.\r
214  * You must call getMesh(i) at least once before using mesh[i].\r
215  *\r
216  * @param i Mesh ID\r
217  */\r
218 void getMesh(int i) { // getMesh(i) loads mesh[i] if it isn't already loaded.  \r
219     char fileName[220];\r
220     if(i>=NMESH || i<0) {\r
221         printf("Error in getMesh - wrong model number");\r
222         exit(1);\r
223     }\r
224     if(meshes[i] != NULL)\r
225         return;\r
226     sprintf(fileName, "%s/model%d.x", dataDir, i+1);\r
227     meshes[i] = loadMesh(fileName);\r
228 }\r
229 \r
230 /**\r
231  * Loads texture i if it isn't already loaded\r
232  *\r
233  * After calling getTexture(i), you can make texture i the current texture using\r
234  *      glBindTexture(GL_TEXTURE_2D, i);\r
235  * Use i=0 to return to the default plain texture.\r
236  *\r
237  * You can then scale the texture via:\r
238  *      glMatrixMode(GL_TEXTURE);\r
239  * See the textbook, section 8.8.3.\r
240  *\r
241  * You must call getTexture(i) at least once before using texture i.\r
242  * @param i Texture ID\r
243  */\r
244 void getTexture(int i) {\r
245     char fileName[220];\r
246     if(i<1 || i>NTEXTURE) {\r
247         printf("Error in getTexture - wrong texture number");\r
248         exit(1);\r
249     }\r
250     if(textures[i-1] != NULL)\r
251         return;\r
252     sprintf(fileName, "%s/texture%d.bmp", dataDir, i);\r
253 \r
254     textures[i-1] = loadTexture(fileName);\r
255 \r
256     glBindTexture(GL_TEXTURE_2D, i);\r
257 \r
258     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textures[i-1]->width, textures[i-1]->height,\r
259                  0, GL_RGB, GL_UNSIGNED_BYTE, textures[i-1]->rgbData);\r
260     gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, textures[i-1]->width, textures[i-1]->height, GL_RGB, \r
261                       GL_UNSIGNED_BYTE, textures[i-1]->rgbData);\r
262     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);\r
263     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);\r
264     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);\r
265     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);\r
266 \r
267     glBindTexture(GL_TEXTURE_2D, 0);  // Back to default texture\r
268 }\r
269 \r
270 /**\r
271  * Event hander for main menu events\r
272  * @param id ID of menu item selected\r
273  */\r
274 void processMainEvents(int id) {\r
275   switch (id) {\r
276     case ROTATE_MOVE_CAMERA:\r
277       // Do stuff\r
278       break;\r
279 \r
280     case POSITION_SCALE:\r
281       // Do stuff\r
282       break;\r
283 \r
284     case ROTATION_TEXTURE_SCALE:\r
285       // Do stuff\r
286       break;\r
287 \r
288     case EXIT:\r
289       exit(EXIT_SUCCESS);\r
290 \r
291   }\r
292 }\r
293 \r
294 /**\r
295  * Event hander for materials menu events\r
296  * @param id ID of menu item selected\r
297  */\r
298 void processMaterialEvents(int id) {\r
299   switch (id) {\r
300     case MATERIAL_ALL_RGB:\r
301       // Do stuff\r
302       break;\r
303 \r
304     case MATERIAL_AMBIENT_RGB:\r
305       // Do stuff\r
306       break;\r
307 \r
308     case MATERIAL_DIFFUSE_RGB:\r
309       // Do stuff\r
310       break;\r
311 \r
312     case MATERIAL_SPECULAR_RGB:\r
313       // Do stuff\r
314       break;\r
315 \r
316     case MATERIAL_ALL_ADSS:\r
317       // Do stuff\r
318       break;\r
319 \r
320     case MATERIAL_RED_ADSS:\r
321       // Do stuff\r
322       break;\r
323 \r
324     case MATERIAL_GREEN_ADSS:\r
325       // Do stuff\r
326       break;\r
327 \r
328     case MATERIAL_BLUE_ADSS:\r
329       // Do stuff\r
330       break;\r
331 \r
332   }\r
333 }\r
334 \r
335 /**\r
336  * Event hander for light menu events\r
337  * @param id ID of menu item selected\r
338  */\r
339 void processLightEvents(int id) {\r
340   switch (id) {\r
341     case LIGHT_MOVE_LIGHT_1:\r
342       // Do stuff\r
343       break;\r
344 \r
345     case LIGHT_RGBALL_LIGHT_1:\r
346       // Do stuff\r
347       break;\r
348 \r
349     case LIGHT_MOVE_LIGHT_2:\r
350       // Do stuff\r
351       break;\r
352 \r
353     case LIGHT_RGBALL_LIGHT_2:\r
354       // Do stuff\r
355       break;\r
356 \r
357   }\r
358 }\r
359 \r
360 /**\r
361  * Event hander for object menu events\r
362  * @param id ID of object selected\r
363  */\r
364 void processObjectEvents(int id) {\r
365 \r
366 }\r
367 \r
368 /**\r
369  * Event hander for texture menu events\r
370  * @param id ID of texutre selected\r
371  */\r
372 void processTextureEvents(int id) {\r
373 \r
374 }\r
375 \r
376 /**\r
377  * Event hander for ground texture menu events\r
378  * @param id ID of ground texture selected\r
379  */\r
380 void processGTextureEvents(int id) {\r
381 \r
382 }\r
383 \r
384 /**\r
385  * Rounds up numbers, from http://stackoverflow.com/questions/3407012/c-rounding-up-to-the-nearest-multiple-of-a-number\r
386  * @param numToRound Number to round\r
387  * @param multiple Multiple to round up to\r
388  * @return Rounded number\r
389  */\r
390 int roundUp(int numToRound, int multiple) {\r
391   if(multiple == 0) {\r
392     return numToRound;\r
393   }\r
394 \r
395   int remainder = numToRound % multiple;\r
396   if (remainder == 0)\r
397     return numToRound;\r
398   return numToRound + multiple - remainder;\r
399 }\r
400 \r
401 /**\r
402  * Makes a submenu from an array of items, splitting the list into subsubmenus\r
403  * of only 10 items.\r
404  * @param menuEntries Array of menu items\r
405  * @param menuEntriesSize Size of menuEntries\r
406  * @param callback Callback function for this array of menu items\r
407  * @return Reference to menu created\r
408  */\r
409 int makeSubmenuFromArray( const char *menuEntries[], unsigned int menuEntriesSize, void *callback ) {\r
410   if ( menuEntriesSize == 0 ) return -1;\r
411 \r
412   int menuNumber = roundUp(menuEntriesSize, 10) / 10;\r
413   int submenuObjects[menuNumber-1];\r
414 \r
415   for( int i = 0; i < menuNumber; i++ ) {\r
416     submenuObjects[i] = glutCreateMenu(callback);\r
417     int startNum = i*11 - (i-1);\r
418     for ( int j = startNum - 1; j < (startNum+9); j++ ) {\r
419       if ( j == menuEntriesSize ) break; // Detect if we've reached the end of the array\r
420       glutAddMenuEntry( menuEntries[j], j + 1 );\r
421     }\r
422   } \r
423 \r
424   int mainMenu = glutCreateMenu(callback);\r
425   for ( int i = 0; i < menuNumber; i++ ) {\r
426     char name[10]; // buffer to hold name\r
427     int startNum = i*11 - (i-1);\r
428     int endNum = startNum + 9;\r
429     if ( i == menuNumber - 1 ) { // We're on the last one\r
430       endNum = startNum + (menuEntriesSize - startNum); // Work out final number\r
431     }\r
432     sprintf(name, "%d-%d", startNum, endNum);\r
433     glutAddSubMenu( name, submenuObjects[i] );\r
434   }\r
435 \r
436   return mainMenu;\r
437 }\r
438 \r
439 /**\r
440  * Creates menu for program\r
441  */\r
442 void makeMenu() {\r
443   // Construct material menu\r
444   int materialMenu = glutCreateMenu(processMaterialEvents);\r
445   glutAddMenuEntry("All R/G/B", MATERIAL_ALL_RGB);\r
446   glutAddMenuEntry("Ambient R/G/B", MATERIAL_AMBIENT_RGB);\r
447   glutAddMenuEntry("Diffuse R/G/B", MATERIAL_DIFFUSE_RGB);\r
448   glutAddMenuEntry("Specular R/G/B", MATERIAL_SPECULAR_RGB);\r
449   glutAddMenuEntry("All Amb/Diff/Spec/Shine", MATERIAL_ALL_ADSS);\r
450   glutAddMenuEntry("Red Amb/Diff/Spec/Shine", MATERIAL_RED_ADSS);\r
451   glutAddMenuEntry("Green Amb/Diff/Spec/Shine", MATERIAL_GREEN_ADSS);\r
452   glutAddMenuEntry("Blue Amb/Diff/Spec/Shine", MATERIAL_BLUE_ADSS);\r
453 \r
454   // Construct light menu\r
455   int lightMenu = glutCreateMenu(processLightEvents);\r
456   glutAddMenuEntry("Move Light 1", LIGHT_MOVE_LIGHT_1);\r
457   glutAddMenuEntry("R/G/B/All Light 1", LIGHT_RGBALL_LIGHT_1);\r
458   glutAddMenuEntry("Move Light 2", LIGHT_MOVE_LIGHT_2);\r
459   glutAddMenuEntry("R/G/B/All Light 2", LIGHT_RGBALL_LIGHT_2);\r
460 \r
461   // Construct object menu\r
462   int objectMenuEntriesSize = sizeof(objectMenuEntries) / sizeof(objectMenuEntries[0]);\r
463   int objectMenu = makeSubmenuFromArray( objectMenuEntries, objectMenuEntriesSize, processObjectEvents );\r
464 \r
465   // Construct texture / ground texture menus\r
466   int textureMenuEntriesSize = sizeof(textureMenuEntries) / sizeof(textureMenuEntries[0]);\r
467   int textureMenu = makeSubmenuFromArray( textureMenuEntries, textureMenuEntriesSize, processTextureEvents );\r
468   int gTextureMenu = makeSubmenuFromArray( textureMenuEntries, textureMenuEntriesSize, processGTextureEvents );\r
469 \r
470   // Construct main menu\r
471   glutCreateMenu(processMainEvents);\r
472   //glutAddMenuEntry("Rotate/Move Camera", ROTATE_MOVE_CAMERA);\r
473   //glutAddSubMenu("Add object", objectMenu);\r
474   //glutAddMenuEntry("Position/Scale", POSITION_SCALE);\r
475   //glutAddMenuEntry("Rotation/Texture Scale", ROTATION_TEXTURE_SCALE);\r
476   //glutAddSubMenu("Material", materialMenu);\r
477   //glutAddSubMenu("Texture", textureMenu);\r
478   //glutAddSubMenu("Ground texture", gTextureMenu);\r
479   //glutAddSubMenu("Lights", lightMenu);\r
480   glutAddMenuEntry("Exit", EXIT);\r
481 \r
482   // Bind to right mouse button\r
483   glutAttachMenu(GLUT_RIGHT_BUTTON);\r
484 }\r
485 \r
486 /**\r
487  * Called when window is resized\r
488  * @param w New width\r
489  * @param h New height\r
490  */\r
491 void windowReshape(int w, int h) {\r
492   zoomFactor = zoomFactor - 1;\r
493   glViewport(0, 0, (GLsizei) w, (GLsizei) h);\r
494   glMatrixMode(GL_PROJECTION);\r
495   glLoadIdentity();\r
496   if (w <= h) \r
497     glOrtho(zoomFactor*near, zoomFactor*far, zoomFactor*near*(GLfloat)h/(GLfloat)w,\r
498              zoomFactor*far*(GLfloat)h/(GLfloat)w, near, far);\r
499   else\r
500     glOrtho(zoomFactor*near*(GLfloat)w/(GLfloat)h,\r
501              zoomFactor*far*(GLfloat)w/(GLfloat)h, zoomFactor*near, zoomFactor*far, near, far);\r
502    glMatrixMode(GL_MODELVIEW); \r
503    glLoadIdentity();\r
504 }\r
505 \r
506 /**\r
507  * Called when mouse event occurs\r
508  * @param btn Mouse button\r
509  * @param state State of mouse button\r
510  * @param x Mouse x position\r
511  * @param y Mouse y position\r
512  */\r
513 /*void mouse(int btn, int state, int x, int y) {\r
514   \r
515 }*/\r
516 static void\r
517 mouse(int button, int state, int x, int y)\r
518 {\r
519   if (button == GLUT_LEFT_BUTTON) {\r
520     if (state == GLUT_DOWN) {\r
521       moving = 1;\r
522       startx = x;\r
523       starty = y;\r
524     }\r
525     if (state == GLUT_UP) {\r
526       moving = 0;\r
527     }\r
528   }\r
529   if (button == GLUT_MIDDLE_BUTTON) {\r
530     if (state == GLUT_DOWN) {\r
531       lightMoving = 1;\r
532       lightStartX = x;\r
533       lightStartY = y;\r
534     }\r
535     if (state == GLUT_UP) {\r
536       lightMoving = 0;\r
537     }\r
538   }\r
539 }\r
540 \r
541 static void\r
542 motion(int x, int y)\r
543 {\r
544   if (moving) {\r
545     angle = angle + (x - startx);\r
546     angle2 = angle2 + (y - starty);\r
547     startx = x;\r
548     starty = y;\r
549     glutPostRedisplay();\r
550   }\r
551   if (lightMoving) {\r
552     lightAngle += (x - lightStartX)/40.0;\r
553     lightHeight += (lightStartY - y)/20.0;\r
554     lightStartX = x;\r
555     lightStartY = y;\r
556     glutPostRedisplay();\r
557   }\r
558 }\r
559 \r
560 void idle() {\r
561   angle = (int)(angle + 10) % 360;\r
562   //angle2 = (int)(angle2 + 10) % 360;\r
563   printf("Angle 1: %f, Angle 2: %f\n", angle, angle2);\r
564   sleep(1);\r
565   glutPostRedisplay();\r
566 }\r
567 \r
568 \r
569 int drawFloorRecurse = 5;\r
570 \r
571 void drawSquare(int recurseLevel, float x1, float y1, float x2, float y2) {\r
572 \r
573   if ( drawFloorRecurse != recurseLevel ) {\r
574     float xm = (x1 + x2) / 2.0;\r
575     float ym = (y1 + y2) / 2.0;\r
576     int rnew = recurseLevel + 1;\r
577 \r
578     // Split into four sub-quads\r
579     drawSquare(rnew, x1, y1, xm, ym);\r
580     drawSquare(rnew, x1, ym, xm, y2);\r
581     drawSquare(rnew, xm, ym, x2, y2);\r
582     drawSquare(rnew, xm, y1, x2, ym);\r
583 \r
584   } else {\r
585     glBegin(GL_QUADS);\r
586       glVertex3f(x1, 0.0, y1);\r
587       glVertex3f(x1, 0.0, y2);\r
588       glVertex3f(x2, 0.0, y2);\r
589       glVertex3f(x2, 0.0, y1);\r
590     glEnd();\r
591   }\r
592 \r
593 }\r
594 \r
595 /**\r
596  * Draw a floor.\r
597  */\r
598 void drawFloor() {\r
599  \r
600 drawSquare(0, -100.0, -100.0, 100.0, 100.0);\r
601 \r
602   /*if (useTexture) {\r
603     glDisable(GL_TEXTURE_2D);\r
604   }*/\r
605 \r
606 }\r
607 \r
608 /*void drawSquare(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2) {\r
609   drawSquare(0, x1, y1, x2, y2);\r
610 }*/\r
611 \r
612 /**\r
613  * Display function\r
614  */\r
615 void display() {\r
616   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);\r
617   glLoadIdentity();\r
618   gluLookAt(\r
619     0.0, 0.0, 10.0,  /* eye is at (x,y,z) */\r
620     0.0, 0.0,  0.0,  /* center is at (x,y,z) */\r
621     0.0, 1.0,  0.0   /* up is in postivie Y direction */\r
622     );\r
623 \r
624   glRotatef(30.0, 1.0, 0.0, 0.0);\r
625 \r
626   /* Reposition the light source. */\r
627   lightPosition[0] = 12*cos(lightAngle);\r
628   lightPosition[1] = lightHeight;\r
629   lightPosition[2] = 12*sin(lightAngle);\r
630   lightPosition[3] = 0.0;\r
631 \r
632   glPushMatrix();\r
633       /* Perform scene rotations based on user mouse input. */\r
634     \r
635     glRotatef(angle, 0.0, 1.0, 0.0);\r
636 \r
637     glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);\r
638 \r
639     glEnable(GL_BLEND);\r
640     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
641     drawFloor();\r
642     glDisable(GL_BLEND);\r
643 \r
644     glPushMatrix();\r
645     \r
646       glTranslatef(0.0, 1.0, 0.0);\r
647       glutWireTeapot(1); // Draw teapot for test\r
648     glPopMatrix();\r
649 \r
650     glPushMatrix();\r
651       glDisable(GL_LIGHTING);\r
652       glColor3f(1.0, 1.0, 1.0);\r
653 \r
654       /* Draw a yellow ball at the light source. */\r
655       glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);\r
656       glutSolidSphere(1.0, 50, 50);\r
657 \r
658       glEnable(GL_LIGHTING);\r
659     glPopMatrix();\r
660 \r
661   glPopMatrix();\r
662 \r
663   glutSwapBuffers();\r
664 }\r
665 \r
666 /**\r
667  * init function; sets initial OpenGL state\r
668  */\r
669 void init() {\r
670   glMatrixMode(GL_PROJECTION);\r
671   glLoadIdentity();\r
672 \r
673   gluPerspective(\r
674        60.0,  /* field of view in degree */\r
675         1.0,  /* aspect ratio */\r
676      near,  /* Z near */\r
677      far   /* Z far */\r
678     );    \r
679 \r
680 \r
681 GLfloat diffuse0[] = {1.0, 1.0, 1.0, 1.0};\r
682 GLfloat ambient0[] = {1.0, 1.0, 1.0, 1.0};\r
683 GLfloat specular0[] = {1.0, 1.0, 1.0, 1.0};\r
684 GLfloat light0_pos[] ={ 1.0, 2.0, 3,0, 1.0};\r
685 \r
686 glEnable(GL_LIGHT0);\r
687 glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);\r
688 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);\r
689 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);\r
690 glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);\r
691   glEnable(GL_LIGHTING);\r
692 \r
693   glMatrixMode(GL_MODELVIEW);\r
694   glLoadIdentity();\r
695 }\r
696 \r
697 /**\r
698  * Main function\r
699  * @param argc Number of arguments\r
700  * @param argv Array of arguments\r
701  * @return Program exit code\r
702  */\r
703 int main(int argc, char **argv) {\r
704   if(argc>1)\r
705     strcpy(dataDir, argv[1]);\r
706   else if(opendir(dirDefault1))\r
707     strcpy(dataDir, dirDefault1);\r
708   else if(opendir(dirDefault2))\r
709     strcpy(dataDir, dirDefault2);\r
710   else fileErr(dirDefault1);\r
711 \r
712   for(int i=0; i<NMESH; i++) meshes[i]=NULL;\r
713   for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;\r
714 \r
715   glutInit(&argc, argv);\r
716 \r
717   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);\r
718 \r
719   glutInitWindowSize(500, 500);\r
720   glutCreateWindow("Scene Editor");\r
721 \r
722   glShadeModel(GL_SMOOTH); // Enables Smooth Shading\r
723   glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background\r
724   glClearDepth(1.0f); // Depth Buffer Setup\r
725   glEnable(GL_DEPTH_TEST); // Enables Depth Testing\r
726   glDepthFunc(GL_LEQUAL);  // the type\r
727   glEnable(GL_CULL_FACE);\r
728   glEnable(GL_TEXTURE_2D);\r
729   glLineWidth(1.0);\r
730 \r
731   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);\r
732 \r
733   glutReshapeFunc(windowReshape);\r
734   glutDisplayFunc(display);\r
735   glutMouseFunc(mouse);\r
736   glutMotionFunc(motion);\r
737   //glutIdleFunc(idle);\r
738 \r
739   makeMenu();\r
740 \r
741   init();\r
742 \r
743   glutMainLoop();\r
744 }\r

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