/* Demo of the classic barn object. This uses two helper functions that improve the abstraction and brevity of the code. Implemented Fall 2007 Scott D. Anderson */ #include #include const int barnWidth =30; const int barnHeight=40; const int barnLength=50; twTriple barn[10] = { {0,0,0}, //left, bottom, front {barnWidth,0,0}, // right, bottom, front {barnWidth,barnHeight,0}, // right, top, front {0,barnHeight,0}, // left, top, front {barnWidth/2.0,barnHeight+barnWidth/2.0,0}, // ridge, front {0,0,-barnLength}, // left, bottom, back {barnWidth,0,-barnLength}, // right, bottom, back {barnWidth,barnHeight,-barnLength}, //right, top, back {0,barnHeight,-barnLength}, // left, top, back {barnWidth/2.0,barnHeight+barnWidth/2.0,-barnLength},// ridge, back }; void drawTri(twTriple verts[],int a, int b, int c) { glBegin(GL_TRIANGLES); glVertex3fv(verts[a]); glVertex3fv(verts[b]); glVertex3fv(verts[c]); glEnd(); } void drawQuad(twTriple verts[],int a, int b, int c, int d) { glBegin(GL_QUADS); glVertex3fv(verts[a]); glVertex3fv(verts[b]); glVertex3fv(verts[c]); glVertex3fv(verts[d]); glEnd(); } // Almost every facet is a different color so that we can see them. void drawBarn() { twColorName(TW_RED); // set color to red drawQuad(barn,0,1,2,3); // front drawTri(barn,3,2,4); twColorName(TW_GREEN); drawQuad(barn,5,6,7,8); // back drawTri(barn,7,8,9); twColorName(TW_PURPLE); drawQuad(barn,0,3,8,5); // left side twColorName(TW_MAROON); drawQuad(barn,1,2,7,6); // right side twColorName(TW_OLIVE); drawQuad(barn,3,4,9,8); // left roof drawQuad(barn,2,4,9,7); // right roof twError(); // check for errors after a sequence of // geometric primitives } void display(void) { twDisplayInit(); twCamera(); drawBarn(); glFlush(); glutSwapBuffers(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); twInitWindowSize(500,500); glutCreateWindow(argv[0]); glutDisplayFunc(display); twVertexArray(barn, 10); // determines dimensions of barn twMainInit(); glutMainLoop(); return 0; }