예제 #1
0
    def initializeGL(self):
        # setup some OpenGL options
        glEnable(GL_DEPTH_TEST)

        _shaders = self.loadShaders('5.parallax_mapping.vs',
                                    '5.parallax_mapping.frag')
        self.__shader = glCreateProgram()
        [glAttachShader(self.__shader, s) for s in _shaders if s]
        self.__shader = shaders.ShaderProgram(self.__shader)
        glLinkProgram(self.__shader)
        glUseProgram(self.__shader)
        glUniform1i(glGetUniformLocation(self.__shader, 'diffuseMap'), 0)
        glUniform1i(glGetUniformLocation(self.__shader, 'normalMap'), 1)
        glUniform1i(glGetUniformLocation(self.__shader, 'depthMap'), 2)
        self.__shader.check_validate()
        self.__shader.check_linked()
        [glDeleteShader(s) for s in _shaders if s]

        # light source
        self.lightPos = np.array([0.5, 1.0, 0.3], np.float32)

        # load texture
        self.diffuseMap = loadTexture(
            os.path.join(abPath, '..', '..', 'resources', 'textures',
                         'bricks2.jpg'))
        self.normalMap = loadTexture(
            os.path.join(abPath, '..', '..', 'resources', 'textures',
                         'bricks2_normal.jpg'))
        self.heightMap = loadTexture(
            os.path.join(abPath, '..', '..', 'resources', 'textures',
                         'bricks2_disp.jpg'))
예제 #2
0
 def Load(self, name, uniforms, attributes):
     vertex_name, fragment_name = (os.path.join(
         'drawing', 'shaders', '%s_%s.glsl' % (name, typeof))
                                   for typeof in ('vertex', 'fragment'))
     codes = []
     for name in vertex_name, fragment_name:
         with open(name, 'rb') as f:
             data = f.read()
         codes.append(data)
     VERTEX_SHADER = shaders.compileShader(codes[0], GL_VERTEX_SHADER)
     FRAGMENT_SHADER = shaders.compileShader(codes[1], GL_FRAGMENT_SHADER)
     self.program = glCreateProgram()
     shads = (VERTEX_SHADER, FRAGMENT_SHADER)
     for shader in shads:
         glAttachShader(self.program, shader)
     self.fragment_shader_attrib_binding()
     self.program = shaders.ShaderProgram(self.program)
     glLinkProgram(self.program)
     self.program.check_validate()
     self.program.check_linked()
     for shader in shads:
         glDeleteShader(shader)
     #self.program    = shaders.compileProgram(VERTEX_SHADER,FRAGMENT_SHADER)
     for (namelist, func) in ((uniforms, glGetUniformLocation),
                              (attributes, glGetAttribLocation)):
         for name in namelist:
             setattr(self.locations, name, func(self.program, name))
예제 #3
0
    def display(self):
        """
        This is the function that performs the actual rendering.

        """
        with glsl.ShaderProgram(self._program.handle):
            try:
                # Initialize the OpenGL state
                glClearColor(*self._clear_color)

                glMatrixMode(GL_MODELVIEW)
                glLoadIdentity()
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

                glEnable(GL_CULL_FACE)
                glEnable(GL_DEPTH_TEST)
                glDepthFunc(GL_LESS)

                # Determine the model-view transform from the camera
                orientation = self.camera.orientation
                axis = orientation.axis()
                angle = orientation.angle()

                glRotatef(-180.0 * angle / math.pi, axis[0], axis[1], axis[2])

                position = self.camera.position
                x = position[0]
                y = position[1]
                z = position[2]

                glTranslate(-x, -y, -z)

                self._program.light_position(0, 0, 5000)

                # Reconstruct the VBO if necessary
                if self._vbo_voxels is None:
                    self._construct_vbo_voxels()

                # Pass all of the data to the hardware
                if self._vbo_voxels is not None:
                    self._vbo_voxels.bind()
                    try:
                        glEnableClientState(GL_VERTEX_ARRAY)
                        glEnableClientState(GL_NORMAL_ARRAY)
                        glEnableClientState(GL_COLOR_ARRAY)
                        glVertexPointer(3, GL_FLOAT, 40, self._vbo_voxels)
                        glNormalPointer(GL_FLOAT, 40, self._vbo_voxels + 12)
                        glColorPointer(4, GL_FLOAT, 40, self._vbo_voxels + 24)
                        glDrawArrays(GL_QUADS, 0, len(self._vbo_voxels))
                    finally:
                        self._vbo_voxels.unbind()
                        glDisableClientState(GL_VERTEX_ARRAY)
                        glDisableClientState(GL_NORMAL_ARRAY)
                        glDisableClientState(GL_COLOR_ARRAY)
            except Exception:
                # @todo need to print out the relevant information and terminate
                # execution.
                raise

        glutSwapBuffers()
