def _init_texture2d_draw(self): """Initialize geometry and shader for drawing FBO layers""" if not TextureHelper._quad: TextureHelper._quad = geometry.quad_fs() # Shader for drawing color layers TextureHelper._texture2d_shader = context.ctx().program( vertex_shader=""" #version 330 in vec3 in_position; in vec2 in_uv; out vec2 uv; uniform vec2 offset; uniform vec2 scale; void main() { uv = in_uv; gl_Position = vec4((in_position.xy + vec2(1.0, 1.0)) * scale + offset, 0.0, 1.0); } """, fragment_shader=""" #version 330 out vec4 out_color; in vec2 uv; uniform sampler2D texture0; void main() { out_color = texture(texture0, uv); } """) TextureHelper._texture2d_sampler = self.ctx.sampler( filter=(moderngl.LINEAR, moderngl.LINEAR), )
def __init__(self): self.vao = geometry.quad_fs() self.program = self.get_program("shader") self.texture1 = self.get_texture("WarpspeedTexture") self.texture2 = self.get_texture("WarpspeedTexture2") self.zangle = 0.0
def test_basic_render(self): """Ensure we actually draw something to the screen""" vao = geometry.quad_fs() program = self.load_program(path='vf_pos_color.glsl') program["color"].value = (1.0, 1.0, 1.0, 1.0) vao.render(program) data = self.window.fbo.read() self.assertEqual(data[:10], b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff')
def __init__(self, width, height, gbuffer=None, lightbuffer=None): self.ctx = context.ctx() self.width = width self.height = height self.size = (width, height) self.depth_sampler = samplers.create(texture_compare_mode=False, min_filter=moderngl.LINEAR, mag_filter=moderngl.LINEAR) # FBOs self.gbuffer = gbuffer self.lightbuffer = lightbuffer # Light Info self.point_lights = [] # Create geometry buffer if not supplied depth_buffer = DepthTexture(self.size) if not self.gbuffer: self.gbuffer = FBO.create_from_textures( [ Texture2D.create(self.size, 4, dtype='f1'), Texture2D.create(self.size, 3, dtype='f2'), ], depth_buffer=depth_buffer, ) if not self.lightbuffer: self.lightbuffer = FBO.create_from_textures( [Texture2D.create(self.size, 4)], # depth_buffer=depth_buffer, ) # Unit cube for point lights (cube with radius 1.0) self.unit_cube = geometry.cube(width=2, height=2, depth=2) self.point_light_shader = resources.shaders.get( "deferred/light_point.glsl", create=True) # Debug draw lights self.debug_shader = resources.shaders.get("deferred/debug.glsl", create=True) # Combine shader self.combine_shader = resources.shaders.get("deferred/combine.glsl", create=True) self.quad = geometry.quad_fs()
def _init_depth_texture_draw(): """Initialize geometry and shader for drawing FBO layers""" from demosys.opengl import ShaderProgram from demosys import geometry if DepthTexture.quad: return DepthTexture.quad = geometry.quad_fs() # Shader for drawing depth layers src = [ "#version 330", "#if defined VERTEX_SHADER", "in vec3 in_position;", "in vec2 in_uv;", "out vec2 uv;", "uniform vec2 offset;", "uniform vec2 scale;", "", "void main() {", " uv = in_uv;" " gl_Position = vec4((in_position.xy + vec2(1.0, 1.0)) * scale + offset, 0.0, 1.0);", "}", "", "#elif defined FRAGMENT_SHADER", "out vec4 out_color;", "in vec2 uv;", "uniform sampler2D texture0;", "uniform float near;" "uniform float far;" "void main() {", " float z = texture(texture0, uv).r;" " float d = (2.0 * near) / (far + near - z * (far - near));" " out_color = vec4(d);", "}", "#endif", ] program = ShaderProgram(name="depth_shader") program.set_source("\n".join(src)) program.prepare() DepthTexture.sampler = samplers.create( min_filter=moderngl.LINEAR, mag_filter=moderngl.LINEAR, texture_compare_mode=False, ) DepthTexture.shader = program
def _init_depth_texture_draw(self): """Initialize geometry and shader for drawing FBO layers""" from demosys import geometry if not TextureHelper._quad: TextureHelper._quad = geometry.quad_fs() # Shader for drawing depth layers TextureHelper._depth_shader = context.ctx().program(vertex_shader=""" #version 330 in vec3 in_position; in vec2 in_uv; out vec2 uv; uniform vec2 offset; uniform vec2 scale; void main() { uv = in_uv; gl_Position = vec4((in_position.xy + vec2(1.0, 1.0)) * scale + offset, 0.0, 1.0); } """, fragment_shader=""" #version 330 out vec4 out_color; in vec2 uv; uniform sampler2D texture0; uniform float near; uniform float far; void main() { float z = texture(texture0, uv).r; float d = (2.0 * near) / (far + near - z * (far - near)); out_color = vec4(d); } """) TextureHelper._depth_sampler = self.ctx.sampler( filter=(moderngl.LINEAR, moderngl.LINEAR), compare_func='', )
def __init__(self): self.cube_shader1 = self.get_shader('geocubes/cube_multi_fade.glsl') self.cube_shader2 = self.get_shader('geocubes/cube_texture_light.glsl') self.quad_shader = self.get_shader('geocubes/quad_fs_uvscale.glsl') self.texture1 = self.get_texture('geocubes/texture.png') self.texture2 = self.get_texture('geocubes/GreenFabric.png') self.cube = geometry.cube(4.0, 4.0, 4.0) v = 100.0 r = (-v, v) self.points = geometry.points_random_3d(50_000, range_x=r, range_y=r, range_z=r, seed=7656456) self.quad = geometry.quad_fs() self.fbo = FBO.create((512, 512), depth=True)
def _init_texture2d_draw(): """Initialize geometry and shader for drawing FBO layers""" from demosys.opengl import ShaderProgram from demosys import geometry if Texture2D.quad: return Texture2D.quad = geometry.quad_fs() # Shader for drawing color layers src = [ "#version 330", "#if defined VERTEX_SHADER", "in vec3 in_position;", "in vec2 in_uv;", "out vec2 uv;", "uniform vec2 offset;", "uniform vec2 scale;", "", "void main() {", " uv = in_uv;" " gl_Position = vec4((in_position.xy + vec2(1.0, 1.0)) * scale + offset, 0.0, 1.0);", "}", "", "#elif defined FRAGMENT_SHADER", "out vec4 out_color;", "in vec2 uv;", "uniform sampler2D texture0;", "void main() {", " out_color = texture(texture0, uv);", "}", "#endif", ] program = ShaderProgram(name="fbo_shader") program.set_source("\n".join(src)) program.prepare() Texture2D.shader = program
def __init__(self): self.cube_prog1 = self.get_program('cube_multi_fade') self.cube_prog2 = self.get_program('cube_texture_light') self.quad_prog = self.get_program('quad_fs_uvscale') self.texture1 = self.get_texture('texture') self.texture2 = self.get_texture('GreenFabric') self.cube = geometry.cube(4.0, 4.0, 4.0) axis_range = 100.0 range_tuple = (-axis_range, axis_range) self.points = geometry.points_random_3d(50_000, range_x=range_tuple, range_y=range_tuple, range_z=range_tuple, seed=7656456) self.quad = geometry.quad_fs() self.fbo = self.ctx.framebuffer( self.ctx.texture((512, 512), 4), depth_attachment=self.ctx.depth_texture((512, 512)), )
def test_quad(self): vao = geometry.quad_fs() vao.draw(self.shader)
def test_quad(self): vao = geometry.quad_fs() vao.render(self.program)