(no commit message)
[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       manipulateState = STATE_LIGHT_1_MOVE;\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       manipulateState = STATE_LIGHT_2_MOVE;\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 = 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   if ( button == GLUT_LEFT_BUTTON && glutGetModifiers() == GLUT_ACTIVE_SHIFT ) {\r
203     button = GLUT_MIDDLE_BUTTON; // Holding shift with left button is the same as the middle button\r
204   }\r
205   switch(button) {\r
206     case GLUT_LEFT_BUTTON:\r
207     case GLUT_MIDDLE_BUTTON:\r
208       if ( state == GLUT_DOWN ) {\r
209         buttonSelected = button;\r
210       } else if ( state == GLUT_UP ) {\r
211         buttonSelected = -1;\r
212       }\r
213       startx = x;\r
214       starty = y;\r
215       break;\r
216   }\r
217 }\r
218 \r
219 /**\r
220  * Keybord event handler\r
221  * w/s increase/decrease the z\r
222  * a/d increase/decrease the x\r
223  * q/e increase/decrease the y\r
224  * z/x increase/decrease the angle\r
225  * @param key Key pressed\r
226  * @param x x co-ordinate of mouse\r
227  * @param y y co-ordinate of mouse\r
228  */\r
229 void keyboard(unsigned char key, int x, int y) {\r
230   switch(key) {\r
231     case 'w':\r
232     case 'W':\r
233       camz -= factor;\r
234       break;\r
235     case 'a':\r
236     case 'A':\r
237       camx -= factor;\r
238       break;\r
239     case 's':\r
240     case 'S':\r
241       camz += factor;\r
242       break;\r
243     case 'd':\r
244     case 'D':\r
245       camx += factor;\r
246       break;\r
247     case 'q':\r
248     case 'Q':\r
249       camy += factor;\r
250       break;\r
251     case 'e':\r
252     case 'E':\r
253       camy -= factor;\r
254       break;\r
255     case 'z':\r
256     case 'Z':\r
257       keyrot += factor;\r
258       break;\r
259     case 'x':\r
260     case 'X':\r
261       keyrot -= factor;\r
262       break;\r
263     case '=':\r
264     case '+':\r
265       factor += 0.1;\r
266       printf("Factor of change is now %f\n", factor);\r
267       break;\r
268     case '-':\r
269     case '_':\r
270       factor -= 0.1;\r
271       printf("Factor of change is now %f\n", factor);\r
272       break;\r
273 \r
274   }\r
275   printf("Camera is now at (%f, %f, %f), angle %f\n", camx, camy, camz, keyrot);\r
276   glutPostRedisplay();\r
277 }\r
278 \r
279 /**\r
280  * Called when motion event occurs\r
281  * @param x Mouse x position\r
282  * @param y Mouse y position\r
283  */\r
284 void motion(int x, int y) {\r
285   if ( buttonSelected == -1 ) return; // No button selected, no action\r
286 \r
287   float diffx = x - startx;\r
288   float diffy = y - starty;\r
289 \r
290   switch ( manipulateState ) {\r
291     case STATE_CAMERA_ROTATE_MOVE:\r
292       // w: rotate\r
293       rotate += diffx;\r
294 \r
295       if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
296         // h: zoom\r
297         zoom += diffy;\r
298       } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
299         // h: tilt\r
300         camAngle += diffy;\r
301       }\r
302       break;\r
303 \r
304     case STATE_OBJECT_POSITION_SCALE:\r
305 \r
306       if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
307         // w: left/right, h: near/far\r
308         float angler = 2 * M_PI * ( (rotate*camRotateFactor)/360.0 );\r
309         sceneObjs[curObject].x += diffx * cos(angler) * leftrightFactor + diffy * cos(M_PI/2 + angler) * nearfarFactor;\r
310         sceneObjs[curObject].z += diffx * sin(angler) * leftrightFactor + diffy * sin(M_PI/2 + angler) * nearfarFactor;\r
311 \r
312       } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
313         // w: big/small\r
314         float max = (float)height/bigsmallFactor;\r
315         float scaling = (diffx + max) / max;\r
316 \r
317         sceneObjs[curObject].scale[0] *= scaling;\r
318         sceneObjs[curObject].scale[1] *= scaling;\r
319         sceneObjs[curObject].scale[2] *= scaling;\r
320 \r
321         // h: up/down\r
322         sceneObjs[curObject].y -= diffy * updownFactor;\r
323       }\r
324 \r
325       break;\r
326 \r
327     case STATE_OBJECT_ROTATION_TEXTURE_SCALE:\r
328 \r
329       if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
330         // w: rotate on y\r
331         sceneObjs[curObject].rotation.y += diffx * rotateFactor;\r
332 \r
333         // h: rotate on x\r
334         sceneObjs[curObject].rotation.x += diffy * rotateFactor;\r
335 \r
336       } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
337         // w: rotate on x\r
338         sceneObjs[curObject].rotation.x += diffx * rotateFactor;\r
339 \r
340         // h: texture scale\r
341         float max = (float)height/texscaleFactor;\r
342         float scaling = (diffy + max) / max;\r
343         sceneObjs[curObject].texture.scale *= scaling;\r
344 \r
345       }\r
346       \r
347       break;\r
348 \r
349     case STATE_LIGHT_1_MOVE:\r
350     case STATE_LIGHT_2_MOVE:\r
351       ; // Semi-colon required to allow variable declaration below\r
352       \r
353       int i = 0;\r
354       if ( manipulateState == STATE_LIGHT_2_MOVE ) i = 1;\r
355 \r
356       if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
357         // w: left/right, h: near/far\r
358         float angler = 2 * M_PI * ( (rotate*camRotateFactor)/360.0 );\r
359         lightObjs[i].position[0] += diffx * cos(angler) * lleftrightFactor + diffy * cos(M_PI/2 + angler) * lnearfarFactor;\r
360         lightObjs[i].position[2] += diffx * sin(angler) * lleftrightFactor + diffy * sin(M_PI/2 + angler) * lnearfarFactor;\r
361 \r
362       } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
363         // w: increase/decrease some light param\r
364         // **NOTE: Currently not implemented\r
365 \r
366         // h: up/down\r
367         lightObjs[i].position[1] -= diffy * updownFactor;\r
368       }\r
369 \r
370       break;\r
371 \r
372   }\r
373 \r
374   starty = y;\r
375   startx = x;\r
376 \r
377   glutPostRedisplay();\r
378 }\r
379 \r
380 /**\r
381  * Display function\r
382  */\r
383 void display() {\r
384   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);\r
385   \r
386   // Redraw projection matrix\r
387   glMatrixMode(GL_PROJECTION);\r
388   glLoadIdentity();\r
389 \r
390   float aspect;\r
391   if ( width <= height ) {\r
392     aspect = (float)height / (float)width;\r
393   } else {\r
394     aspect = (float)width / (float)height;\r
395   }\r
396 \r
397   gluPerspective(\r
398     75.0,\r
399     aspect,\r
400     0.1,\r
401     300\r
402     );\r
403 \r
404   glMatrixMode(GL_MODELVIEW);\r
405   glLoadIdentity();\r
406 \r
407   gluLookAt(\r
408     0.0,  0.0,  15.0 + (zoom*zoomFactor),  /* eye is at (x,y,z) */\r
409     0.0,  0.0,  0.0,  /* center is at (x,y,z) */\r
410     0.0,  10.0,  0.0   /* up is in postivie Y direction */\r
411     );\r
412 \r
413   glRotatef(camAngle*camAngleFactor, 1.0, 0.0, 0.0); // Set camera angle upward\r
414 \r
415   glPushMatrix();\r
416 \r
417     /* Perform scene rotations based on user mouse/keyboard input. */\r
418     glRotatef(rotate*camRotateFactor, 0.0, 1.0, 0.0);\r
419     glTranslatef(camx, camy, camz);\r
420     glRotatef(keyrot, 1.0, 0.0, 0.0);\r
421 \r
422     drawFloor();\r
423     \r
424     // Draw sceneObjs array\r
425     for ( int i = 0; i < nObjects; i++ ) {\r
426       glPushMatrix();\r
427         SceneObject so = sceneObjs[i];\r
428 \r
429         // Apply translation vector\r
430         glTranslatef(so.x, so.y, so.z);\r
431 \r
432         // Apply independant rotation vectors\r
433         glRotatef(so.rotation.x, 1.0, 0.0, 0.0);\r
434         glRotatef(so.rotation.y, 0.0, 1.0, 0.0);\r
435         glRotatef(so.rotation.z, 0.0, 0.0, 1.0);\r
436 \r
437         // Apply scaling vector\r
438         glScalef(so.scale[0], so.scale[1], so.scale[2]);\r
439 \r
440         // Apply texture\r
441         if ( so.texture.id > 0 ) {\r
442           getTexture(so.texture.id);\r
443           glBindTexture(GL_TEXTURE_2D, so.texture.id);\r
444           glMatrixMode(GL_TEXTURE);\r
445           glScalef(so.texture.scale, so.texture.scale, so.texture.scale);\r
446           glMatrixMode(GL_MODELVIEW);\r
447         } else {\r
448           glBindTexture(GL_TEXTURE_2D, 0);\r
449         }\r
450 \r
451         // Draw actual object\r
452         if ( so.mesh > 0 ) {\r
453           // drawMesh();\r
454         } else if ( so.mesh == -1 ) { // a mesh of -1 draws the teapot\r
455           // The teapot does not obey the right-hand rule\r
456           glFrontFace(GL_CW);\r
457           glutSolidTeapot(1);\r
458           glFrontFace(GL_CCW);\r
459         }\r
460 \r
461         glBindTexture(GL_TEXTURE_2D, 0);\r
462       glPopMatrix();\r
463     }\r
464 \r
465     updateLights();\r
466 \r
467     drawAxisLines();\r
468 \r
469   glPopMatrix();\r
470 \r
471   glutSwapBuffers();\r
472 }\r
473 \r
474 /**\r
475  * Main function\r
476  * @param argc Number of arguments\r
477  * @param argv Array of arguments\r
478  * @return Program exit code\r
479  */\r
480 int main(int argc, char **argv) {\r
481   if(argc>1)\r
482     strcpy(dataDir, argv[1]);\r
483   else if(opendir(dirDefault1))\r
484     strcpy(dataDir, dirDefault1);\r
485   else if(opendir(dirDefault2))\r
486     strcpy(dataDir, dirDefault2);\r
487   else fileErr(dirDefault1);\r
488 \r
489   for(int i=0; i<NMESH; i++) meshes[i]=NULL;\r
490   for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;\r
491 \r
492   glutInit(&argc, argv);\r
493 \r
494   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);\r
495 \r
496   glutInitWindowSize(width, height);\r
497   glutCreateWindow("Scene Editor - Ashley Tyndall (20915779), Jenna de la Harpe (20367932)");\r
498 \r
499   glShadeModel(GL_SMOOTH); // Enables Smooth Shading\r
500   glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background\r
501   glClearDepth(1.0f); // Depth Buffer Setup\r
502   glDepthRange(0,1);\r
503   glEnable(GL_DEPTH_TEST); // Enables Depth Testing\r
504   glDepthFunc(GL_LEQUAL);  // the type\r
505   glEnable(GL_NORMALIZE);\r
506   glLineWidth(2.0);\r
507 \r
508   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);\r
509 \r
510   glutReshapeFunc(windowReshape);\r
511   glutDisplayFunc(display);\r
512   glutMouseFunc(mouse);\r
513   glutKeyboardFunc(keyboard);\r
514   glutMotionFunc(motion);\r
515 \r
516   makeMenu();\r
517 \r
518   initializeLights();\r
519 \r
520   /* Initial light 0 position. */\r
521   lightObjs[0].position[0] = 4;\r
522   lightObjs[0].position[1] = 5;\r
523   lightObjs[0].position[2] = 6;\r
524   lightObjs[0].position[3] = 0.0;\r
525 \r
526   /* Initial light 1 position. */\r
527   lightObjs[1].position[0] = -4;\r
528   lightObjs[1].position[1] = 5;\r
529   lightObjs[1].position[2] = -6;\r
530   lightObjs[1].position[3] = 0.0;\r
531 \r
532   glutMainLoop();\r
533 }

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