Remove referenes to keyboard debugging function. Electing not to use glColorMaterial...
[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         // **NOTE: Seems to be problem with scaling, delayed action\r
283         float max = (float)height/texscaleFactor;\r
284         float scaling = (-diffy + max) / max;\r
285         sceneObjs[curObject].texture.scale *= scaling;\r
286 \r
287       }\r
288       \r
289       break;\r
290 \r
291     case STATE_LIGHT_1_MOVE:\r
292     case STATE_LIGHT_2_MOVE:\r
293       ; // Semi-colon required to allow variable declaration below\r
294       \r
295       int i = 0;\r
296       if ( manipulateState == STATE_LIGHT_2_MOVE ) i = 1;\r
297 \r
298       if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
299         // w: left/right, h: near/far\r
300         float angler = 2 * M_PI * ( (rotate*camRotateFactor)/360.0 );\r
301         lightObjs[i].position[0] += diffx * cos(angler) * lleftrightFactor + diffy * cos(M_PI/2 + angler) * lnearfarFactor;\r
302         lightObjs[i].position[2] += diffx * sin(angler) * lleftrightFactor + diffy * sin(M_PI/2 + angler) * lnearfarFactor;\r
303 \r
304       } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
305         // w: increase/decrease some light param\r
306         // **NOTE: Currently not implemented\r
307 \r
308         // h: up/down\r
309         lightObjs[i].position[1] -= diffy * updownFactor;\r
310       }\r
311 \r
312       break;\r
313 \r
314   }\r
315 \r
316   starty = y;\r
317   startx = x;\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); // Set camera angle upward\r
356 \r
357   glPushMatrix();\r
358 \r
359     /* Perform scene rotations based on user mouse input. */\r
360     glRotatef(rotate*camRotateFactor, 0.0, 1.0, 0.0);\r
361 \r
362     drawFloor();\r
363     \r
364     // Draw sceneObjs array\r
365     for ( int i = 0; i < nObjects; i++ ) {\r
366       glPushMatrix();\r
367         SceneObject so = sceneObjs[i];\r
368 \r
369         // Apply translation vector\r
370         glTranslatef(so.x, so.y, so.z);\r
371 \r
372         // Apply independant rotation vectors\r
373         glRotatef(so.rotation.x, 1.0, 0.0, 0.0);\r
374         glRotatef(so.rotation.y, 0.0, 1.0, 0.0);\r
375         glRotatef(so.rotation.z, 0.0, 0.0, 1.0);\r
376 \r
377         // Apply scaling vector\r
378         glScalef(so.scale[0], so.scale[1], so.scale[2]);\r
379 \r
380         // Apply texture\r
381         if ( so.texture.id > 0 ) {\r
382           getTexture(so.texture.id);\r
383           glBindTexture(GL_TEXTURE_2D, so.texture.id);\r
384           glMatrixMode(GL_TEXTURE);\r
385           glScalef(so.texture.scale, so.texture.scale, so.texture.scale);\r
386           glMatrixMode(GL_MODELVIEW);\r
387         } else {\r
388           glBindTexture(GL_TEXTURE_2D, 0);\r
389         }\r
390 \r
391         // Draw actual object\r
392         if ( so.mesh > 0 ) {\r
393           // drawMesh();\r
394         } else if ( so.mesh == -1 ) { // a mesh of -1 draws the teapot\r
395           // The teapot does not obey the right-hand rule\r
396           glFrontFace(GL_CW);\r
397           glutSolidTeapot(1);\r
398           glFrontFace(GL_CCW);\r
399         }\r
400 \r
401         glBindTexture(GL_TEXTURE_2D, 0);\r
402       glPopMatrix();\r
403     }\r
404 \r
405     updateLights();\r
406 \r
407     drawAxisLines();\r
408 \r
409   glPopMatrix();\r
410 \r
411   glutSwapBuffers();\r
412 }\r
413 \r
414 /**\r
415  * Main function\r
416  * @param argc Number of arguments\r
417  * @param argv Array of arguments\r
418  * @return Program exit code\r
419  */\r
420 int main(int argc, char **argv) {\r
421   if(argc>1)\r
422     strcpy(dataDir, argv[1]);\r
423   else if(opendir(dirDefault1))\r
424     strcpy(dataDir, dirDefault1);\r
425   else if(opendir(dirDefault2))\r
426     strcpy(dataDir, dirDefault2);\r
427   else fileErr(dirDefault1);\r
428 \r
429   for(int i=0; i<NMESH; i++) meshes[i]=NULL;\r
430   for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;\r
431 \r
432   glutInit(&argc, argv);\r
433 \r
434   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);\r
435 \r
436   glutInitWindowSize(width, height);\r
437   glutCreateWindow("Scene Editor - Ashley Tyndall (20915779), Jenna de la Harpe (20367932)");\r
438 \r
439   glShadeModel(GL_SMOOTH); // Enables Smooth Shading\r
440   glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background\r
441   glClearDepth(1.0f); // Depth Buffer Setup\r
442   glDepthRange(0,1);\r
443   glEnable(GL_DEPTH_TEST); // Enables Depth Testing\r
444   glDepthFunc(GL_LEQUAL);  // the type\r
445   //glEnable(GL_CULL_FACE);\r
446   glLineWidth(1.0);\r
447 \r
448   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);\r
449 \r
450   glutReshapeFunc(windowReshape);\r
451   glutDisplayFunc(display);\r
452   glutMouseFunc(mouse);\r
453   glutMotionFunc(motion);\r
454 \r
455   makeMenu();\r
456   initializeLights();\r
457   initializeState();\r
458 \r
459   glutMainLoop();\r
460 }

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