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
248 printf("Camera is now at (%f, %f, %f), angle %f\n", camx, camy, camz, rot);
\r
249 glutPostRedisplay();
\r
253 * Called when motion event occurs
\r
254 * @param x Mouse x position
\r
255 * @param y Mouse y position
\r
257 void motion(int x, int y) {
\r
259 zoomFactor += (y - starty);
\r
260 rotateFactor += (x - startx);
\r
263 glutPostRedisplay();
\r
266 lightAngle += (x - lightStartX)/40.0;
\r
267 lightHeight += (lightStartY - y)/20.0;
\r
270 glutPostRedisplay();
\r
278 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
\r
280 // Redraw projection matrix
\r
281 glMatrixMode(GL_PROJECTION);
\r
285 if ( width <= height ) {
\r
286 aspect = (float)height / (float)width;
\r
288 aspect = (float)width / (float)height;
\r
298 glMatrixMode(GL_MODELVIEW);
\r
301 GLfloat zcoord = 5.0 + (zoomFactor*0.2);
\r
302 if ( zcoord < 0.0 ) {
\r
308 0.0, 0.0, zcoord, /* eye is at (x,y,z) */
\r
309 0.0, 0.0, 0.0, /* center is at (x,y,z) */
\r
310 0.0, 1.0, 0.0 /* up is in postivie Y direction */
\r
313 glRotatef(50.0, 1.0, 0.0, 0.0);
\r
315 /* Reposition the light source. */
\r
316 lightPosition[0] = 12*cos(lightAngle);
\r
317 lightPosition[1] = lightHeight;
\r
318 lightPosition[2] = 12*sin(lightAngle);
\r
319 lightPosition[3] = 0.0;
\r
322 //glRotatef(angle, 0.0, 1.0, 0.0);
\r
323 // glRotatef(angle2, 1.0, 0.0, 0.0);
\r
327 /* Perform scene rotations based on user mouse/keyboard input. */
\r
328 glRotatef(rotateFactor*0.5, 0.0, 1.0, 0.0);
\r
329 //glTranslatef(camx, camy, camz);
\r
330 //glRotatef(rot, 1.0, 0.0, 0.0);
\r
332 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
\r
338 // Draw teapot for a test object
\r
340 glTranslatef(0.0, 0.5, 0.0); // **NOTE: Teapot currently does not rest on surface
\r
341 glColor3f(0.0, 0.0, 0.0);
\r
342 glFrontFace(GL_CW);
\r
343 glutSolidTeapot(1);
\r
344 glFrontFace(GL_CCW);
\r
347 // Draw a white ball over the light source
\r
349 glDisable(GL_LIGHTING);
\r
350 glColor3f(1.0, 1.0, 0.0);
\r
351 glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);
\r
352 glutSolidSphere(1.0, 50, 50);
\r
353 glEnable(GL_LIGHTING);
\r
362 * init function; sets initial OpenGL state
\r
365 glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
\r
366 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
\r
367 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
\r
368 glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);
\r
369 glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction0);
\r
371 glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 90.0);
\r
373 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glightmodel);
\r
375 glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
\r
376 glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
\r
377 glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
\r
378 glMaterialfv(GL_FRONT, GL_EMISSION, emission0);
\r
379 glMaterialf(GL_FRONT, GL_SHININESS, shine);
\r
381 glEnable(GL_LIGHT0);
\r
382 glEnable(GL_LIGHTING);
\r
387 * @param argc Number of arguments
\r
388 * @param argv Array of arguments
\r
389 * @return Program exit code
\r
391 int main(int argc, char **argv) {
\r
393 strcpy(dataDir, argv[1]);
\r
394 else if(opendir(dirDefault1))
\r
395 strcpy(dataDir, dirDefault1);
\r
396 else if(opendir(dirDefault2))
\r
397 strcpy(dataDir, dirDefault2);
\r
398 else fileErr(dirDefault1);
\r
400 for(int i=0; i<NMESH; i++) meshes[i]=NULL;
\r
401 for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;
\r
403 glutInit(&argc, argv);
\r
405 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
\r
407 glutInitWindowSize(500, 500);
\r
408 glutCreateWindow("Scene Editor");
\r
410 glShadeModel(GL_SMOOTH); // Enables Smooth Shading
\r
411 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
\r
412 glClearDepth(1.0f); // Depth Buffer Setup
\r
414 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
\r
415 glDepthFunc(GL_LEQUAL); // the type
\r
416 glEnable(GL_TEXTURE_2D);
\r
417 glEnable(GL_CULL_FACE);
\r
418 glEnable(GL_NORMALIZE);
\r
421 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
\r
423 glutReshapeFunc(windowReshape);
\r
424 glutDisplayFunc(display);
\r
425 glutMouseFunc(mouse);
\r
426 glutKeyboardFunc(keyboard);
\r
427 glutMotionFunc(motion);
\r