X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=scene.c;h=638683e95c05a4d7fb817c282d537057b32aefac;hb=a94c1e2ec02ba03c8f7c4dee751f2eded3203b8f;hp=4fea15a9586f97fff5809ab65c8f86e35d5a0738;hpb=1fbbf873ce0993536eafe9f9f01eb0ca24fececa;p=atyndall%2Fcits2231.git diff --git a/scene.c b/scene.c index 4fea15a..638683e 100644 --- a/scene.c +++ b/scene.c @@ -460,34 +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 w, int h) { - /*glViewport(0, 0, (GLsizei)width, (GLsizei)height); - printf("Width: %d, height: %d\n", width, height); + GLdouble near = -10.0; + GLdouble far = 10.0; + + glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective( 60 * ((GLfloat)height/(GLfloat)width), (GLfloat)width/(GLfloat)height, 0.1, 1000.0); - glMatrixMode(GL_MODELVIEW); - - GLdouble near = 0.1; - GLdouble far = 1000.0; - GLdouble a = (GLfloat)height/(GLfloat)width; - GLdouble fov = 60; - glFrustum( -a*near*tan(fov/2), a*near*tan(fov/2), -a*near*tan(fov/2), a*near*tan(fov/2), near, far )*/ - glViewport(0, 0, (GLsizei) w, (GLsizei) h); // what's GLsizei? Why is it called inside glViewPort? - glMatrixMode(GL_PROJECTION); // is it necessary to reset the projection matrix? + 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(); - if (w <= h) // is this calculation universal, could I use it on another program? - glOrtho(-5.0, 5.0, -5.0*(GLfloat)h/(GLfloat)w, - 5.0*(GLfloat)h/(GLfloat)w, -5.0, 5.0); - - else - glOrtho(-5.0*(GLfloat)w/(GLfloat)h, - 5.0*(GLfloat)w/(GLfloat)h, -5.0, 5.0, -5.0, 5.0); - glMatrixMode(GL_MODELVIEW); // why do I set to GL_MODELVIEW at the end? - glLoadIdentity(); // why does it get a reset? } /** @@ -501,6 +491,47 @@ 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[] = {0.8, 1.0, 0.8, 1.0}; /* green-tinted */ +static GLfloat lightPosition[4]; +static float lightAngle = 0.0, lightHeight = 20; + + /** * Display function */ @@ -508,13 +539,71 @@ void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); - glTranslatef(0.0f,0.0f,-10.0f); // Move into the Screen 10.0 + /*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();*/ + + + //glTranslatef( 0.0f, 0.0f, -5.0f); // Move into the Screen 10.0 + //glutSolidTeapot(1); + + /* 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();*/ - glutSolidTeapot(1); + /* 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(); } + /** * Main function * @param argc Number of arguments @@ -536,17 +625,20 @@ int main(int argc, char **argv) { glutInit(&argc, argv); - glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); + //glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); + + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL | GLUT_MULTISAMPLE); + glutInitWindowSize(500, 500); glutCreateWindow("Scene Editor"); - glShadeModel(GL_SMOOTH); // Enables Smooth Shading - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background - glClearDepth(1.0f); // Depth Buffer Setup - glEnable(GL_DEPTH_TEST); // Enables Depth Testing - glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do + //glShadeModel(GL_SMOOTH); // Enables Smooth Shading + //glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background + //glClearDepth(1.0f); // Depth Buffer Setup + //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); @@ -554,10 +646,29 @@ int main(int argc, char **argv) { makeMenu(); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective( 60, 1, 0.1, 1000.0); - glMatrixMode(GL_MODELVIEW); + //init(); + + + glEnable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + glEnable(GL_TEXTURE_2D); + glLineWidth(3.0); + +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(); }