X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=scene.c;h=6a9399260d853c9e3a0d74e984e85adb05130ee7;hb=a4bfff194cb2440cbab29c871a03130877cdad19;hp=a7748d89cc8efeac571363210929ff1609570008;hpb=c9675ed3a32c2977921f0afc02a6866b651a9f01;p=atyndall%2Fcits2231.git diff --git a/scene.c b/scene.c index a7748d8..6a93992 100644 --- a/scene.c +++ b/scene.c @@ -460,15 +460,24 @@ void makeMenu() { /** * Called when window is resized - * @param width New width - * @param height New height + * @param w New width + * @param h New height */ -void windowReshape(int width, int height) { - glViewport(0, 0, (GLsizei)width, (GLsizei)height); +void windowReshape(int w, int h) { + GLdouble near = -10.0; + GLdouble far = 10.0; + + glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective(60, 1, 0.1, 1000.0); - glMatrixMode(GL_MODELVIEW); + 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(); } /** @@ -482,24 +491,116 @@ 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); - glTranslatef(0.0f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0 + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + drawFloor(); + glDisable(GL_BLEND); - 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(); + glutSolidTeapot(1); // Draw teapot for test - glutSwapBuffers(); + 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(); + + 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, 999.0, /* center is at (0,8,0) */ + 0.0, 1.0, 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); } /** @@ -509,36 +610,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); + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); - for(int i=0; i