From 59ab0befd5f3d6ea6a2b72aeeb045ab93103995a Mon Sep 17 00:00:00 2001 From: Ash Tyndall Date: Tue, 18 Oct 2011 11:39:37 +0800 Subject: [PATCH] --- scene.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/scene.c b/scene.c index 34824a4..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 */ @@ -553,6 +558,47 @@ void display() { 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 @@ -617,5 +664,12 @@ int main(int argc, char **argv) { 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(); } -- 2.20.1