(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 #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       // Do stuff\r
29       break;\r
30 \r
31     case M_POSITION_SCALE:\r
32       // Do stuff\r
33       break;\r
34 \r
35     case M_ROTATION_TEXTURE_SCALE:\r
36       // Do stuff\r
37       break;\r
38 \r
39     case M_EXIT:\r
40       exit(EXIT_SUCCESS);\r
41 \r
42   }\r
43 }\r
44 \r
45 /**\r
46  * Event hander for materials menu events\r
47  * @param id ID of menu item selected\r
48  */\r
49 void processMaterialEvents(int id) {\r
50   switch (id) {\r
51     case M_MATERIAL_ALL_RGB:\r
52       // Do stuff\r
53       break;\r
54 \r
55     case M_MATERIAL_AMBIENT_RGB:\r
56       // Do stuff\r
57       break;\r
58 \r
59     case M_MATERIAL_DIFFUSE_RGB:\r
60       // Do stuff\r
61       break;\r
62 \r
63     case M_MATERIAL_SPECULAR_RGB:\r
64       // Do stuff\r
65       break;\r
66 \r
67     case M_MATERIAL_ALL_ADSS:\r
68       // Do stuff\r
69       break;\r
70 \r
71     case M_MATERIAL_RED_ADSS:\r
72       // Do stuff\r
73       break;\r
74 \r
75     case M_MATERIAL_GREEN_ADSS:\r
76       // Do stuff\r
77       break;\r
78 \r
79     case M_MATERIAL_BLUE_ADSS:\r
80       // Do stuff\r
81       break;\r
82 \r
83   }\r
84 }\r
85 \r
86 /**\r
87  * Event hander for light menu events\r
88  * @param id ID of menu item selected\r
89  */\r
90 void processLightEvents(int id) {\r
91   switch (id) {\r
92     case M_LIGHT_MOVE_LIGHT_1:\r
93       // Do stuff\r
94       break;\r
95 \r
96     case M_LIGHT_RGBALL_LIGHT_1:\r
97       // Do stuff\r
98       break;\r
99 \r
100     case M_LIGHT_MOVE_LIGHT_2:\r
101       // Do stuff\r
102       break;\r
103 \r
104     case M_LIGHT_RGBALL_LIGHT_2:\r
105       // Do stuff\r
106       break;\r
107 \r
108   }\r
109 }\r
110 \r
111 /**\r
112  * Event hander for object menu events\r
113  * @param id ID of object selected\r
114  */\r
115 void processObjectEvents(int id) {\r
116 \r
117 }\r
118 \r
119 /**\r
120  * Event hander for texture menu events\r
121  * @param id ID of texutre selected\r
122  */\r
123 void processTextureEvents(int id) {\r
124 \r
125 }\r
126 \r
127 /**\r
128  * Event hander for ground texture menu events\r
129  * @param id ID of ground texture selected\r
130  */\r
131 void processGTextureEvents(int id) {\r
132 \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   if (button == GLUT_LEFT_BUTTON) {\r
200     if (state == GLUT_DOWN) {\r
201       moving = 1;\r
202       startx = x;\r
203       starty = y;\r
204     }\r
205     if (state == GLUT_UP) {\r
206       moving = 0;\r
207     }\r
208   }\r
209 }\r
210 \r
211 /**\r
212  * Keybord event handler\r
213  * w/s increase/decrease the z\r
214  * a/d increase/decrease the x\r
215  * q/e increase/decrease the y\r
216  * z/x increase/decrease the angle\r
217  * @param key Key pressed\r
218  * @param x x co-ordinate of mouse\r
219  * @param y y co-ordinate of mouse\r
220  */\r
221 void keyboard(unsigned char key, int x, int y) {\r
222   switch(key) {\r
223     case 'w':\r
224     case 'W':\r
225       camz -= factor;\r
226       break;\r
227     case 'a':\r
228     case 'A':\r
229       camx -= factor;\r
230       break;\r
231     case 's':\r
232     case 'S':\r
233       camz += factor;\r
234       break;\r
235     case 'd':\r
236     case 'D':\r
237       camx += factor;\r
238       break;\r
239     case 'q':\r
240     case 'Q':\r
241       camy += factor;\r
242       break;\r
243     case 'e':\r
244     case 'E':\r
245       camy -= factor;\r
246       break;\r
247     case 'z':\r
248     case 'Z':\r
249       keyrot += factor;\r
250       break;\r
251     case 'x':\r
252     case 'X':\r
253       keyrot -= factor;\r
254       break;\r
255     case '=':\r
256     case '+':\r
257       factor += 0.1;\r
258       printf("Factor of change is now %f\n", 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 \r
266   }\r
267   printf("Camera is now at (%f, %f, %f), angle %f\n", camx, camy, camz, keyrot);\r
268   glutPostRedisplay();\r
269 }\r
270 \r
271 /**\r
272  * Called when motion event occurs\r
273  * @param x Mouse x position\r
274  * @param y Mouse y position\r
275  */\r
276 void motion(int x, int y) {\r
277   if (moving) {\r
278     zoom += (y - starty);\r
279     rotate += (x - startx);\r
280     startx = x;\r
281     starty = y;\r
282     glutPostRedisplay();\r
283   }\r
284   if (lightMoving) {\r
285     lightAngle += (x - lightStartX)/40.0;\r
286     lightHeight += (lightStartY - y)/20.0;\r
287     lightStartX = x;\r
288     lightStartY = y;\r
289     glutPostRedisplay();\r
290   }\r
291 }\r
292 \r
293 /**\r
294  * Display function\r
295  */\r
296 void display() {\r
297   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);\r
298   \r
299   // Redraw projection matrix\r
300   glMatrixMode(GL_PROJECTION);\r
301   glLoadIdentity();\r
302 \r
303   float aspect;\r
304   if ( width <= height ) {\r
305     aspect = (float)height / (float)width;\r
306   } else {\r
307     aspect = (float)width / (float)height;\r
308   }\r
309 \r
310   gluPerspective(\r
311     75.0,\r
312     aspect,\r
313     0.1,\r
314     200\r
315     );\r
316 \r
317   glMatrixMode(GL_MODELVIEW);\r
318   glLoadIdentity();\r
319 \r
320   gluLookAt(\r
321     0.0,  0.0,  5.0 + (zoom*zoomFactor),  /* eye is at (x,y,z) */\r
322     0.0,  0.0,  0.0,  /* center is at (x,y,z) */\r
323     0.0,  1.0,  0.0   /* up is in postivie Y direction */\r
324     );\r
325 \r
326   glRotatef(50.0, 1.0, 0.0, 0.0);\r
327 \r
328   /* Reposition the light source. */\r
329   lightPosition[0] = 12*cos(lightAngle);\r
330   lightPosition[1] = lightHeight;\r
331   lightPosition[2] = 12*sin(lightAngle);\r
332   lightPosition[3] = 0.0;\r
333 \r
334   glPushMatrix();\r
335 \r
336     /* Perform scene rotations based on user mouse/keyboard input. */\r
337     glRotatef(rotate*rotateFactor, 0.0, 1.0, 0.0);\r
338     glTranslatef(camx, camy, camz);\r
339     glRotatef(keyrot, 1.0, 0.0, 0.0);\r
340 \r
341     glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);\r
342 \r
343     drawFloor();\r
344 \r
345     drawLine();\r
346     \r
347     // Draw teapot for a test object\r
348     glPushMatrix();\r
349       glTranslatef(0.0, 0.5, 0.0); // **NOTE: Teapot currently does not rest on surface\r
350       glColor3f(0.0, 0.0, 0.0);\r
351       glFrontFace(GL_CW);\r
352       glutSolidTeapot(1);\r
353       glFrontFace(GL_CCW);\r
354     glPopMatrix();\r
355 \r
356     // Draw a white ball over the light source\r
357     glPushMatrix();\r
358       glDisable(GL_LIGHTING);\r
359       glColor3f(1.0, 1.0, 0.0);\r
360       glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);\r
361       glutSolidSphere(1.0, 50, 50);\r
362       glEnable(GL_LIGHTING);\r
363     glPopMatrix();\r
364 \r
365   glPopMatrix();\r
366 \r
367   glutSwapBuffers();\r
368 }\r
369 \r
370 /**\r
371  * init function; sets initial OpenGL state\r
372  */\r
373 void init() {\r
374   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);\r
375   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);\r
376   glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);\r
377   glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction0);\r
378 \r
379   glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 90.0);\r
380 \r
381   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glightmodel);\r
382 \r
383   glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);\r
384   glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);\r
385   glMaterialfv(GL_FRONT, GL_SPECULAR, specular);\r
386   glMaterialfv(GL_FRONT, GL_EMISSION, emission0);\r
387   glMaterialf(GL_FRONT, GL_SHININESS, shine);\r
388 \r
389   glEnable(GL_LIGHT0);\r
390   glEnable(GL_LIGHTING);\r
391 }\r
392 \r
393 /**\r
394  * Main function\r
395  * @param argc Number of arguments\r
396  * @param argv Array of arguments\r
397  * @return Program exit code\r
398  */\r
399 int main(int argc, char **argv) {\r
400   if(argc>1)\r
401     strcpy(dataDir, argv[1]);\r
402   else if(opendir(dirDefault1))\r
403     strcpy(dataDir, dirDefault1);\r
404   else if(opendir(dirDefault2))\r
405     strcpy(dataDir, dirDefault2);\r
406   else fileErr(dirDefault1);\r
407 \r
408   for(int i=0; i<NMESH; i++) meshes[i]=NULL;\r
409   for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;\r
410 \r
411   glutInit(&argc, argv);\r
412 \r
413   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);\r
414 \r
415   glutInitWindowSize(500, 500);\r
416   glutCreateWindow("Scene Editor");\r
417 \r
418   glShadeModel(GL_SMOOTH); // Enables Smooth Shading\r
419   glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background\r
420   glClearDepth(1.0f); // Depth Buffer Setup\r
421   glDepthRange(0,1);\r
422   glEnable(GL_DEPTH_TEST); // Enables Depth Testing\r
423   glDepthFunc(GL_LEQUAL);  // the type\r
424   glEnable(GL_TEXTURE_2D);\r
425   glEnable(GL_CULL_FACE);\r
426   glEnable(GL_NORMALIZE);\r
427   glLineWidth(2.0);\r
428 \r
429   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);\r
430 \r
431   glutReshapeFunc(windowReshape);\r
432   glutDisplayFunc(display);\r
433   glutMouseFunc(mouse);\r
434   glutKeyboardFunc(keyboard);\r
435   glutMotionFunc(motion);\r
436 \r
437   makeMenu();\r
438 \r
439   init();\r
440 \r
441   glutMainLoop();\r
442 }

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