def __init__(self, fragment_src, vertex_src, uniform): self.program = shaders.compileProgram( shaders.compileShader(vertex_src, GL_VERTEX_SHADER), shaders.compileShader(fragment_src, GL_FRAGMENT_SHADER)) self.uniform = { } for name in uniform: self.uniform[name] = shaders.glGetUniformLocation(self.program, name)
void main() { gl_Position = projection * vec4( particles[0], particles[1], 0., 1. ); color = fire(1.0-particles[3]); }""").substitute({ 'size_x': lattice_x, 'size_y': lattice_y }), GL_VERTEX_SHADER) particle_program = shaders.compileProgram(particle_shader, fragment_shader) projection_id = shaders.glGetUniformLocation(particle_program, 'projection') lattice = Lattice( descriptor = D2Q9, geometry = Geometry(lattice_x, lattice_y), moments = lbm.moments(optimize = False), collide = lbm.bgk(f_eq = lbm.equilibrium(), tau = relaxation_time), boundary_src = boundary, opengl = True ) lattice.apply_material_map( get_channel_material_map(lattice.geometry)) lattice.sync_material() particles = Particles(
vec2 unit(vec2 v) { return vec2(v[0] / $size_x, v[1] / $size_y); } void main(){ const vec2 sample_pos = unit(frag_pos); result = texture(moments, sample_pos); } """).substitute({ "size_x": lattice_x, "size_y": lattice_y, "inflow": inflow }), GL_FRAGMENT_SHADER) shader_program = shaders.compileProgram(vertex_shader, fragment_shader) projection_id = shaders.glGetUniformLocation(shader_program, 'projection') lattice = Lattice( descriptor = D2Q9, geometry = Geometry(lattice_x, lattice_y), moments = lbm.moments(optimize = False), collide = lbm.bgk(f_eq = lbm.equilibrium(), tau = relaxation_time), boundary_src = boundary, opengl = True ) lattice.apply_material_map( get_channel_material_map(lattice.geometry)) lattice.sync_material() streamline_texture = Streamlines(
void main(){ const vec3 ray = normalize(frag_pos - camera_pos.xyz); result = vec4(trace(frag_pos, ray), 1.0); } """, lookup=TemplateLookup(directories=[Path(__file__).parent])).render( size_x=lattice_x, size_y=lattice_y, size_z=lattice_z, inflow=inflow, max_ray_length=max(lattice_x, lattice_y, lattice_z)**3, sdf_source=channel), GL_FRAGMENT_SHADER) domain_program = shaders.compileProgram(vertex_shader, fragment_shader) domain_projection_id = shaders.glGetUniformLocation(domain_program, 'projection') domain_rotation_id = shaders.glGetUniformLocation(domain_program, 'rotation') raycast_program = shaders.compileProgram(raycast_vertex_shader, raycast_fragment_shader) raycast_projection_id = shaders.glGetUniformLocation(raycast_program, 'projection') raycast_rotation_id = shaders.glGetUniformLocation(raycast_program, 'rotation') raycast_camera_pos_id = shaders.glGetUniformLocation(raycast_program, 'camera_pos') lattice = Lattice(descriptor=D3Q27, geometry=Geometry(lattice_x, lattice_y, lattice_z), moments=lbm.moments(optimize=True), collide=lbm.bgk(f_eq=lbm.equilibrium(), tau=relaxation_time,
def load(folder, vertexAppend=[], fragmentAppend=[]): vertexShader = "" fragmentShader = "" try: # vertex files vertexFiles = list(vertexAppend) + [folder + "vertex.txt"] for file in vertexFiles: with open(file) as v: if vertexShader == "": vertexShader += "\n" vertexShader += v.read() # attribute file fragmentFiles = list(fragmentAppend) + [folder + "fragment.txt"] for file in fragmentFiles: with open(file) as f: if fragmentShader == "": fragmentShader += "\n" fragmentShader += f.read() # get uniforms and attributes uniforms = [] attributes = [] for shader in (vertexShader, fragmentShader): i = -1 while True: i = shader.find("uniform", i + 1) if i == -1: break start = shader.find(" ", i + 8) end = shader.find(";", start + 1) uniforms.append(shader[start:end].strip()) i = -1 while True: i = shader.find("attribute", i + 1) if i == -1: break start = shader.find(" ", i + 10) end = shader.find(";", start + 1) attributes.append(shader[start:end].strip()) vertexCompiled = shaders.compileShader(vertexShader, shaders.GL_VERTEX_SHADER) fragmentCompiled = shaders.compileShader(fragmentShader, shaders.GL_FRAGMENT_SHADER) shader = shaders.compileProgram(vertexCompiled, fragmentCompiled) # record unused uniforms and attributes errors = [] for uniform in uniforms: location = shaders.glGetUniformLocation(shader, uniform) if location in (None, -1): errors.append('No uniform: %s' % (uniform)) setattr(shader, uniform, location) for attribute in attributes: location = shaders.glGetAttribLocation(shader, attribute) if location in (None, -1): errors.append('No attribute: %s' % (attribute)) setattr(shader, attribute, location) return (shader, tuple(errors)) except: if vertexShader == "": raise ValueError("Vertex shader files not found") elif fragmentShader == "": raise ValueError( "Vertex shader or fragment Shader files not found") else: raise ValueError("Shader compile failure")
def __init__(self, vertex_shader, fragment_shader, attribute0_name): import sys self.program = shaders.glCreateProgram() if self.program == 0: check_GLerror("Unable to create program") return self.uniforms = {} self.attributes = {} self.vs = shaders.glCreateShader(GL.GL_VERTEX_SHADER) shaders.glShaderSource(self.vs, vertex_shader) shaders.glCompileShader(self.vs) shaders.glAttachShader(self.program, self.vs) self.fs = shaders.glCreateShader(GL.GL_FRAGMENT_SHADER) shaders.glShaderSource(self.fs, fragment_shader) shaders.glCompileShader(self.fs) shaders.glAttachShader(self.program, self.fs) compiled = True status = shaders.glGetShaderiv(self.vs, GL.GL_COMPILE_STATUS) if not status: compiled = False log = shaders.glGetShaderInfoLog(self.vs) print("compiling vertex shader failed:\n%s" % log, file=sys.stderr) status = shaders.glGetShaderiv(self.fs, GL.GL_COMPILE_STATUS) if not status: compiled = False log = shaders.glGetShaderInfoLog(self.fs) print("compiling fragment shader failed:\n%s" % log, file=sys.stderr) if not compiled: raise RuntimeError("failed to compile shader program") if attribute0_name: shaders.glBindAttribLocation(self.program, 0, attribute0_name.encode('utf-8')) shaders.glLinkProgram(self.program) status = shaders.glGetProgramiv(self.program, GL.GL_LINK_STATUS) if not status: log = shaders.glGetProgramInfoLog(self.program) print("unable to link program:\n%s" % log, file=sys.stderr) # introspect program uniforms num_uniforms = shaders.glGetProgramiv(self.program, GL.GL_ACTIVE_UNIFORMS) for i in range(num_uniforms): name, size, type = shaders.glGetActiveUniform(self.program, i) if size != 1: print("uniform arrays are not supported", file=sys.stderr) loc = shaders.glGetUniformLocation(self.program, name) if loc == -1: continue name = name.decode('utf-8') self.uniforms[name] = ShaderVariable(self, name, cvt_type(type), loc) # introspect vertex attributes num_attributes = shaders.glGetProgramiv(self.program, GL.GL_ACTIVE_ATTRIBUTES) for i in range(num_attributes): name, size, type = shaders.glGetActiveAttrib(self.program, i) if size != 1: print("attribue arrays are not supported", file=sys.stderr) loc = shaders.glGetAttribLocation(self.program, name) if loc == -1: continue name = name.decode('utf-8') self.attributes[name] = ShaderVariable(self, name, cvt_type(type), loc) return print('uniforms:', self.uniforms) print('attributes:', self.attributes)
def get_uniform(self, name): return glGetUniformLocation(self.program, name)
const vec3 light_color = vec3(1.0,1.0,1.0); const vec3 ray = camera_pos.xyz - frag_pos; float brightness = dot(frag_normal, ray) / length(ray); brightness = clamp(brightness, 0, 1); result = vec4(max(0.4,brightness) * light_color * color.rgb, 1.0); } """).substitute({ "size_x": lattice_x, "size_y": lattice_y, "size_z": lattice_z }), GL_FRAGMENT_SHADER) particle_program = shaders.compileProgram(particle_shader, fragment_shader) particle_projection_id = shaders.glGetUniformLocation(particle_program, 'projection') particle_rotation_id = shaders.glGetUniformLocation(particle_program, 'rotation') domain_program = shaders.compileProgram(vertex_shader, fragment_shader) domain_projection_id = shaders.glGetUniformLocation(domain_program, 'projection') domain_rotation_id = shaders.glGetUniformLocation(domain_program, 'rotation') obstacle_program = shaders.compileProgram(lighting_vertex_shader, lighting_fragment_shader) obstacle_projection_id = shaders.glGetUniformLocation(obstacle_program, 'projection') obstacle_rotation_id = shaders.glGetUniformLocation(obstacle_program, 'rotation') obstacle_camera_pos_id = shaders.glGetUniformLocation(obstacle_program,
def send_matrix(self, name, matrix, func=glUniformMatrix4fv): position = shaders.glGetUniformLocation(self.id, name) if position < 0: raise ValueError(f'{position} {name} in {self.id}') return func(position, 1, GL_FALSE, matrix)
def uniform(self, name): return shaders.glGetUniformLocation(self.id, name)
gl_Position = projection * rotation * vertex; color = vec3(1.0,1.0,1.0); }""").substitute({}), GL_VERTEX_SHADER) fragment_shader = shaders.compileShader( """ #version 430 in vec3 color; void main(){ gl_FragColor = vec4(color.xyz, 0.0); }""", GL_FRAGMENT_SHADER) particle_program = shaders.compileProgram(particle_shader, fragment_shader) particle_projection_id = shaders.glGetUniformLocation(particle_program, 'projection') particle_rotation_id = shaders.glGetUniformLocation(particle_program, 'rotation') geometry_program = shaders.compileProgram(vertex_shader, fragment_shader) geometry_projection_id = shaders.glGetUniformLocation(geometry_program, 'projection') geometry_rotation_id = shaders.glGetUniformLocation(geometry_program, 'rotation') lattice = Lattice(descriptor=D3Q19, geometry=Geometry(lattice_x, lattice_y, lattice_z), moments=lbm.moments(optimize=True), collide=lbm.bgk(f_eq=lbm.equilibrium(), tau=relaxation_time), boundary_src=boundary, opengl=True)