2 * CITS2231 Graphics Scene Editor
\r
3 * @author Ashley Tyndall (20915779), Jenna de la Harpe (20367932)
\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 addSceneObject(id);
\r
114 manipulateState = STATE_OBJECT_POSITION_SCALE;
\r
115 glutPostRedisplay();
\r
119 * Event hander for texture menu events
\r
120 * @param id ID of texutre selected
\r
122 void processTextureEvents(int id) {
\r
123 if ( curObject >= 0 ) {
\r
124 sceneObjs[curObject].texture.id = id;
\r
125 glutPostRedisplay();
\r
130 * Event hander for ground texture menu events
\r
131 * @param id ID of ground texture selected
\r
133 void processGTextureEvents(int id) {
\r
134 currentGroundTexture = id;
\r
135 glutPostRedisplay();
\r
139 * Creates menu for program
\r
142 // Construct material menu
\r
143 int materialMenu = glutCreateMenu(processMaterialEvents);
\r
144 glutAddMenuEntry("All R/G/B", M_MATERIAL_ALL_RGB);
\r
145 glutAddMenuEntry("Ambient R/G/B", M_MATERIAL_AMBIENT_RGB);
\r
146 glutAddMenuEntry("Diffuse R/G/B", M_MATERIAL_DIFFUSE_RGB);
\r
147 glutAddMenuEntry("Specular R/G/B", M_MATERIAL_SPECULAR_RGB);
\r
148 glutAddMenuEntry("All Amb/Diff/Spec/Shine", M_MATERIAL_ALL_ADSS);
\r
149 glutAddMenuEntry("Red Amb/Diff/Spec/Shine", M_MATERIAL_RED_ADSS);
\r
150 glutAddMenuEntry("Green Amb/Diff/Spec/Shine", M_MATERIAL_GREEN_ADSS);
\r
151 glutAddMenuEntry("Blue Amb/Diff/Spec/Shine", M_MATERIAL_BLUE_ADSS);
\r
153 // Construct light menu
\r
154 int lightMenu = glutCreateMenu(processLightEvents);
\r
155 glutAddMenuEntry("Move Light 1", M_LIGHT_MOVE_LIGHT_1);
\r
156 glutAddMenuEntry("R/G/B/All Light 1", M_LIGHT_RGBALL_LIGHT_1);
\r
157 glutAddMenuEntry("Move Light 2", M_LIGHT_MOVE_LIGHT_2);
\r
158 glutAddMenuEntry("R/G/B/All Light 2", M_LIGHT_RGBALL_LIGHT_2);
\r
160 // Construct object menu
\r
161 int objectMenu = makeSubmenuFromArray( objectMenuEntries, NMESH, processObjectEvents );
\r
163 // Construct texture / ground texture menus
\r
164 int textureMenu = makeSubmenuFromArray( textureMenuEntries, NTEXTURE, processTextureEvents );
\r
165 int gTextureMenu = makeSubmenuFromArray( textureMenuEntries, NTEXTURE, processGTextureEvents );
\r
167 // Construct main menu
\r
168 glutCreateMenu(processMainEvents);
\r
169 glutAddMenuEntry("Rotate/Move Camera", M_ROTATE_MOVE_CAMERA);
\r
170 glutAddSubMenu("Add object", objectMenu);
\r
171 glutAddMenuEntry("Position/Scale", M_POSITION_SCALE);
\r
172 glutAddMenuEntry("Rotation/Texture Scale", M_ROTATION_TEXTURE_SCALE);
\r
173 //glutAddSubMenu("Material", materialMenu);
\r
174 glutAddSubMenu("Texture", textureMenu);
\r
175 glutAddSubMenu("Ground texture", gTextureMenu);
\r
176 //glutAddSubMenu("Lights", lightMenu);
\r
177 glutAddMenuEntry("Exit", M_EXIT);
\r
179 // Bind to right mouse button
\r
180 glutAttachMenu(GLUT_RIGHT_BUTTON);
\r
184 * Called when window is resized
\r
185 * @param w New width
\r
186 * @param h New height
\r
188 void windowReshape(int w, int h) {
\r
189 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
\r
195 * Called when mouse event occurs
\r
196 * @param btn Mouse button
\r
197 * @param state State of mouse button
\r
198 * @param x Mouse x position
\r
199 * @param y Mouse y position
\r
201 void mouse(int button, int state, int x, int y) {
\r
202 if ( button == GLUT_LEFT_BUTTON && glutGetModifiers() == GLUT_ACTIVE_SHIFT ) {
\r
203 button = GLUT_MIDDLE_BUTTON; // Holding shift with left button is the same as the middle button
\r
206 case GLUT_LEFT_BUTTON:
\r
207 case GLUT_MIDDLE_BUTTON:
\r
208 if ( state == GLUT_DOWN ) {
\r
209 buttonSelected = button;
\r
210 } else if ( state == GLUT_UP ) {
\r
211 buttonSelected = -1;
\r
220 * Keybord event handler
\r
221 * w/s increase/decrease the z
\r
222 * a/d increase/decrease the x
\r
223 * q/e increase/decrease the y
\r
224 * z/x increase/decrease the angle
\r
225 * @param key Key pressed
\r
226 * @param x x co-ordinate of mouse
\r
227 * @param y y co-ordinate of mouse
\r
229 void keyboard(unsigned char key, int x, int y) {
\r
266 printf("Factor of change is now %f\n", factor);
\r
271 printf("Factor of change is now %f\n", factor);
\r
275 printf("Camera is now at (%f, %f, %f), angle %f\n", camx, camy, camz, keyrot);
\r
276 glutPostRedisplay();
\r
280 * Called when motion event occurs
\r
281 * @param x Mouse x position
\r
282 * @param y Mouse y position
\r
284 void motion(int x, int y) {
\r
285 if ( buttonSelected == -1 ) return; // No button selected, no action
\r
287 float diffx = x - startx;
\r
288 float diffy = y - starty;
\r
290 switch ( manipulateState ) {
\r
291 case STATE_CAMERA_ROTATE_MOVE:
\r
295 if ( buttonSelected == GLUT_LEFT_BUTTON ) {
\r
298 } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {
\r
304 case STATE_OBJECT_POSITION_SCALE:
\r
306 if ( buttonSelected == GLUT_LEFT_BUTTON ) {
\r
307 // w: left/right, h: near/far
\r
309 // **NOTE: Currently a work in progress, does not work correctly
\r
310 printf("cam angle: %f\n", rotate*camRotateFactor);
\r
311 // float arc = arctan()
\r
312 sceneObjs[curObject].x += diffx * sin(rotate*camRotateFactor) * 0.3;
\r
313 sceneObjs[curObject].z += diffx * cos(rotate*camRotateFactor) * 0.3;
\r
315 } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {
\r
317 float max = (float)height/bigsmallFactor;
\r
318 float scaling = (diffx + max) / max;
\r
320 sceneObjs[curObject].scale[0] *= scaling;
\r
321 sceneObjs[curObject].scale[1] *= scaling;
\r
322 sceneObjs[curObject].scale[2] *= scaling;
\r
325 sceneObjs[curObject].y -= diffy * updownFactor;
\r
330 case STATE_OBJECT_ROTATION_TEXTURE_SCALE:
\r
332 if ( buttonSelected == GLUT_LEFT_BUTTON ) {
\r
334 sceneObjs[curObject].rotation.y += diffx * rotateFactor;
\r
337 sceneObjs[curObject].rotation.x += diffy * rotateFactor;
\r
339 } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {
\r
341 sceneObjs[curObject].rotation.x += diffx * rotateFactor;
\r
343 // h: texture scale
\r
344 float max = (float)height/texscaleFactor;
\r
345 float scaling = (diffy + max) / max;
\r
346 sceneObjs[curObject].texture.scale *= scaling;
\r
357 glutPostRedisplay();
\r
364 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
\r
366 // Redraw projection matrix
\r
367 glMatrixMode(GL_PROJECTION);
\r
371 if ( width <= height ) {
\r
372 aspect = (float)height / (float)width;
\r
374 aspect = (float)width / (float)height;
\r
384 glMatrixMode(GL_MODELVIEW);
\r
388 0.0, 0.0, 15.0 + (zoom*zoomFactor), /* eye is at (x,y,z) */
\r
389 0.0, 0.0, 0.0, /* center is at (x,y,z) */
\r
390 0.0, 10.0, 0.0 /* up is in postivie Y direction */
\r
393 glRotatef(camAngle*camAngleFactor, 1.0, 0.0, 0.0); // Set camera angle upward
\r
395 /* Reposition the light source 0. */
\r
396 lightPosition0[0] = 12*cos(lightAngle0);
\r
397 lightPosition0[1] = lightHeight0;
\r
398 lightPosition0[2] = 12*sin(lightAngle0);
\r
399 lightPosition0[3] = 0.0;
\r
401 direction0[0] = lightPosition0[0];
\r
402 direction0[2] = lightPosition0[2];
\r
404 /* Reposition the light source 1. */
\r
405 lightPosition1[0] = 12*cos(lightAngle1);
\r
406 lightPosition1[1] = lightHeight1;
\r
407 lightPosition1[2] = 12*sin(lightAngle1);
\r
408 lightPosition1[3] = 0.0;
\r
410 direction1[0] = lightPosition1[0];
\r
411 direction1[2] = lightPosition1[2];
\r
415 /* Perform scene rotations based on user mouse/keyboard input. */
\r
416 glRotatef(rotate*camRotateFactor, 0.0, 1.0, 0.0);
\r
417 glTranslatef(camx, camy, camz);
\r
418 glRotatef(keyrot, 1.0, 0.0, 0.0);
\r
420 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition0);
\r
421 glLightfv(GL_LIGHT1, GL_POSITION, lightPosition1);
\r
425 // Draw sceneObjs array
\r
426 for ( int i = 0; i < nObjects; i++ ) {
\r
428 SceneObject so = sceneObjs[i];
\r
430 // Apply translation vector
\r
431 glTranslatef(so.x, so.y, so.z);
\r
433 // Apply independant rotation vectors
\r
434 glRotatef(so.rotation.x, 1.0, 0.0, 0.0);
\r
435 glRotatef(so.rotation.y, 0.0, 1.0, 0.0);
\r
436 glRotatef(so.rotation.z, 0.0, 0.0, 1.0);
\r
438 // Apply scaling vector
\r
439 glScalef(so.scale[0], so.scale[1], so.scale[2]);
\r
442 if ( so.texture.id > 0 ) {
\r
443 getTexture(so.texture.id);
\r
444 glBindTexture(GL_TEXTURE_2D, so.texture.id);
\r
445 glMatrixMode(GL_TEXTURE);
\r
446 glScalef(so.texture.scale, so.texture.scale, so.texture.scale);
\r
447 glMatrixMode(GL_MODELVIEW);
\r
449 glBindTexture(GL_TEXTURE_2D, 0);
\r
452 // Draw actual object
\r
453 if ( so.mesh > 0 ) {
\r
455 } else if ( so.mesh == -1 ) { // a mesh of -1 draws the teapot
\r
456 // The teapot does not obey the right-hand rule
\r
457 glFrontFace(GL_CW);
\r
458 glutSolidTeapot(1);
\r
459 glFrontFace(GL_CCW);
\r
462 glBindTexture(GL_TEXTURE_2D, 0);
\r
466 // Draw a white ball over the light sources
\r
467 glDisable(GL_LIGHTING);
\r
468 glColor3f(1.0, 1.0, 1.0);
\r
471 glTranslatef(lightPosition0[0], lightPosition0[1], lightPosition0[2]);
\r
472 glutSolidSphere(0.5, 50, 50);
\r
476 glTranslatef(lightPosition1[0], lightPosition1[1], lightPosition1[2]);
\r
477 glutSolidSphere(0.5, 50, 50);
\r
480 glEnable(GL_LIGHTING);
\r
490 * init function; sets initial OpenGL state
\r
493 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
\r
494 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
\r
495 glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);
\r
496 glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction0);
\r
498 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
\r
499 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
\r
500 glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);
\r
501 glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, direction1);
\r
503 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glightmodel);
\r
504 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
\r
506 glEnable(GL_LIGHT0);
\r
507 glEnable(GL_LIGHT1);
\r
508 glEnable(GL_LIGHTING);
\r
513 * @param argc Number of arguments
\r
514 * @param argv Array of arguments
\r
515 * @return Program exit code
\r
517 int main(int argc, char **argv) {
\r
519 strcpy(dataDir, argv[1]);
\r
520 else if(opendir(dirDefault1))
\r
521 strcpy(dataDir, dirDefault1);
\r
522 else if(opendir(dirDefault2))
\r
523 strcpy(dataDir, dirDefault2);
\r
524 else fileErr(dirDefault1);
\r
526 for(int i=0; i<NMESH; i++) meshes[i]=NULL;
\r
527 for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;
\r
529 glutInit(&argc, argv);
\r
531 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
\r
533 glutInitWindowSize(width, height);
\r
534 glutCreateWindow("Scene Editor - Ashley Tyndall (20915779), Jenna de la Harpe (20367932)");
\r
536 glShadeModel(GL_SMOOTH); // Enables Smooth Shading
\r
537 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
\r
538 glClearDepth(1.0f); // Depth Buffer Setup
\r
540 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
\r
541 glDepthFunc(GL_LEQUAL); // the type
\r
542 glEnable(GL_NORMALIZE);
\r
545 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
\r
547 glutReshapeFunc(windowReshape);
\r
548 glutDisplayFunc(display);
\r
549 glutMouseFunc(mouse);
\r
550 glutKeyboardFunc(keyboard);
\r
551 glutMotionFunc(motion);
\r