def DrawInhabitants(nShadow): if nShadow == 0: glColor4f(1.0, 1.0, 1.0, 1.0) else: glColor4f(0.0, 0.0, 0.0, 0.6) # Shadow color # Draw the randomly located spheres glBindTexture(GL_TEXTURE_2D, textureObjects[SPHERE_TEXTURE]) for sphere in spheres: glPushMatrix() sphere.ApplyActorTransform() glutSolidSphere(0.3, 21, 11) glPopMatrix() glPushMatrix() glTranslatef(0.0, 0.1, -2.5) glPushMatrix() glRotatef(-yRot * 2.0, 0.0, 1.0, 0.0) glTranslatef(1.0, 0.0, 0.0) glutSolidSphere(0.1, 21, 11) glPopMatrix() if nShadow == 0: # Torus alone will be specular glMaterialfv(GL_FRONT, GL_SPECULAR, fBrightLight) glRotatef(yRot, 0.0, 1.0, 0.0) glBindTexture(GL_TEXTURE_2D, textureObjects[TORUS_TEXTURE]) gltDrawTorus(0.35, 0.15, 61, 37) glMaterialfv(GL_FRONT, GL_SPECULAR, fNoLight) glPopMatrix()
def on_draw(self): # Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Draw the ground, we do manual shading to a darker green # in the background to give the illusion of depth glBegin(GL_QUADS) glColor3ub(0, 32, 0) glVertex3f(400.0, -150.0, -200.0) glVertex3f(-400.0, -150.0, -200.0) glColor3ub(0, 255, 0) glVertex3f(-400.0, -150.0, 200.0) glVertex3f(400.0, -150.0, 200.0) glEnd() # Save the matrix state and do the rotations glPushMatrix() # Draw jet at new orientation, put light in correct position # before rotating the jet glEnable(GL_LIGHTING) glLightfv(GL_LIGHT0, GL_POSITION, lightPos) glRotatef(xRot, 1.0, 0.0, 0.0) glRotatef(yRot, 0.0, 1.0, 0.0) DrawJet(0) # Restore original matrix state glPopMatrix() # Get ready to draw the shadow and the ground # First disable lighting and save the projection state glDisable(GL_DEPTH_TEST) glDisable(GL_LIGHTING) glPushMatrix() # Multiply by shadow projection matrix glMultMatrixf(shadowMat) # Now rotate the jet around in the new flattend space glRotatef(xRot, 1.0, 0.0, 0.0) glRotatef(yRot, 0.0, 1.0, 0.0) # Pass true to indicate drawing shadow DrawJet(1) # Restore the projection to normal glPopMatrix() # Draw the light source glPushMatrix() glTranslatef(lightPos[0], lightPos[1], lightPos[2]) glColor3ub(255, 255, 0) glutSolidSphere(5.0, 10, 10) glPopMatrix() # Restore lighting state variables glEnable(GL_DEPTH_TEST)
def DrawGeometry(): # Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glPushMatrix() DrawGround() # Place the moving sphere glColor3f(1.0, 0.0, 0.0) glTranslatef(0.0, 0.5, -3.5) glRotatef(-(yRot * 2.0), 0.0, 1.0, 0.0) glTranslatef(1.0, 0.0, 0.0) glutSolidSphere(0.1, 17, 9) glPopMatrix()
def DrawWorld(): glColor3f(1.0, 0.0, 0.0) glPushMatrix() glTranslatef(0.0, 0.5, -3.5) glPushMatrix() glRotatef(-yRot * 2.0, 0.0, 1.0, 0.0) glTranslatef(1.0, 0.0, 0.0) glutSolidSphere(0.1, 17, 9) glPopMatrix() glRotatef(yRot, 0.0, 1.0, 0.0) gltDrawTorus(0.35, 0.15, 61, 37) glPopMatrix()
def DrawModels(drawBasePlane): if drawBasePlane: # Draw plane that the objects rest on glColor3f(0.0, 0.0, 0.90) # Blue glNormal3f(0.0, 1.0, 0.0) glBegin(GL_QUADS) glVertex3f(-100.0, -25.0, -100.0) glVertex3f(-100.0, -25.0, 100.0) glVertex3f(100.0, -25.0, 100.0) glVertex3f(100.0, -25.0, -100.0) glEnd() # Draw red cube glColor3f(1.0, 0.0, 0.0) glutSolidCube(48.0) # Draw green sphere glColor3f(0.0, 1.0, 0.0) glPushMatrix() glTranslatef(-60.0, 0.0, 0.0) glutSolidSphere(25.0, 50, 50) glPopMatrix() # Draw yellow cone glColor3f(1.0, 1.0, 0.0) glPushMatrix() glRotatef(-90.0, 1.0, 0.0, 0.0) glTranslatef(60.0, 0.0, -24.0) glutSolidCone(25.0, 50.0, 50, 50) glPopMatrix() # Draw magenta torus glColor3f(1.0, 0.0, 1.0) glPushMatrix() glTranslatef(0.0, 0.0, 60.0) glutSolidTorus(8.0, 16.0, 50, 50) glPopMatrix() # Draw cyan octahedron glColor3f(0.0, 1.0, 1.0) glPushMatrix() glTranslatef(0.0, 0.0, -60.0) glScalef(25.0, 25.0, 25.0) glutSolidOctahedron() glPopMatrix()
def DrawGLScene(): if iShade == MODE_FLAT: glShadeModel(GL_FLAT) else: # iShade = MODE_SMOOTH glShadeModel(GL_SMOOTH) # Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # First place the light # Save the coordinate transformation glPushMatrix() # Rotate coordinate system glRotatef(yRot, 0.0, 1.0, 0.0) glRotatef(xRot, 1.0, 0.0, 0.0) # Specify new position and direction in rotated coords. glLightfv(GL_LIGHT0, GL_POSITION, lightPos) glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDir) # Draw a red cone to enclose the light source glColor3ub(255, 0, 0) # Translate origin to move the cone out to where the light # is positioned. glTranslatef(lightPos[0], lightPos[1], lightPos[2]) glutSolidCone(4.0, 6.0, 15, 15) # Draw a smaller displaced sphere to denote the light bulb # Save the lighting state variables glPushAttrib(GL_LIGHTING_BIT) # Turn off lighting and specify a bright yellow sphere glDisable(GL_LIGHTING) glColor3ub(255, 255, 0) glutSolidSphere(3.0, 15, 15) # Restore lighting state variables glPopAttrib() # Restore coordinate transformations glPopMatrix() # Set material color and draw a sphere in the middle glColor3ub(0, 0, 255) if iTess == MODE_VERYLOW: glutSolidSphere(30.0, 7, 7) else: if iTess == MODE_MEDIUM: glutSolidSphere(30.0, 15, 15) else: # iTess = MODE_VERYHIGH glutSolidSphere(30.0, 50, 50) glutSwapBuffers()
def on_draw(self): if iShade == MODE_FLAT: glShadeModel(GL_FLAT) else: # iShade = MODE_SMOOTH glShadeModel(GL_SMOOTH) # Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # First place the light # Save the coordinate transformation glPushMatrix() # Rotate coordinate system glRotatef(yRot, 0.0, 1.0, 0.0) glRotatef(xRot, 1.0, 0.0, 0.0) # Specify new position and direction in rotated coords. glLightfv(GL_LIGHT0,GL_POSITION,lightPos) glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotDir) # Draw a red cone to enclose the light source glColor3ub(255,0,0) # Translate origin to move the cone out to where the light # is positioned. glTranslatef(lightPos[0],lightPos[1],lightPos[2]) glutSolidCone(4.0,6.0,15,15) # Draw a smaller displaced sphere to denote the light bulb # Save the lighting state variables glPushAttrib(GL_LIGHTING_BIT) # Turn off lighting and specify a bright yellow sphere glDisable(GL_LIGHTING) glColor3ub(255,255,0) glutSolidSphere(3.0, 15, 15) # Restore lighting state variables glPopAttrib() # Restore coordinate transformations glPopMatrix() # Set material color and draw a sphere in the middle glColor3ub(0, 0, 255) if iTess == MODE_VERYLOW: glutSolidSphere(30.0, 7, 7) else: if iTess == MODE_MEDIUM: glutSolidSphere(30.0, 15, 15) else: # iTess = MODE_VERYHIGH glutSolidSphere(30.0, 50, 50)
def DrawInhabitants(nShadow): global yRot if nShadow == 0: pass #yRot += 0.5 else: glColor3f(0.0, 0.0, 0.0) # Draw the randomly located spheres if nShadow == 0: glColor3f(0.0, 1.0, 0.0) for sphere in spheres: glPushMatrix() sphere.ApplyActorTransform() glutSolidSphere(0.3, 17, 9) glPopMatrix() glPushMatrix() # -y is up in pyglet glTranslatef(0.0, 0.1, -2.5) if nShadow == 0: glColor3f(0.0, 0.0, 1.0) glPushMatrix() glRotatef(-yRot * 2.0, 0.0, 1.0, 0.0) glTranslatef(1.0, 0.0, 0.0) glutSolidSphere(0.1, 17, 9) glPopMatrix() if nShadow == 0: # Torus alone will be specular glColor3f(1.0, 0.0, 0.0) glMaterialfv(GL_FRONT, GL_SPECULAR, fBrightLight) glRotatef(yRot, 0.0, 1.0, 0.0) gltDrawTorus(0.35, 0.15, 61, 37) glMaterialfv(GL_FRONT, GL_SPECULAR, fNoLight) glPopMatrix()
def DrawGLScene(): # Clear the window with the current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glPushMatrix() frameCamera.ApplyCameraTransform() # Draw the ground DrawGround() # Draw the randomly located spheres for sphere in spheres: glPushMatrix() sphere.ApplyActorTransform() glutSolidSphere(0.1, 13, 26) glPopMatrix() glPushMatrix() glTranslatef(0.0, 0.0, -2.5) glPushMatrix() glRotatef(-yRot * 2.0, 0.0, 1.0, 0.0) glTranslatef(1.0, 0.0, 0.0) glutSolidSphere(0.1, 13, 26) glPopMatrix() glRotatef(yRot, 0.0, 1.0, 0.0) gltDrawTorus(0.35, 0.15, 40, 20) glPopMatrix() glPopMatrix() glutSwapBuffers()
def on_draw(self): # Clear the window with the current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glPushMatrix() frameCamera.ApplyCameraTransform() # Draw the ground DrawGround() # Draw the randomly located spheres for sphere in spheres: glPushMatrix() sphere.ApplyActorTransform() glutSolidSphere(0.1, 13, 26) glPopMatrix() glPushMatrix() glTranslatef(0.0, 0.0, -2.5) glPushMatrix() glRotatef(-yRot * 2.0, 0.0, 1.0, 0.0) glTranslatef(1.0, 0.0, 0.0) glutSolidSphere(0.1, 13, 26) glPopMatrix() glRotatef(yRot, 0.0, 1.0, 0.0) gltDrawTorus(0.35, 0.15, 40, 20) glPopMatrix() glPopMatrix()
def on_draw(self): # Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Draw the ground, we do manual shading to a darker green # in the background to give the illusion of depth glBegin(GL_QUADS) glColor3ub(0,32,0) glVertex3f(400.0, -150.0, -200.0) glVertex3f(-400.0, -150.0, -200.0) glColor3ub(0,255,0) glVertex3f(-400.0, -150.0, 200.0) glVertex3f(400.0, -150.0, 200.0) glEnd() # Save the matrix state and do the rotations glPushMatrix() # Draw jet at new orientation, put light in correct position # before rotating the jet glEnable(GL_LIGHTING) glLightfv(GL_LIGHT0,GL_POSITION,lightPos) glRotatef(xRot, 1.0, 0.0, 0.0) glRotatef(yRot, 0.0, 1.0, 0.0) DrawJet(0) # Restore original matrix state glPopMatrix() # Get ready to draw the shadow and the ground # First disable lighting and save the projection state glDisable(GL_DEPTH_TEST) glDisable(GL_LIGHTING) glPushMatrix() # Multiply by shadow projection matrix glMultMatrixf(shadowMat) # Now rotate the jet around in the new flattend space glRotatef(xRot, 1.0, 0.0, 0.0) glRotatef(yRot, 0.0, 1.0, 0.0) # Pass true to indicate drawing shadow DrawJet(1) # Restore the projection to normal glPopMatrix() # Draw the light source glPushMatrix() glTranslatef(lightPos[0],lightPos[1], lightPos[2]) glColor3ub(255,255,0) glutSolidSphere(5.0,10,10) glPopMatrix() # Restore lighting state variables glEnable(GL_DEPTH_TEST)
def gltDrawSphere(a, b, c): glutSolidSphere(a, b, c)
def InitGL(Width, Height): global mShadowMatrix # pyglet reverses y axis vPoints = (M3DVector3f * 3)((0.0, -0.4, 0.0), (10.0, -0.4, 0.0), (5.0,-0.4, -5.0) ) # Grayish background glClearColor(fLowLight[0], fLowLight[1], fLowLight[2], fLowLight[3]) # Clear stencil buffer with zero, increment by one whenever anybody # draws into it. When stencil function is enabled, only write where # stencil value is zero. This prevents the transparent shadow from drawing # over itself glStencilOp(GL_INCR, GL_INCR, GL_INCR) glClearStencil(0) glStencilFunc(GL_EQUAL, 0x0, 0x01) # Cull backs of polygons glCullFace(GL_BACK) glFrontFace(GL_CCW) glEnable(GL_CULL_FACE) glEnable(GL_DEPTH_TEST) glEnable(GL_MULTISAMPLE_ARB) # Setup light parameters glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fNoLight) glLightfv(GL_LIGHT0, GL_AMBIENT, fLowLight) glLightfv(GL_LIGHT0, GL_DIFFUSE, fBrightLight) glLightfv(GL_LIGHT0, GL_SPECULAR, fBrightLight) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) # TODO: this doesn't seem to be enough to make this work on my computer? glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR) # Calculate shadow matrix pPlane = m3dGetPlaneEquation(vPoints[0], vPoints[1] , vPoints[2]) mShadowMatrix = m3dMakePlanarShadowMatrix(pPlane, fLightPos) # Mostly use material tracking glEnable(GL_COLOR_MATERIAL) glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) glMaterialfv(GL_FRONT, GL_SPECULAR, fBrightLight) glMateriali(GL_FRONT, GL_SHININESS, 128) # Randomly place sphere inhabitants for sphere in spheres: # Pick a random location between -20 and 20 at .1 increments sphere.setOrigin(float(randint(-200, 200)) * 0.1, 0.0, float(randint(-200, 200)) * 0.1) # Set up texture maps glEnable(GL_TEXTURE_2D) glGenTextures(NUM_TEXTURES, textureObjects) glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) for iLoop in range(0, NUM_TEXTURES): glBindTexture(GL_TEXTURE_2D, textureObjects[iLoop]) # Load this texture map img = Image.open(szTextureFiles[iLoop]).convert("RGB") raw_image = img.tobytes() gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, img.width, img.height, GL_RGB, GL_UNSIGNED_BYTE, raw_image) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) # Create display lists firstlist = glGenLists(3) groundList = GLuint(firstlist) sphereList = GLuint(firstlist + 1) torusList = GLuint(firstlist + 2) print (firstlist) # Create sphere display list glNewList(sphereList, GL_COMPILE) glutSolidSphere(0.1, 40, 20) glEndList() # Create torus display list glNewList(torusList, GL_COMPILE) gltDrawTorus(0.35, 0.15, 61, 37) glEndList() # Create the ground display list glNewList(groundList, GL_COMPILE) DrawGround() glEndList()
def __init__(self, *args, **kwargs): global groundList, sphereList, torusList window.Window.__init__(self, *args, **kwargs) # pyglet reverses y axis vPoints = (M3DVector3f * 3)((0.0, -0.4, 0.0), (10.0, -0.4, 0.0), (5.0,-0.4, -5.0) ) # Grayish background glClearColor(fLowLight[0], fLowLight[1], fLowLight[2], fLowLight[3]) # Clear stencil buffer with zero, increment by one whenever anybody # draws into it. When stencil function is enabled, only write where # stencil value is zero. This prevents the transparent shadow from drawing # over itself glStencilOp(GL_INCR, GL_INCR, GL_INCR) glClearStencil(0) glStencilFunc(GL_EQUAL, 0x0, 0x01) # Cull backs of polygons glCullFace(GL_BACK) glFrontFace(GL_CCW) glEnable(GL_CULL_FACE) glEnable(GL_DEPTH_TEST) glEnable(GL_MULTISAMPLE_ARB) # Setup light parameters glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fNoLight) glLightfv(GL_LIGHT0, GL_AMBIENT, fLowLight) glLightfv(GL_LIGHT0, GL_DIFFUSE, fBrightLight) glLightfv(GL_LIGHT0, GL_SPECULAR, fBrightLight) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) # TODO: this doesn't seem to be enough to make this work on my computer? glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR) # Calculate shadow matrix pPlane = m3dGetPlaneEquation(vPoints[0], vPoints[1] , vPoints[2]) mShadowMatrix = m3dMakePlanarShadowMatrix(pPlane, fLightPos) # Mostly use material tracking glEnable(GL_COLOR_MATERIAL) glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE) glMaterialfv(GL_FRONT, GL_SPECULAR, fBrightLight) glMateriali(GL_FRONT, GL_SHININESS, 128) # Randomly place sphere inhabitants for sphere in spheres: # Pick a random location between -20 and 20 at .1 increments sphere.setOrigin(float(randint(-200, 200)) * 0.1, 0.0, float(randint(-200, 200)) * 0.1) # Set up texture maps glEnable(GL_TEXTURE_2D) glGenTextures(NUM_TEXTURES, textureObjects) glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) for iLoop in range(0, NUM_TEXTURES): glBindTexture(GL_TEXTURE_2D, textureObjects[iLoop]) # Load this texture map img = pyglet.image.load(szTextureFiles[iLoop]) gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, img.width, img.height, GL_RGB, GL_UNSIGNED_BYTE, img.get_data('RGB', img.pitch)) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) # Create display lists firstlist = glGenLists(3) groundList = GLuint(firstlist) sphereList = GLuint(firstlist + 1) torusList = GLuint(firstlist + 2) print firstlist # Create sphere display list glNewList(sphereList, GL_COMPILE) glutSolidSphere(0.1, 40, 20) glEndList() # Create torus display list glNewList(torusList, GL_COMPILE) gltDrawTorus(0.35, 0.15, 61, 37) glEndList() # Create the ground display list glNewList(groundList, GL_COMPILE) DrawGround() glEndList()