def move_all(self): self.check_life() if not self.life_status: return self.move(self.movement_queue) movement = tr.identity() if self.next_direction == "W": movement = tr.translate(0, 1, 0) self.locationY += 1 elif self.next_direction == "A": movement = tr.translate(-1, 0, 0) self.locationX += -1 elif self.next_direction == "S": movement = tr.translate(0, -1, 0) self.locationY += -1 elif self.next_direction == "D": movement = tr.translate(1, 0, 0) self.locationX += 1 self.queue_movement(self.next_direction) self.last_direction = self.next_direction if self.check_apple(): self.eat_apple() self.last_position = [self.locationX, self.locationY] self.occupied_positions = [self.last_position ] + self.occupied_positions[1:] sg.findNode(self.model, "snake_head").transform = tr.matmul( [sg.findTransform(self.model, "snake_head"), movement])
def getTransform(showTransform, theta): if showTransform == TR_STANDARD: return tr.identity() elif showTransform == TR_ROTATE_ZP: return tr.rotationZ(theta) elif showTransform == TR_ROTATE_ZM: return tr.rotationZ(-theta) elif showTransform == TR_TRANSLATE: return tr.translate(0.3 * np.cos(theta), 0.3 * np.cos(theta), 0) elif showTransform == TR_UNIFORM_SCALE: return tr.uniformScale(0.7 + 0.5 * np.cos(theta)) elif showTransform == TR_NONUNIF_SCALE: return tr.scale(1.0 - 0.5 * np.cos(1.5 * theta), 1.0 + 0.5 * np.cos(2 * theta), 1.0) elif showTransform == TR_REFLEX_Y: return tr.scale(1, -1, 1) elif showTransform == TR_SHEARING_XY: return tr.shearing(0.3 * np.cos(theta), 0, 0, 0, 0, 0) else: # This should NEVER happend raise Exception()
def drawSceneGame(node, pipeline, transformName, delta_time, isPaused, parentTransform=tr.identity()): assert (isinstance(node, sg.SceneGraphNode)) newTransform = np.matmul(parentTransform, node.transform) #se ha encontrado una animacion para dibujar if len(node.childs) == 1 and isinstance(node.childs[0], anim.Anim_Controller): node.childs[0].Update(pipeline, delta_time, newTransform, isPaused) #las hitboxes no se dibujan con este pipeline elif isinstance(node, cl.CollisionShape): return #si se encuentra una textura la dibuja elif len(node.childs) == 1 and isinstance(node.childs[0], es.GPUShape): leaf = node.childs[0] glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, transformName), 1, GL_TRUE, newTransform) pipeline.drawShape(leaf) else: for child in node.childs: drawSceneGame(child, pipeline, transformName, delta_time, isPaused, newTransform)
def setTransform(n, theta, translation, scale, x): t = translation shearing = tr.shearing(0.1 * np.sin(theta), 0.1 * np.cos(theta), 0, 0, 0, 0) scale = tr.uniformScale(scale) theta = theta / 4 if n == 1: tx = 0.7 * np.cos(theta / (2 * x)) ty = 0.4 * np.cos(theta / x) + 0.3 * np.cos(theta) elif n == 2: tx = 0.4 * np.sin(theta) + 0.7 * np.cos(theta / 2) ty = 0.2 * np.sin(theta) * np.cos(theta / x) + 0.1 * np.sin(theta) elif n == 3: tx = 0.5 * np.cos(theta) * np.sin(theta / (2 * x)) + 0.4 * np.cos(theta) ty = 0.2 * np.cos(theta) * np.sin(theta / x) + 0.3 * np.cos(theta) elif n == 4: tx = 0.4 * np.cos(theta / x) * np.cos(theta) ty = 0.2 * np.cos(theta / (x)) elif n == 5: tx = 0.4 * np.cos(theta / x) * np.sin(theta / x) ty = 0.2 * np.cos(theta / x) * np.sin(theta) * np.cos(theta) else: return tr.identity() tx = t[0] + tx ty = t[1] + ty pos = posInLimits(tx, ty) translation = tr.translate(pos[0], pos[1], 0) return tr.matmul([shearing, scale, translation])
def drawCollisionShapes(node, pipeline, transformName, parentTransform=tr.identity()): assert (isinstance(node, sg.SceneGraphNode)) newTransform = np.matmul(parentTransform, node.transform) #si se encuentra una animacion o un gameObject sin hitboxes no las dibuja if len(node.childs) == 1 and isinstance(node.childs[0], anim.Anim_Controller): return elif len(node.childs) == 1 and isinstance(node, gameObject): return #si encuentra un gameObject con hitbox, la dibuja elif len(node.childs) == 2 and isinstance(node, gameObject): drawCollisionShapes(node.childs[1], pipeline, transformName, newTransform) elif len(node.childs) == 1 and isinstance(node.childs[0], es.GPUShape): leaf = node.childs[0] glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, transformName), 1, GL_TRUE, newTransform) pipeline.drawShape(leaf) else: for child in node.childs: drawCollisionShapes(child, pipeline, transformName, newTransform)
def findPosition(node, name, parentTransform=tr.identity()): foundTransform = findTransform(node, name, parentTransform) if isinstance(foundTransform, (np.ndarray, np.generic)): zero = np.array([[0, 0, 0, 1]], dtype=np.float32).T foundPosition = np.matmul(foundTransform, zero) return foundPosition return None
def updateGameOverAnim(delta, pipeline): global scale, color #modifica el pipeline anterior (el de grafo de escena) a una escala de grises progresivamente pipeline.grayScale(color) #pipeline para dibujar normalmente la animacion temp_pipeline = es.SimpleTextureTransformShaderProgram() glUseProgram(temp_pipeline.shaderProgram) BACKGROUND_ANIM.Update(temp_pipeline, delta, tr.identity(), False) LETTERS_ANIM.Update(temp_pipeline, delta, tr.identity(), False) #se vuelve al pipeline anterior glUseProgram(pipeline.shaderProgram) #efecto de letras crecientes if scale < 0.3: scale += delta *0.1 LETTERS_ANIM.scale = [2 * (0.4 + scale), WIDTH / HEIGHT *(0.4 + scale), 1] #efecto progresivo de la escala de grises if color > 0: color -= delta else: color = 0
def drawSceneGraphNode(node, pipeline, transformName, parentTransform=tr.identity()): assert (isinstance(node, SceneGraphNode)) # Composing the transformations through this path newTransform = np.matmul(parentTransform, node.transform) # If the child node is a leaf, it should be a GPUShape. # Hence, it can be drawn with drawShape if len(node.childs) == 1 and isinstance(node.childs[0], es.GPUShape): leaf = node.childs[0] glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, transformName), 1, GL_TRUE, newTransform) pipeline.drawShape(leaf) # If the child node is not a leaf, it MUST be a SceneGraphNode, # so this draw function is called recursively else: for child in node.childs: drawSceneGraphNode(child, pipeline, transformName, newTransform)
def findTransform(node, name, parentTransform=tr.identity()): # The name was not found in this path if isinstance(node, es.GPUShape): return None newTransform = np.matmul(parentTransform, node.transform) # This is the requested node if node.name == name: return newTransform # All childs are checked for the requested name else: for child in node.childs: foundTransform = findTransform(child, name, newTransform) if isinstance(foundTransform, (np.ndarray, np.generic)): return foundTransform # No child of this node had the requested name return None
def move(self, move_Q): moveX = 0 moveY = 0 movement = tr.identity() body_positions = [] for i in range(len(self.body_queue)): direction = move_Q[len(move_Q) - 1 - i] if direction == "W": movement = tr.translate(0, 1, 0) moveY = 1 elif direction == "A": movement = tr.translate(-1, 0, 0) moveX = -1 elif direction == "S": movement = tr.translate(0, -1, 0) moveY = -1 elif direction == "D": movement = tr.translate(1, 0, 0) moveX = 1 sg.findNode(self.model, self.body_queue[i]["name"]).transform = tr.matmul([ sg.findTransform(self.model, self.body_queue[i]["name"]), movement ]) self.body_queue[i]["last_position"][0] = self.body_queue[i][ "position"][0] self.body_queue[i]["last_position"][1] = self.body_queue[i][ "position"][1] self.body_queue[i]["position"][ 0] = self.body_queue[i]["position"][0] + moveX self.body_queue[i]["position"][ 1] = self.body_queue[i]["position"][1] + moveY moveX = 0 moveY = 0 body_positions.append(self.body_queue[i]["position"]) self.occupied_positions = self.occupied_positions[0] + body_positions
size -= 1 # Deletes the remaining data fishes = updateFishes(i, fishes) print("[DELETE]: Pez " + str(i + 1) + " fue asesinado por tu click.") break print("[INFO]: Ahora hay " + str(len(FishBowl.fishes)) + " pececitos") controller.leftClickOn = False # DRAWING THE SCENES # Water drawShape(shaderProgram, water, tr.identity()) # Bubbles for i in range(100): bubble = FishBowl.bubbles[i] drawShape( shaderProgram, lightBlueCircle, tr.matmul([ tr.uniformScale(bubble.size), tr.translate(bubble.x + 0.2 * np.sin(theta / 4), bubble.y + 0.003, 0.0) ])) FishBowl.bubbles[i].y += 0.003 if FishBowl.bubbles[i].y > 1: FishBowl.bubbles[i].y = -1 # Little Castle
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # The axis is drawn without lighting effects if controller.showAxis: glUseProgram(colorPipeline.shaderProgram) glUniformMatrix4fv( glGetUniformLocation(colorPipeline.shaderProgram, "projection"), 1, GL_TRUE, projection) glUniformMatrix4fv( glGetUniformLocation(colorPipeline.shaderProgram, "view"), 1, GL_TRUE, view) glUniformMatrix4fv( glGetUniformLocation(colorPipeline.shaderProgram, "model"), 1, GL_TRUE, tr.identity()) colorPipeline.drawShape(gpuAxis, GL_LINES) # Using the lighting shader program glUseProgram(lightingPipeline.shaderProgram) glUniformMatrix4fv( glGetUniformLocation(lightingPipeline.shaderProgram, "projection"), 1, GL_TRUE, projection) glUniformMatrix4fv( glGetUniformLocation(lightingPipeline.shaderProgram, "view"), 1, GL_TRUE, view) glUniformMatrix4fv( glGetUniformLocation(lightingPipeline.shaderProgram, "model"), 1, GL_TRUE, tr.identity()) # Setting all uniform shader variables
def exportGraphHelper(f, graph, transformList=[tr.identity()]): global exporter if type(graph) is es.GPUShape: offsetCopy = exporter.offset transform = tr.matmul(transformList) vertexLine = "v %s %s %s \nvn %s %s %s" indexLine = "f %s//%s %s//%s %s//%s" vertexData = [] normalData = [] indexData = [] shape = graph.shape # Saving the vertex and normal data for i, vertex in enumerate(shape.vertices): if i % 9 == 0: vertexData = [vertex] elif i % 9 < 3: vertexData.append(vertex) elif i % 9 == 6: normalData = [vertex] elif i % 9 > 5: normalData.append(vertex) if i % 9 == 8: vertexData = list(tr.matmul([transform, vertexData + [1]])) f.write(vertexLine % tuple(vertexData[:3] + normalData) + '\n') offsetCopy += 1 # Saving the index data for i, index in enumerate(shape.indices): offIndex = index + exporter.offset if i % 3 == 0: indexData = [offIndex, offIndex] elif i % 3 == 1: indexData += [offIndex, offIndex] else: indexData += [offIndex, offIndex] f.write(indexLine % tuple(indexData) + '\n') exporter.offset = offsetCopy else: nextTransform = transformList + [graph.transform] # Continue with its children for child in graph.childs: exportGraphHelper(f, child, nextTransform)
def updateWinAnim(delta, pipeline): WIN_ANIM.Update(pipeline, delta, tr.identity(), False)
mousePosX = 2 * (controller.mousePos[0] - width / 2) / width mousePosY = 2 * (height / 2 - controller.mousePos[1]) / height # Setting up the projection transform projection = tr.perspective(45, float(width) / float(height), 0.1, 100) # Setting up the view transform atX = -6 * mousePosX atZ = 5 * mousePosY atPos = np.array([atX, 0, atZ]) view = tr.lookAt(np.array([0, 5, 2]), atPos, np.array([0, 0, 1])) # Setting up the model transform model = tr.identity() # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # The landscape is drawn with texture lighting glUseProgram(phongTexturePipeline.shaderProgram) glUniform3f( glGetUniformLocation(phongTexturePipeline.shaderProgram, "La"), 1.0, 1.0, 1.0) glUniform3f( glGetUniformLocation(phongTexturePipeline.shaderProgram, "Ld"), 1.0, 1.0, 1.0) glUniform3f( glGetUniformLocation(phongTexturePipeline.shaderProgram, "Ls"), 1.0, 1.0, 1.0)
# Telling OpenGL to use our shader program glUseProgram(shaderProgram) # Setting up the clear screen color glClearColor(0.35, 0.35, 0.35, 1.0) # Our shapes here are always fully painted glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) BARRA = barra() TAPA = tapa() while not glfw.window_should_close(window): # Using GLFW to check for input events glfw.poll_events() # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT) # Drawing the arbol sg.drawSceneGraphNode(BARRA, shaderProgram, tr.identity()) sg.drawSceneGraphNode(TAPA, shaderProgram, tr.translate(0, obj.val + 0.5, 0)) sg.drawSceneGraphNode(circulo(obj.grab), shaderProgram, tr.identity()) # Once the render is done, buffers are swapped, showing only the complete scene. glfw.swap_buffers(window) glfw.terminate()
# Telling OpenGL to use our shader program glUseProgram(shaderProgram) # Setting up the clear screen color glClearColor(0.85, 0.85, 0.85, 1.0) # Creating shapes on GPU memory car = createCar() # Our shapes here are always fully painted glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) while not glfw.window_should_close(window): # Using GLFW to check for input events glfw.poll_events() # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT) # Modifying only a specific node in the scene graph wheelRotationNode = sg.findNode(car, "wheelRotation") theta = -10 * glfw.get_time() wheelRotationNode.transform = tr.rotationZ(theta) # Drawing the Car sg.drawSceneGraphNode(car, shaderProgram, tr.identity()) # Once the render is done, buffers are swapped, showing only the complete scene. glfw.swap_buffers(window) glfw.terminate()
glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0, -5, 0)) pipeline.drawShape(gpuYellowCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0, 0, 5)) pipeline.drawShape(gpuCyanCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0, 0, -5)) pipeline.drawShape(gpuPurpleCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.identity()) pipeline.drawShape(gpuRainbowCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.identity()) pipeline.drawShape(gpuAxis, GL_LINES) glUseProgram(textPipe.shaderProgram) glUniformMatrix4fv( glGetUniformLocation(textPipe.shaderProgram, "model"), 1, GL_TRUE, tr.translate(-5, 0, 0)) textPipe.drawShape(gpuGreenCube) # Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen. glfw.swap_buffers(window)
def __init__(self, name): self.name = name self.transform = tr.identity() self.childs = []
) # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Filling or not the shapes depending on the controller state if (controller.fillPolygon): glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) if controller.showAxis: glUseProgram(mvpPipeline.shaderProgram) glUniformMatrix4fv(glGetUniformLocation(mvpPipeline.shaderProgram, "projection"), 1, GL_TRUE, projection) glUniformMatrix4fv(glGetUniformLocation(mvpPipeline.shaderProgram, "view"), 1, GL_TRUE, view) glUniformMatrix4fv(glGetUniformLocation(mvpPipeline.shaderProgram, "model"), 1, GL_TRUE, tr.identity()) mvpPipeline.drawShape(gpuAxis, GL_LINES) # Draw Tree glUseProgram(phongPipeline.shaderProgram) # White light in all components: ambient, diffuse and specular. glUniform3f(glGetUniformLocation(phongPipeline.shaderProgram, "La"), 1.0, 1.0, 1.0) glUniform3f(glGetUniformLocation(phongPipeline.shaderProgram, "Ld"), 1.0, 1.0, 1.0) glUniform3f(glGetUniformLocation(phongPipeline.shaderProgram, "Ls"), 1.0, 1.0, 1.0) # Object is barely visible at only ambient. Diffuse behavior is slightly red. Sparkles are white glUniform3f(glGetUniformLocation(phongPipeline.shaderProgram, "Ka"), 0.3, 0.3, 0.3) glUniform3f(glGetUniformLocation(phongPipeline.shaderProgram, "Kd"), 0.9, 0.5, 0.5) glUniform3f(glGetUniformLocation(phongPipeline.shaderProgram, "Ks"), 0.4, 0.4, 0.4)
if (controller.fillPolygon): glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT) theta = glfw.get_time() tx = 0.7 * np.sin(0.5 * theta) ty = 0.2 * np.sin(5 * theta) # derivative of tx give us the direction dtx = 0.7 * 0.5 * np.cos(0.5 * theta) if dtx > 0: reflex = tr.identity() else: reflex = tr.scale(-1, 1, 1) # Drawing the shapes glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, tr.matmul([ tr.translate(tx, ty, 0), tr.scale(0.5, 0.5, 1.0), reflex])) pipeline.drawShape(gpuBoo) glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, questionBoxTransform) pipeline.drawShape(gpuQuestionBox) # Once the render is done, buffers are swapped, showing only the complete scene. glfw.swap_buffers(window)