2 * CITS2231 Graphics Scene Editor
\r
3 * @author Ashley Tyndall (20915779)
\r
12 #include <GL/glut.h>
\r
16 #include "globals.h"
\r
22 * Event hander for main menu events
\r
23 * @param id ID of menu item selected
\r
25 void processMainEvents(int id) {
\r
27 case M_ROTATE_MOVE_CAMERA:
\r
28 manipulateState = STATE_CAMERA_ROTATE_MOVE;
\r
30 case M_POSITION_SCALE:
\r
31 manipulateState = STATE_OBJECT_POSITION_SCALE;
\r
33 case M_ROTATION_TEXTURE_SCALE:
\r
34 manipulateState = STATE_OBJECT_ROTATION_TEXTURE_SCALE;
\r
43 * Event hander for materials menu events
\r
44 * @param id ID of menu item selected
\r
46 void processMaterialEvents(int id) {
\r
48 case M_MATERIAL_ALL_RGB:
\r
52 case M_MATERIAL_AMBIENT_RGB:
\r
56 case M_MATERIAL_DIFFUSE_RGB:
\r
60 case M_MATERIAL_SPECULAR_RGB:
\r
64 case M_MATERIAL_ALL_ADSS:
\r
68 case M_MATERIAL_RED_ADSS:
\r
72 case M_MATERIAL_GREEN_ADSS:
\r
76 case M_MATERIAL_BLUE_ADSS:
\r
84 * Event hander for light menu events
\r
85 * @param id ID of menu item selected
\r
87 void processLightEvents(int id) {
\r
89 case M_LIGHT_MOVE_LIGHT_1:
\r
93 case M_LIGHT_RGBALL_LIGHT_1:
\r
97 case M_LIGHT_MOVE_LIGHT_2:
\r
101 case M_LIGHT_RGBALL_LIGHT_2:
\r
109 * Event hander for object menu events
\r
110 * @param id ID of object selected
\r
112 void processObjectEvents(int id) {
\r
113 // **NOTE: For the testing phase, only have the teapot
\r
114 addSceneObject(id);
\r
118 * Event hander for texture menu events
\r
119 * @param id ID of texutre selected
\r
121 void processTextureEvents(int id) {
\r
126 * Event hander for ground texture menu events
\r
127 * @param id ID of ground texture selected
\r
129 void processGTextureEvents(int id) {
\r
130 currentGroundTexture = id;
\r
131 glutPostRedisplay();
\r
135 * Creates menu for program
\r
138 // Construct material menu
\r
139 int materialMenu = glutCreateMenu(processMaterialEvents);
\r
140 glutAddMenuEntry("All R/G/B", M_MATERIAL_ALL_RGB);
\r
141 glutAddMenuEntry("Ambient R/G/B", M_MATERIAL_AMBIENT_RGB);
\r
142 glutAddMenuEntry("Diffuse R/G/B", M_MATERIAL_DIFFUSE_RGB);
\r
143 glutAddMenuEntry("Specular R/G/B", M_MATERIAL_SPECULAR_RGB);
\r
144 glutAddMenuEntry("All Amb/Diff/Spec/Shine", M_MATERIAL_ALL_ADSS);
\r
145 glutAddMenuEntry("Red Amb/Diff/Spec/Shine", M_MATERIAL_RED_ADSS);
\r
146 glutAddMenuEntry("Green Amb/Diff/Spec/Shine", M_MATERIAL_GREEN_ADSS);
\r
147 glutAddMenuEntry("Blue Amb/Diff/Spec/Shine", M_MATERIAL_BLUE_ADSS);
\r
149 // Construct light menu
\r
150 int lightMenu = glutCreateMenu(processLightEvents);
\r
151 glutAddMenuEntry("Move Light 1", M_LIGHT_MOVE_LIGHT_1);
\r
152 glutAddMenuEntry("R/G/B/All Light 1", M_LIGHT_RGBALL_LIGHT_1);
\r
153 glutAddMenuEntry("Move Light 2", M_LIGHT_MOVE_LIGHT_2);
\r
154 glutAddMenuEntry("R/G/B/All Light 2", M_LIGHT_RGBALL_LIGHT_2);
\r
156 // Construct object menu
\r
157 int objectMenu = makeSubmenuFromArray( objectMenuEntries, NMESH, processObjectEvents );
\r
159 // Construct texture / ground texture menus
\r
160 int textureMenu = makeSubmenuFromArray( textureMenuEntries, NTEXTURE, processTextureEvents );
\r
161 int gTextureMenu = makeSubmenuFromArray( textureMenuEntries, NTEXTURE, processGTextureEvents );
\r
163 // Construct main menu
\r
164 glutCreateMenu(processMainEvents);
\r
165 glutAddMenuEntry("Rotate/Move Camera", M_ROTATE_MOVE_CAMERA);
\r
166 glutAddSubMenu("Add object", objectMenu);
\r
167 glutAddMenuEntry("Position/Scale", M_POSITION_SCALE);
\r
168 glutAddMenuEntry("Rotation/Texture Scale", M_ROTATION_TEXTURE_SCALE);
\r
169 //glutAddSubMenu("Material", materialMenu);
\r
170 //glutAddSubMenu("Texture", textureMenu);
\r
171 glutAddSubMenu("Ground texture", gTextureMenu);
\r
172 //glutAddSubMenu("Lights", lightMenu);
\r
173 glutAddMenuEntry("Exit", M_EXIT);
\r
175 // Bind to right mouse button
\r
176 glutAttachMenu(GLUT_RIGHT_BUTTON);
\r
180 * Called when window is resized
\r
181 * @param w New width
\r
182 * @param h New height
\r
184 void windowReshape(int w, int h) {
\r
185 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
\r
191 * Called when mouse event occurs
\r
192 * @param btn Mouse button
\r
193 * @param state State of mouse button
\r
194 * @param x Mouse x position
\r
195 * @param y Mouse y position
\r
197 void mouse(int button, int state, int x, int y) {
\r
199 case GLUT_LEFT_BUTTON:
\r
200 case GLUT_MIDDLE_BUTTON:
\r
201 if ( state == GLUT_DOWN ) {
\r
202 buttonSelected = button;
\r
203 } else if ( state == GLUT_UP ) {
\r
204 buttonSelected = -1;
\r
213 * Keybord event handler
\r
214 * w/s increase/decrease the z
\r
215 * a/d increase/decrease the x
\r
216 * q/e increase/decrease the y
\r
217 * z/x increase/decrease the angle
\r
218 * @param key Key pressed
\r
219 * @param x x co-ordinate of mouse
\r
220 * @param y y co-ordinate of mouse
\r
222 void keyboard(unsigned char key, int x, int y) {
\r
259 printf("Factor of change is now %f\n", factor);
\r
264 printf("Factor of change is now %f\n", factor);
\r
268 printf("Camera is now at (%f, %f, %f), angle %f\n", camx, camy, camz, keyrot);
\r
269 glutPostRedisplay();
\r
273 * Called when motion event occurs
\r
274 * @param x Mouse x position
\r
275 * @param y Mouse y position
\r
277 void motion(int x, int y) {
\r
278 if ( buttonSelected == -1 ) return; // No button selected, no action
\r
280 switch ( manipulateState ) {
\r
281 case STATE_CAMERA_ROTATE_MOVE:
\r
282 rotate += (x - startx);
\r
284 if ( buttonSelected == GLUT_LEFT_BUTTON ) {
\r
285 // w: rotate, h: zoom
\r
286 zoom += (y - starty);
\r
287 } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {
\r
288 // w: rotate, h: tilt
\r
289 camAngle += (y - starty);
\r
297 case STATE_OBJECT_POSITION_SCALE:
\r
298 //SceneObject co = sceneObjs[curObject];
\r
300 if ( buttonSelected == GLUT_LEFT_BUTTON ) {
\r
301 // w: left/right, h: near/far
\r
303 } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {
\r
304 // w: big/small, h: up/down
\r
308 case STATE_OBJECT_ROTATION_TEXTURE_SCALE:
\r
309 if ( buttonSelected == GLUT_LEFT_BUTTON ) {
\r
310 // w: rotate on width, h: rotate on h
\r
311 } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {
\r
312 // w & h, rotate on h
\r
318 glutPostRedisplay();
\r
325 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
\r
327 // Redraw projection matrix
\r
328 glMatrixMode(GL_PROJECTION);
\r
332 if ( width <= height ) {
\r
333 aspect = (float)height / (float)width;
\r
335 aspect = (float)width / (float)height;
\r
345 glMatrixMode(GL_MODELVIEW);
\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
354 glRotatef(camAngle*camAngleFactor, 1.0, 0.0, 0.0);
\r
356 /* Reposition the light source 0. */
\r
357 lightPosition0[0] = 12*cos(lightAngle0);
\r
358 lightPosition0[1] = lightHeight0;
\r
359 lightPosition0[2] = 12*sin(lightAngle0);
\r
360 lightPosition0[3] = 0.0;
\r
362 direction0[0] = lightPosition0[0];
\r
363 direction0[2] = lightPosition0[2];
\r
365 /* Reposition the light source 1. */
\r
366 lightPosition1[0] = 12*cos(lightAngle1);
\r
367 lightPosition1[1] = lightHeight1;
\r
368 lightPosition1[2] = 12*sin(lightAngle1);
\r
369 lightPosition1[3] = 0.0;
\r
371 direction1[0] = lightPosition1[0];
\r
372 direction1[2] = lightPosition1[2];
\r
376 /* Perform scene rotations based on user mouse/keyboard input. */
\r
377 glRotatef(rotate*rotateFactor, 0.0, 1.0, 0.0);
\r
378 glTranslatef(camx, camy, camz);
\r
379 glRotatef(keyrot, 1.0, 0.0, 0.0);
\r
381 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition0);
\r
382 glLightfv(GL_LIGHT1, GL_POSITION, lightPosition1);
\r
386 // Draw sceneObjs array
\r
388 for ( int i = 0; i < nObjects; i++ ) {
\r
389 SceneObject so = sceneObjs[i];
\r
391 // Apply rotation vector
\r
392 /*vector* rv = so.rotation.vect;
\r
393 glRotatef(so.rotation.parameter, *rv[0], *rv[1], *rv[2]);
\r
395 // Apply scaling vector
\r
396 vector* sv = so.scale;
\r
397 glScalef(*sv[0], *sv[1], *sv[2]);
\r
399 // Apply translation vector
\r
400 glTranslatef(so.x, so.y, so.z);
\r
403 if ( so.texture > 0 ) {
\r
404 getTexture(so.texture);
\r
405 glBindTexture(GL_TEXTURE_2D, so.texture);
\r
407 glBindTexture(GL_TEXTURE_2D, 0);
\r
410 // Draw actual object
\r
411 if ( so.mesh > 0 ) {
\r
413 } else if ( so.mesh == -1 ) { // a mesh of -1 draws the teapot
\r
414 // The teapot does not obey the right-hand rule
\r
415 glFrontFace(GL_CW);
\r
416 glutSolidTeapot(1);
\r
417 glFrontFace(GL_CCW);
\r
420 glBindTexture(GL_TEXTURE_2D, 0);
\r
424 // Draw a white ball over the light source
\r
426 glDisable(GL_LIGHTING);
\r
427 glColor3f(1.0, 1.0, 1.0);
\r
428 glTranslatef(lightPosition0[0], lightPosition0[1], lightPosition0[2]);
\r
429 glutSolidSphere(0.5, 50, 50);
\r
430 glEnable(GL_LIGHTING);
\r
433 // Draw a white ball over the light source
\r
435 glDisable(GL_LIGHTING);
\r
436 glColor3f(1.0, 1.0, 1.0);
\r
437 glTranslatef(lightPosition1[0], lightPosition1[1], lightPosition1[2]);
\r
438 glutSolidSphere(0.5, 50, 50);
\r
439 glEnable(GL_LIGHTING);
\r
450 * init function; sets initial OpenGL state
\r
453 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
\r
454 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
\r
455 glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);
\r
456 glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction0);
\r
458 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
\r
459 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
\r
460 glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);
\r
461 glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, direction1);
\r
463 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glightmodel);
\r
464 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
\r
466 glEnable(GL_LIGHT0);
\r
467 glEnable(GL_LIGHT1);
\r
468 glEnable(GL_LIGHTING);
\r
473 * @param argc Number of arguments
\r
474 * @param argv Array of arguments
\r
475 * @return Program exit code
\r
477 int main(int argc, char **argv) {
\r
479 strcpy(dataDir, argv[1]);
\r
480 else if(opendir(dirDefault1))
\r
481 strcpy(dataDir, dirDefault1);
\r
482 else if(opendir(dirDefault2))
\r
483 strcpy(dataDir, dirDefault2);
\r
484 else fileErr(dirDefault1);
\r
486 for(int i=0; i<NMESH; i++) meshes[i]=NULL;
\r
487 for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;
\r
489 glutInit(&argc, argv);
\r
491 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
\r
493 glutInitWindowSize(500, 500);
\r
494 glutCreateWindow("Scene Editor");
\r
496 glShadeModel(GL_SMOOTH); // Enables Smooth Shading
\r
497 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
\r
498 glClearDepth(1.0f); // Depth Buffer Setup
\r
500 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
\r
501 glDepthFunc(GL_LEQUAL); // the type
\r
502 glEnable(GL_NORMALIZE);
\r
505 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
\r
507 glutReshapeFunc(windowReshape);
\r
508 glutDisplayFunc(display);
\r
509 glutMouseFunc(mouse);
\r
510 glutKeyboardFunc(keyboard);
\r
511 glutMotionFunc(motion);
\r