/* Using fog in a scene. Written by Caroline Geiersbach and Scott D. Anderson scott.anderson@acm.org Summer 2003 */ #include #include #include #include "tw.h" #include //overhead light source; directional GLfloat light1[4]={-12,8,5,0}; //texture mapping GLuint textureIDs[5]; GLfloat sgenparams[] = {0,1,0,0}; //lighting void lamp() { //table lamp GLfloat ambient1 [] = {0.7,0.7,0.6,1}; GLfloat diffuse1 [] = {0.7,0.7,0.6,1}; GLfloat specular1 [] = {0.7,0.7,0.6,1}; glLightfv(GL_LIGHT1, GL_POSITION, light1); glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1); glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1); glLightfv(GL_LIGHT1, GL_SPECULAR, specular1); glEnable(GL_LIGHT1); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); glEnable(GL_LIGHTING); } void fog() { glEnable(GL_FOG); GLfloat fogColor[4]={0.7,0.7,0.7}; GLuint fogMode =GL_EXP; glFogi(GL_FOG_MODE,fogMode); glFogfv(GL_FOG_COLOR,fogColor); glFogf(GL_FOG_DENSITY, 0.1); glHint(GL_FOG_HINT,GL_DONT_CARE); } //draws lamp from bedside table void drawLamp() { twTriple gray = {0.6, 0.6, 0.6}; twColor(gray,0.4,60); glPushMatrix(); glutSolidSphere(0.8,20,20); glTranslatef(0,0.9,0); glRotatef(-90,1,0,0); twTriple lightGray = {0.7, 0.7, 0.7}; twColor( lightGray,0.2,30); twCylinder(1.3,0.8,1.5,20,20); glPopMatrix(); } //draws a leg of the table void drawLeg(GLdouble top, GLdouble base, GLdouble height) { // twColor(0.8,0.3,0.3,128); twTriple brown = {0.5, 0.3, 0}; twColor(brown,0.1,15); glBindTexture(GL_TEXTURE_2D, textureIDs[1]); glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_GEN_S); glTexGeni(GL_S,GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); glTexGenfv(GL_S,GL_OBJECT_PLANE,sgenparams); glPushMatrix(); twSolidCylinder(top,base,height,20,20); glPopMatrix(); glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_GEN_S); } void drawTable() { glPushMatrix(); glScalef(1.5,1.5,1.5); drawLeg(3.8,4,0.5); //actually not a leg (table top) glPushMatrix(); glTranslatef(3*cos(M_PI/4.5),0,-3*sin(M_PI/4.5)); drawLeg(0.4,0.4,3.5); glPopMatrix(); glPushMatrix(); glTranslatef(-3*cos(M_PI/4.5),0,-3*sin(M_PI/4.5)); drawLeg(0.4,0.4,3.5); glPopMatrix(); //front left glPushMatrix(); glTranslatef(-3*cos(M_PI/4.5),0,3*sin(M_PI/4.5)); drawLeg(0.4,0.4,3.5); glPopMatrix(); //front right glPushMatrix(); glTranslatef(3*cos(M_PI/4.5),0,3*sin(M_PI/4.5)); drawLeg(0.4,0.4,3.5); glPopMatrix(); glTranslatef(-2,0.8,0); drawLamp(); glPopMatrix(); } void drawSides() { //ground glBindTexture(GL_TEXTURE_2D, textureIDs[0]); twTriple darkGray = {0.5, 0.5, 0.5}; twColor(darkGray,0.5, 128); glEnable(GL_TEXTURE_2D); glPushMatrix(); glScalef(30,30,30); // glNormal3f(0,1,0); twDrawUnitSquare(15,15); glPopMatrix(); glDisable(GL_TEXTURE_2D); twTriple lightGray = { 0.8, 0.8, 0.8 }; twColor(lightGray,0.8,50); glBindTexture(GL_TEXTURE_2D, textureIDs[2]); glEnable(GL_TEXTURE_2D); //left wall glPushMatrix(); //make sure that normals face inside the room glRotatef(270,0,0,1); glTranslatef(-30,0,0); //glNormal3f(0,-1,0); //NORMAL glScalef(30,30,30); twDrawUnitSquare(10,10); glPopMatrix(); //right wall glPushMatrix(); glTranslatef(30,0,0); glRotatef(90,0,0,1); glScalef(30,30,30); twDrawUnitSquare(10,10); glPopMatrix(); //back wall glPushMatrix(); glTranslatef(0,30,0); glRotatef(90,1,0,0); glScalef(30,30,30); twDrawUnitSquare(10,10); glPopMatrix(); glDisable(GL_TEXTURE_2D); //ceiling twTriple dark = {0.3, 0.3, 0.3}; twColor(dark, 0.4, 50); glPushMatrix(); glTranslatef(0,30,30); glRotatef(180,1,0,0); glScalef(30,30,30); twDrawUnitSquare(10,5); glPopMatrix(); } void display(void) { glClearColor(1,1,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); twCamera(); lamp(); fog(); //draw table and objects on table before everything //else so we do not get spotlight on tiled floor glPushMatrix(); glTranslatef(15,5.5,15); drawTable(); glPopMatrix(); //draw ceiling, ground, and walls drawSides(); glFlush(); glutSwapBuffers(); } /* Loads the textures */ void loadTextures() { glGenTextures(5,textureIDs); //generate as many textures as you load! twLoadTexture(textureIDs[0], "woodTile2.ppm"); //for ground twLoadTexture(textureIDs[1], "redWood.ppm"); //for table twLoadTexture(textureIDs[2], "grayFlowerWallpaper.ppm"); //for walls } int main(int argc, char **argv) { /* need both double buffering and z buffer */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); twInitWindowSize(650, 650); twBoundingBox(0,30,0,20,0,8); glutCreateWindow("room scene"); glutDisplayFunc(display); loadTextures(); glEnable(GL_NORMALIZE); twMainInit(); glutMainLoop(); return 0; }