//sample 3d 1 floor building (just floor and walls) // perspective viewing, camera is infront of doorway, relatively far // no interaction #include #include"RGBpixmap.h" #include #define HL 15 // house length #define HW 9 // house width #define TH 0.15 //thickness of floor and walls #define HH 1.25 // wall heigh #define DW 1.5 // doorway width #define COLUMN_RAD 0.5 #define TEXBRK 2001 //texture name for brick #define NUM_BRK_HORIZONTAL 10 #define NUM_BRK_VERTICAL 5 void myinit(); void display(); void kbd(unsigned char key, int x, int y); // keyboard call back void brick(double x, double y, double z); //builds one panel with dimensions, x,y,z void house(); //builds the house void tcolumn(double rad, double height);// builds textured column at the origin RGBpixmap pix[1]; // global object, image array, see RGBpixmap.h GLUquadricObj *cyl; GLdouble theta = 0; int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(700,700); glutInitWindowPosition(0,0); glutCreateWindow("Press u to change view"); glutDisplayFunc(display); glutKeyboardFunc(kbd); myinit(); glutMainLoop(); return 0; } void kbd(unsigned char key, int x, int y) { if (key == 'q'|| key == 'Q') exit(0); if (key == 'u' || key == 'U') theta += 2; glutPostRedisplay(); } void myinit() { glEnable(GL_DEPTH_TEST); glClearColor(1.0,1.0,1.0,0.0); glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL); pix[0].readBMPFile("brk.bmp"); pix[0].setTexture(TEXBRK); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-3.0,3.0,-3,3,5,100); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } //textured brick colum void tcolumn(double rad, double height) { glPushMatrix(); /*** texture column ***/ glEnable(GL_TEXTURE_2D); cyl = gluNewQuadric(); gluQuadricTexture(cyl, GL_TRUE); gluCylinder(cyl,rad,rad,height, 20, NUM_BRK_VERTICAL); glDisable(GL_TEXTURE_2D); gluDeleteQuadric(cyl); glPopMatrix(); } void display() { glLoadIdentity(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //change view //change view glTranslatef(-HL/1.9,-HH/2,-2*HW) ; // at the level of 4/5 of wall height, glRotatef(theta,1,0,0); //draw house house(); glFlush(); glutSwapBuffers(); } //brick with lower left corner at the origin void brick(double x, double y, double z) { glPushMatrix(); glTranslatef(x*0.5, y*0.5, -z*0.5); glScaled(x*0.5,y*0.5, z*0.5); glutSolidCube(2.0); glPopMatrix(); } //each wall panel and the floor is bouilt from a single brick that is scaled appropriately //the floor is placed first, then the whole construction of all walls is placed on top // of it, by a translation T1 // Since each wall (floor) is a "brick" it has local coordiante system lower left corner // The whole house has a coordinate system the lower left corner of the floor (that is the // world coordiante system. // //The tree model is very simple: // floor // |T1 // ---------------------------------------------------------------------- // | | | | | | | // front_left front_right side_left side_right back interior_left interor_right // // floor, outsie walls, interior walls are all in different colors void house() { //glRotatef(90,1, 0,0); // uncomment this to see a top view glPushMatrix(); glPushAttrib(GL_ALL_ATTRIB_BITS); glColor3f(0.6,0.5,0.1); brick(HL,TH,HW); // floor glPushMatrix(); // put textured column up and place on side of front openning of the house glPushMatrix(); glTranslatef((HL-DW)/2,0, 1.5*COLUMN_RAD); glRotatef(-90,1,0,0); tcolumn(COLUMN_RAD, 2*HH); glPopMatrix(); glTranslatef(0,TH,0); // place all walls on top of floor //side walls of house glColor3f(0.7, 0.5, 6.0); brick((HL-DW)/2,HH,TH); // front left panel //***********face with bricks outside front left panel******** // create a textured polygon same size as the wall, place it // infront of the wall glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D,TEXBRK); glBegin(GL_QUADS); glTexCoord2f(0.0,0.0); glVertex3f(0.0f, 0.0f, 0.1f); glTexCoord2f(NUM_BRK_HORIZONTAL,0.0); glVertex3f((HL-DW)/2, 0.0f, 0.1f); glTexCoord2f(NUM_BRK_HORIZONTAL, NUM_BRK_VERTICAL); glVertex3f((HL-DW)/2, HH, 0.1f); glTexCoord2f(0.0,NUM_BRK_VERTICAL); glVertex3f(0.0f, HH, 0.1f); glEnd(); glDisable(GL_TEXTURE_2D); //****** texture disabled glPushMatrix(); glTranslatef((HL-DW)/2+DW, 0, 0); // place right panel in place among other walls brick((HL-DW)/2,HH,TH); // front right panel glPopMatrix(); glPushMatrix(); glTranslatef(0,0,-TH); // place left side panel in place among other walls brick(TH,HH,HW-2*TH); //left sise panel glPopMatrix(); glPushMatrix(); glTranslatef(HL-TH,0,-TH); // place right side panel in place among other walls brick(TH,HH,HW-2*TH); // right side panel glPopMatrix(); glPushMatrix(); glTranslatef(0,0,-HW+TH); //put back wall in place brick(HL,HH,TH); // back wall // interior walls glColor3f(0.5, 0.5, 0.6); glPopMatrix(); glPushMatrix(); glColor3f(0.5,0.5,0.5); glTranslatef((HL-DW)/2,0,-HW/3); brick(TH,HH,2*HW/3); //left glPopMatrix(); glTranslatef((HL-DW)/2+DW,0,-TH); brick(TH,HH,2*HW/3); // right glPopMatrix(); glPopAttrib(); glPopMatrix(); } #include"RGBpixmap.cpp"