def createGPUColorQuad(r, g, b): shapeQuad = bs.createColorQuad(r, g, b) gpuQuad = es.GPUShape().initBuffers() pipeline.setupVAO(gpuQuad) gpuQuad.fillBuffers(shapeQuad.vertices, shapeQuad.indices, GL_STATIC_DRAW) return gpuQuad
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 createTextureGPUShape(shape, pipeline, path): # Funcion Conveniente para facilitar la inicializacion de un GPUShape con texturas gpuShape = es.GPUShape().initBuffers() pipeline.setupVAO(gpuShape) gpuShape.fillBuffers(shape.vertices, shape.indices, GL_STATIC_DRAW) gpuShape.texture = es.textureSimpleSetup(path, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST) return gpuShape
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 createGPUShape(shape, pipeline): # Funcion Conveniente para facilitar la inicializacion de un GPUShape gpuShape = es.GPUShape().initBuffers() pipeline.setupVAO(gpuShape) gpuShape.fillBuffers(shape.vertices, shape.indices, GL_STATIC_DRAW) return gpuShape
glfw.make_context_current(window) # Connecting the callback function 'on_key' to handle keyboard events glfw.set_key_callback(window, on_key) # Creating shader program pipeline = es.SimpleTextureTransformShaderProgram() # Setting up the clear screen color glClearColor(0.25, 0.25, 0.25, 1.0) # Shape on CPU memory shapeTextureQuad = bs.createTextureQuad(1, 1) # Shapes on GPU memory gpuShapeWithoutMipmap = es.GPUShape().initBuffers() pipeline.setupVAO(gpuShapeWithoutMipmap) gpuShapeWithoutMipmap.fillBuffers(shapeTextureQuad.vertices, shapeTextureQuad.indices, GL_STATIC_DRAW) gpuShapeWithoutMipmap.texture = es.textureSimpleSetup( getAssetPath("red_woodpecker.jpg"), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_LINEAR, GL_LINEAR) # Since we want to draw the same shape, but with mipmaps in its texture, there is no need to duplicate # the information in the GPU, we can just use the same buffers... gpuShapeWithMipmap = es.GPUShape() gpuShapeWithMipmap.vao = gpuShapeWithoutMipmap.vao gpuShapeWithMipmap.vbo = gpuShapeWithoutMipmap.vbo gpuShapeWithMipmap.ebo = gpuShapeWithoutMipmap.ebo gpuShapeWithMipmap.size = gpuShapeWithoutMipmap.size
texture2dPipeline = es.SimpleTextureTransformShaderProgram() # Setting up the clear screen color glClearColor(0.25, 0.25, 0.25, 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) # Enabling transparencies glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) # 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) rainbowCube = bs.createRainbowCube() gpuRainbowCube = es.GPUShape().initBuffers() mvpPipeline.setupVAO(gpuRainbowCube) gpuRainbowCube.fillBuffers(rainbowCube.vertices, rainbowCube.indices, GL_STATIC_DRAW) shapeBoo = bs.createTextureQuad(1,1) gpuBoo = es.GPUShape().initBuffers() texture2dPipeline.setupVAO(gpuBoo) gpuBoo.fillBuffers(shapeBoo.vertices, shapeBoo.indices, GL_STATIC_DRAW) gpuBoo.texture = es.textureSimpleSetup( getAssetPath("boo.png"), GL_REPEAT, GL_REPEAT, GL_NEAREST, GL_NEAREST)
def createGPUShape(pipeline, shape): gpuShape = es.GPUShape().initBuffers() pipeline.setupVAO(gpuShape) gpuShape.fillBuffers(shape.vertices, shape.indices, GL_STATIC_DRAW) return gpuShape
# Assembling the shader program pipeline = es.SimpleModelViewProjectionShaderProgram() # Telling OpenGL to use our shader program glUseProgram(pipeline.shaderProgram) # Setting up the clear screen color 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() pipeline.setupVAO(gpuAxis) gpuAxis.fillBuffers(cpuAxis.vertices, cpuAxis.indices, GL_STATIC_DRAW) simpleParaboloid = lambda x, y: paraboloid(x, y, 3.0, 3.0) # generate a numpy array with 40 samples between -10 and 10 xs = np.ogrid[-10:10:20j] ys = np.ogrid[-10:10:20j] cpuSurface = generateMesh(xs, ys, simpleParaboloid, [1,0,0]) gpuSurface = es.GPUShape().initBuffers() pipeline.setupVAO(gpuSurface) gpuSurface.fillBuffers(cpuSurface.vertices, cpuSurface.indices, GL_STATIC_DRAW) t0 = glfw.get_time() camera_theta = np.pi/7
glBindVertexArray(VAO) # 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) # 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 shapeCube = bs.createRainbowCube() gpuCube = es.GPUShape().initBuffers() pipeline.setupVAO(gpuCube) gpuCube.fillBuffers(shapeCube.vertices, shapeCube.indices, GL_STATIC_DRAW) 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 in both, color and depth glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# now that we actually created the framebuffer and added all attachments we want to check if it is actually complete now if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE: print("ERROR::FRAMEBUFFER:: Framebuffer is not complete!") # Binding again the default shape buffer glBindFramebuffer(GL_FRAMEBUFFER, 0) # Creating shader programs for textures and for colors ####################### textureShaderProgram = es.SimpleTextureTransformShaderProgram() colorShaderProgram = es.SimpleModelViewProjectionShaderProgram() # Creating shapes on GPU memory ####################### rainbowCube = bs.createRainbowCube() gpuRainbowCube = es.GPUShape().initBuffers() colorShaderProgram.setupVAO(gpuRainbowCube) gpuRainbowCube.fillBuffers(rainbowCube.vertices, rainbowCube.indices, GL_STATIC_DRAW) shapeQuad = bs.createTextureQuad(1, 1) gpuQuad = es.GPUShape().initBuffers() textureShaderProgram.setupVAO(gpuQuad) gpuQuad.fillBuffers(shapeQuad.vertices, shapeQuad.indices, GL_STATIC_DRAW) gpuQuad.texture = textureColorbuffer # <--- Here is where the magic happens! # Setting up the scene view = tr.lookAt(np.array([1.0, 1.0, 1.0]), np.array([0.0, 0.0, 0.0]), np.array([0.0, 0.0, 1.0])) projection = tr.ortho(-1, 1, -1, 1, 0.1, 100)
# Binding artificial vertex array object for validation VAO = glGenVertexArrays(1) glBindVertexArray(VAO) # A simple shader program with position and texture coordinates as inputs. pipeline = es.SimpleTextureTransformShaderProgram() # Telling OpenGL to use our shader program glUseProgram(pipeline.shaderProgram) # Setting up the clear screen color glClearColor(0.25, 0.25, 0.25, 1.0) # Creating shapes on GPU memory shape = bs.createTextureQuad(2, 2) gpuShape = es.GPUShape().initBuffers() pipeline.setupVAO(gpuShape) 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)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) # Creating texture with all characters textBitsTexture = tx.generateTextBitsTexture() # Moving texture to GPU memory gpuText3DTexture = tx.toOpenGLTexture(textBitsTexture) # Testing text 3D texture. Check the terminal! for char in "hi!": print(textBitsTexture[:, :, ord(char)].transpose() * 255) print() # Creating shapes on GPU memory backgroundShape = bs.createTextureQuad(1, 1) bs.scaleVertices(backgroundShape, 5, [2, 2, 1]) gpuBackground = es.GPUShape().initBuffers() texturePipeline.setupVAO(gpuBackground) gpuBackground.fillBuffers(backgroundShape.vertices, 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)
glfw.set_key_callback(window, on_key) # Creating shader programs for textures and for colors textureShaderProgram = es.SimpleTextureModelViewProjectionShaderProgram() colorShaderProgram = es.SimpleModelViewProjectionShaderProgram() # Setting up the clear screen color 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 shapeDice = createDice() gpuDice = es.GPUShape().initBuffers() textureShaderProgram.setupVAO(gpuDice) gpuDice.fillBuffers(shapeDice.vertices, shapeDice.indices, GL_STATIC_DRAW) gpuDice.texture = es.textureSimpleSetup(getAssetPath("dice_blue.jpg"), GL_REPEAT, GL_REPEAT, GL_LINEAR, GL_LINEAR) cpuAxis = bs.createAxis(2) gpuAxis = es.GPUShape().initBuffers() colorShaderProgram.setupVAO(gpuAxis) gpuAxis.fillBuffers(cpuAxis.vertices, cpuAxis.indices, GL_STATIC_DRAW) while not glfw.window_should_close(window): # Using GLFW to check for input events glfw.poll_events()
# Si hay una colision entre la tienda y el jugador, se termina el juego if store.collisionPlayer(player) and not controller.end: controller.end = True # Se crea una espiral mediante modificaciones de vertices segun el tiempo y transformaciones en CPU spiral = createSpiral(50, t1) for m in range(0, len(spiral.vertices), 6): current_vert = [ spiral.vertices[m], spiral.vertices[m + 1], spiral.vertices[m + 2], 1 ] new_vert = tr.matmul([tr.scale(0.02, 0.02, 1), current_vert]) spiral.vertices[m] = new_vert[0] spiral.vertices[m + 1] = new_vert[1] spiral.vertices[m + 2] = new_vert[2] GPUspiral = es.GPUShape().initBuffers() pipeline.setupVAO(GPUspiral) GPUspiral.fillBuffers(spiral.vertices, spiral.indices, GL_STREAM_DRAW) # Si el jugador esta infectado, aparece una espiral rotando encima de su modelo y se distoricona la vision 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(
if not window: glfw.terminate() glfw.set_window_should_close(window, True) glfw.make_context_current(window) # Creating our shader program and telling OpenGL to use it pipeline = ModulationTransformShaderProgram() glUseProgram(pipeline.shaderProgram) # Setting up the clear screen color glClearColor(0.15, 0.15, 0.15, 1.0) # Creating shapes on GPU memory shapeQuad = bs.createRainbowQuad() gpuQuad = es.GPUShape().initBuffers() pipeline.setupVAO(gpuQuad) gpuQuad.fillBuffers(shapeQuad.vertices, shapeQuad.indices, GL_STATIC_DRAW) # initilize imgui context (see documentation) imgui.create_context() impl = GlfwRenderer(window) # Connecting the callback function 'on_key' to handle keyboard events # It is important to set the callback after the imgui setup glfw.set_key_callback(window, on_key) locationX = 0.0 locationY = 0.0 angle = 0.0 color = (1.0, 1.0, 1.0)
glfw.set_key_callback(window, on_key) # 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) # 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 shapeTriangle = bs.createRainbowTriangle() gpuTriangle = es.GPUShape().initBuffers() pipeline.setupVAO(gpuTriangle) gpuTriangle.fillBuffers(shapeTriangle.vertices, shapeTriangle.indices, GL_STATIC_DRAW) shapeQuad = bs.createRainbowQuad() gpuQuad = es.GPUShape().initBuffers() pipeline.setupVAO(gpuQuad) gpuQuad.fillBuffers(shapeQuad.vertices, shapeQuad.indices, GL_STATIC_DRAW) shapeCube = bs.createRainbowCube() gpuCube = es.GPUShape().initBuffers() pipeline.setupVAO(gpuCube) gpuCube.fillBuffers(shapeCube.vertices, shapeCube.indices, GL_STATIC_DRAW) shapeCircle = bs.createRainbowCircle(20)