/* Simple diamond shape, as an affine transformation of a cube. This example is not yet ready. Implemented Fall 2005 Scott D. Anderson */ #include #include twTriple vertices[8] = {{-1,-1,-1}, {1,-1,-1}, {1,1,-1}, {-1,1,-1}, {-1,-1,1}, {1,-1,1}, {1,1,1}, {-1,1,1}}; twTriple colors[8] = {{0,0,0}, {1,0,0}, {1,1,0}, {0,1,0}, {0,0,1}, {1,0,1}, {1,1,1}, {0,1,1}}; /* draw a face given four vertices */ void face(int a, int b, int c , int d) { glBegin(GL_POLYGON); glColor3fv(colors[a]); glVertex3fv(vertices[a]); glColor3fv(colors[b]); glVertex3fv(vertices[b]); glColor3fv(colors[c]); glVertex3fv(vertices[c]); glColor3fv(colors[d]); glVertex3fv(vertices[d]); glEnd(); } void colorcube(void) { /* map vertices to faces */ face(0,3,2,1); face(2,3,7,6); face(0,4,7,3); face(1,2,6,5); face(4,5,6,7); face(0,1,5,4); } /* Non-uniform scaling is a relatively common thing to do, but any rotations after that are usually difficult to understand. An alternative is to think "backwards," imagining that we start with the object, which in this case is the red cube, and work backwards through the affine transformations, without changing the coordinate system, just modifying the object. In this case, it's easy to see how the cube becomes a diamond. Trying to see how to put all the vertices of the cube on the axes before we do the non-uniform scaling is harder to see. */ void display(void) { twDisplayInit(); twCamera(); twColorName(TW_RED); glPushMatrix(); glScalef(0.5,2,1); glRotatef(45,0,0,1); glutWireCube(2); glPopMatrix(); 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); twBoundingBox(-3,+3,-3,+3,-3,+3); glLineWidth(3); // nice fat lines. In this program, we only need to say this once twMainInit(); glutMainLoop(); return 0; }