X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=scene.c;h=d709f6ba469f02acc457edebdf5a3b2766a2ae5c;hb=a337a7a5514f1346ee5c3d611b1ed14ea434f905;hp=49782affe5cbfff2ad1f5923a6ca4175c648c5c1;hpb=1e95989edd005627e67b3ef02810c584e6a98895;p=atyndall%2Fcits2231.git diff --git a/scene.c b/scene.c index 49782af..d709f6b 100644 --- a/scene.c +++ b/scene.c @@ -464,15 +464,20 @@ void makeMenu() { * @param h New height */ void windowReshape(int w, int h) { - glViewport(0, 0, w, h); // Reset The Current Viewport - glMatrixMode(GL_PROJECTION); // Select The Projection Matrix - glLoadIdentity(); // Reset The Projection Matrix - - // Calculate The Aspect Ratio Of The Window - gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f); - - glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix - glLoadIdentity(); // Reset The Modelview Matrix + GLdouble near = -10.0; + GLdouble far = 10.0; + + glViewport(0, 0, (GLsizei) w, (GLsizei) h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + if (w <= h) + glOrtho(near, far, near*(GLfloat)h/(GLfloat)w, + far*(GLfloat)h/(GLfloat)w, near, far); + else + glOrtho(near*(GLfloat)w/(GLfloat)h, + far*(GLfloat)w/(GLfloat)h, near, far, near, far); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); } /** @@ -486,24 +491,118 @@ void mouse(int btn, int state, int x, int y) { } +static GLfloat floorVertices[4][3] = { + { -20.0, 0.0, 20.0 }, + { 20.0, 0.0, 20.0 }, + { 20.0, 0.0, -20.0 }, + { -20.0, 0.0, -20.0 }, +}; + +/* Draw a floor (possibly textured). */ +static void +drawFloor(void) +{ + glDisable(GL_LIGHTING); + + //if (useTexture) { + // glEnable(GL_TEXTURE_2D); + //} + + glBegin(GL_QUADS); + glTexCoord2f(0.0, 0.0); + glVertex3fv(floorVertices[0]); + glTexCoord2f(0.0, 16.0); + glVertex3fv(floorVertices[1]); + glTexCoord2f(16.0, 16.0); + glVertex3fv(floorVertices[2]); + glTexCoord2f(16.0, 0.0); + glVertex3fv(floorVertices[3]); + glEnd(); + + /*if (useTexture) { + glDisable(GL_TEXTURE_2D); + }*/ + + glEnable(GL_LIGHTING); +} + + +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(); + + /* 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); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + drawFloor(); + glDisable(GL_BLEND); + + glutSolidTeapot(1); // Draw teapot for test + + glPushMatrix(); + glDisable(GL_LIGHTING); + glColor3f(1.0, 1.0, 1.0); - glTranslatef(-1.5f, 0.0f, -6.0f); + /* Draw a yellow ball at the light source. */ + glTranslatef(lightPosition[0], lightPosition[1], lightPosition[2]); + glutSolidSphere(1.0, 5, 5); - glBegin(GL_QUADS); - glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red - glVertex3f(-1.0f, 1.0f, 0.0f); // Top left - glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right - glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right - glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left - glEnd(); + glEnable(GL_LIGHTING); + glPopMatrix(); - glutSwapBuffers(); + glPopMatrix(); + + glutSwapBuffers(); +} + +/** + * 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); + 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); } /** @@ -513,28 +612,39 @@ void display() { * @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); + + for(int i=0; i1) - 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