示例#1
0
    def draw(self):
        GLFWViewer.poll_events()
        glViewport(0, 0, self.width, self.height)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        with program.use(self.prog) as prog:
            program.set_uniform(prog, 'projectionMatrix',
                                self.window.projection_matrix)
            program.set_uniform(prog, 'viewMatrix', self.window.view_matrix)
            program.set_uniform(prog, 'modelMatrix', np.eye(4))
            glBindTexture(GL_TEXTURE_2D, self.matcap_tex)
            program.set_uniform(prog, 'matCap', 0)
            imdraw.torusknot(prog)

        self.window.swap_buffers()
示例#2
0
window = GLFWViewer(width, height, (0.6, 0.7, 0.7, 1.0))

with window:
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_CULL_FACE)

    while not window.should_close():
        glViewport(0, 0, window.width, window.height)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        with program.use(skybox_program):
            program.set_uniform(skybox_program, 'projection',
                                window.projection_matrix)
            sky_view = glm.mat4(glm.mat3(window.view_matrix))
            program.set_uniform(skybox_program, 'view', sky_view)
            camera_pos = glm.transpose(
                glm.transpose(glm.inverse(window.view_matrix)))[3].xyz
            program.set_uniform(skybox_program, 'cameraPos', camera_pos)
            program.set_uniform(skybox_program, 'skybox', 0)
            program.set_uniform(skybox_program, 'groundProjection', True)
            glActiveTexture(GL_TEXTURE0 + 0)
            glBindTexture(GL_TEXTURE_CUBE_MAP, env_cubemap)
            imdraw.cube(skybox_program, flip=True)

            glBindTexture(GL_TEXTURE_CUBE_MAP, 0)
        glDepthMask(GL_TRUE)

        # draw grid

        window.swap_buffers()
        GLFWViewer.poll_events()
