/* Test of Angel's shadow example */ /* See pgs. 177-178 for his explanations of glVertexPointer, glColorPointer, glDrawElements */ #include #include #include "tw.h" GLfloat light[3]={5.0, 5.0, -1.0}; GLuint textureIDs[1]; void sun() { GLfloat ambient [] = {1,1,1,1}; GLfloat diffuse [] = {1,1,1,1}; GLfloat specular [] = {0.8,0.8,0.8,1}; glLightfv(GL_LIGHT1, GL_POSITION, light); glLightfv(GL_LIGHT1, GL_AMBIENT, ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT1, GL_SPECULAR, specular); glShadeModel(GL_SMOOTH); glEnable(GL_LIGHT1); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); } void drawColorToy() { glPushMatrix(); glScalef(1.7,0.3,1.7); twTriple purple = {0.5, 0, 0.5}; twColor(purple,0.8,100); glutSolidCube(1.0); //base glPopMatrix(); glPushMatrix(); glTranslatef(0,2,0); twSolidCylinder(0.1,0.2,2,20,20); //center shaft glPopMatrix(); glPushMatrix(); glRotatef(-90,1,0,0); //so toruses are facing right direction glScalef(1,1,0.7); glPushMatrix(); glTranslatef(0,0,0.5); twTriple blue = {0,0,0.8}; twColor(blue, 0.8, 100); glutSolidTorus(0.40,0.45,20,20); //blue torus twTriple darkGreen = {0, 0.5, 0}; twColor(darkGreen, 0.8, 100); glTranslatef(0,0,0.6); glutSolidTorus(0.30,0.35,20,20); //green torus twTriple yellow = {0.8, 0.8, 0}; twColor(yellow, 0.8, 100); glTranslatef(0,0,0.5); glutSolidTorus(0.25,0.30,20,20); //yellow torus twTriple orange = {1, 0.3, 0}; twColor(orange,0.8,100); glTranslatef(0,0,0.4); glutSolidTorus(0.20,0.25,20,20); //orange torus twTriple red = {0.8, 0, 0}; twColor(red, 0.8, 100); glTranslatef(0,0,0.35); glutSolidTorus(0.15,0.20,20,20); //red torus glPopMatrix(); glPopMatrix(); } void drawToyShadow() { glColor4f(0.3,0.3,0.3,0.2); glPushMatrix(); glScalef(1.7,0.3,1.7); glutSolidCube(1.0); //base glPopMatrix(); glPushMatrix(); glTranslatef(0,2,0); twSolidCylinder(0.1,0.2,2,20,20); //center shaft glPopMatrix(); glPushMatrix(); glRotatef(-90,1,0,0); //so toruses are facing right direction glScalef(1,1,0.7); glPushMatrix(); glTranslatef(0,0,0.5); glutSolidTorus(0.40,0.45,20,20); glTranslatef(0,0,0.6); glutSolidTorus(0.30,0.35,20,20); glTranslatef(0,0,0.5); glutSolidTorus(0.25,0.30,20,20); glTranslatef(0,0,0.4); glutSolidTorus(0.20,0.25,20,20); glTranslatef(0,0,0.35); glutSolidTorus(0.15,0.20,20,20); glPopMatrix(); glPopMatrix(); } void drawGround() { glBindTexture(GL_TEXTURE_2D, textureIDs[0]); glColor4f(0.6, 0.6, 0.6, 1); glEnable(GL_TEXTURE_2D); glPushMatrix(); glTranslatef(-10,0,-10); glScalef(20,20,20); twDrawUnitSquare(20,20); glPopMatrix(); glDisable(GL_TEXTURE_2D); } void loadTexture() { glGenTextures(1, textureIDs); twLoadTexture(textureIDs[0], "woodTile2.ppm"); } void display(void) { GLfloat m[16]; int i,j; for(i=0;i<16;i++) m[i]=0.0; m[0]=m[5]=m[10]=1.0; m[7]=-1.0/light[1]; twDisplayInit(); twCamera(); glEnable(GL_LIGHTING); glPushMatrix(); glTranslatef(0.15,0.15,0); drawColorToy(); glPopMatrix(); glPushMatrix(); glTranslatef(light[0], light[1],light[2]); twColorName(TW_BROWN); glutSolidSphere(0.1,10,10); glMultMatrixf(m); // flattens out the cube glTranslatef(-light[0], -light[1],-light[2]); glDisable(GL_LIGHTING); /* glEnable(GL_BLEND); glDepthMask(GL_FALSE); drawGround(); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);*/ drawToyShadow(); /* glDisable(GL_BLEND); glDepthMask(GL_TRUE);*/ glPopMatrix(); glutSwapBuffers(); } int main(int argc, char **argv) { /* need both double buffering and z buffer */ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); twInitWindowSize(500, 500); twBoundingBox(-4,4,0,5,-4,4); glutCreateWindow("colorcube"); glutDisplayFunc(display); loadTexture(); twMainInit(); sun(); glutMainLoop(); } /* saved version /* See pgs. 177-178 for his explanations of glVertexPointer, glColorPointer, glDrawElements */ /*#include #include #include "tw.h" //square figure GLfloat vertices[] = {-3.0,-2.0,-1.0, 1.0,-1.0,-1.0, 2.0,2.0,-2.0, -2.0,2.0,-2.0, -3.0,-2.0,2.0, 1.0,-1.0,1.0, 2.0,2.0,2.0,-2.0,2.0,2.0 }; GLfloat colors[] = {0.0,0.0,0.0,1.0,0.0,0.0, 1.0,1.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0, 1.0,0.0,1.0, 1.0,1.0,1.0, 0.0,1.0,1.0}; // colors for the shadow GLfloat bcolors[] = {0.0,0.0,0.0,0.0,0.0,0.0, 0.0,0.0,0.0, 0.0,0.0,0.0, 0.0,0.0,0.0, 0.0,0.0,0.0, 0.0,0.0,0.0, 0.0,0.0,0.0}; GLubyte cubeIndices[]={0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4}; void display(void) { GLfloat light[3]={0.0, 20.0, 0.0}; GLfloat m[16]; int i,j; for(i=0;i<16;i++) m[i]=0.0; m[0]=m[5]=m[10]=1.0; m[7]=-1.0/light[1]; glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); twCamera(); glPushMatrix(); glTranslatef(0.0, 3.0, 0.0); glColorPointer(3,GL_FLOAT, 0, colors); glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices); glPopMatrix(); glPushMatrix(); glTranslatef(light[0], light[1],light[2]); glMultMatrixf(m); // flattens out the cube glTranslatef(-light[0], -light[1],-light[2]); glColor3f(0.0,0.0,0.0); glColorPointer(3,GL_FLOAT, 0, bcolors); glDrawElements(GL_LINE_STRIP, 24, GL_UNSIGNED_BYTE, cubeIndices); glPopMatrix(); glutSwapBuffers(); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); twInitWindowSize(500, 500); twBoundingBox(-10,10,-10,10,-10,10); glutCreateWindow("colorcube"); glutDisplayFunc(display); twMainInit(); glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, vertices); glColorPointer(3,GL_FLOAT, 0, colors); glutMainLoop(); } */