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 createShape(): """ Generating a circle, where the vertices at the border are generated via matrix transformations. """ # Adding the vertex at the center, white color to identify it # position color vertices = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0] indices = [] # This vector will be used as reference to be transformed xt = np.array([1, 0, 0, 1]) # We iterate generating vertices over the circle border for i in range(0, 30): # attempt 1: modifying manually each vertex. # positions colors #vertices += [r * np.cos(0.1 *i * np.pi), r * np.sin(0.1 *i * np.pi), 0.0, 1,0,0] # attempt 2: using matrix transformations transformation = tr.rotationZ(0.1 * i * np.pi) xtp = np.matmul(transformation, xt) # returning to cartesian coordinates from homogeneous coordinates xtr = np.array([xtp[0], xtp[1], xtp[2]]) / xtp[3] # Adding the new vertex in clue color # position color vertices += [xtr[0], xtr[1], xtr[2], 0.0, 0.0, 1.0] # do not forget the indices! indices += [0, i + 1, i + 2] # removing the last spare vertex indices.pop() return bs.Shape(vertices, indices)
# 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()
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
cars = createCars(pipeline, 5) # 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(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
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
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 glUseProgram(colorShaderProgram.shaderProgram) glUniformMatrix4fv( glGetUniformLocation(colorShaderProgram.shaderProgram, "projection"), 1, GL_TRUE, projection) glUniformMatrix4fv( glGetUniformLocation(colorShaderProgram.shaderProgram, "model"), 1, GL_TRUE, tr.rotationZ(theta)) glUniformMatrix4fv( glGetUniformLocation(colorShaderProgram.shaderProgram, "view"), 1, GL_TRUE, view) colorShaderProgram.drawCall(gpuRainbowCube) # 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:
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) gpuDate.fillBuffers(dateShape.vertices, dateShape.indices, GL_STATIC_DRAW)
# 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)
if player.infected: for entity in entityList: entity.pos[0] += math.cos(t1) * delta * 0.2 * entity.pos[1] full_scene.transform = tr.shearing( math.cos(t1) * 0.2, 0, 0, 0, 0, 0) decoration_scene.transform = tr.shearing( math.cos(t1) * 0.2, 0, 0, 0, 0, 0) fullStoreNode.transform = tr.shearing( math.cos(t1) * 0.2, 0, 0, 0, 0, 0) glUseProgram(pipeline.shaderProgram) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, tr.matmul([ tr.translate(player.pos[0], player.pos[1] + 0.1, 0), tr.rotationZ(t1) ])) pipeline.drawCall(GPUspiral, GL_LINE_STRIP) # Se quitan de la lista de entidades aquellas que salieron de la pantalla esta iteracion entityList = interationEntities entityModelsList = iterationModels # Si el jugador se transforma en zombie, se acaba el juego y se dibuja la escena de game over if not player.human: if game_over_pos > 0: game_over_pos -= 1.2 * delta gameOverScene.transform = tr.translate(game_over_pos, 0, 0) glUseProgram(tex_pipeline.shaderProgram) sg.drawSceneGraphNode(gameOverScene, tex_pipeline, "transform")
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 = np.sin(3.0 * glfw.get_time()) leftArm = sg.findNode(snowman, "leftArm") leftArm.transform = tr.matmul([ tr.translate(-7, 7, 0), tr.rotationZ(theta), #tr.scale(1, 1, 1) ]) rightArm = sg.findNode(snowman, "rightArm") rightArm.transform = tr.matmul([ tr.translate(7, 7, 0), tr.rotationZ(theta), #tr.scale(1, 1, 1) ]) # Drawing the Car sg.drawSceneGraphNode( snowman, pipeline, "transform", np.matmul(tr.translate(0, -0.5, 0), tr.scale(0.03, 0.03, 1)))
glClearColor(0.85, 0.85, 0.85, 1.0) # 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)
# 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), 0.5 + 0.2 * np.sin(2 * theta),
glClearColor(0.85, 0.85, 0.85, 1.0) # 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