def drawRotor(self, pMat, mvMat, rotMat, transMat, speed): """ Zeichne den Rotor mit allen Matrizen. Berechne Drehungen korrekt @param pMat: perspektivische Matrix des Helis @param mvMat: modelviewMatrix der Camera @param rotMat: vorberechnete Rotationsmatrix des Rotors @param transMat: translationsmatrix des Helicopters @param speed: aktueller Auftrieb des Helicopters """ if self.start and self.r < self.maximum: if speed < 1: if self.r < self.maximum + 4: self.r += 0.009 elif speed > 1: if self.r > self.maximum - 1: self.r -= 0.009 else: if self.r < self.maximum - 1: self.r += 0.009 else: if self.r > 1: self.r -= 0.009 (a, b, c) = self.dist t = geo.translationMatrix(a, b, c) tb = geo.translationMatrix(-a, -b, -c) f = geo.rotationMatrix(self.r, self.axis) self.rotationMatrix = self.rotationMatrix * t * f * tb glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_NORMAL_ARRAY) glEnableClientState(GL_TEXTURE_COORD_ARRAY) glEnable(GL_TEXTURE_2D) modelMatrix = mvMat * transMat * rotMat * self.rotationMatrix mvpMatrix = pMat * modelMatrix normalMat = linalg.inv(modelMatrix[0:3, 0:3]).transpose() glUseProgram(self.program) geo.sendMat4(self.program, "mvMatrix", modelMatrix) geo.sendMat4(self.program, "mvpMatrix", mvpMatrix) geo.sendMat3(self.program, "normalMatrix", normalMat) self.useRotorGeometry() glDisableClientState(GL_VERTEX_ARRAY) glDisableClientState(GL_NORMAL_ARRAY) glDisableClientState(GL_TEXTURE_COORD_ARRAY)
def initHelicopter(self): self.objectVertices, self.objectTextures, self.objectNormals, self.objectFaces = self.objLoader.loadObjFile(self.filename) self.data, self.heli_vbo = self.objLoader.createDataFromObj() # Create BoundingBox self.boundingBox = [map(min, zip(*self.objectVertices)), map(max, zip(*self.objectVertices))] self.center = [(x[0]+x[1])/2.0 for x in zip(*self.boundingBox)] self.scaleFactor = 2.0/max([(x[1]-x[0]) for x in zip(*self.boundingBox)]) # Scale, Center self.handler.pushModelMatrix(geo.scaleMatrix(self.scaleFactor, self.scaleFactor, self.scaleFactor)) self.handler.pushModelMatrix(geo.translationMatrix(-self.center[0], -self.center[1], -self.center[2])) im = Image.open("./heli_data/500DLINE.JPG") width, height = im.size image = array(im)[::-1,:].tostring() self.textureIDs = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, self.textureIDs) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image) glGenerateMipmap(GL_TEXTURE_2D)
def step(self): """ Berechnet fuer alle Objekten die neue Position im neuen step Fuer den Helicopter wird eine Vorberechnung der moeglichen neuen Position durchgefuehrt und bei Erfolg diese Position uebernommen """ detection, padDetection = False, False # flag for collision detection # with all objects up = Vector(0, 1, 0).multMatrix(self.heli.actOri.T) temp = self.heli.position + (up + self.gravity) * 0.15 # kraftvektor temp += Vector(0, 1, 0) + self.gravity * self.heli.speed # auftrieb tempBB = map(lambda x: x + temp, self.heli.bb) if self.intersectBB(tempBB): detection = True else: for obj in self.objs: if self.intersect(tempBB, obj.bb): padDetection = True self.heli.speed = 1 self.collision = True if detection else False self.padCollision = True if padDetection else False if not self.collision and not self.padCollision: self.heli.position = temp (x, y, z) = self.heli.position self.heli.translationMatrix = geo.translationMatrix(x, y, z)
def reset(self): """ setzt helicopter an Ursprung zurueck """ self.heli.position = self.heli.startPosition self.heli.translationMatrix =\ geo.translationMatrix(self.heli.position[0], self.heli.position[1], self.heli.position[2]) self.heli.speed = 1 self.heli.actOri = geo.identity() self.heli.collision = False
def __init__(self, program, radius, gamesize, height, faces, x, z): """ Konstruktor fuer Helipad @param program: image-Shader fuer Helipad @param radius: Radius fuer die Saeule des Helipads @param gamesize: Groesse / 2 der Skybox @param height: Absolute Hoehe des Helipads @param faces: Flaechen der Saeule, Approximativer Kreis @param x: Verschiebung auf der Welt nach x @param z: Verschiebung auf der Welt nach z """ self.radius = radius self.height = height self.faces = faces self.program = program self.x = x self.z = z self.gamesize = gamesize self.tMat = geo.translationMatrix(x, -gamesize, z) self.initHeliPad()
def initHelicopter(self): self.objectVertices, self.objectTextures, self.objectNormals, self.objectFaces = self.objLoader.loadObjFile( self.filename) self.data, self.heli_vbo = self.objLoader.createDataFromObj() # Create BoundingBox self.boundingBox = [ map(min, zip(*self.objectVertices)), map(max, zip(*self.objectVertices)) ] self.center = [(x[0] + x[1]) / 2.0 for x in zip(*self.boundingBox)] self.scaleFactor = 2.0 / max([(x[1] - x[0]) for x in zip(*self.boundingBox)]) # Scale, Center self.handler.pushModelMatrix( geo.scaleMatrix(self.scaleFactor, self.scaleFactor, self.scaleFactor)) self.handler.pushModelMatrix( geo.translationMatrix(-self.center[0], -self.center[1], -self.center[2])) im = Image.open("./heli_data/500DLINE.JPG") width, height = im.size image = array(im)[::-1, :].tostring() self.textureIDs = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, self.textureIDs) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image) glGenerateMipmap(GL_TEXTURE_2D)
def gier(self, angle): self.handler.pushModelMatrix(geo.rotationMatrix(angle, (1, 0, 0))) self.handler.pushModelMatrix(geo.translationMatrix(0, 0, 1))
def initHelicopter(self): """ initialisiere Helicopter mit Hilfe von Obj-Loader """ # load obj File self.objectVertices, self.facesDic, self.matDic, self.bb =\ self.objLoader.loadObjFile(self.filename) (a, b, c), (d, e, f) = self.bb[0], self.bb[1] self.bb = [Vector(a, b, c), Vector(d, e, f)] self.startPosition[1] += abs(b) # vector self.position = self.startPosition self.translationMatrix = geo.translationMatrix(self.position[0], self.position[1], self.position[2]) # initial Helicopter Geometry self.initHelicopterGeometry() rojoData = self.objLoader.createData(self.facesDic["500-D_Rojo"]) maxRotor = [max([x[0] for x in rojoData]), max([x[1] for x in rojoData]), max([x[2] for x in rojoData])] minRotor = [min([x[0] for x in rojoData]), min([x[1] for x in rojoData]), min([x[2] for x in rojoData])] mittel = map(lambda x, y: x + (y - x) / 2, minRotor, maxRotor) #### MAIN self.mainRotor = Rotor(self.vboTextureDic[self.mainRotorMat][0], self.vboTextureDic[self.mainRotorMat][1], self.vboTextureDic[self.mainRotorMat][2], self.objLoader.createData(self.facesDic[ self.mainRotorMat]), self.matDic, self.program, self.mainRotorMat, "main", mittel) #### TAIL und FrontLine separieren tailRotorFaces = [] frontLine = [] for facesList in self.facesDic[self.tailRotorMat]: for faces in facesList: if [singleFace for singleFace in faces if singleFace > 2720]\ != []: tailRotorFaces.append([singleFace for singleFace in faces\ if singleFace > 2720]) if [singleFace for singleFace in faces if singleFace < 2720]\ != []: frontLine.append([singleFace for singleFace in faces]) self.tailRotorData = self.objLoader.createData([tailRotorFaces]) self.tailRotorDataLen = len(self.tailRotorData) self.tailRotorVbo = vbo.VBO(np.array(self.tailRotorData, 'f')) self.tailRotorTextureId = self.vboTextureDic[self.tailRotorMat][1] frontLineData = self.objLoader.createData([frontLine]) frontLineDataLen = len(frontLineData) self.frontLineDataLenList = [] self.frontLineDataLenList.append(frontLineDataLen) frontLineVbo = vbo.VBO(np.array(frontLineData, 'f')) self.frontLineVboList = [] self.frontLineVboList.append(frontLineVbo) frontLineTextureId = self.vboTextureDic[self.tailRotorMat][1] self.frontLineTextureIdList = [] self.frontLineTextureIdList.append(frontLineTextureId) maxRotor = [max([x[0] for x in self.tailRotorData]), max([x[1] for x in self.tailRotorData]), max([x[2] for x in self.tailRotorData])] minRotor = [min([x[0] for x in self.tailRotorData]), min([x[1] for x in self.tailRotorData]), min([x[2] for x in self.tailRotorData])] mittel = map(lambda x, y: x + (y - x) / 2, minRotor, maxRotor) self.tailRotor = Rotor(vbo.VBO(np.array(self.tailRotorData, 'f')), self.vboTextureDic[self.tailRotorMat][1], len(self.tailRotorData), self.objLoader.createData([tailRotorFaces]), self.matDic, self.program, self.tailRotorMat, "heck", mittel, 12) self.actOri = geo.identity() # Frontline zum vboTextureDic hinzufuegen del(self.vboTextureDic[self.tailRotorMat]) if not self.tailRotorMat in self.vboTextureDic.keys(): self.vboTextureDic[self.tailRotorMat] =\ self.vboTextureDic.get(self.tailRotorMat, self.frontLineVboList\ + self.frontLineTextureIdList\ + self.frontLineDataLenList) else: self.vboTextureDic[self.tailRotorMat].append(self.frontLineVboList\ + self.frontLineTextureIdList\ + self.frontLineDataLenList)