X-Git-Url: https://git.ucc.asn.au/?p=atyndall%2Fcits2231.git;a=blobdiff_plain;f=scene.c;h=7afde9cd41ca880037b945980ede82e8eef951d6;hp=7e72ae66e4dc340802a0567e44fb20a7bd43ed04;hb=f8f4c489085c83bc31276148991166165085b557;hpb=ef6b009c8781673f3e353d47ae270a9f712bd4bf diff --git a/scene.c b/scene.c index 7e72ae6..7afde9c 100644 --- a/scene.c +++ b/scene.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "bitmap.h" @@ -109,6 +110,38 @@ char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures"; char dataDir[200]; // Stores the directory name for the meshes and textures. +static GLfloat lightColor[] = {1.0, 1.0, 1.0, 1.0}; // White light +static GLfloat lightPosition[4]; + +int moving, startx, starty; +int lightMoving = 0, lightStartX, lightStartY; + +/* Time varying or user-controled variables. */ +static float jump = 0.0; +static float lightAngle = 0.0, lightHeight = 5; +GLfloat angle = -150; /* in degrees */ +GLfloat angle2 = 30; /* in degrees */ + +/* Near and far parameters */ +GLfloat near = -10; +GLfloat far = 10; + +/* Zoom factor for mouse movements */ +GLfloat zoomFactor = 1.0; + +/* Recursion level for floor drawing */ +int drawFloorRecurse = 5; + +/* Size of floor, from -n to n */ +int floorSize = 100; + +/* Light 0 parameters */ +GLfloat diffuse0[] = {1.0, 1.0, 1.0, 1.0}; +GLfloat ambient0[] = {1.0, 1.0, 1.0, 1.0}; +GLfloat specular0[] = {1.0, 1.0, 1.0, 1.0}; +GLfloat light0_pos[] ={ 1.0, 2.0, 3,0, 1.0}; + + /** * Prints out error message when file cannot be read * @param fileName Name of file that could not be read @@ -460,22 +493,21 @@ void makeMenu() { /** * Called when window is resized - * @param width New width - * @param height New height + * @param w New width + * @param h New height */ -void windowReshape(int width, int height) { - /*glViewport(0, 0, (GLsizei)width, (GLsizei)height); - printf("Width: %d, height: %d\n", width, height); +void windowReshape(int w, int h) { + glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective( 60 * ((GLfloat)height/(GLfloat)width), (GLfloat)width/(GLfloat)height, 0.1, 1000.0); - glMatrixMode(GL_MODELVIEW); - - GLdouble near = 0.1; - GLdouble far = 1000.0; - GLdouble a = (GLfloat)height/(GLfloat)width; - GLdouble fov = 60; - glFrustum( -a*near*tan(fov/2), a*near*tan(fov/2), -a*near*tan(fov/2), a*near*tan(fov/2), near, far )*/ + if (w <= h) + glOrtho(near, far, near*(GLfloat)h/(GLfloat)w, + far*(GLfloat)h/(GLfloat)w, near, far); + else + glOrtho(near*(GLfloat)w/(GLfloat)h, + far*(GLfloat)w/(GLfloat)h, near, far, near, far); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); } /** @@ -485,22 +517,171 @@ void windowReshape(int width, int height) { * @param x Mouse x position * @param y Mouse y position */ -void mouse(int btn, int state, int x, int y) { - +void mouse(int button, int state, int x, int y) { + if (button == GLUT_LEFT_BUTTON) { + if (state == GLUT_DOWN) { + moving = 1; + startx = x; + starty = y; + } + if (state == GLUT_UP) { + moving = 0; + } + } + if (button == GLUT_MIDDLE_BUTTON) { + if (state == GLUT_DOWN) { + lightMoving = 1; + lightStartX = x; + lightStartY = y; + } + if (state == GLUT_UP) { + lightMoving = 0; + } + } +} + +/** + * Called when motion event occurs + * @param x Mouse x position + * @param y Mouse y position + */ +void motion(int x, int y) { + if (moving) { + angle = angle + (x - startx); + angle2 = angle2 + (y - starty); + startx = x; + starty = y; + glutPostRedisplay(); + } + if (lightMoving) { + lightAngle += (x - lightStartX)/40.0; + lightHeight += (lightStartY - y)/20.0; + lightStartX = x; + lightStartY = y; + glutPostRedisplay(); + } +} + +/** + * Recursive function to draw a square by drawing smaller and smaller + * divisions of the square, determined by drawFloorRecurse. + * @param recurseLevel Current level of recursion, only pass 0 + * @param x1 top-left x + * @param z1 top-left z + * @param x2 bottom-left x + * @param z2 bottom-left z + */ +void drawSquare(int recurseLevel, float x1, float z1, float x2, float z2) { + + if ( drawFloorRecurse != recurseLevel ) { + // Calculate middle points + float xm = (x1 + x2) / 2.0; + float zm = (z1 + z2) / 2.0; + + // Increment recursion level + int rnew = recurseLevel + 1; + + // Split into four sub-quads + drawSquare(rnew, x1, z1, xm, zm); + drawSquare(rnew, x1, zm, xm, z2); + drawSquare(rnew, xm, zm, x2, z2); + drawSquare(rnew, xm, z1, x2, zm); + + } else { + // Draw square. + // **NOTE: Is the polygon facing in the right direction? + glBegin(GL_QUADS); + glVertex3f(x1, 0.0, z1); + glVertex3f(x1, 0.0, z2); + glVertex3f(x2, 0.0, z2); + glVertex3f(x2, 0.0, z1); + glEnd(); + } + +} + +/** + * Draw a floor by calling the drawSquare recursion + */ +void drawFloor() { + drawSquare(0, -floorSize, -floorSize, floorSize, floorSize); } /** * Display function */ void display() { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glLoadIdentity(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glLoadIdentity(); + gluLookAt( + 0.0, 0.0, 10.0, /* eye is at (x,y,z) */ + 0.0, 0.0, 0.0, /* center is at (x,y,z) */ + 0.0, 1.0, 0.0 /* up is in postivie Y direction */ + ); + + // **NOTE: Currently this rotation function is all that moves the camera off + // the flat surface. Need to integrate function into gluLookAt + glRotatef(30.0, 1.0, 0.0, 0.0); + + /* Reposition the light source. */ + lightPosition[0] = 12*cos(lightAngle); + lightPosition[1] = lightHeight; + lightPosition[2] = 12*sin(lightAngle); + lightPosition[3] = 0.0; + + glPushMatrix(); + + /* Perform scene rotations based on user mouse input. */ + glRotatef(angle, 0.0, 1.0, 0.0); + //glRotatef(angle2, 1.0, 0.0, 0.0); **NOTE: Only one degree of freedom + + glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); + + drawFloor(); + + // Draw teapot for a test object + glPushMatrix(); + glTranslatef(0.0, 1.0, 0.0); // **NOTE: Teapot does not rest on surface + glutWireTeapot(1); + glPopMatrix(); + + // Draw a white ball over the light source + glPushMatrix(); + glDisable(GL_LIGHTING); + glColor3f(1.0, 1.0, 1.0); + glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]); + glutSolidSphere(1.0, 50, 50); + glEnable(GL_LIGHTING); + glPopMatrix(); + + glPopMatrix(); + + glutSwapBuffers(); +} - glTranslatef(0.0f,0.0f,-10.0f); // Move into the Screen 10.0 +/** + * init function; sets initial OpenGL state + */ +void init() { + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + gluPerspective( + 60.0, /* field of view in degree */ + 1.0, /* aspect ratio */ + near, /* Z near */ + far /* Z far */ + ); - glutSolidTeapot(1); + glEnable(GL_LIGHT0); + glLightfv(GL_LIGHT0, GL_POSITION, light0_pos); + glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0); + glLightfv(GL_LIGHT0, GL_SPECULAR, specular0); + glEnable(GL_LIGHTING); - glutSwapBuffers(); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); } /** @@ -510,42 +691,43 @@ void display() { * @return Program exit code */ int main(int argc, char **argv) { + if(argc>1) + strcpy(dataDir, argv[1]); + else if(opendir(dirDefault1)) + strcpy(dataDir, dirDefault1); + else if(opendir(dirDefault2)) + strcpy(dataDir, dirDefault2); + else fileErr(dirDefault1); - if(argc>1) - strcpy(dataDir, argv[1]); - else if(opendir(dirDefault1)) - strcpy(dataDir, dirDefault1); - else if(opendir(dirDefault2)) - strcpy(dataDir, dirDefault2); - else fileErr(dirDefault1); + for(int i=0; i