Ejemplo n.º 1
0
class CommonBuffer(object):
    
    def __init__(self):
        self.data = C_CommonBuffer()
        self.UBO = UBO()
    
    def load(self, scene, resolution, sample_offset, sample_count):
        self.data.CAMERA = tuple(scene.camera.camera_matrix)
        self.data.PROJECTION = tuple(scene.camera.projection_matrix)
        self.data.RESOLUTION = resolution
        self.data.SAMPLE_OFFSET = sample_offset
        self.data.SAMPLE_COUNT = sample_count
        self.data.FRAME = scene.frame
        self.data.TIME = scene.time

        self.UBO.load_data(self.data)
    
    def bind(self, location):
        self.UBO.bind(location)
Ejemplo n.º 2
0
class LightsBuffer(object):
    def __init__(self):
        self.data = C_LightsBuffer()
        self.UBO = UBO()

    def load(self, scene):
        for i, light in enumerate(scene.lights):
            self.data.lights[i].color = light.color
            self.data.lights[i].type = light.type
            self.data.lights[i].position = light.position
            self.data.lights[i].radius = light.radius
            self.data.lights[i].direction = light.direction
            self.data.lights[i].spot_angle = light.spot_angle
            self.data.lights[i].spot_blend = light.spot_blend
        self.data.lights_count = len(scene.lights)

        self.UBO.load_data(self.data)

    def bind(self, location):
        self.UBO.bind(location)
Ejemplo n.º 3
0
 def __init__(self):
     self.data = C_LightsBuffer()
     self.UBO = UBO()
     self.shadowmaps = ShadowMaps()
     self.common_buffer = Common.CommonBuffer()
Ejemplo n.º 4
0
class LightsBuffer(object):
    
    def __init__(self):
        self.data = C_LightsBuffer()
        self.UBO = UBO()
        self.shadowmaps = ShadowMaps()
        self.common_buffer = Common.CommonBuffer()
    
    def load(self, scene, pipeline, pass_name, cascades_distribution_exponent):
        #TODO: Automatic distribution exponent based on FOV
        #TODO: Configurable cascades number ???

        scene = copy.copy(scene)
        real_scene_camera = scene.camera
        scene.camera = copy.deepcopy(scene.camera)
        self.shadowmaps.load(scene)
        UBOS = {
            'COMMON_UNIFORMS' : self.common_buffer
        }
        spot_count = 0
        sun_count = 0
        point_count = 0

        for i, light in enumerate(scene.lights):
            self.data.lights[i].color = light.color
            self.data.lights[i].type = light.type
            self.data.lights[i].position = light.position
            self.data.lights[i].radius = light.radius
            self.data.lights[i].direction = light.direction
            self.data.lights[i].spot_angle = light.spot_angle
            self.data.lights[i].spot_blend = light.spot_blend

            if light.type == LIGHT_SPOT:
                self.data.lights[i].type_index = spot_count

                camera_matrix = pyrr.Matrix44(light.matrix)
                
                projection_matrix = make_projection_matrix(light.spot_angle,1,0.01,light.radius)
                spot_matrix = projection_matrix * camera_matrix
                
                self.data.spot_matrices[spot_count] = tuple([e for vector in spot_matrix for e in vector])

                scene.camera.camera_matrix = light.matrix
                scene.camera.projection_matrix = tuple([e for vector in projection_matrix for e in vector])
                
                offset = pipeline.get_samples()[pipeline.sample_count]
                self.common_buffer.load(scene, (spot_resolution, spot_resolution), offset, pipeline.sample_count)

                self.shadowmaps.spot_fbos[spot_count].clear(depth=1)
                pipeline.draw_scene_pass(self.shadowmaps.spot_fbos[spot_count], 
                    scene.objects, pass_name, pipeline.default_shader[pass_name], UBOS)

                spot_count+=1
            
            if light.type == LIGHT_SUN:
                self.data.lights[i].type_index = sun_count

                sun_matrix = pyrr.Matrix44(light.matrix)
                projection_matrix = pyrr.Matrix44(real_scene_camera.projection_matrix)
                view_matrix = projection_matrix * pyrr.Matrix44(real_scene_camera.camera_matrix)

                cascades_matrices = get_sun_cascades(sun_matrix, projection_matrix, view_matrix, sun_cascades, cascades_distribution_exponent)
                
                for i, cascade in enumerate(cascades_matrices):
                    cascade = tuple([e for vector in cascade for e in vector])
                    
                    scene.camera.camera_matrix = cascade
                    scene.camera.projection_matrix = tuple([e for vector in pyrr.Matrix44.identity() for e in vector])

                    self.data.sun_matrices[sun_count * sun_cascades + i] = cascade
                
                    offset = pipeline.get_samples()[pipeline.sample_count]
                    self.common_buffer.load(scene, (sun_resolution, sun_resolution), offset, pipeline.sample_count)

                    fbo = self.shadowmaps.sun_fbos[sun_count * sun_cascades + i]
                    fbo.clear(depth=1)
                    glEnable(GL_DEPTH_CLAMP)
                    pipeline.draw_scene_pass(fbo, scene.objects, pass_name, pipeline.default_shader[pass_name], UBOS)
                    glDisable(GL_DEPTH_CLAMP)

                sun_count+=1
            
            
        self.data.lights_count = len(scene.lights)
        
        self.UBO.load_data(self.data)
    
    def bind(self, location):
        self.UBO.bind(location)

    def shader_callback(self, shader):
        shader.textures['SPOT_SHADOWMAPS'] = self.shadowmaps.spot_t
        shader.textures['SUN_SHADOWMAPS'] = self.shadowmaps.sun_t
