/**\r
* CITS2231 Graphics Scene Editor\r
- * @author Ashley Tyndall (20915779)\r
+ * @author Ashley Tyndall (20915779), Jenna de la Harpe (20367932)\r
*/\r
\r
#include <stdlib.h>\r
void processMainEvents(int id) {\r
switch (id) {\r
case M_ROTATE_MOVE_CAMERA:\r
- // Do stuff\r
+ manipulateState = STATE_CAMERA_ROTATE_MOVE;\r
break;\r
-\r
case M_POSITION_SCALE:\r
- // Do stuff\r
+ manipulateState = STATE_OBJECT_POSITION_SCALE;\r
break;\r
-\r
case M_ROTATION_TEXTURE_SCALE:\r
- // Do stuff\r
+ manipulateState = STATE_OBJECT_ROTATION_TEXTURE_SCALE;\r
break;\r
-\r
case M_EXIT:\r
exit(EXIT_SUCCESS);\r
\r
* @param id ID of object selected\r
*/\r
void processObjectEvents(int id) {\r
-\r
+ addSceneObject(id);\r
+ manipulateState = STATE_OBJECT_POSITION_SCALE;\r
+ glutPostRedisplay();\r
}\r
\r
/**\r
* @param id ID of texutre selected\r
*/\r
void processTextureEvents(int id) {\r
-\r
+ if ( curObject >= 0 ) {\r
+ sceneObjs[curObject].texture = id;\r
+ glutPostRedisplay();\r
+ }\r
}\r
\r
/**\r
\r
// Construct main menu\r
glutCreateMenu(processMainEvents);\r
- //glutAddMenuEntry("Rotate/Move Camera", M_ROTATE_MOVE_CAMERA);\r
- //glutAddSubMenu("Add object", objectMenu);\r
- //glutAddMenuEntry("Position/Scale", M_POSITION_SCALE);\r
- //glutAddMenuEntry("Rotation/Texture Scale", M_ROTATION_TEXTURE_SCALE);\r
+ glutAddMenuEntry("Rotate/Move Camera", M_ROTATE_MOVE_CAMERA);\r
+ glutAddSubMenu("Add object", objectMenu);\r
+ glutAddMenuEntry("Position/Scale", M_POSITION_SCALE);\r
+ glutAddMenuEntry("Rotation/Texture Scale", M_ROTATION_TEXTURE_SCALE);\r
//glutAddSubMenu("Material", materialMenu);\r
- //glutAddSubMenu("Texture", textureMenu);\r
+ glutAddSubMenu("Texture", textureMenu);\r
glutAddSubMenu("Ground texture", gTextureMenu);\r
//glutAddSubMenu("Lights", lightMenu);\r
glutAddMenuEntry("Exit", M_EXIT);\r
* @param y Mouse y position\r
*/\r
void mouse(int button, int state, int x, int y) {\r
- if (button == GLUT_LEFT_BUTTON) {\r
- if (state == GLUT_DOWN) {\r
- moving = 1;\r
+ switch(button) {\r
+ case GLUT_LEFT_BUTTON:\r
+ case GLUT_MIDDLE_BUTTON:\r
+ if ( state == GLUT_DOWN ) {\r
+ buttonSelected = button;\r
+ } else if ( state == GLUT_UP ) {\r
+ buttonSelected = -1;\r
+ }\r
startx = x;\r
starty = y;\r
- }\r
- if (state == GLUT_UP) {\r
- moving = 0;\r
- }\r
+ break;\r
}\r
}\r
\r
* @param y Mouse y position\r
*/\r
void motion(int x, int y) {\r
- if (moving) {\r
- zoom += (y - starty);\r
- rotate += (x - startx);\r
- startx = x;\r
- starty = y;\r
- glutPostRedisplay();\r
- }\r
- if (lightMoving0) {\r
- lightAngle0 += (x - lightStartX0)/40.0;\r
- lightHeight0 += (lightStartY0 - y)/20.0;\r
- lightStartX0 = x;\r
- lightStartY0 = y;\r
- glutPostRedisplay();\r
- }\r
- if (lightMoving1) {\r
- lightAngle1 += (x - lightStartX1)/40.0;\r
- lightHeight1 += (lightStartY1 - y)/20.0;\r
- lightStartX1 = x;\r
- lightStartY1 = y;\r
- glutPostRedisplay();\r
+ if ( buttonSelected == -1 ) return; // No button selected, no action\r
+\r
+ switch ( manipulateState ) {\r
+ case STATE_CAMERA_ROTATE_MOVE:\r
+ rotate += (x - startx);\r
+\r
+ if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
+ // w: rotate, h: zoom\r
+ zoom += (y - starty);\r
+ } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
+ // w: rotate, h: tilt\r
+ camAngle += (y - starty);\r
+ }\r
+\r
+ starty = y;\r
+ startx = x;\r
+\r
+ break;\r
+\r
+ case STATE_OBJECT_POSITION_SCALE:\r
+ //so.x, so.y, so.z\r
+ if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
+ // w: left/right, h: near/far\r
+ //sceneObjs[curObject].x += (x - startx)*leftrightFactor;\r
+ } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
+ // w: big/small, h: up/down\r
+\r
+ // For big/small scaling, we map negative numbers to 0->1 and positive numbers to 1->inf\r
+ float diff = (y - starty);\r
+ float map = ( diff + (float)height/2 ) / ( (float)height/2 );\r
+ float scaling;\r
+\r
+ if ( diff < 0 ) {\r
+ scaling = map;\r
+ } else {\r
+ scaling = map;\r
+ }\r
+\r
+ if ( scaling != 0 ) {\r
+ \r
+ \r
+ sceneObjs[curObject].scale[0] *= scaling;\r
+ sceneObjs[curObject].scale[1] *= scaling;\r
+ sceneObjs[curObject].scale[2] *= scaling;\r
+ printf("Diff is %f, Scaling by %f on all axii, 1/map is %f\n", diff, sceneObjs[curObject].scale[0], map);\r
+\r
+ }\r
+\r
+ starty = y;\r
+ }\r
+\r
+ startx = x;\r
+ break;\r
+\r
+ case STATE_OBJECT_ROTATION_TEXTURE_SCALE:\r
+ if ( buttonSelected == GLUT_LEFT_BUTTON ) {\r
+ // w: rotate on width, h: rotate on h\r
+\r
+ } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) {\r
+ // w: rotate on height, h: texture scale\r
+ \r
+ }\r
+ break;\r
+\r
}\r
+\r
+ glutPostRedisplay();\r
}\r
\r
/**\r
0.0, 10.0, 0.0 /* up is in postivie Y direction */\r
);\r
\r
- glRotatef(40.0, 1.0, 0.0, 0.0);\r
+ glRotatef(camAngle*camAngleFactor, 1.0, 0.0, 0.0);\r
\r
/* Reposition the light source 0. */\r
lightPosition0[0] = 12*cos(lightAngle0);\r
lightPosition0[2] = 12*sin(lightAngle0);\r
lightPosition0[3] = 0.0;\r
\r
+ direction0[0] = lightPosition0[0];\r
+ direction0[2] = lightPosition0[2];\r
+\r
/* Reposition the light source 1. */\r
lightPosition1[0] = 12*cos(lightAngle1);\r
lightPosition1[1] = lightHeight1;\r
lightPosition1[2] = 12*sin(lightAngle1);\r
lightPosition1[3] = 0.0;\r
\r
+ direction1[0] = lightPosition1[0];\r
+ direction1[2] = lightPosition1[2];\r
+\r
glPushMatrix();\r
\r
/* Perform scene rotations based on user mouse/keyboard input. */\r
\r
drawFloor();\r
\r
- // Draw teapot for a test object\r
- glPushMatrix();\r
- glTranslatef(0.0, 0.5, 0.0); // **NOTE: Teapot currently does not rest on surface\r
- glColor3f(0.0, 0.0, 0.0);\r
- glFrontFace(GL_CW); // The teapot does not obey the right-hand rule\r
- glutSolidTeapot(1);\r
- glFrontFace(GL_CCW);\r
- glPopMatrix();\r
+ // Draw sceneObjs array\r
+ for ( int i = 0; i < nObjects; i++ ) {\r
+ glPushMatrix();\r
+ SceneObject so = sceneObjs[i];\r
+\r
+ // Apply rotation vector\r
+ glRotatef(so.rotation.amount, so.rotation.vector[0], so.rotation.vector[1], so.rotation.vector[2]);\r
+\r
+ // Apply scaling vector\r
+ glScalef(so.scale[0], so.scale[1], so.scale[2]);\r
+\r
+ // Apply translation vector\r
+ glTranslatef(so.x, so.y, so.z);\r
+\r
+ // Apply texture\r
+ if ( so.texture > 0 ) {\r
+ getTexture(so.texture);\r
+ glBindTexture(GL_TEXTURE_2D, so.texture);\r
+ } else {\r
+ glBindTexture(GL_TEXTURE_2D, 0);\r
+ }\r
+\r
+ // Draw actual object\r
+ if ( so.mesh > 0 ) {\r
+ // drawMesh();\r
+ } else if ( so.mesh == -1 ) { // a mesh of -1 draws the teapot\r
+ // The teapot does not obey the right-hand rule\r
+ glFrontFace(GL_CW);\r
+ glutSolidTeapot(1);\r
+ glFrontFace(GL_CCW);\r
+ }\r
+\r
+ glBindTexture(GL_TEXTURE_2D, 0);\r
+ glPopMatrix();\r
+ }\r
+ \r
\r
// Draw a white ball over the light source\r
glPushMatrix();\r
glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);\r
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction0);\r
\r
- glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 90.0);\r
-\r
glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);\r
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);\r
glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);\r
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, direction1);\r
\r
- glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 90.0);\r
-\r
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glightmodel);\r
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);\r
\r
- glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);\r
- glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);\r
- glMaterialfv(GL_FRONT, GL_SPECULAR, specular);\r
- glMaterialfv(GL_FRONT, GL_EMISSION, emission);\r
- glMaterialf(GL_FRONT, GL_SHININESS, shine);\r
-\r
glEnable(GL_LIGHT0);\r
- //glEnable(GL_LIGHT1);\r
+ glEnable(GL_LIGHT1);\r
glEnable(GL_LIGHTING);\r
}\r
\r
glDepthRange(0,1);\r
glEnable(GL_DEPTH_TEST); // Enables Depth Testing\r
glDepthFunc(GL_LEQUAL); // the type\r
- glEnable(GL_TEXTURE_2D);\r
- //glEnable(GL_CULL_FACE);\r
glEnable(GL_NORMALIZE);\r
glLineWidth(2.0);\r
\r