X-Git-Url: https://git.ucc.asn.au/?p=atyndall%2Fcits2231.git;a=blobdiff_plain;f=scene.c;h=d9a4ea3be32f39ed4ab99b5a8cb52c6971cabe3c;hp=0c5329b2d01d38f5f52c0f66e6eeb991bc5002fb;hb=ddf25313dfc93155b958b2838b6dba330e2706d9;hpb=84eea711e741871dfb6cc595a0989e1369998d7f diff --git a/scene.c b/scene.c index 0c5329b..d9a4ea3 100644 --- a/scene.c +++ b/scene.c @@ -184,7 +184,8 @@ void makeMenu() { */ void windowReshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); - // **NOTE: windowReshape needs to be re-written using glFrustrum and perspective projection calculations + width = w; + height = h; } /** @@ -205,16 +206,6 @@ void mouse(int button, int state, int x, int y) { moving = 0; } } - if (button == GLUT_MIDDLE_BUTTON) { - if (state == GLUT_DOWN) { - lightMoving = 1; - lightStartX = x; - lightStartY = y; - } - if (state == GLUT_UP) { - lightMoving = 0; - } - } } /** @@ -265,8 +256,8 @@ void keyboard(unsigned char key, int x, int y) { */ void motion(int x, int y) { if (moving) { - angle = angle + (x - startx); - angle2 = angle2 + (y - starty); + zoomFactor += (y - starty); + rotateFactor += (x - startx); startx = x; starty = y; glutPostRedisplay(); @@ -285,21 +276,52 @@ void motion(int x, int y) { */ void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // Redraw projection matrix + glMatrixMode(GL_PROJECTION); glLoadIdentity(); + float aspect; + if ( width <= height ) { + aspect = (float)height / (float)width; + } else { + aspect = (float)width / (float)height; + } + + gluPerspective( + 75.0, + aspect, + 0.1, + 200 + ); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + gluLookAt( + 0.0, 0.0, 5.0 + (zoomFactor*0.2), /* 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 */ + ); + + glRotatef(75.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; + + //glRotatef(angle, 0.0, 1.0, 0.0); + // glRotatef(angle2, 1.0, 0.0, 0.0); + glPushMatrix(); /* Perform scene rotations based on user mouse/keyboard input. */ - glRotatef(angle, 0.0, 1.0, 0.0); - glRotatef(angle2, 1.0, 0.0, 0.0); - glTranslatef(camx, camy, camz); - glRotatef(rot, 1.0, 0.0, 0.0); + glRotatef(rotateFactor*0.5, 0.0, 1.0, 0.0); + //glTranslatef(camx, camy, camz); + //glRotatef(rot, 1.0, 0.0, 0.0); glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); @@ -311,7 +333,9 @@ void display() { glPushMatrix(); glTranslatef(0.0, 0.5, 0.0); // **NOTE: Teapot currently does not rest on surface glColor3f(0.0, 0.0, 0.0); + glFrontFace(GL_CW); glutSolidTeapot(1); + glFrontFace(GL_CCW); glPopMatrix(); // Draw a white ball over the light source @@ -332,9 +356,6 @@ void display() { * init function; sets initial OpenGL state */ void init() { - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glLightfv(GL_LIGHT0, GL_POSITION, light0_pos); glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0); @@ -386,25 +407,10 @@ int main(int argc, char **argv) { glDepthRange(0,1); glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // the type - glEnable(GL_CULL_FACE); glEnable(GL_TEXTURE_2D); - glLineWidth(1.0); - - // **NOTE: Currently the perspective and look-at code is static, for testing purposes - glMatrixMode(GL_PROJECTION); - gluPerspective( - 40.0, /* field of view in degree */ - 1.0, /* aspect ratio */ - 1.0, /* Z near */ // **NOTE: Seems to be issue with near < 0 causing invisible everything - 100.0 /* Z far */ - ); - - glMatrixMode(GL_MODELVIEW); - gluLookAt( - 0.0, 35.0, -20.0, /* eye is at (x,y,z) */ - 0.0, 10.0, 0.0, /* center is at (x,y,z) */ - 0.0, 1.0, 0.0 /* up is in postivie Y direction */ - ); + glEnable(GL_CULL_FACE); + glEnable(GL_NORMALIZE); + glLineWidth(2.0); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);