예제 #4
0
    def initializeGL(self):
        # setup some OpenGL options
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)

        _shaders = self.loadShaders('3.2.point_shadows.vs', '3.2.point_shadows.frag')
        self.__shader = glCreateProgram()
        [glAttachShader(self.__shader, s) for s in _shaders if s]
        self.__shader = shaders.ShaderProgram(self.__shader)
        glLinkProgram(self.__shader)
        glUseProgram(self.__shader)
        glUniform1i(glGetUniformLocation(self.__shader, 'diffuseTexture'), 0)
        glUniform1i(glGetUniformLocation(self.__shader, 'depthMap'), 1)
        self.__shader.check_validate()
        self.__shader.check_linked()
        [glDeleteShader(s) for s in _shaders if s]

        vertexShader, fragmentShader, geometryShader = self.loadShaders('3.2.point_shadows_depth.vs', '3.2.point_shadows_depth.frag', '3.2.point_shadows_depth.gs')
        self.__simpleDepthShader = shaders.compileProgram(vertexShader, fragmentShader, geometryShader)

        # light source
        self.lightPos = np.array([0.0, 0.0, 0.0], np.float32)

        # load texture
        self.woodTexture = loadTexture(os.path.join(abPath, '..', '..', 'resources', 'textures', 'wood.png'))

        # Configure depth map FBO
        self.shadowWidth = 1024
        self.shadowHeight = 1024
        self.depthMapFBO = glGenFramebuffers(1)
        # Create depth texture
        self.depthCubeMap = glGenTextures(1)
        glBindTexture(GL_TEXTURE_CUBE_MAP, self.depthCubeMap)
        for i in range(6):
            glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT, self.shadowWidth, self.shadowHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, None)
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE)
        # Attach cubemap as depth map FBO's color buffer
        glBindFramebuffer(GL_FRAMEBUFFER, self.depthMapFBO)
        glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, self.depthCubeMap, 0)
        glDrawBuffer(GL_NONE)
        glReadBuffer(GL_NONE)
        if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE:
            print 'Framebuffer not complete!'
        glBindFramebuffer(GL_FRAMEBUFFER, 0)

        glClearColor(0.1, 0.1, 0.1, 1.0)
