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
31 case M_POSITION_SCALE:
\r
35 case M_ROTATION_TEXTURE_SCALE:
\r
46 * Event hander for materials menu events
\r
47 * @param id ID of menu item selected
\r
49 void processMaterialEvents(int id) {
\r
51 case M_MATERIAL_ALL_RGB:
\r
55 case M_MATERIAL_AMBIENT_RGB:
\r
59 case M_MATERIAL_DIFFUSE_RGB:
\r
63 case M_MATERIAL_SPECULAR_RGB:
\r
67 case M_MATERIAL_ALL_ADSS:
\r
71 case M_MATERIAL_RED_ADSS:
\r
75 case M_MATERIAL_GREEN_ADSS:
\r
79 case M_MATERIAL_BLUE_ADSS:
\r
87 * Event hander for light menu events
\r
88 * @param id ID of menu item selected
\r
90 void processLightEvents(int id) {
\r
92 case M_LIGHT_MOVE_LIGHT_1:
\r
96 case M_LIGHT_RGBALL_LIGHT_1:
\r
100 case M_LIGHT_MOVE_LIGHT_2:
\r
104 case M_LIGHT_RGBALL_LIGHT_2:
\r
112 * Event hander for object menu events
\r
113 * @param id ID of object selected
\r
115 void processObjectEvents(int id) {
\r
120 * Event hander for texture menu events
\r
121 * @param id ID of texutre selected
\r
123 void processTextureEvents(int id) {
\r
128 * Event hander for ground texture menu events
\r
129 * @param id ID of ground texture selected
\r
131 void processGTextureEvents(int id) {
\r
136 * Creates menu for program
\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
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
157 // Construct object menu
\r
158 int objectMenu = makeSubmenuFromArray( objectMenuEntries, NMESH, processObjectEvents );
\r
160 // Construct texture / ground texture menus
\r
161 int textureMenu = makeSubmenuFromArray( textureMenuEntries, NTEXTURE, processTextureEvents );
\r
162 int gTextureMenu = makeSubmenuFromArray( textureMenuEntries, NTEXTURE, processGTextureEvents );
\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
176 // Bind to right mouse button
\r
177 glutAttachMenu(GLUT_RIGHT_BUTTON);
\r
181 * Called when window is resized
\r
182 * @param w New width
\r
183 * @param h New height
\r
185 void windowReshape(int w, int h) {
\r
186 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
\r
192 * Called when mouse event occurs
\r
193 * @param btn Mouse button
\r
194 * @param state State of mouse button
\r
195 * @param x Mouse x position
\r
196 * @param y Mouse y position
\r
198 void mouse(int button, int state, int x, int y) {
\r
199 if (button == GLUT_LEFT_BUTTON) {
\r
200 if (state == GLUT_DOWN) {
\r
205 if (state == GLUT_UP) {
\r
212 * Keybord event handler
\r
213 * w/s increase/decrease the z
\r
214 * a/d increase/decrease the x
\r
215 * q/e increase/decrease the y
\r
216 * z/x increase/decrease the angle
\r
217 * @param key Key pressed
\r
218 * @param x x co-ordinate of mouse
\r
219 * @param y y co-ordinate of mouse
\r
221 void keyboard(unsigned char key, int x, int y) {
\r
258 printf("Factor of change is now %f\n", factor);
\r
263 printf("Factor of change is now %f\n", factor);
\r
267 printf("Camera is now at (%f, %f, %f), angle %f\n", camx, camy, camz, keyrot);
\r
268 glutPostRedisplay();
\r
272 * Called when motion event occurs
\r
273 * @param x Mouse x position
\r
274 * @param y Mouse y position
\r
276 void motion(int x, int y) {
\r
278 zoom += (y - starty);
\r
279 rotate += (x - startx);
\r
282 glutPostRedisplay();
\r
285 lightAngle += (x - lightStartX)/40.0;
\r
286 lightHeight += (lightStartY - y)/20.0;
\r
289 glutPostRedisplay();
\r
297 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
\r
299 // Redraw projection matrix
\r
300 glMatrixMode(GL_PROJECTION);
\r
304 if ( width <= height ) {
\r
305 aspect = (float)height / (float)width;
\r
307 aspect = (float)width / (float)height;
\r
317 glMatrixMode(GL_MODELVIEW);
\r
321 0.0, 0.0, 5.0 + (zoom*zoomFactor), /* eye is at (x,y,z) */
\r
322 0.0, 0.0, 0.0, /* center is at (x,y,z) */
\r
323 0.0, 1.0, 0.0 /* up is in postivie Y direction */
\r
326 glRotatef(50.0, 1.0, 0.0, 0.0);
\r
328 /* Reposition the light source. */
\r
329 lightPosition[0] = 12*cos(lightAngle);
\r
330 lightPosition[1] = lightHeight;
\r
331 lightPosition[2] = 12*sin(lightAngle);
\r
332 lightPosition[3] = 0.0;
\r
336 /* Perform scene rotations based on user mouse/keyboard input. */
\r
337 glRotatef(rotate*rotateFactor, 0.0, 1.0, 0.0);
\r
338 glTranslatef(camx, camy, camz);
\r
339 glRotatef(keyrot, 1.0, 0.0, 0.0);
\r
341 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
\r
347 // Draw teapot for a test object
\r
349 glTranslatef(0.0, 0.5, 0.0); // **NOTE: Teapot currently does not rest on surface
\r
350 glColor3f(0.0, 0.0, 0.0);
\r
351 glFrontFace(GL_CW);
\r
352 glutSolidTeapot(1);
\r
353 glFrontFace(GL_CCW);
\r
356 // Draw a white ball over the light source
\r
358 glDisable(GL_LIGHTING);
\r
359 glColor3f(1.0, 1.0, 0.0);
\r
360 glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);
\r
361 glutSolidSphere(1.0, 50, 50);
\r
362 glEnable(GL_LIGHTING);
\r
371 * init function; sets initial OpenGL state
\r
374 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
\r
375 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
\r
376 glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);
\r
377 glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction0);
\r
379 glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 90.0);
\r
381 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glightmodel);
\r
383 glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
\r
384 glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
\r
385 glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
\r
386 glMaterialfv(GL_FRONT, GL_EMISSION, emission0);
\r
387 glMaterialf(GL_FRONT, GL_SHININESS, shine);
\r
389 glEnable(GL_LIGHT0);
\r
390 glEnable(GL_LIGHTING);
\r
395 * @param argc Number of arguments
\r
396 * @param argv Array of arguments
\r
397 * @return Program exit code
\r
399 int main(int argc, char **argv) {
\r
401 strcpy(dataDir, argv[1]);
\r
402 else if(opendir(dirDefault1))
\r
403 strcpy(dataDir, dirDefault1);
\r
404 else if(opendir(dirDefault2))
\r
405 strcpy(dataDir, dirDefault2);
\r
406 else fileErr(dirDefault1);
\r
408 for(int i=0; i<NMESH; i++) meshes[i]=NULL;
\r
409 for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;
\r
411 glutInit(&argc, argv);
\r
413 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
\r
415 glutInitWindowSize(500, 500);
\r
416 glutCreateWindow("Scene Editor");
\r
418 glShadeModel(GL_SMOOTH); // Enables Smooth Shading
\r
419 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
\r
420 glClearDepth(1.0f); // Depth Buffer Setup
\r
422 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
\r
423 glDepthFunc(GL_LEQUAL); // the type
\r
424 glEnable(GL_TEXTURE_2D);
\r
425 glEnable(GL_CULL_FACE);
\r
426 glEnable(GL_NORMALIZE);
\r
429 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
\r
431 glutReshapeFunc(windowReshape);
\r
432 glutDisplayFunc(display);
\r
433 glutMouseFunc(mouse);
\r
434 glutKeyboardFunc(keyboard);
\r
435 glutMotionFunc(motion);
\r