X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=scene.c;h=d709f6ba469f02acc457edebdf5a3b2766a2ae5c;hb=a337a7a5514f1346ee5c3d611b1ed14ea434f905;hp=79c506e416821bcdccd30f3d0ad5cde31c8e235a;hpb=79f4b6e1d83d5ec50cfac2f71487bd9d07d8fc8e;p=atyndall%2Fcits2231.git diff --git a/scene.c b/scene.c index 79c506e..d709f6b 100644 --- a/scene.c +++ b/scene.c @@ -527,44 +527,82 @@ drawFloor(void) } +static GLfloat lightColor[] = {1.0, 1.0, 1.0, 1.0}; /* green-tinted */ +static GLfloat lightPosition[4]; +static float lightAngle = 10.0, lightHeight = 20; + + /** * Display function */ void display() { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glLoadIdentity(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glLoadIdentity(); - /*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();*/ + /* Reposition the light source. */ + lightPosition[0] = 12*cos(lightAngle); + lightPosition[1] = lightHeight; + lightPosition[2] = 12*sin(lightAngle); + lightPosition[3] = 0.0; + glPushMatrix(); + glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); - glTranslatef( 0.0f, 0.0f, -5.0f); // Move into the Screen 10.0 - glutSolidTeapot(1); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + drawFloor(); + glDisable(GL_BLEND); - /* glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - gluLookAt(0.7f, 0.4f, 0.9f, -2.0f, -1.0f, -7.0f, 1.0f, 10.0f, 1.0f); + glutSolidTeapot(1); // Draw teapot for test + + glPushMatrix(); + glDisable(GL_LIGHTING); + glColor3f(1.0, 1.0, 1.0); + + /* Draw a yellow ball at the light source. */ + glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]); + glutSolidSphere(1.0, 5, 5); + glEnable(GL_LIGHTING); + glPopMatrix(); - glMatrixMode(GL_PROJECTION); - glLoadIdentity();*/ - drawFloor(); - glutSwapBuffers(); + glPopMatrix(); + + glutSwapBuffers(); } /** - * init function, sets OpenGL's starting state + * init function; sets initial OpenGL state */ void init() { + glEnable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + glEnable(GL_TEXTURE_2D); + glLineWidth(3.0); + glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective( 60, 1, 0.1, 1000.0); + gluPerspective( + 60.0, /* field of view in degree */ + 1.0, /* aspect ratio */ + 0.0, /* Z near */ + 900.0 /* Z far */ + ); + 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.0 /* up is in postivie Y direction */ + ); + + glTranslatef( 0.0f, 0.0f, -600.0f); // Move into the Screen 10.0 + + 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); } /** @@ -574,48 +612,39 @@ void init() { * @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