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 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 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 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
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "view"), 1, GL_TRUE, view) # Setting up the projection transform projection = tr.perspective(60, float(width)/float(height), 0.1, 100) 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) glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.identity()) pipeline.drawCall(gpuAxis, GL_LINES) # 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.uniformScale(0.5)) pipeline.drawCall(gpuSurface) # Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen. glfw.swap_buffers(window) # freeing GPU memory gpuSurface.clear() gpuAxis.clear() glfw.terminate()
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) glUseProgram(pipeline.shaderProgram) # Drawing the shapes glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, tr.uniformScale(1.5)) # Bindear los samplers a las unidades de texturas glUniform1i(glGetUniformLocation(pipeline.shaderProgram, "upTexture"), 0) glUniform1i(glGetUniformLocation(pipeline.shaderProgram, "downTexture"), 1) # Posicion vertical del mouse glUniform1f(glGetUniformLocation(pipeline.shaderProgram, "mousePosY"), controller.mousePos[1]) pipeline.drawCall(gpuShape) # Once the render is done, buffers are swapped, showing only the complete scene. glfw.swap_buffers(window) # freeing GPU memory gpuShape.clear() glfw.terminate()
gpuShape.fillBuffers(shape.vertices, shape.indices, GL_STATIC_DRAW) gpuShape.texture = es.textureSimpleSetup(getAssetPath("bricks.jpg"), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_LINEAR, GL_LINEAR) 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 shapes glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, tr.uniformScale(1.5)) pipeline.drawCall(gpuShape) # Once the render is done, buffers are swapped, showing only the complete scene. glfw.swap_buffers(window) # freeing GPU memory gpuShape.clear() glfw.terminate()
if (controller.fillPolygon): glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) theta = 0.3 * np.sin(glfw.get_time()) # Clearing the screen in both, color and depth glClear(GL_COLOR_BUFFER_BIT) glUseProgram(pipeline.shaderProgram) # Drawing the shapes glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, np.matmul(tr.shearing(0, theta, 0, 0, 0, 0), tr.uniformScale(1.5))) # Binding samplers to both texture units glUniform1i(glGetUniformLocation(pipeline.shaderProgram, "upTexture"), 0) glUniform1i( glGetUniformLocation(pipeline.shaderProgram, "downTexture"), 1) # Sending the mouse vertical location to our shader glUniform1f(glGetUniformLocation(pipeline.shaderProgram, "mousePosY"), controller.mousePos[1]) pipeline.drawCall(gpuShape) # Once the render is done, buffers are swapped, showing only the complete scene. glfw.swap_buffers(window)
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 # Here we request the state of a given key
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), 0)
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) else: glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # Drawing shapes glUseProgram(pipeline.shaderProgram) glUniform3f( glGetUniformLocation(pipeline.shaderProgram, "viewPosition"), viewPos[0], viewPos[1], viewPos[2]) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "view"), 1, GL_TRUE, view) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.uniformScale(3)) pipeline.drawCall(gpuSuzanne) glUniformMatrix4fv( glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.matmul([ tr.uniformScale(3), tr.rotationX(np.pi / 2), tr.translate(1.5, -0.25, 0) ])) pipeline.drawCall(gpuCarrot) glUseProgram(mvpPipeline.shaderProgram) glUniformMatrix4fv( glGetUniformLocation(mvpPipeline.shaderProgram, "view"), 1, GL_TRUE, view)