void Display( ) { if( DebugOn != 0 ) { fprintf( stderr, "Display\n" ); } // set which window we want to do the graphics into: glutSetWindow( MainWindow ); // erase the background: glDrawBuffer( GL_BACK ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glEnable( GL_DEPTH_TEST ); //glShadeModel(GL_FLAT); // set the viewport to a square centered in the window: GLsizei vx = glutGet( GLUT_WINDOW_WIDTH ); GLsizei vy = glutGet( GLUT_WINDOW_HEIGHT ); GLsizei v = vx < vy ? vx : vy; // minimum dimension GLint xl = ( vx - v ) / 2; GLint yb = ( vy - v ) / 2; glViewport( xl, yb, v, v ); // set the viewing volume: // remember that the Z clipping values are actually // given as DISTANCES IN FRONT OF THE EYE // USE gluOrtho2D( ) IF YOU ARE DOING 2D ! glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); if( WhichProjection == ORTHO ) glOrtho( -3., 3., -3., 3., 0.1, 1000. ); else gluPerspective( 90., 1., 0.1, 1000. ); // place the objects into the scene: glMatrixMode( GL_MODELVIEW ); glLoadIdentity( ); //project4 setting the light position //glLightfv(GL_LIGHT0, GL_POSITION, Array3(0., 0., 0.)); //glLightfv(GL_LIGHT1, GL_POSITION, Array3(0., 2., 0.)); // set the eye position, look-at position, and up-vector: gluLookAt( 0., 0., 3., 0., 0., 0., 0., 1., 0. ); // rotate the scene: glRotatef( (GLfloat)Yrot, 0., 1., 0. ); glRotatef( (GLfloat)Xrot, 1., 0., 0. ); // uniformly scale the scene: if( Scale < MINSCALE ) Scale = MINSCALE; glScalef( (GLfloat)Scale, (GLfloat)Scale, (GLfloat)Scale ); // set the fog parameters: if( DepthCueOn != 0 ) { glFogi( GL_FOG_MODE, FOGMODE ); glFogfv( GL_FOG_COLOR, FOGCOLOR ); glFogf( GL_FOG_DENSITY, FOGDENSITY ); glFogf( GL_FOG_START, FOGSTART ); glFogf( GL_FOG_END, FOGEND ); glEnable( GL_FOG ); } else { glDisable( GL_FOG ); } // possibly draw the axes: if( AxesOn != 0 ) { glColor3fv( &Colors[WhichColor][0] ); glCallList( AxesList ); } // draw the current object: //glTranslatef(0., .5, 1.); // since we are using glScalef( ), be sure normals get unitized: //project4 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, MulArray3(.3f, White)); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE); SetSpotLight(GL_LIGHT0, 2 * sin(Period), 0., 0., 0., 0., -1., 0., 0., 1.); SetPointLight(GL_LIGHT1, 1., 1.5, -1., 1., 1., 1.); //The light sources LIGHT0 AND LIGHT1: if (Light0On) glEnable(GL_LIGHT0); else glDisable(GL_LIGHT0); if (Light1On) glEnable(GL_LIGHT1); else glDisable(GL_LIGHT1); glEnable(GL_NORMALIZE); // specify shading to be flat or smooth: glShadeModel(GL_SMOOTH); //Draw a stationary light glPushMatrix(); glTranslatef(1., 1.5, -1.); glScalef(.3, .3, .3); glRotatef(180, 1., 0., 0.); Cone(); glPopMatrix(); //draw the moving bulb glColor3f(0., 0., 1.); glPushMatrix(); glTranslatef(1.2 * sin(Period), 0., 0.); MjbSphere(.1, 100, 100); glPopMatrix(); glEnable(GL_LIGHTING); //Draw one torus //make the torus as flat glShadeModel(GL_FLAT); glPushMatrix(); SetMaterial(1., 0., 0., 60.); glTranslatef(-1.7, 0., 0.); glRotatef(90., 0., 1., 0.); glScalef(.1, .1, .1); glutSolidTorus(2.7, 6., SLICES, SLICES); glPopMatrix(); //make the other objects smooth; glShadeModel(GL_SMOOTH); //Draw a rotating solid teapot SetMaterial(1., 1., 0., 1.5); glPushMatrix(); glTranslatef(1., .3,-1.); glRotatef(360. * Time, 0., 1., 0.); glutSolidTeapot(TeapotSize); glPopMatrix(); //the sphere with Texture mapping: SetMaterial(0., 1., 0.5, 20.); glPushMatrix(); glRotatef(360. * Time , 0., 1., 0.); printf("the time is %f\n", Time); glTranslatef(1., 2., -2.); TextureSphere(); glPopMatrix(); glDisable(GL_LIGHTING); // draw some gratuitous text that just rotates on top of the scene: // draw some gratuitous text that is fixed on the screen: // // the projection matrix is reset to define a scene whose // world coordinate system goes from 0-100 in each axis // // this is called "percent units", and is just a convenience // // the modelview matrix is reset to identity as we don't // want to transform these coordinates glDisable( GL_DEPTH_TEST ); glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); gluOrtho2D( 0., 100., 0., 100. ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity( ); // swap the double-buffered framebuffers: glutSwapBuffers( ); // be sure the graphics buffer has been sent: // note: be sure to use glFlush( ) here, not glFinish( ) ! glFlush( ); }
static int populate_lights(Vector *lights) { size_t i; size_t n; int s; hash *light_h; char *name, *type, *aux; Vector *v; set *S; float r, g, b, w; float rgb[3], pos4[4]; float cutoff = 30.0; float exp = 10.0; Light *l; if (!lights) return -1; n = sizeVector(lights); for(i = 0; i < n; i++) { S = CreateSet(); light_h = (hash *) atVector(lights, i); name = (char *) FindHashElement(light_h, "name"); type = (char *) FindHashElement(light_h, "type"); if (!check_light_type(type)) { fprintf(stderr, "[E] populate_lights: bad light type %s (possible values \"positional\", \"directional\", \"spotlight\")\n", type); exit(1); } if (!name) return i; if (!type) return i; InsertSetElement(S, "name"); InsertSetElement(S, "type"); l = SceneRegisterLight(name); v = (Vector *) FindHashElement(light_h, "pos"); if (v) { InsertSetElement(S, "pos"); read_xyz(v, &pos4[0], &pos4[1], &pos4[2]); pos4[3] = 1.0; if (!strcmp(type, "directional")) pos4[3] = 0.0; SetPositionLight(l, &pos4[0]); } v = (Vector *) FindHashElement(light_h, "amb"); if (v) { InsertSetElement(S, "amb"); read_xyz(v, &rgb[0], &rgb[1], &rgb[2]); SetAmbientLight(l, &rgb[0]); } v = (Vector *) FindHashElement(light_h, "dif"); if (v) { InsertSetElement(S, "dif"); read_xyz(v, &rgb[0], &rgb[1], &rgb[2]); SetDiffuseLight(l, &rgb[0]); } v = (Vector *) FindHashElement(light_h, "spec"); if (v) { InsertSetElement(S, "spec"); read_xyz(v, &rgb[0], &rgb[1], &rgb[2]); SetSpecularLight(l, &rgb[0]); } if (!strcmp(type, "spotlight")) { aux = (char *) FindHashElement(light_h, "exp"); if (aux) { InsertSetElement(S, "exp"); sscanf(aux, "%f", &exp); if (exp < 0) { fprintf(stderr, "[E] populate_lights: negative exponent\n"); exit(1); } free(aux); } aux = (char *) FindHashElement(light_h, "cutoff"); if (aux) { InsertSetElement(S, "cutoff"); sscanf(aux, "%f", &cutoff); if (exp < 0) { fprintf(stderr, "[E] populate_lights: negative cutoff\n"); exit(1); } free(aux); } rgb[0] = 0.577; rgb[1] = 0.577; rgb[2] = 0.577; v = (Vector *) FindHashElement(light_h, "spdir"); if (v) { InsertSetElement(S, "spdir"); read_xyz(v, &rgb[0], &rgb[1], &rgb[2]); } SetSpotLight(l, &rgb[0], cutoff, exp); } aux = (char *) FindHashElement(light_h, "switched"); if (aux) { InsertSetElement(S, "switched"); sscanf(aux, "%d", &s); if (!s) SwitchLight(l, 0); free(aux); } free(type); free(name); check_and_destroy("populate_lights", light_h, S); //DestroyHash(&light_h); } DestroyVector(&lights); return -1; }