X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=scene.c;h=6c9540ae85e4a69cb87c5da9cdc8285d6845c088;hb=59ab0befd5f3d6ea6a2b72aeeb045ab93103995a;hp=e2d17e682e7f50682271248993f76fd788b7d48b;hpb=36e1679e77db07de3b54b6054b118607112c360e;p=atyndall%2Fcits2231.git diff --git a/scene.c b/scene.c index e2d17e6..6c9540a 100644 --- a/scene.c +++ b/scene.c @@ -527,6 +527,11 @@ drawFloor(void) } +static GLfloat lightColor[] = {0.8, 1.0, 0.8, 1.0}; /* green-tinted */ +static GLfloat lightPosition[4]; +static float lightAngle = 0.0, lightHeight = 20; + + /** * Display function */ @@ -534,25 +539,66 @@ void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); - glTranslatef( 0.0f, 0.0f, 0.0f); + /*glTranslatef( 0.0f, 0.0f, 0.0f); glBegin(GL_QUADS); glVertex3f( 0.0f, 1.0f, -1.0f); glVertex3f( 0.0f, 1.0f, 1.0f); glVertex3f( 0.0f, -1.0f, 1.0f); glVertex3f( 0.0f, -1.0f, -1.0f); - glEnd(); + glEnd();*/ - glTranslatef( 0.0f, 0.0f, -5.0f); // Move into the Screen 10.0 - glutSolidTeapot(1); + //glTranslatef( 0.0f, 0.0f, -5.0f); // Move into the Screen 10.0 + //glutSolidTeapot(1); - /*glMatrixMode(GL_MODELVIEW); + /* glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.7f, 0.4f, 0.9f, -2.0f, -1.0f, -7.0f, 1.0f, 10.0f, 1.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity();*/ + + + /* Reposition the light source. */ + lightPosition[0] = 12*cos(lightAngle); + lightPosition[1] = lightHeight; + lightPosition[2] = 12*sin(lightAngle); + //if (directionalLight) { + lightPosition[3] = 0.0; + //} else { + // lightPosition[3] = 1.0; + //} + + glPushMatrix(); + + /* Tell GL new light source position. */ + glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); + + /* Draw "bottom" of floor in blue. */ + glFrontFace(GL_CW); /* Switch face orientation. */ + glColor4f(0.1, 0.1, 0.7, 1.0); + drawFloor(); + glFrontFace(GL_CCW); + + /* Draw "top" of floor. Use blending to blend in reflection. */ + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glColor4f(0.7, 0.0, 0.0, 0.3); + glColor4f(1.0, 1.0, 1.0, 0.3); + drawFloor(); + glDisable(GL_BLEND); + + glPushMatrix(); + glDisable(GL_LIGHTING); + + /* Draw a yellow ball at the light source. */ + glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]); + glutSolidSphere(1.0, 5, 5); + + glEnable(GL_LIGHTING); + glPopMatrix(); + drawFloor(); glutSwapBuffers(); } @@ -567,6 +613,7 @@ void init() { glMatrixMode(GL_MODELVIEW); } + /** * Main function * @param argc Number of arguments @@ -598,7 +645,7 @@ int main(int argc, char **argv) { glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // the type - glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations + // glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations glutReshapeFunc(windowReshape); glutDisplayFunc(display); @@ -608,5 +655,21 @@ int main(int argc, char **argv) { init(); + glMatrixMode(GL_PROJECTION); + gluPerspective( /* field of view in degree */ 40.0, + /* aspect ratio */ 1.0, + /* Z near */ 20.0, /* Z far */ 100.0); + glMatrixMode(GL_MODELVIEW); + gluLookAt(0.0, 8.0, 60.0, /* eye is at (0,8,60) */ + 0.0, 8.0, 0.0, /* center is at (0,8,0) */ + 0.0, 1.0, 0.); /* up is in postivie Y direction */ + + glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1); + glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor); + glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1); + glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05); + glEnable(GL_LIGHT0); + glEnable(GL_LIGHTING); + glutMainLoop(); }