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

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