Example #1
0
    def setup(self):
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_CULL_FACE)
        glEnable(GL_PROGRAM_POINT_SIZE)

        self.prog = program.create(*glsl.read("matcap"))
        self.matcap_tex = texture.create(self.matcap_img, 0, GL_RGB)
Example #2
0
def grid(projection, view):
    size = 10
    # create geometry
    # ---------------
    logging.debug("create grid geometry")
    positions = []
    offset = int(size)/2
    for x in range(0, int(size)+1):
        positions.append((x-offset, 0, size/2))
        positions.append((x-offset, 0, -size/2))

    for y in range(0, int(size)+1):
        positions.append((size/2, 0, y-offset))
        positions.append((-size/2, 0, y-offset))

    positions = np.array(positions, dtype=np.float32)

    # create buffers
    logging.debug("create grid buffers")
    vbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, vbo)
    glBufferData(GL_ARRAY_BUFFER, positions.nbytes, positions, GL_STATIC_DRAW)
    glBindBuffer(GL_ARRAY_BUFFER, vbo)

    vao = glGenVertexArrays(1)

    # create program
    logging.debug("create grid program")
    prog = program.create(
        """#version 330 core
        layout (location=0) in vec3 position;
        uniform mat4 projection;
        uniform mat4 view;

        void main(){
            gl_Position = projection * view * vec4(position, 1.0);
        }
        """,
        """#version 330 core
        out vec4 FragColor;
        void main(){
            FragColor = vec4(0.5,0.5,0.5,1);
        }
        """)

    # draw
    with program.use(prog):
        program.set_uniform(prog, "projection", projection)
        program.set_uniform(prog, "view", view)
        glBindVertexArray(vao)
        loc = glGetAttribLocation(prog, "position")
        glVertexAttribPointer(loc, 3, GL_FLOAT, False, 0, None)
        glEnableVertexAttribArray(loc)
        glDrawArrays(GL_LINES, 0, positions.size)
        glBindVertexArray(0)
Example #3
0
def create_program():
    logging.debug("create cubemap program")
    return program.create(
        """
		#version 330 core
		layout (location=0) in vec3 position;
		uniform mat4 projectionMatrix;
		uniform mat4 viewMatrix;
		uniform mat4 modelMatrix;
		out vec3 FragPos;
		void main(){
			FragPos = (modelMatrix * vec4(position, 1.0)).xyz;
			gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
		}

		""", """
		#version 330 core
		out vec4 FragColor;
		in vec3 FragPos;
		uniform samplerCube cubeMap;
		uniform bool useLOD;
		uniform float LOD;
		struct Channels{
			int red;
			int green;
			int blue;
			int alpha;
		};
		uniform Channels shuffle;

		void main(){
			vec3 normal = normalize(FragPos);
			vec3 color = useLOD ? textureLod(cubeMap, normal, LOD).rgb : texture(cubeMap, normal).rgb;
			FragColor = vec4(shuffle.red  >=0 ? color[shuffle.red]   : 0.0, 
		             shuffle.green>=0 ? color[shuffle.green] : 0.0, 
		             shuffle.blue >=0 ? color[shuffle.blue]  : 0.0, 
		             shuffle.alpha>=0 ? color[shuffle.alpha] : 1.0);
		}
		""")
Example #4
0
def texture(tex, rect, shuffle=(0, 1, 2, -1)):
    # create shader program
    debug_quad_program = program.create(
        *glsl.read('debug_quad.vs', 'shuffle.fs'))

    # setup program uniforms
    with program.use(debug_quad_program):
        program.set_uniform(debug_quad_program, 'projectionMatrix', np.eye(4))
        program.set_uniform(debug_quad_program, 'viewMatrix', np.eye(4))
        program.set_uniform(debug_quad_program, 'modelMatrix', np.eye(4))
        program.set_uniform(debug_quad_program, 'tex', 0)
        program.set_uniform(debug_quad_program, 'shuffle.red', shuffle[0])
        program.set_uniform(debug_quad_program, 'shuffle.green', shuffle[1])
        program.set_uniform(debug_quad_program, 'shuffle.blue', shuffle[2])
        program.set_uniform(debug_quad_program, 'shuffle.alpha', shuffle[3])

    # draw texture quad
    glViewport(*rect)
    with program.use(debug_quad_program):
        glActiveTexture(GL_TEXTURE0 + 0)
        glBindTexture(GL_TEXTURE_2D, tex)
        quad(debug_quad_program)
        glBindTexture(GL_TEXTURE_2D, 0)
