class RenderWorld: '''This is the class that renders blocks (and the floor that they sit on). Camera angles are handled by another class''' WINDOW_WIDTH = 400 WINDOW_HEIGHT = 400 def __init__(self, file_name): '''Sets everything up: camera, modes, lighting, and the list of blocks''' self.camera = Camera() self.set_up_graphics() self.makeLights() self.objects = LoadWorld.load(file_name) # glMaterialfv(GL_FRONT, GL_SPECULAR, [1, 1, 1, 1]) # glMaterialfv(GL_FRONT, GL_SHININESS, [50]) glClearColor(.529,.8078,.980,0) glutDisplayFunc(self.display) glutKeyboardFunc(self.keyPressed) glutMainLoop() def set_up_graphics(self): '''Sets up the gl modes that are necessary''' glutInit() glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH) glutInitWindowSize(self.WINDOW_WIDTH, self.WINDOW_HEIGHT) glutCreateWindow('World!') glMatrixMode(GL_PROJECTION) gluPerspective(45,1,.15,100) glMatrixMode(GL_MODELVIEW) glEnable(GL_DEPTH_TEST) def makeLights(self): '''Sets up the light sources and their positions. We have an ambient light and two sets of specular/diffuse lights''' self.diffuse_pos1 = (50,5,0,1) glLightfv(GL_LIGHT0, GL_DIFFUSE, (.5, .5, .5, 1)) glLightfv(GL_LIGHT0, GL_POSITION, self.diffuse_pos1) glLightfv(GL_LIGHT1, GL_AMBIENT, (.2, .2, .2, 1)) glLightfv(GL_LIGHT1, GL_POSITION, (0, 0, 1, 0)) glLightfv(GL_LIGHT2, GL_SPECULAR, (.8, .8, .8, 1)) glLightfv(GL_LIGHT2, GL_POSITION, self.diffuse_pos1) self.diffuse_pos2 = (0,1,0,1) glLightfv(GL_LIGHT3, GL_DIFFUSE, (.5, .5, .5, 1)) glLightfv(GL_LIGHT3, GL_POSITION, self.diffuse_pos2) glLightfv(GL_LIGHT4, GL_SPECULAR, (.8, .8, .8, 1)) glLightfv(GL_LIGHT4, GL_POSITION, self.diffuse_pos2) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glEnable(GL_LIGHT1) glEnable(GL_LIGHT2) def display(self, x=0, y=0): '''Called for every refresh; redraws the floor and blocks and based on the camera angle''' glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() self.camera.renderCamera() self.renderLightSource() self.makeFloor() #Transparent blocks! glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) for obj in self.objects: glPushMatrix() if obj.get_type() == "block": self.draw_block(obj) elif obj.get_type() == "key": self.draw_key(obj) glPopMatrix() glDisable(GL_BLEND) glFlush() def draw_block(self, obj): #Set the blocks shininess, ambient, diffuse, and specular reflections. The blocks are slightly transparent. color = obj.get_color() pos = obj.get_pos() #glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, 75) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, [color[0], color[1], color[2], 1]) glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, [.4, .4, .4, 1]) #glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, [.9, .9, .9, .7]) glTranslate(pos[0],pos[1],pos[2]) glutSolidCube(2) def draw_key(self, obj): color = obj.get_color() pos = obj.get_pos() glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, [color[0], color[1], color[2], .7]) glTranslate(pos[0],pos[1],pos[2]) #glutSolidTorus(.05, .25, 3, 3) glutSolidCone(.1, 2.0, 30, 4) def keyPressed(self, key, x, y): '''Called when a key is pressed''' if key == 'a': self.camera.strafe(-.1) elif key == 'd': self.camera.strafe(.1) elif key == 'w': self.camera.walk(-.1) elif key == 's': self.camera.walk(.1) elif key == 'j': self.camera.rotate(0,3,0) elif key == 'l': self.camera.rotate(0,-3,0) elif key == 'i': self.camera.rotate(3,0,0) elif key == 'k': self.camera.rotate(-3,0,0) elif key == ' ': self.camera.height(.1) elif key == 'c': self.camera.height(-.1) self.display() def renderLightSource(self): '''Resets the light sources to the right position''' glLightfv(GL_LIGHT0, GL_POSITION, self.diffuse_pos1) glLightfv(GL_LIGHT2, GL_POSITION, self.diffuse_pos2) glLightfv(GL_LIGHT3, GL_POSITION, self.diffuse_pos2) glLightfv(GL_LIGHT4, GL_POSITION, self.diffuse_pos2) def makeFloor(self): '''Makes a floor of size size and places an image (texture) on it''' glEnable(GL_TEXTURE_2D) size = 50 image = Image.open("OtherMods/checkerboard.bmp") ix = image.size[0] iy = image.size[1] image = image.tostring("raw", "RGBX", 0, -1) glPixelStorei(GL_UNPACK_ALIGNMENT,1) glTexImage2D(GL_TEXTURE_2D, 0, 3, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL) glBegin(GL_QUADS) glTexCoord2f(0.0, 0.0) ; glVertex(-size,-.5,-size) glTexCoord2f(1.0, 0.0) ; glVertex(size,-.5,-size) glTexCoord2f(1.0, 1.0) ; glVertex(size,-.5,size) glTexCoord2f(0.0, 1.0) ; glVertex(-size,-.5,size) glEnd() glDisable(GL_TEXTURE_2D)