ffa63dd14f3f7cf7b7772685e66766999cb985c0
[atyndall/cits2231.git] / scene.c
1 /**\r
2  * CITS2231 Graphics Scene Editor\r
3  * @author Ashley Tyndall (20915779), Jenna de la Harpe (20367932)\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 #include "globals.h"\r
17 #include "helper.h"\r
18 #include "types.h"\r
19 #include "scene.h"\r
20 \r
21 /**\r
22  * Event hander for main menu events\r
23  * @param id ID of menu item selected\r
24  */\r
25 void processMainEvents(int id) {\r
26   switch (id) {\r
27     case M_ROTATE_MOVE_CAMERA:\r
28       manipulateState = STATE_CAMERA_ROTATE_MOVE;\r
29       break;\r
30     case M_POSITION_SCALE:\r
31       manipulateState = STATE_OBJECT_POSITION_SCALE;\r
32       break;\r
33     case M_ROTATION_TEXTURE_SCALE:\r
34       manipulateState = STATE_OBJECT_ROTATION_TEXTURE_SCALE;\r
35       break;\r
36     case M_EXIT:\r
37       exit(EXIT_SUCCESS);\r
38 \r
39   }\r
40 }\r
41 \r
42 /**\r
43  * Event hander for materials menu events\r
44  * @param id ID of menu item selected\r
45  */\r
46 void processMaterialEvents(int id) {\r
47   switch (id) {\r
48     case M_MATERIAL_ALL_RGB:\r
49       // Do stuff\r
50       break;\r
51 \r
52     case M_MATERIAL_AMBIENT_RGB:\r
53       // Do stuff\r
54       break;\r
55 \r
56     case M_MATERIAL_DIFFUSE_RGB:\r
57       // Do stuff\r
58       break;\r
59 \r
60     case M_MATERIAL_SPECULAR_RGB:\r
61       // Do stuff\r
62       break;\r
63 \r
64     case M_MATERIAL_ALL_ADSS:\r
65       // Do stuff\r
66       break;\r
67 \r
68     case M_MATERIAL_RED_ADSS:\r
69       // Do stuff\r
70       break;\r
71 \r
72     case M_MATERIAL_GREEN_ADSS:\r
73       // Do stuff\r
74       break;\r
75 \r
76     case M_MATERIAL_BLUE_ADSS:\r
77       // Do stuff\r
78       break;\r
79 \r
80   }\r
81 }\r
82 \r
83 /**\r
84  * Event hander for light menu events\r
85  * @param id ID of menu item selected\r
86  */\r
87 void processLightEvents(int id) {\r
88   switch (id) {\r
89     case M_LIGHT_MOVE_LIGHT_1:\r
90       // Do stuff\r
91       break;\r
92 \r
93     case M_LIGHT_RGBALL_LIGHT_1:\r
94       // Do stuff\r
95       break;\r
96 \r
97     case M_LIGHT_MOVE_LIGHT_2:\r
98       // Do stuff\r
99       break;\r
100 \r
101     case M_LIGHT_RGBALL_LIGHT_2:\r
102       // Do stuff\r
103       break;\r
104 \r
105   }\r
106 }\r
107 \r
108 /**\r
109  * Event hander for object menu events\r
110  * @param id ID of object selected\r
111  */\r
112 void processObjectEvents(int id) {\r
113   addSceneObject(id);\r
114   manipulateState = STATE_OBJECT_POSITION_SCALE;\r
115   glutPostRedisplay();\r
116 }\r
117 \r
118 /**\r
119  * Event hander for texture menu events\r
120  * @param id ID of texutre selected\r
121  */\r
122 void processTextureEvents(int id) {\r
123   if ( curObject >= 0 ) {\r
124     sceneObjs[curObject].texture = id;\r
125     glutPostRedisplay();\r
126   }\r
127 }\r
128 \r
129 /**\r
130  * Event hander for ground texture menu events\r
131  * @param id ID of ground texture selected\r
132  */\r
133 void processGTextureEvents(int id) {\r
134   currentGroundTexture = id;\r
135   glutPostRedisplay();\r
136 }\r
137 \r
138 /**\r
139  * Creates menu for program\r
140  */\r
141 void makeMenu() {\r
142   // Construct material menu\r
143   int materialMenu = glutCreateMenu(processMaterialEvents);\r
144   glutAddMenuEntry("All R/G/B", M_MATERIAL_ALL_RGB);\r
145   glutAddMenuEntry("Ambient R/G/B", M_MATERIAL_AMBIENT_RGB);\r
146   glutAddMenuEntry("Diffuse R/G/B", M_MATERIAL_DIFFUSE_RGB);\r
147   glutAddMenuEntry("Specular R/G/B", M_MATERIAL_SPECULAR_RGB);\r
148   glutAddMenuEntry("All Amb/Diff/Spec/Shine", M_MATERIAL_ALL_ADSS);\r
149   glutAddMenuEntry("Red Amb/Diff/Spec/Shine", M_MATERIAL_RED_ADSS);\r
150   glutAddMenuEntry("Green Amb/Diff/Spec/Shine", M_MATERIAL_GREEN_ADSS);\r
151   glutAddMenuEntry("Blue Amb/Diff/Spec/Shine", M_MATERIAL_BLUE_ADSS);\r
152 \r
153   // Construct light menu\r
154   int lightMenu = glutCreateMenu(processLightEvents);\r
155   glutAddMenuEntry("Move Light 1", M_LIGHT_MOVE_LIGHT_1);\r
156   glutAddMenuEntry("R/G/B/All Light 1", M_LIGHT_RGBALL_LIGHT_1);\r
157   glutAddMenuEntry("Move Light 2", M_LIGHT_MOVE_LIGHT_2);\r
158   glutAddMenuEntry("R/G/B/All Light 2", M_LIGHT_RGBALL_LIGHT_2);\r
159 \r
160   // Construct object menu\r
161   int objectMenu = makeSubmenuFromArray( objectMenuEntries, NMESH, processObjectEvents );\r
162 \r
163   // Construct texture / ground texture menus\r
164   int textureMenu = makeSubmenuFromArray( textureMenuEntries, NTEXTURE, processTextureEvents );\r
165   int gTextureMenu = makeSubmenuFromArray( textureMenuEntries, NTEXTURE, processGTextureEvents );\r
166 \r
167   // Construct main menu\r
168   glutCreateMenu(processMainEvents);\r
169   glutAddMenuEntry("Rotate/Move Camera", M_ROTATE_MOVE_CAMERA);\r
170   glutAddSubMenu("Add object", objectMenu);\r
171   //glutAddMenuEntry("Position/Scale", M_POSITION_SCALE);\r
172   //glutAddMenuEntry("Rotation/Texture Scale", M_ROTATION_TEXTURE_SCALE);\r
173   //glutAddSubMenu("Material", materialMenu);\r
174   glutAddSubMenu("Texture", textureMenu);\r
175   glutAddSubMenu("Ground texture", gTextureMenu);\r
176   //glutAddSubMenu("Lights", lightMenu);\r
177   glutAddMenuEntry("Exit", M_EXIT);\r
178 \r
179   // Bind to right mouse button\r
180   glutAttachMenu(GLUT_RIGHT_BUTTON);\r
181 }\r
182 \r
183 /**\r
184  * Called when window is resized\r
185  * @param w New width\r
186  * @param h New height\r
187  */\r
188 void windowReshape(int w, int h) {\r
189   glViewport(0, 0, (GLsizei) w, (GLsizei) h);\r
190   width = w;\r
191   height = h;\r
192 }\r
193 \r
194 /**\r
195  * Called when mouse event occurs\r
196  * @param btn Mouse button\r
197  * @param state State of mouse button\r
198  * @param x Mouse x position\r
199  * @param y Mouse y position\r
200  */\r
201 void mouse(int button, int state, int x, int y) {\r
202   switch(button) {\r
203     case GLUT_LEFT_BUTTON:\r
204     case GLUT_MIDDLE_BUTTON:\r
205       if ( state == GLUT_DOWN ) {\r
206         buttonSelected = button;\r
207       } else if ( state == GLUT_UP ) {\r
208         buttonSelected = -1;\r
209       }\r
210       startx = x;\r
211       starty = y;\r
212       break;\r
213   }\r
214 }\r
215 \r
216 /**\r
217  * Keybord event handler\r
218  * w/s increase/decrease the z\r
219  * a/d increase/decrease the x\r
220  * q/e increase/decrease the y\r
221  * z/x increase/decrease the angle\r
222  * @param key Key pressed\r
223  * @param x x co-ordinate of mouse\r
224  * @param y y co-ordinate of mouse\r
225  */\r
226 void keyboard(unsigned char key, int x, int y) {\r
227   switch(key) {\r
228     case 'w':\r
229     case 'W':\r
230       camz -= factor;\r
231       break;\r
232     case 'a':\r
233     case 'A':\r
234       camx -= factor;\r
235       break;\r
236     case 's':\r
237     case 'S':\r
238       camz += factor;\r
239       break;\r
240     case 'd':\r
241     case 'D':\r
242       camx += factor;\r
243       break;\r
244     case 'q':\r
245     case 'Q':\r
246       camy += factor;\r
247       break;\r
248     case 'e':\r
249     case 'E':\r
250       camy -= factor;\r
251       break;\r
252     case 'z':\r
253     case 'Z':\r
254       keyrot += factor;\r
255       break;\r
256     case 'x':\r
257     case 'X':\r
258       keyrot -= factor;\r
259       break;\r
260     case '=':\r
261     case '+':\r
262       factor += 0.1;\r
263       printf("Factor of change is now %f\n", factor);\r
264       break;\r
265     case '-':\r
266     case '_':\r
267       factor -= 0.1;\r
268       printf("Factor of change is now %f\n", factor);\r
269       break;\r
270 \r
271   }\r
272   printf("Camera is now at (%f, %f, %f), angle %f\n", camx, camy, camz, keyrot);\r
273   glutPostRedisplay();\r
274 }\r
275 \r
276 /**\r
277  * Called when motion event occurs\r
278  * @param x Mouse x position\r
279  * @param y Mouse y position\r
280  */\r
281 void motion(int x, int y) {\r
282   if ( buttonSelected == -1 ) return; // No button selected, no action\r
283 \r
284   switch ( manipulateState ) {\r
285     case STATE_CAMERA_ROTATE_MOVE:\r
286       rotate += (x - startx);\r
287 \r
288       if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
289         // w: rotate, h: zoom\r
290         zoom += (y - starty);\r
291       } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
292         // w: rotate, h: tilt\r
293         camAngle += (y - starty);\r
294       }\r
295 \r
296       starty = y;\r
297       startx = x;\r
298 \r
299       break;\r
300 \r
301     case STATE_OBJECT_POSITION_SCALE:\r
302       //so.x, so.y, so.z\r
303       if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
304         // w: left/right, h: near/far\r
305         sceneObjs[curObject].x += (x - startx);\r
306       } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
307         // w: big/small, h: up/down\r
308       }\r
309 \r
310       startx = x;\r
311       break;\r
312 \r
313     case STATE_OBJECT_ROTATION_TEXTURE_SCALE:\r
314       if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
315         // w: rotate on width, h: rotate on h\r
316 \r
317       } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
318         // w: rotate on height, h: texture scale\r
319         \r
320       }\r
321       break;\r
322 \r
323   }\r
324 \r
325   glutPostRedisplay();\r
326 }\r
327 \r
328 /**\r
329  * Display function\r
330  */\r
331 void display() {\r
332   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);\r
333   \r
334   // Redraw projection matrix\r
335   glMatrixMode(GL_PROJECTION);\r
336   glLoadIdentity();\r
337 \r
338   float aspect;\r
339   if ( width <= height ) {\r
340     aspect = (float)height / (float)width;\r
341   } else {\r
342     aspect = (float)width / (float)height;\r
343   }\r
344 \r
345   gluPerspective(\r
346     75.0,\r
347     aspect,\r
348     0.1,\r
349     300\r
350     );\r
351 \r
352   glMatrixMode(GL_MODELVIEW);\r
353   glLoadIdentity();\r
354 \r
355   gluLookAt(\r
356     0.0,  0.0,  15.0 + (zoom*zoomFactor),  /* eye is at (x,y,z) */\r
357     0.0,  0.0,  0.0,  /* center is at (x,y,z) */\r
358     0.0,  10.0,  0.0   /* up is in postivie Y direction */\r
359     );\r
360 \r
361   glRotatef(camAngle*camAngleFactor, 1.0, 0.0, 0.0);\r
362 \r
363   /* Reposition the light source 0. */\r
364   lightPosition0[0] = 12*cos(lightAngle0);\r
365   lightPosition0[1] = lightHeight0;\r
366   lightPosition0[2] = 12*sin(lightAngle0);\r
367   lightPosition0[3] = 0.0;\r
368 \r
369   direction0[0] = lightPosition0[0];\r
370   direction0[2] = lightPosition0[2];\r
371 \r
372   /* Reposition the light source 1. */\r
373   lightPosition1[0] = 12*cos(lightAngle1);\r
374   lightPosition1[1] = lightHeight1;\r
375   lightPosition1[2] = 12*sin(lightAngle1);\r
376   lightPosition1[3] = 0.0;\r
377 \r
378   direction1[0] = lightPosition1[0];\r
379   direction1[2] = lightPosition1[2];\r
380 \r
381   glPushMatrix();\r
382 \r
383     /* Perform scene rotations based on user mouse/keyboard input. */\r
384     glRotatef(rotate*rotateFactor, 0.0, 1.0, 0.0);\r
385     glTranslatef(camx, camy, camz);\r
386     glRotatef(keyrot, 1.0, 0.0, 0.0);\r
387 \r
388     glLightfv(GL_LIGHT0, GL_POSITION, lightPosition0);\r
389     glLightfv(GL_LIGHT1, GL_POSITION, lightPosition1);\r
390 \r
391     drawFloor();\r
392     \r
393     // Draw sceneObjs array\r
394     for ( int i = 0; i < nObjects; i++ ) {\r
395       glPushMatrix();\r
396         SceneObject so = sceneObjs[i];\r
397 \r
398         // Apply rotation vector\r
399         glRotatef(so.rotation.amount, so.rotation.vector[0], so.rotation.vector[1], so.rotation.vector[2]);\r
400 \r
401         // Apply scaling vector\r
402         glScalef(so.scale[0], so.scale[1], so.scale[2]);\r
403 \r
404         // Apply translation vector\r
405         glTranslatef(so.x, so.y, so.z);\r
406 \r
407         // Apply texture\r
408         if ( so.texture > 0 ) {\r
409           getTexture(so.texture);\r
410           glBindTexture(GL_TEXTURE_2D, so.texture);\r
411         } else {\r
412           glBindTexture(GL_TEXTURE_2D, 0);\r
413         }\r
414 \r
415         // Draw actual object\r
416         if ( so.mesh > 0 ) {\r
417           // drawMesh();\r
418         } else if ( so.mesh == -1 ) { // a mesh of -1 draws the teapot\r
419           // The teapot does not obey the right-hand rule\r
420           glFrontFace(GL_CW);\r
421           glutSolidTeapot(1);\r
422           glFrontFace(GL_CCW);\r
423         }\r
424 \r
425         glBindTexture(GL_TEXTURE_2D, 0);\r
426       glPopMatrix();\r
427     }\r
428     \r
429 \r
430     // Draw a white ball over the light source\r
431     glPushMatrix();\r
432       glDisable(GL_LIGHTING);\r
433       glColor3f(1.0, 1.0, 1.0);\r
434       glTranslatef(lightPosition0[0], lightPosition0[1], lightPosition0[2]);\r
435       glutSolidSphere(0.5, 50, 50);\r
436       glEnable(GL_LIGHTING);\r
437     glPopMatrix();\r
438 \r
439     // Draw a white ball over the light source\r
440     glPushMatrix();\r
441       glDisable(GL_LIGHTING);\r
442       glColor3f(1.0, 1.0, 1.0);\r
443       glTranslatef(lightPosition1[0], lightPosition1[1], lightPosition1[2]);\r
444       glutSolidSphere(0.5, 50, 50);\r
445       glEnable(GL_LIGHTING);\r
446     glPopMatrix();\r
447 \r
448     drawAxisLines();\r
449 \r
450   glPopMatrix();\r
451 \r
452   glutSwapBuffers();\r
453 }\r
454 \r
455 /**\r
456  * init function; sets initial OpenGL state\r
457  */\r
458 void init() {\r
459   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);\r
460   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);\r
461   glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);\r
462   glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction0);\r
463 \r
464   glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);\r
465   glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);\r
466   glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);\r
467   glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, direction1);\r
468 \r
469   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glightmodel);\r
470   glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);\r
471 \r
472   glEnable(GL_LIGHT0);\r
473   glEnable(GL_LIGHT1);\r
474   glEnable(GL_LIGHTING);\r
475 }\r
476 \r
477 /**\r
478  * Main function\r
479  * @param argc Number of arguments\r
480  * @param argv Array of arguments\r
481  * @return Program exit code\r
482  */\r
483 int main(int argc, char **argv) {\r
484   if(argc>1)\r
485     strcpy(dataDir, argv[1]);\r
486   else if(opendir(dirDefault1))\r
487     strcpy(dataDir, dirDefault1);\r
488   else if(opendir(dirDefault2))\r
489     strcpy(dataDir, dirDefault2);\r
490   else fileErr(dirDefault1);\r
491 \r
492   for(int i=0; i<NMESH; i++) meshes[i]=NULL;\r
493   for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;\r
494 \r
495   glutInit(&argc, argv);\r
496 \r
497   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);\r
498 \r
499   glutInitWindowSize(500, 500);\r
500   glutCreateWindow("Scene Editor");\r
501 \r
502   glShadeModel(GL_SMOOTH); // Enables Smooth Shading\r
503   glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background\r
504   glClearDepth(1.0f); // Depth Buffer Setup\r
505   glDepthRange(0,1);\r
506   glEnable(GL_DEPTH_TEST); // Enables Depth Testing\r
507   glDepthFunc(GL_LEQUAL);  // the type\r
508   glEnable(GL_NORMALIZE);\r
509   glLineWidth(2.0);\r
510 \r
511   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);\r
512 \r
513   glutReshapeFunc(windowReshape);\r
514   glutDisplayFunc(display);\r
515   glutMouseFunc(mouse);\r
516   glutKeyboardFunc(keyboard);\r
517   glutMotionFunc(motion);\r
518 \r
519   makeMenu();\r
520 \r
521   init();\r
522 \r
523   glutMainLoop();\r
524 }

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