def createStoreModel(pipeline): # Se crea la tiendo con un grafo de escena a base de shapes redQuadShape = bs.createColorQuad(255 / 255, 105 / 255, 105 / 255) lBlueQuadShape = bs.createColorQuad(133 / 255, 255 / 255, 253 / 255) darkGreenQuadShape = bs.createColorQuad(0, 125 / 255, 0) blackQuadShape = bs.createColorQuad(0, 0, 0) gpuRedQuad = createGPUShape(redQuadShape, pipeline) gpuLBlueQuad = createGPUShape(lBlueQuadShape, pipeline) gpuDarkGreenQuad = createGPUShape(darkGreenQuadShape, pipeline) gpuBlackQuad = createGPUShape(blackQuadShape, pipeline) frontNode = csg.CustomSceneGraphNode("front") frontNode.transform = tr.scale(0.3, 0.6, 1) frontNode.childs = [gpuRedQuad] windowNode = csg.CustomSceneGraphNode("window") windowNode.transform = tr.scale(0.1, 0.2, 1) windowNode.childs = [gpuLBlueQuad] windowDoorNode = csg.CustomSceneGraphNode("door") windowDoorNode.transform = tr.matmul( [tr.translate(0.024, 0.1, 0), tr.scale(0.15, 0.1, 1)]) windowDoorNode.childs = [gpuLBlueQuad] windowNode1 = csg.CustomSceneGraphNode("window2") windowNode1.transform = tr.translate(0, -0.15, 0) windowNode1.childs = [windowNode] windowGroupNode = csg.CustomSceneGraphNode("windowGroup") windowGroupNode.transform = tr.translate(0.05, 0, 0) windowGroupNode.childs = [windowNode1, windowDoorNode] signGreenNode = csg.CustomSceneGraphNode("greenSign") signGreenNode.transform = tr.scale(0.1, 0.7, 1) signGreenNode.childs = [gpuDarkGreenQuad] signBlackNode = csg.CustomSceneGraphNode("blackSign") signBlackNode.transform = tr.scale(0.11, 0.71, 1) signBlackNode.childs = [gpuBlackQuad] signNode = csg.CustomSceneGraphNode("sign") signNode.transform = tr.translate(-0.2, 0, 0) signNode.childs = [signBlackNode, signGreenNode] storeNode = csg.CustomSceneGraphNode("store") storeNode.childs = [frontNode, windowGroupNode, signNode] regularNode = csg.CustomSceneGraphNode("regularStore") regularNode.childs = [storeNode] finalNode = csg.CustomSceneGraphNode("finalStore") finalNode.transform = tr.translate(0, 2, 0) finalNode.childs = [regularNode] return finalNode
def createBackground(pipeline, i): # Se crea el fondo con un grafo de escena de shapes greenQuad = bs.createColorQuad(0.0, 0.6, 0.0) greyQuad = bs.createColorQuad(0.6, 0.6, 0.6) whiteQuad = bs.createColorQuad(255 / 255, 242 / 255, 0) gpuGrass = createGPUShape(greenQuad, pipeline) gpuStreet = createGPUShape(greyQuad, pipeline) gpuLine = createGPUShape(whiteQuad, pipeline) grassNode = sg.SceneGraphNode("grass") grassNode.transform = tr.scale(4, 2, 1) grassNode.childs = [gpuGrass] streetNode = sg.SceneGraphNode("street") streetNode.transform = tr.scale(1, 2, 1) streetNode.childs = [gpuStreet] lineNode = sg.SceneGraphNode("line") lineNode.transform = tr.scale(0.1, 0.25, 1) lineNode.childs = [gpuLine] trLineNode1 = sg.SceneGraphNode("trline1") trLineNode1.transform = tr.identity() trLineNode1.childs = [lineNode] trLineNode2 = sg.SceneGraphNode("trline2") trLineNode2.transform = tr.translate(0, 0.4, 0) trLineNode2.childs = [lineNode] trLineNode3 = sg.SceneGraphNode("trline3") trLineNode3.transform = tr.translate(0, -0.4, 0) trLineNode3.childs = [lineNode] trLineNode4 = sg.SceneGraphNode("trline4") trLineNode4.transform = tr.translate(0, -0.8, 0) trLineNode4.childs = [lineNode] trLineNode5 = sg.SceneGraphNode("trline5") trLineNode5.transform = tr.translate(0, 0.8, 0) trLineNode5.childs = [lineNode] lineGroupNode = sg.SceneGraphNode("lineGroup") lineGroupNode.transform = tr.translate(0, 0, 0) lineGroupNode.childs = [ trLineNode1, trLineNode2, trLineNode3, trLineNode4, trLineNode5 ] finalNode = sg.SceneGraphNode("final" + str(i)) finalNode.childs = [grassNode, streetNode, lineGroupNode] return finalNode
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 update(self, delta, tex_pipeline, tex_green_pipeline, googles, time, move): # Se actualiza su posicion y_dir = self.direction vel = self.vel * delta x_dir = (1 - self.direction) * 0.2 if self.coin: x_dir *= -1 if not move: self.pos[0] += vel * x_dir self.pos[1] += vel * y_dir self.model.transform = tr.matmul([ tr.translate(self.pos[0], self.pos[1], 0), tr.scale(self.size, self.size, 1) ]) # Se dibuja con uno u otro shader dependiendo de su estado de infeccion y si se usan los lentes if self.infected and googles: self.model.curr_pipeline = tex_green_pipeline elif not googles: self.model.curr_pipeline = tex_pipeline # Se controla el tono de verde que tendra si esta infectado self.green(time)
def moveSceneDown(scene, name, pos, vel, delta, fixed_pos): x = csg.findNode(scene, name) newpos = ((pos - vel * delta) % -4) + fixed_pos if newpos < -2: newpos += 4 x.transform = tr.translate(0, newpos, 0) return newpos - fixed_pos
def update(self, delta, tex_pipeline, tex_green_pipeline, googles, time, move): # Se actualiza la posicion del jugador # Si detecta la tecla [D] presionada y no se ha salido de la pantalla se mueve hacia la derecha if self.controller.is_d_pressed and self.pos[0] < (1 - self.size / 2): self.pos[0] += self.vel[0] * delta # Si detecta la tecla [A] presionada y no se ha salido de la pantalla se mueve hacia la izquierda if self.controller.is_a_pressed and self.pos[0] > -(1 - self.size / 2): self.pos[0] -= self.vel[0] * delta # Si detecta la tecla [W] presionada y esta en la mitad inferior de la pantalla se mueve hacia arriba if self.controller.is_w_pressed and self.pos[1] < 0: self.pos[1] += self.vel[1] * delta self.controller.distance += self.vel[1] * delta # Si detecta la tecla [S] presionada y no se ha salido de la pantalla se mueve hacia abajo if self.controller.is_s_pressed and self.pos[1] > (-1 + self.size / 2): self.pos[1] -= self.vel[1] * delta self.controller.distance -= self.vel[1] * delta # Se le aplica la transformacion de traslado segun la posicion actual self.model.transform = tr.matmul([ tr.translate(self.pos[0], self.pos[1], 0), tr.scale(self.size, self.size, 1) ]) # Se dibuja con uno u otro shader dependiendo de su estado de infeccion y si se usan los lentes if self.infected and googles: self.model.curr_pipeline = tex_green_pipeline elif not googles: self.model.curr_pipeline = tex_pipeline # Actualiza el tono de verde que tendra siendo infectado self.green(time)
def createCar(pipeline): # Creating shapes on GPU memory blackQuad = bs.createColorQuad(0, 0, 0) gpuBlackQuad = es.GPUShape().initBuffers() pipeline.setupVAO(gpuBlackQuad) gpuBlackQuad.fillBuffers(blackQuad.vertices, blackQuad.indices, GL_STATIC_DRAW) redQuad = bs.createColorQuad(1, 0, 0) gpuRedQuad = es.GPUShape().initBuffers() pipeline.setupVAO(gpuRedQuad) gpuRedQuad.fillBuffers(redQuad.vertices, redQuad.indices, GL_STATIC_DRAW) # Cheating a single wheel wheel = sg.SceneGraphNode("wheel") wheel.transform = tr.uniformScale(0.2) wheel.childs += [gpuBlackQuad] wheelRotation = sg.SceneGraphNode("wheelRotation") wheelRotation.childs += [wheel] # Instanciating 2 wheels, for the front and back parts frontWheel = sg.SceneGraphNode("frontWheel") frontWheel.transform = tr.translate(0.3, -0.3, 0) frontWheel.childs += [wheelRotation] backWheel = sg.SceneGraphNode("backWheel") backWheel.transform = tr.translate(-0.3, -0.3, 0) backWheel.childs += [wheelRotation] # Creating the chasis of the car chasis = sg.SceneGraphNode("chasis") chasis.transform = tr.scale(1, 0.5, 1) chasis.childs += [gpuRedQuad] car = sg.SceneGraphNode("car") car.childs += [chasis] car.childs += [frontWheel] car.childs += [backWheel] traslatedCar = sg.SceneGraphNode("traslatedCar") traslatedCar.transform = tr.translate(0, 0.3, 0) traslatedCar.childs += [car] return traslatedCar
def createCar(pipeline, r, g, b): # Creating shapes on GPU memory blackCube = bs.createColorCube(0, 0, 0) gpuBlackCube = es.GPUShape().initBuffers() pipeline.setupVAO(gpuBlackCube) gpuBlackCube.fillBuffers(blackCube.vertices, blackCube.indices, GL_STATIC_DRAW) chasisCube = bs.createColorCube(r, g, b) gpuChasisCube = es.GPUShape().initBuffers() pipeline.setupVAO(gpuChasisCube) gpuChasisCube.fillBuffers(chasisCube.vertices, chasisCube.indices, GL_STATIC_DRAW) # Cheating a single wheel wheel = sg.SceneGraphNode("wheel") wheel.transform = tr.scale(0.2, 0.8, 0.2) wheel.childs += [gpuBlackCube] wheelRotation = sg.SceneGraphNode("wheelRotation") wheelRotation.childs += [wheel] # Instanciating 2 wheels, for the front and back parts frontWheel = sg.SceneGraphNode("frontWheel") frontWheel.transform = tr.translate(0.3, 0, -0.3) frontWheel.childs += [wheelRotation] backWheel = sg.SceneGraphNode("backWheel") backWheel.transform = tr.translate(-0.3, 0, -0.3) backWheel.childs += [wheelRotation] # Creating the chasis of the car chasis = sg.SceneGraphNode("chasis") chasis.transform = tr.scale(1, 0.7, 0.5) chasis.childs += [gpuChasisCube] # All pieces together car = sg.SceneGraphNode("car") car.childs += [chasis] car.childs += [frontWheel] car.childs += [backWheel] return car
def createCars(pipeline, N): # First we scale a car scaledCar = sg.SceneGraphNode("traslatedCar") scaledCar.transform = tr.uniformScale(0.15) scaledCar.childs += [createCar(pipeline)] # Re-using the previous function cars = sg.SceneGraphNode("cars") baseName = "scaledCar" for i in range(N): # A new node is only locating a scaledCar in the scene depending on index i newNode = sg.SceneGraphNode(baseName + str(i)) newNode.transform = tr.translate(0.4 * i - 0.9, 0.9 - 0.4 * i, 0) newNode.childs += [scaledCar] # Now this car is added to the 'cars' scene graph cars.childs += [newNode] return cars
def update(self, delta, tex_pipeline, tex_green_pipeline, googles, time, move): # Se actualiza la posicion del zombie y_dir = self.direction vel = self.vel * delta x_dir = (1 - self.direction) * 0.2 if self.coin: x_dir *= -1 if not move: self.pos[0] += vel * x_dir self.pos[1] += vel * y_dir self.model.transform = tr.matmul([ tr.translate(self.pos[0], self.pos[1], 0), tr.scale(self.size, self.size, 1) ]) # Se dibuja con el pipeline de textura normal, no se ve verde self.model.curr_pipeline = tex_pipeline
if ((glfw.get_key(window, glfw.KEY_RIGHT) == glfw.PRESS) or\ (glfw.get_key(window, glfw.KEY_UP) == glfw.PRESS)) and\ scale < 1.0: scale += 2 * dt # Clearing the screen glClear(GL_COLOR_BUFFER_BIT) # Drawing shapes glUseProgram(pipeline.shaderProgram) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, tr.matmul( [tr.translate(-0.5, 0, 0), tr.scale(scale, 2 * scale, 1)])) pipeline.drawCall(gpuShapeWithoutMipmap) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, tr.matmul([tr.translate(0.5, 0, 0), tr.scale(scale, 2 * scale, 1)])) pipeline.drawCall(gpuShapeWithMipmap) # Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen. glfw.swap_buffers(window) # freeing GPU memory gpuShapeWithoutMipmap.clear()
glUniformMatrix4fv(glGetUniformLocation(mvpPipeline.shaderProgram, "model"), 1, GL_TRUE, model) mvpPipeline.drawCall(gpuRainbowCube) 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) glUseProgram(texture2dPipeline.shaderProgram) glUniformMatrix4fv(glGetUniformLocation(texture2dPipeline.shaderProgram, "transform"), 1, GL_TRUE, tr.matmul([ tr.translate(tx, ty, 0), tr.scale(0.5, 0.5, 1.0), reflex])) texture2dPipeline.drawCall(gpuBoo) # Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen. glfw.swap_buffers(window) # freeing GPU memory gpuBoo.clear() gpuAxis.clear() gpuRainbowCube.clear() glfw.terminate()
pipeline.setupVAO(gpuBoo) gpuBoo.fillBuffers(shapeBoo.vertices, shapeBoo.indices, GL_STATIC_DRAW) gpuBoo.texture = es.textureSimpleSetup(getAssetPath("boo.png"), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST) shapeQuestionBoxes = bs.createTextureQuad(10, 1) gpuQuestionBoxes = GPUShape().initBuffers() pipeline.setupVAO(gpuQuestionBoxes) gpuQuestionBoxes.fillBuffers(shapeQuestionBoxes.vertices, shapeQuestionBoxes.indices, GL_STATIC_DRAW) gpuQuestionBoxes.texture = es.textureSimpleSetup( getAssetPath("cg_box.png"), GL_REPEAT, GL_REPEAT, GL_NEAREST, GL_NEAREST) questionBoxesTransform = np.matmul(tr.translate(0, -0.8, 0), tr.scale(2, 0.2, 1)) while not glfw.window_should_close(window): # Using GLFW to check for input events glfw.poll_events() 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()
def draw(self): glUniformMatrix4fv( glGetUniformLocation(self.pipeline.shaderProgram, "transform"), 1, GL_TRUE, tr.translate(self.position[0], self.position[1], 0.0)) self.pipeline.drawCall(self.gpuShape)
glGetUniformLocation(pipeline.shaderProgram, "projection"), 1, GL_TRUE, projection) # 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) # Drawing shapes with different model transformations glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(5, 0, 0)) pipeline.drawCall(gpuRedCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(-5, 0, 0)) pipeline.drawCall(gpuGreenCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0, 5, 0)) pipeline.drawCall(gpuBlueCube) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0, -5, 0)) pipeline.drawCall(gpuYellowCube)
def update(self): # Se actualiza su posicion self.model.transform = tr.translate(self.pos[0], self.pos[1], 0)
def createSnowman(pipeline): # Convenience function to ease initialization def createGPUShape(shape): gpuShape = es.GPUShape().initBuffers() pipeline.setupVAO(gpuShape) gpuShape.fillBuffers(shape.vertices, shape.indices, GL_STATIC_DRAW) return gpuShape # basic GPUShapes gpuWhiteCircle = createGPUShape(createColorCircle(30, 1, 1, 1, 1)) gpuBlackCircle = createGPUShape(createColorCircle(10, 1, 0, 0, 0)) gpuBrownQuad = createGPUShape(bs.createColorQuad(0.6, 0.3, 0)) gpuOrangeTriangle = createGPUShape(createColorTriangle(1, 0.6, 0)) # Leaf nodes whiteCircleNode = sg.SceneGraphNode("whiteCircleNode") whiteCircleNode.childs = [gpuWhiteCircle] blackCircleNode = sg.SceneGraphNode("blackCircleNode") blackCircleNode.childs = [gpuBlackCircle] brownQuadNode = sg.SceneGraphNode("brownQuadNode") brownQuadNode.childs = [gpuBrownQuad] orangeTriangleNode = sg.SceneGraphNode("orangeTriangleNode") orangeTriangleNode.childs = [gpuOrangeTriangle] # Body snowballBody = sg.SceneGraphNode("snowballBody") snowballBody.transform = tr.scale(10, 10, 0) snowballBody.childs = [whiteCircleNode] arm = sg.SceneGraphNode("arm") arm.transform = tr.matmul([ tr.translate(0, 5, 0), #tr.rotationZ(0), tr.scale(2, 10, 1) ]) arm.childs = [brownQuadNode] leftArm = sg.SceneGraphNode("leftArm") leftArm.transform = tr.matmul([ tr.translate(-7, 7, 0), tr.rotationZ(60 * np.pi / 180), #tr.scale(1, 1, 1) ]) leftArm.childs = [arm] rightArm = sg.SceneGraphNode("rightArm") rightArm.transform = tr.matmul([ tr.translate(7, 7, 0), tr.rotationZ(-60 * np.pi / 180), #tr.scale(1, 1, 1) ]) rightArm.childs = [arm] body = sg.SceneGraphNode("body") body.transform = tr.translate(0,10,0) body.childs = [snowballBody, leftArm, rightArm] # Head snowballHead = sg.SceneGraphNode("snowballHead") snowballHead.transform = tr.scale(8, 8, 0) snowballHead.childs = [whiteCircleNode] leftEye = sg.SceneGraphNode("leftEye") leftEye.transform = tr.matmul([ tr.translate(0, 5, 0), #tr.rotationZ(0), tr.scale(2, 2, 1) ]) leftEye.childs = [blackCircleNode] rightEye = sg.SceneGraphNode("rightEye") rightEye.transform = tr.matmul([ tr.translate(5, 5, 0), #tr.rotationZ(0), tr.scale(2, 2, 1) ]) rightEye.childs = [blackCircleNode] baseTriangle = sg.SceneGraphNode("baseTriangle") baseTriangle.transform = tr.matmul([ tr.translate(0, 3.5, 0), #tr.rotationZ(0), tr.scale(2, 7, 1) ]) baseTriangle.childs = [orangeTriangleNode] nose = sg.SceneGraphNode("nose") nose.transform = tr.matmul([ tr.translate(2, 0, 0), tr.rotationZ(-70 * np.pi / 180), #tr.scale(1, 1, 1) ]) nose.childs = [baseTriangle] head = sg.SceneGraphNode("head") head.transform = tr.translate(0, 27, 0) head.childs = [snowballHead, leftEye, rightEye, nose] # Snowman, the one and only snowman = sg.SceneGraphNode("snowman") snowman.childs = [body, head] return snowman
# Creating shapes on GPU memory snowman = createSnowman(pipeline) # 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() 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) # Drawing the Car sg.drawSceneGraphNode(snowman, pipeline, "transform", np.matmul(tr.translate(0, -0.5, 0), tr.scale(0.03, 0.03, 1))) # Once the render is done, buffers are swapped, showing only the complete scene. glfw.swap_buffers(window) # freeing GPU memory snowman.clear() glfw.terminate()
# Rendering the rendered texture to the viewport as a simple quad ####################### glBindFramebuffer(GL_FRAMEBUFFER, 0) glDisable(GL_DEPTH_TEST) glClearColor(0.85, 0.85, 0.85, 1.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) if (controller.fillTexture): glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glUseProgram(textureShaderProgram.shaderProgram) glUniformMatrix4fv( glGetUniformLocation(textureShaderProgram.shaderProgram, "transform"), 1, GL_TRUE, tr.matmul([ tr.translate(0.3 * np.cos(theta), 0, 0), tr.shearing(0.3 * np.cos(theta), 0, 0, 0, 0, 0) ])) textureShaderProgram.drawCall(gpuQuad) # Once the render is done, buffers are swapped, showing only the complete scene. glfw.swap_buffers(window) # freeing GPU memory gpuRainbowCube.clear() gpuQuad.clear() glfw.terminate()
backgroundShape.indices, GL_STATIC_DRAW) gpuBackground.texture = es.textureSimpleSetup( getAssetPath("torres-del-paine-sq.jpg"), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_LINEAR, GL_LINEAR) headerText = "Torres del Paine" headerCharSize = 0.1 headerCenterX = headerCharSize * len(headerText) / 2 headerShape = tx.textToShape(headerText, headerCharSize, headerCharSize) gpuHeader = es.GPUShape().initBuffers() textPipeline.setupVAO(gpuHeader) gpuHeader.fillBuffers(headerShape.vertices, headerShape.indices, GL_STATIC_DRAW) gpuHeader.texture = gpuText3DTexture headerTransform = tr.matmul([ tr.translate(0.9, -headerCenterX, 0), tr.rotationZ(np.pi / 2), ]) dateCharSize = 0.15 timeCharSize = 0.1 now = datetime.datetime.now() dateStr = now.strftime("%d/%m/%Y") timeStr = now.strftime("%H:%M:%S.%f")[:-3] dateShape = tx.textToShape(dateStr, dateCharSize, dateCharSize) timeShape = tx.textToShape(timeStr, timeCharSize, timeCharSize) gpuDate = es.GPUShape().initBuffers() gpuTime = es.GPUShape().initBuffers() textPipeline.setupVAO(gpuDate) textPipeline.setupVAO(gpuTime)
# Creating our shader program and telling OpenGL to use it pipeline = es.SimpleTransformShaderProgram() glUseProgram(pipeline.shaderProgram) # Setting up the clear screen color glClearColor(0.15, 0.15, 0.15, 1.0) # Creating shapes on GPU memory shape = createShape() gpuShape = es.GPUShape().initBuffers() pipeline.setupVAO(gpuShape) gpuShape.fillBuffers(shape.vertices, shape.indices, GL_STATIC_DRAW) # We do not need to update the transform in every frame, so we can do it here transform = tr.translate(0, -0.5, 0) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, transform) while not glfw.window_should_close(window): # Using GLFW to check for input events glfw.poll_events() # 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) # Clearing the screen and drawing
raise Exception() glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "projection"), 1, GL_TRUE, projection) # 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) # Drawing shapes with different model transformations glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(5,0,0)) pipeline.drawCall(gpuRedCube) glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(-5,0,0)) pipeline.drawCall(gpuGreenCube) glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0,5,0)) pipeline.drawCall(gpuBlueCube) glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0,-5,0)) pipeline.drawCall(gpuYellowCube) glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0,0,5)) pipeline.drawCall(gpuCyanCube) glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0,0,-5)) pipeline.drawCall(gpuPurpleCube)
def createDecorations(pipeline, tex_pipeline, x_range, y_range, n, i_z): # Se crean las decoraciones con un grafo de escena que admite shapes y texturas triangleShape = createTriangle() quadShape = createQuad() whiteQuad = bs.createColorQuad(1, 1, 1) greyQuad = bs.createColorQuad(0.8, 0.8, 0.8) umbrellaShape = createUmbrellaCircle() gpuLeaves = createTextureGPUShape(triangleShape, tex_pipeline, getAssetPath("hojas_arboles.jpg")) gpuTrunk = createTextureGPUShape(quadShape, tex_pipeline, getAssetPath("tronco.jpeg")) gpuWhiteQuad = createGPUShape(whiteQuad, pipeline) gpuGreyQuad = createGPUShape(greyQuad, pipeline) gpuUmbrella = createGPUShape(umbrellaShape, pipeline) leavesNode = csg.CustomSceneGraphNode("hojas") leavesNode.transform = tr.translate(0, 0.5, 0) leavesNode.childs = [gpuLeaves] trunkNode = csg.CustomSceneGraphNode("tronco") trunkNode.transform = tr.matmul( [tr.translate(0, -0.25, 0), tr.scale(0.3, 0.5, 1)]) trunkNode.childs = [gpuTrunk] scaledTreeNode = csg.CustomSceneGraphNode("arbol_escalado") scaledTreeNode.transform = tr.uniformScale(0.3) scaledTreeNode.childs = [trunkNode, leavesNode] treeGroupNode = csg.CustomSceneGraphNode("grupo_arboles") # Se crean arboles en posiciones generadas al azar for i in range(n): x = 100 - x_range * 100 x_rand = rd.randint(0, x) * 0.01 r_x = x_rand + x_range coin = rd.randint(0, 1) if coin: r_x *= -1 r_y = rd.randint(y_range[0] * 100, y_range[1] * 100) * 0.01 treeNode = csg.CustomSceneGraphNode("arbol_" + str(i)) treeNode.transform = tr.translate(r_x, r_y, 0) treeNode.childs = [scaledTreeNode] treeGroupNode.childs += [treeNode] treeGroupNode.childs.sort(reverse=True, key=height) cartBodyNode = csg.CustomSceneGraphNode("ej") cartBodyNode.transform = tr.scale(0.3, 0.5, 1) cartBodyNode.childs = [gpuWhiteQuad] cartInsideNode = csg.CustomSceneGraphNode("inside") cartInsideNode.transform = tr.scale(0.2, 0.3, 1) cartInsideNode.childs = [gpuGreyQuad] umbrellaNode = csg.CustomSceneGraphNode("umbrella") umbrellaNode.transform = tr.rotationZ(math.pi / 8) umbrellaNode.childs = [gpuUmbrella] umbrellaScaled = csg.CustomSceneGraphNode("umbrellaS") umbrellaScaled.transform = tr.scale(0.3, 0.3, 1) umbrellaScaled.childs = [umbrellaNode] umbrellaTranslated = csg.CustomSceneGraphNode("umbrellaT") umbrellaTranslated.transform = tr.translate(-0.1, 0.1, 0) umbrellaTranslated.childs = [umbrellaScaled] cartNode = csg.CustomSceneGraphNode("cart") cartNode.transform = tr.translate(0.8, -0.5, 0) cartNode.childs = [cartBodyNode, cartInsideNode, umbrellaTranslated] regularNode = csg.CustomSceneGraphNode("regular") regularNode.transform = tr.identity() regularNode.childs = [cartNode] regularNode.curr_pipeline = pipeline texNode = csg.CustomSceneGraphNode("tex") texNode.childs = [treeGroupNode] texNode.curr_pipeline = tex_pipeline decNode = csg.CustomSceneGraphNode("decorations" + str(i_z)) decNode.childs = [regularNode, texNode] return decNode
# As we work in 3D, we need to check which part is in front, # and which one is at the back glEnable(GL_DEPTH_TEST) # Creating shapes on GPU memory cpuAxis = bs.createAxis(7) gpuAxis = es.GPUShape().initBuffers() mvpPipeline.setupVAO(gpuAxis) gpuAxis.fillBuffers(cpuAxis.vertices, cpuAxis.indices, GL_STATIC_DRAW) redCarNode = createCar(mvpPipeline, 1, 0, 0) blueCarNode = createCar(mvpPipeline, 0, 0, 1) blueCarNode.transform = np.matmul(tr.rotationZ(-np.pi / 4), tr.translate(3.0, 0, 0.5)) # Using the same view and projection matrices in the whole application projection = tr.perspective(45, float(width) / float(height), 0.1, 100) glUniformMatrix4fv( glGetUniformLocation(mvpPipeline.shaderProgram, "projection"), 1, GL_TRUE, projection) view = tr.lookAt(np.array([5, 5, 7]), np.array([0, 0, 0]), np.array([0, 0, 1])) glUniformMatrix4fv(glGetUniformLocation(mvpPipeline.shaderProgram, "view"), 1, GL_TRUE, view) perfMonitor = pm.PerformanceMonitor(glfw.get_time(), 0.5) # glfw will swap buffers as soon as possible
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT) # imgui function impl.process_inputs() locationX, locationY, angle, color = \ transformGuiOverlay(locationX, locationY, angle, color) # Setting uniforms and drawing the Quad glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, np.matmul(tr.translate(locationX, locationY, 0.0), tr.rotationZ(angle))) glUniform3f( glGetUniformLocation(pipeline.shaderProgram, "modulationColor"), color[0], color[1], color[2]) pipeline.drawCall(gpuQuad) # Drawing the imgui texture over our drawing glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) impl.render(imgui.get_draw_data()) # Once the render is done, buffers are swapped, showing only the complete scene. glfw.swap_buffers(window) # freeing GPU memory gpuQuad.clear()
# Atributos entidades entities_current_pos = 0 entities_pos = 0 zombie_vel = 1 human_vel = 1 # Tamaño entidades sprite_size = 0.2 # Creacion de escenas decorations = createDecorations(pipeline, statictex_pipeline, 0.6, [-1, 1], treeN, 1) decorations_2 = createDecorations(pipeline, statictex_pipeline, 0.6, [-1, 1], treeN, 2) decorations_2.transform = tr.translate(0, 2, 0) decoration_scene = csg.CustomSceneGraphNode("decoration_node") decoration_scene.childs = [decorations, decorations_2] scene = createBackground(pipeline, 0) scene_2 = createBackground(pipeline, 1) scene_2.transform = tr.translate(0, 2, 0) full_scene = sg.SceneGraphNode("full_scene") full_scene.childs = [scene, scene_2] # Creacion jugador player = Player(sprite_size, P) playerModel = createPlayerModel(tex_pipeline)
"linearAttenuation"), 0.03) glUniform1f( glGetUniformLocation(lightingPipeline.shaderProgram, "quadraticAttenuation"), 0.01) glUniformMatrix4fv( glGetUniformLocation(lightingPipeline.shaderProgram, "projection"), 1, GL_TRUE, projection) glUniformMatrix4fv( glGetUniformLocation(lightingPipeline.shaderProgram, "view"), 1, GL_TRUE, view) # Drawing glUniformMatrix4fv( glGetUniformLocation(lightingPipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0.75, 0, 0)) lightingPipeline.drawCall(gpuDice) glUniformMatrix4fv( glGetUniformLocation(lightingPipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(-0.75, 0, 0)) lightingPipeline.drawCall(gpuDiceBlue) # Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen. glfw.swap_buffers(window) # freeing GPU memory gpuAxis.clear() gpuDice.clear() gpuDiceBlue.clear()
# Using GLFW to check for input events glfw.poll_events() # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT) # While left click is pressed, we have continous rotation movement if controller.leftClickOn: controller.theta += 4 * dt if controller.theta >= 2 * np.pi: controller.theta -= 2 * np.pi # Setting transform and drawing the rotating red quad glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, tr.matmul([ tr.rotationZ(controller.theta), tr.translate(0.5, 0.0, 0.0), tr.uniformScale(0.5) ])) pipeline.drawCall(redQuad) # Getting the mouse location in opengl coordinates mousePosX = 2 * (controller.mousePos[0] - width/2) / width mousePosY = 2 * (height/2 - controller.mousePos[1]) / height glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, np.matmul( tr.translate(mousePosX, mousePosY, 0), tr.uniformScale(0.3) )) pipeline.drawCall(greenQuad) # This is another way to work with keyboard inputs
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(cars, "wheelRotation") theta = -10 * glfw.get_time() wheelRotationNode.transform = tr.rotationZ(theta) # Modifying only car 3 car3 = sg.findNode(cars, "scaledCar3") car3.transform = tr.translate(0.3, 0.5 * np.sin(0.1 * theta), 0) # Uncomment to see the position of scaledCar_3, it will fill your terminal #print("car3Position =", sg.findPosition(cars, "scaledCar3")) # Drawing the Car sg.drawSceneGraphNode(cars, pipeline, "transform") # Once the render is done, buffers are swapped, showing only the complete scene. glfw.swap_buffers(window) # freeing GPU memory cars.clear() glfw.terminate()
# 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) # Clearing the screen glClear(GL_COLOR_BUFFER_BIT) # Using the time as the theta parameter theta = glfw.get_time() # Triangle triangleTransform = tr.matmul([ tr.translate(0.5, 0.5, 0), tr.rotationZ(2 * theta), tr.uniformScale(0.5) ]) # Updating the transform attribute glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, triangleTransform) # Drawing function pipeline.drawCall(gpuTriangle) # Another instance of the triangle triangleTransform2 = tr.matmul([ tr.translate(-0.5, 0.5, 0), tr.scale( 0.5 + 0.2 * np.cos(1.5 * theta),