Example #5
0
width, height = 1024, 768
model_matrix = np.identity(4)
window = GLFWViewer(width, height, (0.6, 0.7, 0.7, 1.0))

import logging

logging.basicConfig(
    filename=None,
    level=logging.ERROR,
    format='%(levelname)s:%(module)s.%(funcName)s: %(message)s')

with window:
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_CULL_FACE)

    ssao_geometry_program = program.create(*glsl.read("ssao_geometry"))

    # create gBuffer
    # --------------
    gBuffer = glGenFramebuffers(1)
    ssao_width = window.width
    ssao_height = window.height
    gPosition, gNormal, gAlbedo = glGenTextures(3)

    with fbo.bind(gBuffer):
        # create color attachments
        glDrawBuffers(
            3,
            [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2])
        for i, tex in enumerate([gPosition, gNormal, gAlbedo]):
            glBindTexture(GL_TEXTURE_2D, tex)
Example #6
0
    def setup(self):
        logging.debug("setup viewer")
        # Render passes
        # -------------

        # geometry
        self.geometry_pass.setup()

        # environment
        self.environment_tex = texture.create(self.environment_image,
                                              0,
                                              format=GL_RGB,
                                              wrap_s=GL_CLAMP_TO_EDGE,
                                              wrap_t=GL_CLAMP_TO_EDGE,
                                              min_filter=GL_LINEAR,
                                              mag_filter=GL_LINEAR)
        self.environment_pass.texture = self.environment_tex
        self.environment_pass.setup()
        self.environment_pass.render()

        # ibl
        self.prefilter_pass.setup()
        self.prefilter_pass.environment = self.environment_pass.cubemap
        self.prefilter_pass.render()

        self.irradiance_pass.setup()
        self.irradiance_pass.environment = self.environment_pass.cubemap
        self.irradiance_pass.render()

        self.brdf_pass.setup()
        self.brdf_pass.render()

        # shadows
        dirlight.shadowpass.setup()
        spotlight.shadowpass.setup()
        pointlight.shadowpass.setup()

        # lighting
        self.lighting_pass.setup()
        self.lighting_pass.irradiance = self.irradiance_pass.irradiance
        self.lighting_pass.prefilter = self.prefilter_pass.prefilter
        self.lighting_pass.brdf = self.brdf_pass.brdflut

        # setup forward rendering
        self.forward_fbo = glGenFramebuffers(1)
        with fbo.bind(self.forward_fbo):
            glDrawBuffers(1, [GL_COLOR_ATTACHMENT0 + 0])
            self.forward_texture = glGenTextures(1)
            glBindTexture(GL_TEXTURE_2D, self.forward_texture)
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, self.width, self.height,
                         0, GL_RGBA, GL_FLOAT, None)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                   GL_TEXTURE_2D, self.forward_texture, 0)
            glBindTexture(GL_TEXTURE_2D, 0)

            # create depth+stencil buffer
            rbo = glGenRenderbuffers(1)
            glBindRenderbuffer(GL_RENDERBUFFER, rbo)
            glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8,
                                  self.width, self.height)
            glBindRenderbuffer(GL_RENDERBUFFER, 0)

            glFramebufferRenderbuffer(GL_FRAMEBUFFER,
                                      GL_DEPTH_STENCIL_ATTACHMENT,
                                      GL_RENDERBUFFER, rbo)
            assert glCheckFramebufferStatus(
                GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE

        self.skybox_program = program.create(*glsl.read('skybox'))

        # tonemapping
        self.tonemapping_pass.setup()
Example #7
0
environment_data *= 1

with window:
    glEnable(GL_PROGRAM_POINT_SIZE)
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_CULL_FACE)

    # Create shading programs
    # =======================

    ## create material textures
    diffuse_tex = texture.create(diffuse_data, slot=0, format=GL_BGR)
    specular_tex = texture.create(specular_data, slot=1, format=GL_BGR)

    ## link programs
    lambert_program = program.create(*glsl.read('lambert'))
    phong_program = program.create(*glsl.read('phong'))
    pbr_program = program.create(*glsl.read('pbr'))

    # Setup Shadow mapping
    # ====================
    depth_program = program.create(*glsl.read('simple_depth'))
    shadow_fbo = glGenFramebuffers(1)
    shadow_fbo_width, shadow_fbo_height = 1024, 1024
    shadow_tex = texture.create((shadow_fbo_width, shadow_fbo_height),
                                slot=2,
                                format=GL_DEPTH_COMPONENT,
                                wrap_s=GL_CLAMP_TO_BORDER,
                                wrap_t=GL_CLAMP_TO_BORDER,
                                border_color=(1.0, 1.0, 1.0, 1.0))
