From e1fc70add9281f69123aef6261d7ac016f3275d6 Mon Sep 17 00:00:00 2001 From: Ash Tyndall Date: Tue, 18 Oct 2011 21:01:59 +0800 Subject: [PATCH] --- scene.c | 110 ++++++++++++++++++++++++++------------------------------ 1 file changed, 51 insertions(+), 59 deletions(-) diff --git a/scene.c b/scene.c index e5d2a17..b4469b7 100644 --- a/scene.c +++ b/scene.c @@ -110,13 +110,6 @@ char *dirDefault2 = "/cslinux/examples/CITS2231/project-files/models-textures"; char dataDir[200]; // Stores the directory name for the meshes and textures. -static GLfloat floorVertices[4][3] = { - { -1000.0, 0.0, 1000.0 }, - { 1000.0, 0.0, 1000.0 }, - { 1000.0, 0.0, -1000.0 }, - { -1000.0, 0.0, -1000.0 }, -}; - static GLfloat lightColor[] = {1.0, 1.0, 1.0, 1.0}; // White light static GLfloat lightPosition[4]; @@ -129,10 +122,18 @@ 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; -static float zoomFactor = 1.0; +/* 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; /** * Prints out error message when file cannot be read @@ -492,15 +493,12 @@ void windowReshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - GLfloat x = 2.0f*(200 + 0.5)/w-1.0; - GLfloat y = 2.0f*(200 + 0.5)/h-1.0; - glTranslatef(-x,-y,0.0f); if (w <= h) - glOrtho(zoomFactor*near, zoomFactor*far, zoomFactor*near*(GLfloat)h/(GLfloat)w, - zoomFactor*far*(GLfloat)h/(GLfloat)w, near, far); + glOrtho(near, far, near*(GLfloat)h/(GLfloat)w, + far*(GLfloat)h/(GLfloat)w, near, far); else - glOrtho(zoomFactor*near*(GLfloat)w/(GLfloat)h, - zoomFactor*far*(GLfloat)w/(GLfloat)h, zoomFactor*near, zoomFactor*far, near, far); + glOrtho(near*(GLfloat)w/(GLfloat)h, + far*(GLfloat)w/(GLfloat)h, near, far, near, far); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } @@ -512,12 +510,7 @@ void windowReshape(int w, int h) { * @param x Mouse x position * @param y Mouse y position */ -/*void mouse(int btn, int state, int x, int y) { - -}*/ -static void -mouse(int button, 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; @@ -540,9 +533,12 @@ mouse(int button, int state, int x, int y) } } -static void -motion(int x, int y) -{ +/** + * 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); @@ -559,58 +555,51 @@ motion(int x, int y) } } -void idle() { - angle = (int)(angle + 10) % 360; - //angle2 = (int)(angle2 + 10) % 360; - printf("Angle 1: %f, Angle 2: %f\n", angle, angle2); - sleep(1); - glutPostRedisplay(); -} - - -int drawFloorRecurse = 5; - -void drawSquare(int recurseLevel, float x1, float y1, float x2, float y2) { +/** + * 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 ym = (y1 + y2) / 2.0; + float zm = (z1 + z2) / 2.0; + + // Increment recursion level int rnew = recurseLevel + 1; // Split into four sub-quads - drawSquare(rnew, x1, y1, xm, ym); - drawSquare(rnew, x1, ym, xm, y2); - drawSquare(rnew, xm, ym, x2, y2); - drawSquare(rnew, xm, y1, x2, ym); + 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, y1); - glVertex3f(x1, 0.0, y2); - glVertex3f(x2, 0.0, y2); - glVertex3f(x2, 0.0, y1); + glVertex3f(x1, 0.0, z1); + glVertex3f(x1, 0.0, z2); + glVertex3f(x2, 0.0, z2); + glVertex3f(x2, 0.0, z1); glEnd(); } } /** - * Draw a floor. + * Draw a floor by calling the drawSquare recursion */ void drawFloor() { - -drawSquare(0, -100.0, -100.0, 100.0, 100.0); - - /*if (useTexture) { - glDisable(GL_TEXTURE_2D); - }*/ - + drawSquare(0, -floorSize, -floorSize, floorSize, floorSize); } -/*void drawSquare(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2) { - drawSquare(0, x1, y1, x2, y2); -}*/ - /** * Display function */ @@ -623,6 +612,8 @@ void display() { 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. */ @@ -632,9 +623,10 @@ void display() { lightPosition[3] = 0.0; glPushMatrix(); - /* Perform scene rotations based on user mouse input. */ - + + /* Perform scene rotations based on user mouse input. */ glRotatef(angle, 0.0, 1.0, 0.0); + glRotatef(angle2, 1.0, 0.0, 0.0); glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); -- 2.20.1