示例#3
0
    def render(self):
        # Animate
        # -------
        import math, time
        spotlight.position = glm.vec3(math.cos(time.time() * 3) * 4, 0.3, -4)
        spotlight.direction = -spotlight.position
        pointlight.position = glm.vec3(
            math.cos(time.time()) * 4, 4,
            math.sin(time.time()) * 4)
        self.camera.transform = glm.inverse(self.window.view_matrix)

        # Render passes
        # -------------
        ## Geometry
        self.geometry_pass.camera = self.camera  # window.projection_matrix, window.view_matrix
        self.geometry_pass.render()

        ## Shadowmaps
        dirlight.shadowpass.camera = dirlight.camera
        dirlight.shadowpass.render()
        spotlight.shadowpass.camera = spotlight.camera
        spotlight.shadowpass.render()
        pointlight.shadowpass.position = pointlight.position
        pointlight.shadowpass.render()

        ## Lighting
        self.lighting_pass.cameraPos = self.camera.position
        self.lighting_pass.gPosition = self.geometry_pass.gPosition
        self.lighting_pass.gNormal = self.geometry_pass.gNormal
        self.lighting_pass.gAlbedoSpecular = self.geometry_pass.gAlbedoSpecular
        self.lighting_pass.gRoughness = self.geometry_pass.gRoughness
        self.lighting_pass.gMetallic = self.geometry_pass.gMetallic
        self.lighting_pass.gEmissive = self.geometry_pass.gEmissive
        self.lighting_pass.render()

        ## Forward rendering

        #  - Copy depth from geometry pass
        glBindFramebuffer(GL_READ_FRAMEBUFFER, self.geometry_pass.gBuffer)
        glBindFramebuffer(GL_DRAW_FRAMEBUFFER,
                          self.forward_fbo)  # write to default framebuffer
        glBlitFramebuffer(0, 0, self.width, self.height, 0, 0, self.width,
                          self.height, GL_DEPTH_BUFFER_BIT, GL_NEAREST)
        glBindFramebuffer(GL_READ_FRAMEBUFFER, self.lighting_pass.fbo)
        glBlitFramebuffer(0, 0, self.width, self.height, 0, 0, self.width,
                          self.height, GL_COLOR_BUFFER_BIT, GL_NEAREST)
        glBindFramebuffer(GL_FRAMEBUFFER, 0)
        glEnable(GL_DEPTH_TEST)
        glDepthFunc(GL_LEQUAL)
        glDepthMask(GL_FALSE)

        with fbo.bind(self.forward_fbo):
            # draw skybox
            glViewport(0, 0, self.width, self.height)
            with program.use(self.skybox_program) as prog:
                program.set_uniform(prog, 'projection', self.camera.projection)
                sky_view = glm.mat4(glm.mat3(self.camera.view))
                program.set_uniform(prog, 'view', sky_view)
                program.set_uniform(prog, 'cameraPos', self.camera.position)
                program.set_uniform(prog, 'skybox', 0)
                program.set_uniform(prog, 'groundProjection', True)
                glActiveTexture(GL_TEXTURE0 + 0)
                glBindTexture(GL_TEXTURE_CUBE_MAP,
                              self.environment_pass.cubemap)
                imdraw.cube(prog, flip=True)

                glBindTexture(GL_TEXTURE_CUBE_MAP, 0)
            glDepthMask(GL_TRUE)

        ## Tonemapping
        self.tonemapping_pass.hdrimage = self.lighting_pass.beauty
        self.tonemapping_pass.exposure = 0.0
        self.tonemapping_pass.gamma = 2.2
        self.tonemapping_pass.render()

        # Render to screen
        # ----------------
        glViewport(0, 0, self.width, self.height)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glDisable(GL_DEPTH_TEST)

        # draw to screen
        imdraw.texture(self.tonemapping_pass.ldrimage,
                       (0, 0, self.width, self.height))

        # debug
        imdraw.texture(self.geometry_pass.gPosition, (0, 0, 90, 90))
        imdraw.texture(self.geometry_pass.gNormal, (100, 0, 90, 90))
        imdraw.texture(self.geometry_pass.gAlbedoSpecular, (200, 0, 90, 90),
                       shuffle=(0, 1, 2, -1))
        imdraw.texture(self.geometry_pass.gAlbedoSpecular, (300, 0, 90, 90),
                       shuffle=(3, 3, 3, -1))
        imdraw.texture(self.geometry_pass.gRoughness, (400, 0, 90, 90),
                       shuffle=(0, 0, 0, -1))
        imdraw.texture(self.geometry_pass.gMetallic, (500, 0, 90, 90),
                       shuffle=(0, 0, 0, -1))

        imdraw.texture(dirlight.shadowpass.texture, (0, 100, 90, 90),
                       shuffle=(0, 0, 0, -1))
        imdraw.texture(spotlight.shadowpass.texture, (100, 100, 90, 90),
                       shuffle=(0, 0, 0, -1))
        imdraw.cubemap(pointlight.shadowpass.cubemap, (200, 100, 90, 90),
                       self.window.projection_matrix, self.window.view_matrix)

        imdraw.texture(self.environment_tex, (0, 200, 90, 90))
        imdraw.cubemap(self.environment_pass.cubemap, (100, 200, 90, 90),
                       self.window.projection_matrix, self.window.view_matrix)

        imdraw.cubemap(self.irradiance_pass.irradiance, (200, 200, 90, 90),
                       self.window.projection_matrix, self.window.view_matrix)
        imdraw.cubemap(self.prefilter_pass.prefilter, (300, 200, 90, 90),
                       self.window.projection_matrix, self.window.view_matrix)
        imdraw.texture(self.brdf_pass.brdflut, (400, 200, 90, 90))

        imdraw.texture(self.lighting_pass.beauty, (0, 300, 90, 90))

        # swap buffers
        # ------------
        self.window.swap_buffers()
        GLFWViewer.poll_events()