Example #8
0
pointlight = Pointlight(position=glm.vec3(5, 2, 0.5),
                        color=glm.vec3(1, 0.7, 0.1) * 30,
                        near=1.0,
                        far=8.0)

with window:
    # SETUP GL
    # ========
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_CULL_FACE)

    # Geometry Pass
    # -------------
    gBuffer = glGenFramebuffers(1)
    geometry_program = program.create(*glsl.read("deferred_geometry"))
    gPosition, gNormal, gAlbedoSpecular = glGenTextures(3)

    glBindFramebuffer(GL_FRAMEBUFFER, gBuffer)
    glDrawBuffers(3, [
        GL_COLOR_ATTACHMENT0 + 0, GL_COLOR_ATTACHMENT0 + 1,
        GL_COLOR_ATTACHMENT0 + 2
    ])
    for i, tex in enumerate([gPosition, gNormal, gAlbedoSpecular]):
        glBindTexture(GL_TEXTURE_2D, tex)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, 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)
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i,
                               GL_TEXTURE_2D, tex, 0)
Example #9
0
window = GLFWViewer(width, height, (0.6, 0.7, 0.7, 1.0))

with window:
    glEnable(GL_DEPTH_TEST)
    glEnable(GL_CULL_FACE)

    prog = program.create(
        """
		#version 330 core
		uniform mat4 projection;
		uniform mat4 view;
		uniform mat4 model;

		layout (location = 0) in vec3 position;

		void main(){
			gl_Position = projection * view * model * vec4(position, 1.0);
		}
		""", """
		#version 330 core
		out vec4 FragColor;
		void main(){
			FragColor = vec4(1,1,1,1);
		}
		""")

    while not window.should_close():
        glViewport(0, 0, window.width, window.height)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        with program.use(prog):
            program.set_uniform(prog, 'projection', window.projection_matrix)
Example #10
0
    lambert_program = program.create(
        """
		#version 330 core
		uniform mat4 projection;
		uniform mat4 view;
		uniform mat4 model;
		uniform float farPlane;

		layout (location = 0) in vec3 position;
		layout (location = 2) in vec3 normal;

		out vec3 Normal;
		out vec3 FragPos;

		void main(){
			Normal = normal;
			FragPos = (model * vec4(position, 1.0)).xyz;
			gl_Position = projection * view * model * vec4(position, 1.0);
		}
		""", """
		#version 330 core

		in vec3 FragPos;
		in vec3 Normal;
		uniform vec3 lightPos;
		uniform float farPlane;
		out vec4 FragColor;

		uniform samplerCube depthShadowCubemap;

		float PointShadowCalculation(vec3 L, samplerCube shadowCubemap, float farPlane){
			float closestDepth = texture(shadowCubemap, normalize(L)).r;
			closestDepth*=farPlane;

			float currentDepth = length(L);

			float bias = 0.1; 
    		float shadow = currentDepth -  bias > closestDepth ? 1.0 : 0.0;

    		return shadow;
		}

		void main(){
			// calc simple lambert shading
			vec3 N = normalize(Normal);
			vec3 L = normalize(lightPos-FragPos);
			float distance = length(lightPos-FragPos);
			float luminance = 10*max(dot(L, N), 0.0) / (distance*distance);

			// apply shadow
			float shadow = PointShadowCalculation(FragPos-lightPos, depthShadowCubemap, farPlane);
			luminance*=1-shadow;

			// create surface color
			vec3 color = vec3(luminance);

			// reinhardt tonemapping
    		color = color / (color+vec3(1.0));

			// gamma correction
			const float gamma = 2.2;
    		color = pow(color, vec3(1.0 / gamma));  

			// output surface color
			FragColor = vec4(color, 1.0);
		}
		""")