def setup(self): self.va = glGenVertexArrays(1) glBindVertexArray(self.va) self.vbuf = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, self.vbuf) self.shader = Shader.default_shader()
in vec4 vertex_colors; in vec4 vertex_normals; in vec2 texture_coords; out vec4 final_colors; uniform sampler2D our_texture; void main() { // TODO: implement lighting, and do something with normals and materials. vec4 nothing = vertex_normals - vec4(1.0, 1.0, 1.0, 1.0); final_colors = texture(our_texture, texture_coords) + vertex_colors * nothing; } """ _default_vert_shader = Shader(vertex_source, 'vertex') _default_frag_shader = Shader(fragment_source, 'fragment') default_shader_program = ShaderProgram(_default_vert_shader, _default_frag_shader) class TexturedMaterialGroup(graphics.Group): def __init__(self, material, texture): super().__init__() self.material = material self.texture = texture self.program = default_shader_program self.rotation = 0, 0, 0 self.translation = 0, 0, 0 def set_state(self):
class TexturedMaterialGroup(BaseMaterialGroup): _default_vert_shader = Shader( """#version 330 core in vec3 vertices; in vec3 normals; in vec2 tex_coords; in vec4 colors; out vec4 vertex_colors; out vec3 vertex_normals; out vec2 texture_coords; out vec3 vertex_position; uniform WindowBlock { mat4 projection; mat4 view; } window; void main() { vec4 pos = window.view * vec4(vertices, 1.0); gl_Position = window.projection * pos; mat3 normal_matrix = transpose(inverse(mat3(window.view))); vertex_position = pos.xyz; vertex_colors = colors; texture_coords = tex_coords; vertex_normals = normal_matrix * normals; } """, 'vertex') _default_frag_shader = Shader( """#version 330 core in vec4 vertex_colors; in vec3 vertex_normals; in vec2 texture_coords; in vec3 vertex_position; out vec4 final_colors; uniform sampler2D our_texture; void main() { float l = dot(normalize(-vertex_position), normalize(vertex_normals)); final_colors = (texture(our_texture, texture_coords) * vertex_colors) * l * 1.2; } """, 'fragment') _default_program = ShaderProgram(_default_vert_shader, _default_frag_shader) def __init__(self, material, texture): super().__init__(material) self.texture = texture def set_state(self): glActiveTexture(GL_TEXTURE0) glBindTexture(self.texture.target, self.texture.id) self.program.use() self._set_modelview_matrix() def unset_state(self): glBindTexture(self.texture.target, 0) def __eq__(self, other): return False def __hash__(self): return hash((id(self.parent), self.texture.id, self.texture.target))
if TYPE_CHECKING: from konkyo.asset.image import ImageAsset vert = Shader( """#version 420 core in vec4 position; in vec4 color; in vec2 uv; out vec4 frag_color; out vec2 frag_uv; layout(binding = 0) uniform WindowBlock { mat4 projection; mat4 view; } window; void main() { gl_Position = window.projection * window.view * position; //gl_Position = projection * view * position; frag_color = color; frag_uv = uv; } """, 'vertex') frag = Shader( """#version 420 core in vec4 frag_color;
} """ _fragment_source = """#version 330 core in vec3 texture_coords; out vec4 final_colors; uniform sampler2D our_texture; void main() { final_colors = texture(our_texture, texture_coords.xy); } """ vert_shader = Shader(_vertex_source, 'vertex') frag_shader = Shader(_fragment_source, 'fragment') shader_program = ShaderProgram(vert_shader, frag_shader) ##################################################### # Define a custom `Group` to encapsulate OpenGL state ##################################################### class RenderGroup(Group): """A Group that enables and binds a Texture and ShaderProgram. RenderGroups are equal if their Texture and ShaderProgram are equal. """ def __init__(self, texture, program, order=0, parent=None): """Create a RenderGroup.