(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   /*glMatrixMode(GL_PROJECTION);\r
188   glLoadIdentity();\r
189   if (w <= h) \r
190     glFrustum(near, far, near*(GLfloat)h/(GLfloat)w,\r
191              far*(GLfloat)h/(GLfloat)w, -100, 100);\r
192   else\r
193     glFrustum(near*(GLfloat)w/(GLfloat)h,\r
194              far*(GLfloat)w/(GLfloat)h, near, far, nearClip, farClip);\r
195    glMatrixMode(GL_MODELVIEW);\r
196    glLoadIdentity();*/\r
197 }\r
198 \r
199 /**\r
200  * Called when mouse event occurs\r
201  * @param btn Mouse button\r
202  * @param state State of mouse button\r
203  * @param x Mouse x position\r
204  * @param y Mouse y position\r
205  */\r
206 void mouse(int button, int state, int x, int y) {\r
207   if (button == GLUT_LEFT_BUTTON) {\r
208     if (state == GLUT_DOWN) {\r
209       moving = 1;\r
210       startx = x;\r
211       starty = y;\r
212     }\r
213     if (state == GLUT_UP) {\r
214       moving = 0;\r
215     }\r
216   }\r
217   if (button == GLUT_MIDDLE_BUTTON) {\r
218     if (state == GLUT_DOWN) {\r
219       lightMoving = 1;\r
220       lightStartX = x;\r
221       lightStartY = y;\r
222     }\r
223     if (state == GLUT_UP) {\r
224       lightMoving = 0;\r
225     }\r
226   }\r
227 }\r
228 \r
229 /**\r
230  * Keybord event handler\r
231  * w/s increase/decrease the z\r
232  * a/d increase/decrease the x\r
233  * q/e increase/decrease the y\r
234  * z/x increase/decrease the angle\r
235  * @param key Key pressed\r
236  * @param x x co-ordinate of mouse\r
237  * @param y y co-ordinate of mouse\r
238  */\r
239 void keyboard(unsigned char key, int x, int y) {\r
240   switch(key) {\r
241     case 'w':\r
242       camz = camz - 1;\r
243       break;\r
244     case 'a':\r
245       camx = camx - 1;\r
246       break;\r
247     case 's':\r
248       camz = camz + 1;\r
249       break;\r
250     case 'd':\r
251       camx = camx + 1;\r
252       break;\r
253     case 'q':\r
254       camy = camy + 1;\r
255       break;\r
256     case 'e':\r
257       camy = camy - 1;\r
258       break;\r
259     case 'z':\r
260       rot = rot + 1;\r
261       break;\r
262     case 'x':\r
263       rot = rot - 1;\r
264       break;\r
265   }\r
266   printf("Camera is now at (%f, %f, %f), angle %f\n", camx, camy, camz, rot);\r
267   glutPostRedisplay();\r
268 }\r
269 \r
270 /**\r
271  * Called when motion event occurs\r
272  * @param x Mouse x position\r
273  * @param y Mouse y position\r
274  */\r
275 void motion(int x, int y) {\r
276   if (moving) {\r
277     angle = angle + (x - startx);\r
278     angle2 = angle2 + (y - starty);\r
279     startx = x;\r
280     starty = y;\r
281     glutPostRedisplay();\r
282   }\r
283   if (lightMoving) {\r
284     lightAngle += (x - lightStartX)/40.0;\r
285     lightHeight += (lightStartY - y)/20.0;\r
286     lightStartX = x;\r
287     lightStartY = y;\r
288     glutPostRedisplay();\r
289   }\r
290 }\r
291 \r
292 /**\r
293  * Display function\r
294  */\r
295 void display() {\r
296   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);\r
297   glLoadIdentity();\r
298   //gluLookAt(\r
299   //  0.0, 0.0, 30.0,  /* eye is at (x,y,z) */\r
300   //  0.0, 0.0,  0.0,  /* center is at (x,y,z) */\r
301   //  0.0, 1.0,  0.0   /* up is in postivie Y direction */\r
302    // );\r
303 \r
304   glTranslatef(camx, camy, camz);\r
305   \r
306   // **NOTE: Currently this rotation function is all that moves the camera off\r
307   //         the flat surface. Need to integrate function into gluLookAt\r
308   glRotatef(rot, 1.0, 0.0, 0.0);\r
309 \r
310   /* Reposition the light source. */\r
311   lightPosition[0] = 12*cos(lightAngle);\r
312   lightPosition[1] = lightHeight;\r
313   lightPosition[2] = 12*sin(lightAngle);\r
314   lightPosition[3] = 0.0;\r
315 \r
316   glPushMatrix();\r
317 \r
318     /* Perform scene rotations based on user mouse input. */\r
319     glRotatef(angle, 0.0, 1.0, 0.0);\r
320     glRotatef(angle2, 1.0, 0.0, 0.0); //**NOTE: Only one degree of freedom\r
321 \r
322     glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);\r
323 \r
324     drawFloor();\r
325 \r
326     drawLine();\r
327     \r
328     // Draw teapot for a test object\r
329     glPushMatrix();\r
330       glTranslatef(0.0, 0.5, 0.0); // **NOTE: Teapot does not rest on surface\r
331       glColor3f(0.0, 0.0, 0.0);\r
332       glutSolidTeapot(1);\r
333     glPopMatrix();\r
334 \r
335     // Draw a white ball over the light source\r
336     glPushMatrix();\r
337       glDisable(GL_LIGHTING);\r
338       glColor3f(1.0, 1.0, 0.0);\r
339       glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);\r
340       glutSolidSphere(1.0, 50, 50);\r
341       glEnable(GL_LIGHTING);\r
342     glPopMatrix();\r
343 \r
344   glPopMatrix();\r
345 \r
346   glutSwapBuffers();\r
347 }\r
348 \r
349 /**\r
350  * init function; sets initial OpenGL state\r
351  */\r
352 void init() {\r
353   //glMatrixMode(GL_PROJECTION);\r
354   //glLoadIdentity();\r
355 \r
356    //gluPerspective(\r
357    // 60.0,  /* field of view in degree */\r
358    //  1.0,  /* aspect ratio */\r
359    // nearClip,  /* Z near */\r
360     // farClip   /* Z far */\r
361    // );\r
362   \r
363   glMatrixMode(GL_MODELVIEW);\r
364   glLoadIdentity();\r
365 \r
366   // **NOTE: Needs tidy\r
367 glEnable(GL_NORMALIZE);\r
368   glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);\r
369   glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);\r
370   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);\r
371   glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);\r
372   GLfloat direction0[] = {0.0, 0.0, 0.0};\r
373   glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction0);\r
374   glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 90.0);\r
375   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glightmodel);\r
376 \r
377   GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};\r
378   GLfloat diffuse[] = {1.0, 0.8, 0.0, 1.0};\r
379   GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};\r
380   GLfloat shine = 100.0;\r
381   glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);\r
382   glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);\r
383   glMaterialfv(GL_FRONT, GL_SPECULAR, specular);\r
384   glMaterialf(GL_FRONT, GL_SHININESS, shine);\r
385 \r
386   GLfloat emission[] = {0.0, 0.3, 0.3, 1.0};\r
387   glMaterialfv(GL_FRONT, GL_EMISSION, emission);\r
388 \r
389   glEnable(GL_LIGHT0);\r
390   glEnable(GL_LIGHTING);\r
391   //glEnable(GL_COLOR_MATERIAL);\r
392   //glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );\r
393   //glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, specular0);\r
394   //glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, emission0);\r
395 \r
396   \r
397 \r
398 }\r
399 \r
400 /**\r
401  * Main function\r
402  * @param argc Number of arguments\r
403  * @param argv Array of arguments\r
404  * @return Program exit code\r
405  */\r
406 int main(int argc, char **argv) {\r
407   if(argc>1)\r
408     strcpy(dataDir, argv[1]);\r
409   else if(opendir(dirDefault1))\r
410     strcpy(dataDir, dirDefault1);\r
411   else if(opendir(dirDefault2))\r
412     strcpy(dataDir, dirDefault2);\r
413   else fileErr(dirDefault1);\r
414 \r
415   for(int i=0; i<NMESH; i++) meshes[i]=NULL;\r
416   for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;\r
417 \r
418   glutInit(&argc, argv);\r
419 \r
420   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);\r
421 \r
422   glutInitWindowSize(500, 500);\r
423   glutCreateWindow("Scene Editor");\r
424 \r
425   glShadeModel(GL_SMOOTH); // Enables Smooth Shading\r
426   glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background\r
427   glClearDepth(1.0f); // Depth Buffer Setup\r
428   glDepthRange(0,1);\r
429   glEnable(GL_DEPTH_TEST); // Enables Depth Testing\r
430   glDepthFunc(GL_LEQUAL);  // the type\r
431   glEnable(GL_CULL_FACE);\r
432   glEnable(GL_TEXTURE_2D);\r
433   glEnable(GL_BLEND);\r
434   glLineWidth(1.0);\r
435 \r
436   glMatrixMode(GL_PROJECTION);\r
437   gluPerspective(\r
438      40.0, /* field of view in degree */\r
439       1.0, /* aspect ratio */\r
440       1.0, /* Z near */\r
441     100.0  /* Z far */\r
442     );\r
443 \r
444   glMatrixMode(GL_MODELVIEW);\r
445   gluLookAt(\r
446     0.0,  35.0, -20.0,  /* eye is at (0,8,60) */\r
447     0.0,  10.0,  0.0,  /* center is at (0,8,0) */\r
448     0.0,  1.0,   0.0   /* up is in postivie Y direction */\r
449     );\r
450 \r
451   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);\r
452 \r
453   glutReshapeFunc(windowReshape);\r
454   glutDisplayFunc(display);\r
455   glutMouseFunc(mouse);\r
456   glutKeyboardFunc(keyboard);\r
457   glutMotionFunc(motion);\r
458 \r
459   makeMenu();\r
460 \r
461   init();\r
462 \r
463   glutMainLoop();\r
464 }\r

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