(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     sceneObjs[curObject].texture.scale = 1;\r
126     glutPostRedisplay();\r
127   }\r
128 }\r
129 \r
130 /**\r
131  * Event hander for ground texture menu events\r
132  * @param id ID of ground texture selected\r
133  */\r
134 void processGTextureEvents(int id) {\r
135   currentGroundTexture = id;\r
136   glutPostRedisplay();\r
137 }\r
138 \r
139 /**\r
140  * Creates menu for program\r
141  */\r
142 void makeMenu() {\r
143   // Construct material menu\r
144   int materialMenu = glutCreateMenu(processMaterialEvents);\r
145   glutAddMenuEntry("All R/G/B", M_MATERIAL_ALL_RGB);\r
146   glutAddMenuEntry("Ambient R/G/B", M_MATERIAL_AMBIENT_RGB);\r
147   glutAddMenuEntry("Diffuse R/G/B", M_MATERIAL_DIFFUSE_RGB);\r
148   glutAddMenuEntry("Specular R/G/B", M_MATERIAL_SPECULAR_RGB);\r
149   glutAddMenuEntry("All Amb/Diff/Spec/Shine", M_MATERIAL_ALL_ADSS);\r
150   glutAddMenuEntry("Red Amb/Diff/Spec/Shine", M_MATERIAL_RED_ADSS);\r
151   glutAddMenuEntry("Green Amb/Diff/Spec/Shine", M_MATERIAL_GREEN_ADSS);\r
152   glutAddMenuEntry("Blue Amb/Diff/Spec/Shine", M_MATERIAL_BLUE_ADSS);\r
153 \r
154   // Construct light menu\r
155   int lightMenu = glutCreateMenu(processLightEvents);\r
156   glutAddMenuEntry("Move Light 1", M_LIGHT_MOVE_LIGHT_1);\r
157   //glutAddMenuEntry("R/G/B/All Light 1", M_LIGHT_RGBALL_LIGHT_1);\r
158   glutAddMenuEntry("Move Light 2", M_LIGHT_MOVE_LIGHT_2);\r
159   //glutAddMenuEntry("R/G/B/All Light 2", M_LIGHT_RGBALL_LIGHT_2);\r
160 \r
161   // Construct object menu\r
162   int objectMenu = makeSubmenuFromArray( objectMenuEntries, NMESH, processObjectEvents );\r
163 \r
164   // Construct texture / ground texture menus\r
165   int textureMenu = makeSubmenuFromArray( textureMenuEntries, NTEXTURE, processTextureEvents );\r
166   int gTextureMenu = makeSubmenuFromArray( textureMenuEntries, NTEXTURE, processGTextureEvents );\r
167 \r
168   // Construct main menu\r
169   glutCreateMenu(processMainEvents);\r
170   glutAddMenuEntry("Rotate/Move Camera", M_ROTATE_MOVE_CAMERA);\r
171   glutAddSubMenu("Add object", objectMenu);\r
172   glutAddMenuEntry("Position/Scale", M_POSITION_SCALE);\r
173   glutAddMenuEntry("Rotation/Texture Scale", M_ROTATION_TEXTURE_SCALE);\r
174   //glutAddSubMenu("Material", materialMenu);\r
175   glutAddSubMenu("Texture", textureMenu);\r
176   glutAddSubMenu("Ground texture", gTextureMenu);\r
177   glutAddSubMenu("Lights", lightMenu);\r
178   glutAddMenuEntry("Exit", M_EXIT);\r
179 \r
180   // Bind to right mouse button\r
181   glutAttachMenu(GLUT_RIGHT_BUTTON);\r
182 }\r
183 \r
184 /**\r
185  * Called when window is resized\r
186  * @param w New width\r
187  * @param h New height\r
188  */\r
189 void windowReshape(int w, int h) {\r
190   glViewport(0, 0, (GLsizei) w, (GLsizei) h);\r
191   width = w;\r
192   height = h;\r
193 }\r
194 \r
195 /**\r
196  * Called when mouse event occurs\r
197  * @param btn Mouse button\r
198  * @param state State of mouse button\r
199  * @param x Mouse x position\r
200  * @param y Mouse y position\r
201  */\r
202 void mouse(int button, int state, int x, int y) {\r
203   if ( button == GLUT_LEFT_BUTTON && glutGetModifiers() == GLUT_ACTIVE_SHIFT ) {\r
204     button = GLUT_MIDDLE_BUTTON; // Holding shift with left button is the same as the middle button\r
205   }\r
206   switch(button) {\r
207     case GLUT_LEFT_BUTTON:\r
208     case GLUT_MIDDLE_BUTTON:\r
209       if ( state == GLUT_DOWN ) {\r
210         buttonSelected = button;\r
211       } else if ( state == GLUT_UP ) {\r
212         buttonSelected = -1;\r
213       }\r
214       startx = x;\r
215       starty = y;\r
216       break;\r
217   }\r
218 }\r
219 \r
220 /**\r
221  * Called when motion event occurs\r
222  * @param x Mouse x position\r
223  * @param y Mouse y position\r
224  */\r
225 void motion(int x, int y) {\r
226   if ( buttonSelected == -1 ) return; // No button selected, no action\r
227 \r
228   float diffx = x - startx;\r
229   float diffy = y - starty;\r
230 \r
231   switch ( manipulateState ) {\r
232     case STATE_CAMERA_ROTATE_MOVE:\r
233       // w: rotate\r
234       rotate += diffx;\r
235 \r
236       if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
237         // h: zoom\r
238         zoom += diffy;\r
239       } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
240         // h: tilt\r
241         camAngle += diffy;\r
242       }\r
243       break;\r
244 \r
245     case STATE_OBJECT_POSITION_SCALE:\r
246 \r
247       if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
248         // w: left/right, h: near/far\r
249         float angler = 2 * M_PI * ( (rotate*camRotateFactor)/360.0 );\r
250         sceneObjs[curObject].x += diffx * cos(angler) * leftrightFactor + diffy * cos(M_PI/2 + angler) * nearfarFactor;\r
251         sceneObjs[curObject].z += diffx * sin(angler) * leftrightFactor + diffy * sin(M_PI/2 + angler) * nearfarFactor;\r
252 \r
253       } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
254         // w: big/small\r
255         float max = (float)height/bigsmallFactor;\r
256         float scaling = (diffx + max) / max;\r
257 \r
258         sceneObjs[curObject].scale[0] *= scaling;\r
259         sceneObjs[curObject].scale[1] *= scaling;\r
260         sceneObjs[curObject].scale[2] *= scaling;\r
261 \r
262         // h: up/down\r
263         sceneObjs[curObject].y -= diffy * updownFactor;\r
264       }\r
265 \r
266       break;\r
267 \r
268     case STATE_OBJECT_ROTATION_TEXTURE_SCALE:\r
269 \r
270       if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
271         // w: rotate on y\r
272         sceneObjs[curObject].rotation.y += diffx * rotateFactor;\r
273 \r
274         // h: rotate on x\r
275         sceneObjs[curObject].rotation.x += diffy * rotateFactor;\r
276 \r
277       } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
278         // w: rotate on x\r
279         sceneObjs[curObject].rotation.x += diffx * rotateFactor;\r
280 \r
281         // h: texture scale\r
282         float max = (float)height/texscaleFactor;\r
283         float scaling = (-diffy + max) / max;\r
284         sceneObjs[curObject].texture.scale *= scaling;\r
285 \r
286       }\r
287       \r
288       break;\r
289 \r
290     case STATE_LIGHT_1_MOVE:\r
291     case STATE_LIGHT_2_MOVE:\r
292       ; // Semi-colon required to allow variable declaration below\r
293       \r
294       int i = 0;\r
295       if ( manipulateState == STATE_LIGHT_2_MOVE ) i = 1;\r
296 \r
297       if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
298         // w: left/right, h: near/far\r
299         float angler = 2 * M_PI * ( (rotate*camRotateFactor)/360.0 );\r
300         lightObjs[i].position[0] += diffx * cos(angler) * lleftrightFactor + diffy * cos(M_PI/2 + angler) * lnearfarFactor;\r
301         lightObjs[i].position[2] += diffx * sin(angler) * lleftrightFactor + diffy * sin(M_PI/2 + angler) * lnearfarFactor;\r
302 \r
303       } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
304         // w: increase/decrease some light param\r
305         // **NOTE: Currently not implemented\r
306 \r
307         // h: up/down\r
308         lightObjs[i].position[1] -= diffy * updownFactor;\r
309       }\r
310 \r
311       break;\r
312 \r
313   }\r
314 \r
315   starty = y;\r
316   startx = x;\r
317 \r
318   glutPostRedisplay();\r
319 }\r
320 \r
321 /**\r
322  * Display function\r
323  */\r
324 void display() {\r
325   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);\r
326   \r
327   // Redraw projection matrix\r
328   glMatrixMode(GL_PROJECTION);\r
329   glLoadIdentity();\r
330 \r
331   float aspect;\r
332   if ( width <= height ) {\r
333     aspect = (float)height / (float)width;\r
334   } else {\r
335     aspect = (float)width / (float)height;\r
336   }\r
337 \r
338   gluPerspective(\r
339     75.0,\r
340     aspect,\r
341     0.1,\r
342     300\r
343     );\r
344 \r
345   glMatrixMode(GL_MODELVIEW);\r
346   glLoadIdentity();\r
347 \r
348   gluLookAt(\r
349     0.0,  0.0,  15.0 + (zoom*zoomFactor),  /* eye is at (x,y,z) */\r
350     0.0,  0.0,  0.0,  /* center is at (x,y,z) */\r
351     0.0,  10.0,  0.0   /* up is in postivie Y direction */\r
352     );\r
353 \r
354   glRotatef(camAngle*camAngleFactor, 1.0, 0.0, 0.0); // Set camera angle upward\r
355 \r
356   glPushMatrix();\r
357 \r
358     /* Perform scene rotations based on user mouse/keyboard input. */\r
359     glRotatef(rotate*camRotateFactor, 0.0, 1.0, 0.0);\r
360     glTranslatef(camx, camy, camz);\r
361     glRotatef(keyrot, 1.0, 0.0, 0.0);\r
362 \r
363     drawFloor();\r
364     \r
365     // Draw sceneObjs array\r
366     for ( int i = 0; i < nObjects; i++ ) {\r
367       glPushMatrix();\r
368         SceneObject so = sceneObjs[i];\r
369 \r
370         // Apply translation vector\r
371         glTranslatef(so.x, so.y, so.z);\r
372 \r
373         // Apply independant rotation vectors\r
374         glRotatef(so.rotation.x, 1.0, 0.0, 0.0);\r
375         glRotatef(so.rotation.y, 0.0, 1.0, 0.0);\r
376         glRotatef(so.rotation.z, 0.0, 0.0, 1.0);\r
377 \r
378         // Apply scaling vector\r
379         glScalef(so.scale[0], so.scale[1], so.scale[2]);\r
380 \r
381         // Apply texture\r
382         if ( so.texture.id > 0 ) {\r
383           getTexture(so.texture.id);\r
384           glBindTexture(GL_TEXTURE_2D, so.texture.id);\r
385           glMatrixMode(GL_TEXTURE);\r
386           glScalef(so.texture.scale, so.texture.scale, so.texture.scale);\r
387           glMatrixMode(GL_MODELVIEW);\r
388         } else {\r
389           glBindTexture(GL_TEXTURE_2D, 0);\r
390         }\r
391 \r
392         // Draw actual object\r
393         if ( so.mesh > 0 ) {\r
394           // drawMesh();\r
395         } else if ( so.mesh == -1 ) { // a mesh of -1 draws the teapot\r
396           // The teapot does not obey the right-hand rule\r
397           glFrontFace(GL_CW);\r
398           glutSolidTeapot(1);\r
399           glFrontFace(GL_CCW);\r
400         }\r
401 \r
402         glBindTexture(GL_TEXTURE_2D, 0);\r
403       glPopMatrix();\r
404     }\r
405 \r
406     updateLights();\r
407 \r
408     drawAxisLines();\r
409 \r
410   glPopMatrix();\r
411 \r
412   glutSwapBuffers();\r
413 }\r
414 \r
415 /**\r
416  * Main function\r
417  * @param argc Number of arguments\r
418  * @param argv Array of arguments\r
419  * @return Program exit code\r
420  */\r
421 int main(int argc, char **argv) {\r
422   if(argc>1)\r
423     strcpy(dataDir, argv[1]);\r
424   else if(opendir(dirDefault1))\r
425     strcpy(dataDir, dirDefault1);\r
426   else if(opendir(dirDefault2))\r
427     strcpy(dataDir, dirDefault2);\r
428   else fileErr(dirDefault1);\r
429 \r
430   for(int i=0; i<NMESH; i++) meshes[i]=NULL;\r
431   for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;\r
432 \r
433   glutInit(&argc, argv);\r
434 \r
435   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);\r
436 \r
437   glutInitWindowSize(width, height);\r
438   glutCreateWindow("Scene Editor - Ashley Tyndall (20915779), Jenna de la Harpe (20367932)");\r
439 \r
440   glShadeModel(GL_SMOOTH); // Enables Smooth Shading\r
441   glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background\r
442   glClearDepth(1.0f); // Depth Buffer Setup\r
443   glDepthRange(0,1);\r
444   glEnable(GL_DEPTH_TEST); // Enables Depth Testing\r
445   glDepthFunc(GL_LEQUAL);  // the type\r
446   //glEnable(GL_CULL_FACE);\r
447   glLineWidth(1.0);\r
448 \r
449   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);\r
450 \r
451   glutReshapeFunc(windowReshape);\r
452   glutDisplayFunc(display);\r
453   glutMouseFunc(mouse);\r
454   glutMotionFunc(motion);\r
455 \r
456   makeMenu();\r
457   initializeLights();\r
458   initializeState();\r
459 \r
460   glutMainLoop();\r
461 }

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