model-converter-python/d3/shader.py

54 lines
1.5 KiB
Python
Raw Normal View History

2016-11-25 13:56:37 +00: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 12:57:59 +00:00
default_vertex_path = dir_path + '/../assets/shaders/shader.vert'
default_fragment_path = dir_path + '/../assets/shaders/shader.frag'
2016-11-25 13:56:37 +00:00
2017-01-18 12:57:59 +00:00
class Shader:
"""Shader
2016-11-25 13:56:37 +00:00
2017-01-18 12:57:59 +00:00
Loads, compile and binds the shader that are in the assets/shaders
directory
"""
2016-11-25 13:56:37 +00:00
2017-01-18 12:57:59 +00:00
def __init__(self, vertex_path = default_vertex_path, fragment_path = default_fragment_path):
"""Creates a shader object, and compile it
2016-11-25 13:56:37 +00:00
2017-01-18 12:57:59 +00:00
:param vertex_path: path to the vertex shader
:param fragment_path: path to the fragment shader
"""
2016-11-25 13:56:37 +00: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 12:57:59 +00:00
""" Compiles the shader
"""
2016-11-25 13:56:37 +00: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 12:57:59 +00:00
"""Compile the shader program
The shaders must be compiled
"""
2016-11-25 13:56:37 +00:00
self.program = sh.compileProgram(self.vertex_shader, self.fragment_shader)
def bind(self):
2017-01-18 12:57:59 +00:00
"""Bind the current shader to the OpenGL context
"""
2016-11-25 13:56:37 +00:00
gl.glUseProgram(self.program)
def unbind(self):
2017-01-18 12:57:59 +00:00
"""Reset OpenGL shader to 0
"""
2016-11-25 13:56:37 +00:00
gl.glUseProgram(0)