Ejemplo n.º 5
0
class LightsBuffer(object):
    def __init__(self):
        self.data = C_LightsBuffer()
        self.UBO = UBO()
        self.shadowmaps = ShadowMaps()
        self.common_buffer = Common.CommonBuffer()

    def load(self,
             scene,
             pipeline,
             pass_name,
             cascades_distribution_exponent,
             spot_resolution=2048,
             sun_resolution=2048,
             point_resolution=512):
        #TODO: Automatic distribution exponent based on FOV

        scene = copy.copy(scene)
        real_scene_camera = scene.camera
        scene.camera = copy.deepcopy(scene.camera)
        self.shadowmaps.load(scene, spot_resolution, sun_resolution,
                             point_resolution)
        UBOS = {'COMMON_UNIFORMS': self.common_buffer}
        spot_count = 0
        sun_count = 0
        point_count = 0

        for i, light in enumerate(scene.lights):
            self.data.lights[i].color = light.color
            self.data.lights[i].type = light.type
            self.data.lights[i].position = light.position
            self.data.lights[i].radius = light.radius
            self.data.lights[i].direction = light.direction
            self.data.lights[i].spot_angle = light.spot_angle
            self.data.lights[i].spot_blend = light.spot_blend

            if light.type == LIGHT_SPOT:
                self.data.lights[i].type_index = spot_count

                camera_matrix = pyrr.Matrix44(light.matrix)

                projection_matrix = make_projection_matrix(
                    light.spot_angle, 1, 0.01, light.radius)
                spot_matrix = projection_matrix * camera_matrix

                self.data.spot_matrices[spot_count] = tuple(
                    [e for vector in spot_matrix for e in vector])

                scene.camera.camera_matrix = light.matrix
                scene.camera.projection_matrix = tuple(
                    [e for vector in projection_matrix for e in vector])

                offset = pipeline.get_samples()[pipeline.sample_count]
                spot_resolution = self.shadowmaps.spot_resolution
                self.common_buffer.load(scene,
                                        (spot_resolution, spot_resolution),
                                        offset, pipeline.sample_count)

                self.shadowmaps.spot_fbos[spot_count].clear(depth=1)
                pipeline.draw_scene_pass(self.shadowmaps.spot_fbos[spot_count],
                                         scene.objects, pass_name,
                                         pipeline.default_shader[pass_name],
                                         UBOS)

                spot_count += 1

            if light.type == LIGHT_SUN:
                self.data.lights[i].type_index = sun_count

                sun_matrix = pyrr.Matrix44(light.matrix)
                projection_matrix = pyrr.Matrix44(
                    real_scene_camera.projection_matrix)
                view_matrix = projection_matrix * pyrr.Matrix44(
                    real_scene_camera.camera_matrix)

                sun_cascades = self.shadowmaps.sun_cascades
                sun_resolution = self.shadowmaps.sun_resolution
                cascades_matrices = get_sun_cascades(
                    sun_matrix, projection_matrix, view_matrix, sun_cascades,
                    cascades_distribution_exponent)

                for i, cascade in enumerate(cascades_matrices):
                    cascade = tuple([e for vector in cascade for e in vector])

                    scene.camera.camera_matrix = cascade
                    scene.camera.projection_matrix = tuple([
                        e for vector in pyrr.Matrix44.identity()
                        for e in vector
                    ])

                    self.data.sun_matrices[sun_count * sun_cascades +
                                           i] = cascade

                    offset = pipeline.get_samples()[pipeline.sample_count]
                    self.common_buffer.load(scene,
                                            (sun_resolution, sun_resolution),
                                            offset, pipeline.sample_count)

                    fbo = self.shadowmaps.sun_fbos[sun_count * sun_cascades +
                                                   i]
                    fbo.clear(depth=1)
                    glEnable(GL_DEPTH_CLAMP)
                    pipeline.draw_scene_pass(
                        fbo, scene.objects, pass_name,
                        pipeline.default_shader[pass_name], UBOS)
                    glDisable(GL_DEPTH_CLAMP)

                sun_count += 1

            if light.type == LIGHT_POINT:
                self.data.lights[i].type_index = point_count

                cube_map_axes = [((1, 0, 0), (0, -1, 0)),
                                 ((-1, 0, 0), (0, -1, 0)),
                                 ((0, 1, 0), (0, 0, 1)),
                                 ((0, -1, 0), (0, 0, -1)),
                                 ((0, 0, 1), (0, -1, 0)),
                                 ((0, 0, -1), (0, -1, 0))]
                matrices = []
                for axes in cube_map_axes:
                    position = pyrr.Vector3(light.position)
                    front = pyrr.Vector3(axes[0])
                    up = pyrr.Vector3(axes[1])
                    matrices.append(
                        pyrr.Matrix44.look_at(position, position + front, up))

                projection_matrix = make_projection_matrix(
                    math.pi / 2.0, 1.0, 0.01, light.radius)

                for i in range(6):
                    scene.camera.camera_matrix = tuple(
                        [e for vector in matrices[i] for e in vector])
                    scene.camera.projection_matrix = tuple(
                        [e for vector in projection_matrix for e in vector])

                    offset = pipeline.get_samples()[pipeline.sample_count]
                    offset = (0, 0)
                    point_resolution = self.shadowmaps.point_resolution
                    self.common_buffer.load(
                        scene, (point_resolution, point_resolution), offset,
                        pipeline.sample_count)

                    fbo = self.shadowmaps.point_fbos[point_count * 6 + i]
                    fbo.clear(depth=1)
                    pipeline.draw_scene_pass(
                        fbo, scene.objects, pass_name,
                        pipeline.default_shader[pass_name], UBOS)

                point_count += 1

        self.data.lights_count = len(scene.lights)

        self.UBO.load_data(self.data)

    def bind(self, location):
        self.UBO.bind(location)

    def shader_callback(self, shader):
        shader.textures['SPOT_SHADOWMAPS'] = self.shadowmaps.spot_depth_t
        shader.textures['SUN_SHADOWMAPS'] = self.shadowmaps.sun_depth_t
        shader.textures['POINT_SHADOWMAPS'] = self.shadowmaps.point_depth_t
        shader.textures['SPOT_ID_MAPS'] = self.shadowmaps.spot_id_t
        shader.textures['SUN_ID_MAPS'] = self.shadowmaps.sun_id_t
        shader.textures['POINT_ID_MAPS'] = self.shadowmaps.point_id_t
Ejemplo n.º 6
0
 def __init__(self):
     self.data = C_LightsBuffer()
     self.UBO = UBO()
Ejemplo n.º 7
0
 def __init__(self):
     self.data = C_CommonBuffer()
     self.UBO = UBO()