X-Git-Url: https://git.ucc.asn.au/?p=atyndall%2Fcits2231.git;a=blobdiff_plain;f=scene.c;h=69c5a820ed26fa4c983adfd966d7284f76c66f5c;hp=fa0f750da1abd89135505e1503ad43c08de7db4d;hb=8ce9e04b2f947ad161075ba0a8cf822b5594d199;hpb=e4c12e05e66672e8c1d0ed11a1583e0638c32d18 diff --git a/scene.c b/scene.c index fa0f750..69c5a82 100644 --- a/scene.c +++ b/scene.c @@ -87,7 +87,7 @@ void processMaterialEvents(int id) { void processLightEvents(int id) { switch (id) { case M_LIGHT_MOVE_LIGHT_1: - // Do stuff + manipulateState = STATE_LIGHT_1_MOVE; break; case M_LIGHT_RGBALL_LIGHT_1: @@ -95,7 +95,7 @@ void processLightEvents(int id) { break; case M_LIGHT_MOVE_LIGHT_2: - // Do stuff + manipulateState = STATE_LIGHT_2_MOVE; break; case M_LIGHT_RGBALL_LIGHT_2: @@ -122,6 +122,7 @@ void processObjectEvents(int id) { void processTextureEvents(int id) { if ( curObject >= 0 ) { sceneObjs[curObject].texture.id = id; + sceneObjs[curObject].texture.scale = 1; glutPostRedisplay(); } } @@ -153,9 +154,9 @@ void makeMenu() { // Construct light menu int lightMenu = glutCreateMenu(processLightEvents); glutAddMenuEntry("Move Light 1", M_LIGHT_MOVE_LIGHT_1); - glutAddMenuEntry("R/G/B/All Light 1", M_LIGHT_RGBALL_LIGHT_1); + //glutAddMenuEntry("R/G/B/All Light 1", M_LIGHT_RGBALL_LIGHT_1); glutAddMenuEntry("Move Light 2", M_LIGHT_MOVE_LIGHT_2); - glutAddMenuEntry("R/G/B/All Light 2", M_LIGHT_RGBALL_LIGHT_2); + //glutAddMenuEntry("R/G/B/All Light 2", M_LIGHT_RGBALL_LIGHT_2); // Construct object menu int objectMenu = makeSubmenuFromArray( objectMenuEntries, NMESH, processObjectEvents ); @@ -173,7 +174,7 @@ void makeMenu() { //glutAddSubMenu("Material", materialMenu); glutAddSubMenu("Texture", textureMenu); glutAddSubMenu("Ground texture", gTextureMenu); - //glutAddSubMenu("Lights", lightMenu); + glutAddSubMenu("Lights", lightMenu); glutAddMenuEntry("Exit", M_EXIT); // Bind to right mouse button @@ -216,66 +217,6 @@ void mouse(int button, int state, int x, int y) { } } -/** - * Keybord event handler - * w/s increase/decrease the z - * a/d increase/decrease the x - * q/e increase/decrease the y - * z/x increase/decrease the angle - * @param key Key pressed - * @param x x co-ordinate of mouse - * @param y y co-ordinate of mouse - */ -void keyboard(unsigned char key, int x, int y) { - switch(key) { - case 'w': - case 'W': - camz -= factor; - break; - case 'a': - case 'A': - camx -= factor; - break; - case 's': - case 'S': - camz += factor; - break; - case 'd': - case 'D': - camx += factor; - break; - case 'q': - case 'Q': - camy += factor; - break; - case 'e': - case 'E': - camy -= factor; - break; - case 'z': - case 'Z': - keyrot += factor; - break; - case 'x': - case 'X': - keyrot -= factor; - break; - case '=': - case '+': - factor += 0.1; - printf("Factor of change is now %f\n", factor); - break; - case '-': - case '_': - factor -= 0.1; - printf("Factor of change is now %f\n", factor); - break; - - } - printf("Camera is now at (%f, %f, %f), angle %f\n", camx, camy, camz, keyrot); - glutPostRedisplay(); -} - /** * Called when motion event occurs * @param x Mouse x position @@ -305,16 +246,9 @@ void motion(int x, int y) { if ( buttonSelected == GLUT_LEFT_BUTTON ) { // w: left/right, h: near/far - - // **NOTE: Currently a work in progress, does not work correctly float angler = 2 * M_PI * ( (rotate*camRotateFactor)/360.0 ); - printf("cam angle: %f\n", rotate*camRotateFactor); - - // sceneObjs[curObject].x += diffx * cos(angler) * 0.1; - // sceneObjs[curObject].z += diffx * sin(angler) * 0.1; - - sceneObjs[curObject].x += diffy * sin(angler) * 0.1; - sceneObjs[curObject].z += diffy * cos(angler) * 0.1; + sceneObjs[curObject].x += diffx * cos(angler) * leftrightFactor + diffy * cos(M_PI/2 + angler) * nearfarFactor; + sceneObjs[curObject].z += diffx * sin(angler) * leftrightFactor + diffy * sin(M_PI/2 + angler) * nearfarFactor; } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) { // w: big/small @@ -345,14 +279,38 @@ void motion(int x, int y) { sceneObjs[curObject].rotation.x += diffx * rotateFactor; // h: texture scale + // **NOTE: Seems to be problem with scaling, delayed action float max = (float)height/texscaleFactor; - float scaling = (diffy + max) / max; + float scaling = (-diffy + max) / max; sceneObjs[curObject].texture.scale *= scaling; } break; + case STATE_LIGHT_1_MOVE: + case STATE_LIGHT_2_MOVE: + ; // Semi-colon required to allow variable declaration below + + int i = 0; + if ( manipulateState == STATE_LIGHT_2_MOVE ) i = 1; + + if ( buttonSelected == GLUT_LEFT_BUTTON ) { + // w: left/right, h: near/far + float angler = 2 * M_PI * ( (rotate*camRotateFactor)/360.0 ); + lightObjs[i].position[0] += diffx * cos(angler) * lleftrightFactor + diffy * cos(M_PI/2 + angler) * lnearfarFactor; + lightObjs[i].position[2] += diffx * sin(angler) * lleftrightFactor + diffy * sin(M_PI/2 + angler) * lnearfarFactor; + + } else if ( buttonSelected == GLUT_MIDDLE_BUTTON ) { + // w: increase/decrease some light param + // **NOTE: Currently not implemented + + // h: up/down + lightObjs[i].position[1] -= diffy * updownFactor; + } + + break; + } starty = y; @@ -396,33 +354,10 @@ void display() { glRotatef(camAngle*camAngleFactor, 1.0, 0.0, 0.0); // Set camera angle upward - /* Reposition the light source 0. */ - lightPosition0[0] = 12*cos(lightAngle0); - lightPosition0[1] = lightHeight0; - lightPosition0[2] = 12*sin(lightAngle0); - lightPosition0[3] = 0.0; - - direction0[0] = lightPosition0[0]; - direction0[2] = lightPosition0[2]; - - /* Reposition the light source 1. */ - lightPosition1[0] = 12*cos(lightAngle1); - lightPosition1[1] = lightHeight1; - lightPosition1[2] = 12*sin(lightAngle1); - lightPosition1[3] = 0.0; - - direction1[0] = lightPosition1[0]; - direction1[2] = lightPosition1[2]; - glPushMatrix(); - /* Perform scene rotations based on user mouse/keyboard input. */ + /* Perform scene rotations based on user mouse input. */ glRotatef(rotate*camRotateFactor, 0.0, 1.0, 0.0); - glTranslatef(camx, camy, camz); - glRotatef(keyrot, 1.0, 0.0, 0.0); - - glLightfv(GL_LIGHT0, GL_POSITION, lightPosition0); - glLightfv(GL_LIGHT1, GL_POSITION, lightPosition1); drawFloor(); @@ -467,21 +402,7 @@ void display() { glPopMatrix(); } - // Draw a white ball over the light sources - glDisable(GL_LIGHTING); - glColor3f(1.0, 1.0, 1.0); - - glPushMatrix(); - glTranslatef(lightPosition0[0], lightPosition0[1], lightPosition0[2]); - glutSolidSphere(0.5, 50, 50); - glPopMatrix(); - - glPushMatrix(); - glTranslatef(lightPosition1[0], lightPosition1[1], lightPosition1[2]); - glutSolidSphere(0.5, 50, 50); - glPopMatrix(); - - glEnable(GL_LIGHTING); + updateLights(); drawAxisLines(); @@ -490,28 +411,6 @@ void display() { glutSwapBuffers(); } -/** - * init function; sets initial OpenGL state - */ -void init() { - glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0); - glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0); - glLightfv(GL_LIGHT0, GL_SPECULAR, specular0); - glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction0); - - glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1); - glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1); - glLightfv(GL_LIGHT1, GL_SPECULAR, specular1); - glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, direction1); - - glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glightmodel); - glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); - - glEnable(GL_LIGHT0); - glEnable(GL_LIGHT1); - glEnable(GL_LIGHTING); -} - /** * Main function * @param argc Number of arguments @@ -543,20 +442,19 @@ int main(int argc, char **argv) { glDepthRange(0,1); glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // the type - glEnable(GL_NORMALIZE); - glLineWidth(2.0); + //glEnable(GL_CULL_FACE); + glLineWidth(1.0); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glutReshapeFunc(windowReshape); glutDisplayFunc(display); glutMouseFunc(mouse); - glutKeyboardFunc(keyboard); glutMotionFunc(motion); makeMenu(); - - init(); + initializeLights(); + initializeState(); glutMainLoop(); } \ No newline at end of file