예제 #5
0
    def initializeGL(self):
        glViewport(0, 0, self.width(), self.height())
        # setup some OpenGL options
        glEnable(GL_DEPTH_TEST)

        vertexShader, fragmentShader = self.loadShaders('9.ssao_geometry.vs', '9.ssao_geometry.frag')
        self.__geometyPassShader = shaders.compileProgram(vertexShader, fragmentShader)
        vertexShader, fragmentShader = self.loadShaders('9.ssao.vs', '9.ssao_blur.frag')
        self.__ssaoBlurShader = shaders.compileProgram(vertexShader, fragmentShader)
        _shaders = self.loadShaders('9.ssao.vs', '9.ssao_lighting.frag')
        self.__lightingPassShader = glCreateProgram()
        [glAttachShader(self.__lightingPassShader, s) for s in _shaders if s]
        self.__lightingPassShader = shaders.ShaderProgram(self.__lightingPassShader)
        glLinkProgram(self.__lightingPassShader)
        glUseProgram(self.__lightingPassShader)
        glUniform1i(glGetUniformLocation(self.__lightingPassShader, 'gPositionDepth'), 0)
        glUniform1i(glGetUniformLocation(self.__lightingPassShader, 'gNormal'), 1)
        glUniform1i(glGetUniformLocation(self.__lightingPassShader, 'gAlbedo'), 2)
        glUniform1i(glGetUniformLocation(self.__lightingPassShader, 'ssao'), 3)
        self.__lightingPassShader.check_validate()
        self.__lightingPassShader.check_linked()
        [glDeleteShader(s) for s in _shaders if s]
        _shaders = self.loadShaders('9.ssao.vs', '9.ssao.frag')
        self.__ssaoShader = glCreateProgram()
        [glAttachShader(self.__ssaoShader, s) for s in _shaders if s]
        self.__ssaoShader = shaders.ShaderProgram(self.__ssaoShader)
        glLinkProgram(self.__ssaoShader)
        glUseProgram(self.__ssaoShader)
        glUniform1i(glGetUniformLocation(self.__ssaoShader, 'gPositionDepth'), 0)
        glUniform1i(glGetUniformLocation(self.__ssaoShader, 'gNormal'), 1)
        glUniform1i(glGetUniformLocation(self.__ssaoShader, 'texNoise'), 2)
        self.__ssaoShader.check_validate()
        self.__ssaoShader.check_linked()
        [glDeleteShader(s) for s in _shaders if s]

        # models
        modelPath = os.path.join(abPath, '..', '..', 'resources', 'objects', 'nanosuit', 'nanosuit.obj')
        self.cyborg = Model(modelPath)

        # light position
        self.lightPos = np.array([2.0, 4.0, -2.0], np.float32)
        self.lightColor = np.array([0.2, 0.2, 0.7], np.float32)

        # set up G-Buffer
        # 3 textures:
        # 1. Position (RGB)
        # 2. Color (RGB) + Specular (A)
        # 3. Normals (RGB)
        self.gbuffer = glGenFramebuffers(1)
        glBindFramebuffer(GL_FRAMEBUFFER, self.gbuffer)
        self.gPositionDepth, self.gNormal, self.gAlbedo = glGenTextures(3)
        # position color buffer
        glBindTexture(GL_TEXTURE_2D, self.gPositionDepth)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, self.width(), self.height(), 0, GL_RGBA, GL_FLOAT, None)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.gPositionDepth, 0)
        # normal color buffer
        glBindTexture(GL_TEXTURE_2D, self.gNormal)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, self.width(), self.height(), 0, GL_RGB, GL_FLOAT, None)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, self.gNormal, 0)
        # color + specular buffer
        glBindTexture(GL_TEXTURE_2D, self.gAlbedo)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, self.width(), self.height(), 0, GL_RGB, GL_FLOAT, None)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, self.gAlbedo, 0)
        # tell OpenGL which color attachments we'll use (of this framebuffer)
        attachments = [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2]
        glDrawBuffers(3, attachments)
        # create depth buffer (renderbuffer)
        self.rboDepth = glGenRenderbuffers(1)
        glBindRenderbuffer(GL_RENDERBUFFER, self.rboDepth)
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, self.width(), self.height())
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, self.rboDepth)
        if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE:
            print 'GBuffer Framebuffer not complete!'
        glBindFramebuffer(GL_FRAMEBUFFER, 0)

        # Also create framebuffer to hold SSAO processing stage
        self.ssaoFBO, self.ssaoBlurFBO = glGenFramebuffers(2)
        glBindFramebuffer(GL_FRAMEBUFFER, self.ssaoFBO)
        self.ssaoColorBuffer, self.ssaoColorBufferBlur = glGenTextures(2)
        # SSAO Color buffer
        glBindTexture(GL_TEXTURE_2D, self.ssaoColorBuffer)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, self.width(), self.height(), 0, GL_RGB, GL_FLOAT, None)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.ssaoColorBuffer, 0)
        if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE:
            print 'SSAO Framebuffer not complete!'
        # and blur stage
        glBindFramebuffer(GL_FRAMEBUFFER, self.ssaoBlurFBO)
        glBindTexture(GL_TEXTURE_2D, self.ssaoColorBufferBlur)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, self.width(), self.height(), 0, GL_RGB, GL_FLOAT, None)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.ssaoColorBufferBlur, 0)
        if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE:
            print 'SSAO Blur Framebuffer not complete!'
        glBindFramebuffer(GL_FRAMEBUFFER, 0)

        # Sample kernel
        self.ssaoKernel = []
        for i in range(64):
            sample = np.array([random.random() * 2.0 - 1.0,
                               random.random() * 2.0 - 1.0,
                               random.random()], np.float32)
            sample = glm.normalize(sample)
            sample *= random.random()
            scale = i / 64.0
            # scale samples s.t. they're more aligned to center of kernel
            scale = lerp(0.1, 1.0, scale * scale)
            sample *= scale
            self.ssaoKernel.append(sample)

        # Noise texture
        self.ssaoNoise = [np.array([random.random() * 2.0 - 1.0, random.random() * 2.0 - 1.0, 0.0], np.float32) for i in range(16)]
        self.ssaoNoise = np.array(self.ssaoNoise, np.float32)
        # for i in range(16):
        #     noise = np.array([random.random() * 2.0 - 1.0, random.random() * 2.0 - 1.0, 0.0], np.float32)
        self.noiseTexture = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, self.noiseTexture)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, 4, 4, 0, GL_RGB, GL_FLOAT, self.ssaoNoise)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)

        glClearColor(0.0, 0.0, 0.0, 1.0)
