class RenderCube: '''This is the class that renders a rainbow cube Camera angles are handled by Camera.py. ''' WINDOW_WIDTH = 700 WINDOW_HEIGHT = 700 MAP_SIZE =100 def __init__(self): '''Sets up camera, modes, lighting, sounds, and objects.''' self.set_up_graphics() self.makeLights() self.camera = Camera(0,0,5) self.verts=self.GenArray() self.colors=self.GenArray('color') glClearColor(1,1,1,0) glutIdleFunc(self.display) glutDisplayFunc(self.display) glutIgnoreKeyRepeat(GLUT_KEY_REPEAT_OFF) glutKeyboardFunc(self.keyPressed) glutKeyboardUpFunc(self.keyUp) glutSetCursor(GLUT_CURSOR_NONE) glutPassiveMotionFunc(self.mouseMove) glutMainLoop() def set_up_graphics(self): '''Sets up OpenGL to provide double buffering, RGB coloring, depth testing, the correct window size, and a title.''' glutInit() glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) glutInitWindowSize(self.WINDOW_WIDTH, self.WINDOW_HEIGHT) glutCreateWindow('RainbowCube!') 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 cube at current position based on the camera angle.''' glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() self.camera.move() self.camera.renderCamera() self.renderLightSource() glPushMatrix() # Set the object shininess, ambient, diffuse, and # specular reflections. glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, 75) glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, [.4, .4, .4, 1]) glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, [.9, .9, .9, .8]) glTranslate(0,0,0) self.MakeCube() glPopMatrix() glutSwapBuffers() def mouseMove(self, x, y): '''Called when the mouse is moved.''' factor = 2 tmp_x = (self.camera.mouse_x - x)/factor tmp_y = (self.camera.mouse_y - y)/factor if tmp_x > self.camera.ROTATE: tmp_x = self.camera.ROTATE self.camera.rotate(0, tmp_x, 0) x = self.WINDOW_WIDTH/2 y = self.WINDOW_HEIGHT/2 glutWarpPointer(x, y) self.camera.mouse_x = x self.camera.mouse_y = y def keyPressed(self, key, x, y): '''Called when a key is pressed.''' if key.lower() in self.camera.keys: self.camera.keys[key.lower()] = True if glutGetModifiers() == GLUT_ACTIVE_SHIFT: self.camera.keys["shift"] = True if 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) elif key == 'x': exit(0) def keyUp(self, key, x, y): '''Called when a key is released.''' self.camera.keys[key.lower()] = False if not glutGetModifiers() == GLUT_ACTIVE_SHIFT: self.camera.keys["shift"] = False 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 GenArray(self, mode='vert',pts=POINTS): vert_ls=[] if mode=='color': for m in range(pts): for n in range(pts): for i in range(pts): tmp_tup=(i*.1,n*.1,m*.1,1) vert_ls.append(tmp_tup) elif mode=='vert': for m in range(pts): for n in range(pts): for i in range(pts): tmp_tup=(i*.1,n*.1,m*.1) vert_ls.append(tmp_tup) return vert_ls def MakeCube(self): '''Makes the desired cube.''' #TODO: GET COLORS WORKING/ PAD PIXELS ind=[] for i in range(len(self.verts)): ind.append(i) glEnableClientState(GL_COLOR_ARRAY) glEnableClientState(GL_VERTEX_ARRAY) glVertexPointer(3, GL_FLOAT, 0, self.verts) glColorPointer(4, GL_FLOAT, 0, self.colors) glBegin(GL_POINTS) for i in range(len(self.verts)): glArrayElement(i) glEnd() glDrawElements(GL_POINTS, len(self.verts), GL_UNSIGNED_INT,ind ) glDrawArrays(GL_POINTS, 0, len(self.verts)) glDisableClientState(GL_VERTEX_ARRAY) glDisableClientState(GL_COLOR_ARRAY) def get_visible(self, lst): '''Only draws the points sitting in front of the camera. Everything behind it is left undrawn.''' #works off a cube class that has all the points in it with methods to call information about dist, to_use = [] for point in lst: c = point.dist x,z = self.camera.project_move_other() b = self.camera.get_camera_distance(x, 0, z) a = point.get_dist(x, 0, z) angle = 0 try: num = ((b**2)+(c**2)-(a**2))/(2*b*c) angle = math.acos(num)/math.pi*180 except: pass if angle > 90: to_use.append(item) return to_use
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)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(1024, 768); glutInitWindowPosition(100, 100); glutCreateWindow('interactive') initGlutCallbacks() glClearColor(0.0, 0.0, 0.0, 0.0) createVBO() createIBO() compileShaders() camera = Camera() camera.verticalFOV =90.0 camera.height = 768 camera.width = 1024 camera.near = 1.0 camera.far = 100.0 camera.position.z = -3.0 camera.target.z = 2.0 tr = Transform() tr.position.set(0.0, 0.0, 4.0) glutMainLoop() # camera.target.z = 2.0 # camera.position.z = 10.0 # vpMat = camera.viewportTransform() # perMat = camera.perspectiveTransform()