model-converter-python/d3/shader.py

54 lines
1.5 KiB
Python
Raw Permalink Normal View History

2016-11-25 14:56:37 +01:00
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
import OpenGL.GL as gl
import OpenGL.GL.shaders as sh
2017-01-18 13:57:59 +01:00
default_vertex_path = dir_path + '/../assets/shaders/shader.vert'
default_fragment_path = dir_path + '/../assets/shaders/shader.frag'
2016-11-25 14:56:37 +01:00
2017-01-18 13:57:59 +01:00
class Shader:
"""Shader
2016-11-25 14:56:37 +01:00
2017-01-18 13:57:59 +01:00
Loads, compile and binds the shader that are in the assets/shaders
directory
"""
2016-11-25 14:56:37 +01:00
2017-01-18 13:57:59 +01:00
def __init__(self, vertex_path = default_vertex_path, fragment_path = default_fragment_path):
"""Creates a shader object, and compile it
2016-11-25 14:56:37 +01:00
2017-01-18 13:57:59 +01:00
:param vertex_path: path to the vertex shader
:param fragment_path: path to the fragment shader
"""
2016-11-25 14:56:37 +01:00
with open(vertex_path) as f:
self.vertex_src = f.read()
with open(fragment_path) as f:
self.fragment_src = f.read()
self.compile_shaders()
self.compile_program()
def compile_shaders(self):
2017-01-18 13:57:59 +01:00
""" Compiles the shader
"""
2016-11-25 14:56:37 +01:00
self.vertex_shader = sh.compileShader(self.vertex_src, gl.GL_VERTEX_SHADER)
self.fragment_shader = sh.compileShader(self.fragment_src, gl.GL_FRAGMENT_SHADER)
def compile_program(self):
2017-01-18 13:57:59 +01:00
"""Compile the shader program
The shaders must be compiled
"""
2016-11-25 14:56:37 +01:00
self.program = sh.compileProgram(self.vertex_shader, self.fragment_shader)
def bind(self):
2017-01-18 13:57:59 +01:00
"""Bind the current shader to the OpenGL context
"""
2016-11-25 14:56:37 +01:00
gl.glUseProgram(self.program)
def unbind(self):
2017-01-18 13:57:59 +01:00
"""Reset OpenGL shader to 0
"""
2016-11-25 14:56:37 +01:00
gl.glUseProgram(0)