예제 #6
0
    def initializeGL(self):
        glViewport(0, 0, self.width(), self.height())
        # setup some OpenGL options
        glEnable(GL_DEPTH_TEST)

        vertexShader, fragmentShader = self.loadShaders(
            '8.g_buffer.vs', '8.g_buffer.frag')
        self.__geometyPassShader = shaders.compileProgram(
            vertexShader, fragmentShader)
        vertexShader, fragmentShader = self.loadShaders(
            '8.deferred_light_box.vs', '8.deferred_light_box.frag')
        self.__lightBoxShader = shaders.compileProgram(vertexShader,
                                                       fragmentShader)
        _shaders = self.loadShaders('8.deferred_shading.vs',
                                    '8.deferred_shading.frag')
        self.__lightingPassShader = glCreateProgram()
        [glAttachShader(self.__lightingPassShader, s) for s in _shaders if s]
        self.__lightingPassShader = shaders.ShaderProgram(
            self.__lightingPassShader)
        glLinkProgram(self.__lightingPassShader)
        glUseProgram(self.__lightingPassShader)
        glUniform1i(
            glGetUniformLocation(self.__lightingPassShader, 'gPosition'), 0)
        glUniform1i(glGetUniformLocation(self.__lightingPassShader, 'gNormal'),
                    1)
        glUniform1i(
            glGetUniformLocation(self.__lightingPassShader, 'gAlbedoSpec'), 2)
        self.__lightingPassShader.check_validate()
        self.__lightingPassShader.check_linked()
        [glDeleteShader(s) for s in _shaders if s]

        # models
        modelPath = os.path.join(abPath, '..', '..', 'resources', 'objects',
                                 'nanosuit', 'nanosuit.obj')
        self.cyborg = Model(modelPath)
        self.objectPosition = [
            np.array([-3.0, -3.0, -3.0], np.float32),
            np.array([0.0, -3.0, -3.0], np.float32),
            np.array([3.0, -3.0, -3.0], np.float32),
            np.array([-3.0, -3.0, 0.0], np.float32),
            np.array([0.0, -3.0, 0.0], np.float32),
            np.array([3.0, -3.0, 0.0], np.float32),
            np.array([-3.0, -3.0, 3.0], np.float32),
            np.array([0.0, -3.0, 3.0], np.float32),
            np.array([3.0, -3.0, 3.0], np.float32),
        ]

        # light position
        self.lightPos = []
        self.lightColors = []
        random.seed(13)
        for i in range(32):
            # calculate slightly random offsets
            xpos = (random.randint(0, 99) / 100.0) * 6.0 - 3.0
            ypos = (random.randint(0, 99) / 100.0) * 6.0 - 4.0
            zpos = (random.randint(0, 99) / 100.0) * 6.0 - 3.0
            self.lightPos.append(np.array([xpos, ypos, zpos], np.float32))
            # also calculate random color
            rcolor = (random.randint(0, 99) /
                      200.0) + 0.5  # Between 0.5 and 1.0
            gcolor = (random.randint(0, 99) /
                      200.0) + 0.5  # Between 0.5 and 1.0
            bcolor = (random.randint(0, 99) /
                      200.0) + 0.5  # Between 0.5 and 1.0
            self.lightColors.append(
                np.array([rcolor, gcolor, bcolor], np.float32))

        # set up G-Buffer
        # 3 textures:
        # 1. Position (RGB)
        # 2. Color (RGB) + Specular (A)
        # 3. Normals (RGB)
        self.gbuffer = glGenFramebuffers(1)
        glBindFramebuffer(GL_FRAMEBUFFER, self.gbuffer)
        self.gPosition, self.gNormal, self.gAlbedoSpec = glGenTextures(3)
        # position color buffer
        glBindTexture(GL_TEXTURE_2D, self.gPosition)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, self.width(), self.height(),
                     0, GL_RGB, GL_FLOAT, None)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                               GL_TEXTURE_2D, self.gPosition, 0)
        # normal color buffer
        glBindTexture(GL_TEXTURE_2D, self.gNormal)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, self.width(), self.height(),
                     0, GL_RGB, GL_FLOAT, None)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1,
                               GL_TEXTURE_2D, self.gNormal, 0)
        # color + specular buffer
        glBindTexture(GL_TEXTURE_2D, self.gAlbedoSpec)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, self.width(), self.height(), 0,
                     GL_RGBA, GL_UNSIGNED_BYTE, None)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2,
                               GL_TEXTURE_2D, self.gAlbedoSpec, 0)
        # tell OpenGL which color attachments we'll use (of this framebuffer)
        attachments = [
            GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2
        ]
        glDrawBuffers(3, attachments)
        # create depth buffer (renderbuffer)
        self.rboDepth = glGenRenderbuffers(1)
        glBindRenderbuffer(GL_RENDERBUFFER, self.rboDepth)
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT,
                              self.width(), self.height())
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                                  GL_RENDERBUFFER, self.rboDepth)
        if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE:
            print 'Framebuffer not complete!'
        glBindFramebuffer(GL_FRAMEBUFFER, 0)

        glClearColor(0.1, 0.1, 0.1, 1.0)
