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
187 /*glMatrixMode(GL_PROJECTION);
\r
190 glFrustum(near, far, near*(GLfloat)h/(GLfloat)w,
\r
191 far*(GLfloat)h/(GLfloat)w, -100, 100);
\r
193 glFrustum(near*(GLfloat)w/(GLfloat)h,
\r
194 far*(GLfloat)w/(GLfloat)h, near, far, nearClip, farClip);
\r
195 glMatrixMode(GL_MODELVIEW);
\r
196 glLoadIdentity();*/
\r
200 * Called when mouse event occurs
\r
201 * @param btn Mouse button
\r
202 * @param state State of mouse button
\r
203 * @param x Mouse x position
\r
204 * @param y Mouse y position
\r
206 void mouse(int button, int state, int x, int y) {
\r
207 if (button == GLUT_LEFT_BUTTON) {
\r
208 if (state == GLUT_DOWN) {
\r
213 if (state == GLUT_UP) {
\r
217 if (button == GLUT_MIDDLE_BUTTON) {
\r
218 if (state == GLUT_DOWN) {
\r
223 if (state == GLUT_UP) {
\r
230 * Keybord event handler
\r
231 * w/s increase/decrease the z
\r
232 * a/d increase/decrease the x
\r
233 * q/e increase/decrease the y
\r
234 * z/x increase/decrease the angle
\r
235 * @param key Key pressed
\r
236 * @param x x co-ordinate of mouse
\r
237 * @param y y co-ordinate of mouse
\r
239 void keyboard(unsigned char key, int x, int y) {
\r
266 printf("Camera is now at (%f, %f, %f), angle %f\n", camx, camy, camz, rot);
\r
267 glutPostRedisplay();
\r
271 * Called when motion event occurs
\r
272 * @param x Mouse x position
\r
273 * @param y Mouse y position
\r
275 void motion(int x, int y) {
\r
277 angle = angle + (x - startx);
\r
278 angle2 = angle2 + (y - starty);
\r
281 glutPostRedisplay();
\r
284 lightAngle += (x - lightStartX)/40.0;
\r
285 lightHeight += (lightStartY - y)/20.0;
\r
288 glutPostRedisplay();
\r
296 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
\r
299 // 0.0, 0.0, 30.0, /* eye is at (x,y,z) */
\r
300 // 0.0, 0.0, 0.0, /* center is at (x,y,z) */
\r
301 // 0.0, 1.0, 0.0 /* up is in postivie Y direction */
\r
304 glTranslatef(camx, camy, camz);
\r
306 // **NOTE: Currently this rotation function is all that moves the camera off
\r
307 // the flat surface. Need to integrate function into gluLookAt
\r
308 glRotatef(rot, 1.0, 0.0, 0.0);
\r
310 /* Reposition the light source. */
\r
311 lightPosition[0] = 12*cos(lightAngle);
\r
312 lightPosition[1] = lightHeight;
\r
313 lightPosition[2] = 12*sin(lightAngle);
\r
314 lightPosition[3] = 0.0;
\r
318 /* Perform scene rotations based on user mouse input. */
\r
319 glRotatef(angle, 0.0, 1.0, 0.0);
\r
320 glRotatef(angle2, 1.0, 0.0, 0.0); //**NOTE: Only one degree of freedom
\r
322 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
\r
328 // Draw teapot for a test object
\r
330 glTranslatef(0.0, 0.5, 0.0); // **NOTE: Teapot does not rest on surface
\r
331 glColor3f(0.0, 0.0, 0.0);
\r
332 glutSolidTeapot(1);
\r
335 // Draw a white ball over the light source
\r
337 glDisable(GL_LIGHTING);
\r
338 glColor3f(1.0, 1.0, 1.0);
\r
339 glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]);
\r
340 glutSolidSphere(1.0, 50, 50);
\r
341 glEnable(GL_LIGHTING);
\r
350 * init function; sets initial OpenGL state
\r
353 //glMatrixMode(GL_PROJECTION);
\r
354 //glLoadIdentity();
\r
357 // 60.0, /* field of view in degree */
\r
358 // 1.0, /* aspect ratio */
\r
359 // nearClip, /* Z near */
\r
360 // farClip /* Z far */
\r
363 glMatrixMode(GL_MODELVIEW);
\r
366 glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
\r
367 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
\r
368 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
\r
369 glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);
\r
370 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glightmodel);
\r
371 glEnable(GL_LIGHT0);
\r
372 glEnable(GL_LIGHTING);
\r
373 glEnable(GL_COLOR_MATERIAL);
\r
374 glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
\r
375 glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, specular0);
\r
376 glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, emission0);
\r
384 * @param argc Number of arguments
\r
385 * @param argv Array of arguments
\r
386 * @return Program exit code
\r
388 int main(int argc, char **argv) {
\r
390 strcpy(dataDir, argv[1]);
\r
391 else if(opendir(dirDefault1))
\r
392 strcpy(dataDir, dirDefault1);
\r
393 else if(opendir(dirDefault2))
\r
394 strcpy(dataDir, dirDefault2);
\r
395 else fileErr(dirDefault1);
\r
397 for(int i=0; i<NMESH; i++) meshes[i]=NULL;
\r
398 for(int i=0; i<NTEXTURE; i++) textures[i]=NULL;
\r
400 glutInit(&argc, argv);
\r
402 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
\r
404 glutInitWindowSize(500, 500);
\r
405 glutCreateWindow("Scene Editor");
\r
407 glShadeModel(GL_SMOOTH); // Enables Smooth Shading
\r
408 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
\r
409 glClearDepth(1.0f); // Depth Buffer Setup
\r
410 glEnable(GL_DEPTH_TEST); // Enables Depth Testing
\r
411 glDepthFunc(GL_LEQUAL); // the type
\r
412 glEnable(GL_CULL_FACE);
\r
413 glEnable(GL_TEXTURE_2D);
\r
416 glMatrixMode(GL_PROJECTION);
\r
418 40.0, /* field of view in degree */
\r
419 1.0, /* aspect ratio */
\r
424 glMatrixMode(GL_MODELVIEW);
\r
426 0.0, 35.0, -20.0, /* eye is at (0,8,60) */
\r
427 0.0, 10.0, 0.0, /* center is at (0,8,0) */
\r
428 0.0, 1.0, 0.0 /* up is in postivie Y direction */
\r
431 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
\r
433 glutReshapeFunc(windowReshape);
\r
434 glutDisplayFunc(display);
\r
435 glutMouseFunc(mouse);
\r
436 glutKeyboardFunc(keyboard);
\r
437 glutMotionFunc(motion);
\r