Cleaned Shader class

This commit is contained in:
Thomas FORGIONE 2017-01-18 13:57:59 +01:00
parent 9e7d4be17c
commit d99ade3f7b
No known key found for this signature in database
GPG Key ID: 2A210FFC062E00C3
2 changed files with 24 additions and 10 deletions

View File

@ -4,16 +4,22 @@ dir_path = os.path.dirname(os.path.realpath(__file__))
import OpenGL.GL as gl import OpenGL.GL as gl
import OpenGL.GL.shaders as sh import OpenGL.GL.shaders as sh
class DefaultShader: default_vertex_path = dir_path + '/../assets/shaders/shader.vert'
default_fragment_path = dir_path + '/../assets/shaders/shader.frag'
def __init__(self, vertex_path = None, fragment_path = None): class Shader:
"""Shader
if vertex_path is None: Loads, compile and binds the shader that are in the assets/shaders
vertex_path = dir_path + '/../assets/shaders/shader.vert' directory
if fragment_path is None: """
fragment_path = dir_path + '/../assets/shaders/shader.frag'
def __init__(self, vertex_path = default_vertex_path, fragment_path = default_fragment_path):
"""Creates a shader object, and compile it
:param vertex_path: path to the vertex shader
:param fragment_path: path to the fragment shader
"""
with open(vertex_path) as f: with open(vertex_path) as f:
self.vertex_src = f.read() self.vertex_src = f.read()
@ -23,17 +29,25 @@ class DefaultShader:
self.compile_shaders() self.compile_shaders()
self.compile_program() self.compile_program()
def compile_shaders(self): def compile_shaders(self):
""" Compiles the shader
"""
self.vertex_shader = sh.compileShader(self.vertex_src, gl.GL_VERTEX_SHADER) self.vertex_shader = sh.compileShader(self.vertex_src, gl.GL_VERTEX_SHADER)
self.fragment_shader = sh.compileShader(self.fragment_src, gl.GL_FRAGMENT_SHADER) self.fragment_shader = sh.compileShader(self.fragment_src, gl.GL_FRAGMENT_SHADER)
def compile_program(self): def compile_program(self):
"""Compile the shader program
The shaders must be compiled
"""
self.program = sh.compileProgram(self.vertex_shader, self.fragment_shader) self.program = sh.compileProgram(self.vertex_shader, self.fragment_shader)
def bind(self): def bind(self):
"""Bind the current shader to the OpenGL context
"""
gl.glUseProgram(self.program) gl.glUseProgram(self.program)
def unbind(self): def unbind(self):
"""Reset OpenGL shader to 0
"""
gl.glUseProgram(0) gl.glUseProgram(0)
pass

View File

@ -37,7 +37,7 @@ from d3.model.tools import load_model
from d3.geometry import Vector from d3.geometry import Vector
from d3.controls import TrackBallControls, OrbitControls from d3.controls import TrackBallControls, OrbitControls
from d3.camera import Camera from d3.camera import Camera
from d3.shader import DefaultShader from d3.shader import Shader
WINDOW_WIDTH = 1024 WINDOW_WIDTH = 1024
WINDOW_HEIGHT = 1024 WINDOW_HEIGHT = 1024
@ -112,7 +112,7 @@ def main(args):
log(' done!\nGenerating vbos...', file=sys.stderr, end='') log(' done!\nGenerating vbos...', file=sys.stderr, end='')
model.generate_vbos() model.generate_vbos()
shader = DefaultShader() shader = Shader()
log(' Done!\nReady!', file=sys.stderr) log(' Done!\nReady!', file=sys.stderr)