예제 #7
0
    def initializeGL(self):
        glViewport(0, 0, self.width(), self.height())
        # setup some OpenGL options
        glEnable(GL_DEPTH_TEST)

        vertexShader, fragmentShader = self.loadShaders(
            '7.bloom.vs', '7.bloom.frag')
        self.__shader = shaders.compileProgram(vertexShader, fragmentShader)
        vertexShader, fragmentShader = self.loadShaders(
            '7.bloom.vs', '7.light_box.frag')
        self.__lightShader = shaders.compileProgram(vertexShader,
                                                    fragmentShader)
        vertexShader, fragmentShader = self.loadShaders(
            '7.blur.vs', '7.blur.frag')
        self.__blurShader = shaders.compileProgram(vertexShader,
                                                   fragmentShader)
        _shaders = self.loadShaders('7.bloom_final.vs', '7.bloom_final.frag')
        self.__bloomFinalShader = glCreateProgram()
        [glAttachShader(self.__bloomFinalShader, s) for s in _shaders if s]
        self.__bloomFinalShader = shaders.ShaderProgram(
            self.__bloomFinalShader)
        glLinkProgram(self.__bloomFinalShader)
        glUseProgram(self.__bloomFinalShader)
        glUniform1i(glGetUniformLocation(self.__bloomFinalShader, 'scene'), 0)
        glUniform1i(glGetUniformLocation(self.__bloomFinalShader, 'bloomBlur'),
                    1)
        self.__bloomFinalShader.check_validate()
        self.__bloomFinalShader.check_linked()
        [glDeleteShader(s) for s in _shaders if s]

        # light source
        self.lightPos = [
            np.array([0.0, 0.5, 1.5], np.float32),
            np.array([-4.0, 0.0, 0.0], np.float32),
            np.array([0.0, 0.0, 15.0], np.float32),
            np.array([0.0, 1.5, 0.0], np.float32),
        ]

        self.lightColors = [
            np.array([5.0, 5.0, 5.0], np.float32),
            np.array([5.0, 0.0, 0.0], np.float32),
            np.array([0.0, 0.0, 15.0], np.float32),
            np.array([0.0, 1.5, 0.0], np.float32),
        ]

        # load texture
        self.woodTexture = loadTexture(
            os.path.join(abPath, '..', '..', 'resources', 'textures',
                         'wood.png'))
        self.containerTexture = loadTexture(
            os.path.join(abPath, '..', '..', 'resources', 'textures',
                         'container2.png'))

        # Set up floating point framebuffer to render scene to
        self.bloomFBO = glGenFramebuffers(1)
        glBindFramebuffer(GL_FRAMEBUFFER, self.bloomFBO)
        # Create floating point color buffer
        self.colorBuffers = glGenTextures(2)
        for i in range(len(self.colorBuffers)):
            glBindTexture(GL_TEXTURE_2D, self.colorBuffers[i])
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, self.width(),
                         self.height(), 0, GL_RGB, GL_FLOAT, None)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
            # attach texture to framebuffer
            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i,
                                   GL_TEXTURE_2D, self.colorBuffers[i], 0)
        # create depth buffer (renderbuffer)
        self.rboDepth = glGenRenderbuffers(1)
        glBindRenderbuffer(GL_RENDERBUFFER, self.rboDepth)
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT,
                              self.width(), self.height())
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
                                  GL_RENDERBUFFER, self.rboDepth)
        # Tell OpenGL which color attachments we'll use (of this framebuffer) for rendering
        attachments = [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1]
        glDrawBuffers(2, attachments)
        if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE:
            print 'Framebuffer not complete!'
        glBindFramebuffer(GL_FRAMEBUFFER, 0)

        # Ping pong framebuffer for blurring
        self.pingpongFBO = glGenFramebuffers(2)
        self.pingpongColorbuffers = glGenTextures(2)
        for i in range(len(self.pingpongFBO)):
            glBindFramebuffer(GL_FRAMEBUFFER, self.pingpongFBO[i])
            glBindTexture(GL_TEXTURE_2D, self.pingpongColorbuffers[i])
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, self.width(),
                         self.height(), 0, GL_RGB, GL_FLOAT, None)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                   GL_TEXTURE_2D, self.pingpongColorbuffers[i],
                                   0)
            # also check if framebuffer are complete (no need for depth buffer)
            if glCheckFramebufferStatus(
                    GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE:
                print 'Framebuffer not complete!'
        glBindFramebuffer(GL_FRAMEBUFFER, 0)

        glClearColor(0.1, 0.1, 0.1, 1.0)