From c6537a8f8ca54c079848195c95d833292172e056 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Fri, 25 Nov 2016 14:56:37 +0100 Subject: [PATCH] Shader class, some cleaning --- convert.py | 4 +-- d3/camera.py | 8 ++--- d3/controls.py | 10 +++--- d3/{conv => model}/__init__.py | 0 d3/{conv/model.py => model/basemodel.py} | 0 d3/{conv => model}/obj.py | 2 +- d3/{conv => model}/ply.py | 2 +- d3/{conv/loadmodel.py => model/tools.py} | 1 + d3/shader.py | 41 ++++++++++++++++++++++++ viewer.py | 26 ++++----------- 10 files changed, 62 insertions(+), 32 deletions(-) rename d3/{conv => model}/__init__.py (100%) rename d3/{conv/model.py => model/basemodel.py} (100%) rename d3/{conv => model}/obj.py (96%) rename d3/{conv => model}/ply.py (97%) rename d3/{conv/loadmodel.py => model/tools.py} (94%) create mode 100644 d3/shader.py diff --git a/convert.py b/convert.py index 4df00af..54c6560 100755 --- a/convert.py +++ b/convert.py @@ -3,8 +3,8 @@ import argparse import os -from d3.conv.ply import PLYExporter -from d3.conv.loadmodel import convert +from d3.model.ply import PLYExporter +from d3.model.tools import convert from functools import partial def check_path(path, should_exist): diff --git a/d3/camera.py b/d3/camera.py index 8fbfa51..99ecdde 100644 --- a/d3/camera.py +++ b/d3/camera.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -from .conv.model import Vertex +from .geometry import Vector from OpenGL.GL import * from OpenGL.GLU import * @@ -8,9 +8,9 @@ from OpenGL.GLU import * class Camera: def __init__(self, position = None, target = None, up = None): - self.position = Vertex() if position is None else position - self.target = Vertex() if target is None else target - self.up = Vertex(0.0,1.0,0.0) if up is None else target + self.position = Vector() if position is None else position + self.target = Vector() if target is None else target + self.up = Vector(0.0,1.0,0.0) if up is None else target def look(self): gluLookAt( diff --git a/d3/controls.py b/d3/controls.py index 670a89f..6f2a6b4 100644 --- a/d3/controls.py +++ b/d3/controls.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -from .conv.model import Vertex +from .geometry import Vector import pygame @@ -22,7 +22,7 @@ class Controls: class TrackBallControls(Controls): def __init__(self): super().__init__() - self.vertex = Vertex() + self.vertex = Vector() self.theta = 0 def apply(self): @@ -36,7 +36,7 @@ class TrackBallControls(Controls): coeff = 0.001 move = pygame.mouse.get_rel() - dV = Vertex(move[1] * time * coeff, move[0] * time * coeff, 0) + dV = Vector(move[1] * time * coeff, move[0] * time * coeff, 0) dTheta = dV.norm2() if abs(dTheta) < 0.00001: @@ -49,9 +49,9 @@ class TrackBallControls(Controls): cosDT2 = math.cos(dTheta / 2) sinDT2 = math.sin(dTheta / 2) - A = cosT2 * sinDT2 * dV + cosDT2 * sinT2 * self.vertex + sinDT2 * sinT2 * Vertex.cross_product(dV, self.vertex) + A = cosT2 * sinDT2 * dV + cosDT2 * sinT2 * self.vertex + sinDT2 * sinT2 * Vector.cross_product(dV, self.vertex) - self.theta = 2 * math.acos(cosT2 * cosDT2 - sinT2 * sinDT2 * Vertex.dot(dV, self.vertex)) + self.theta = 2 * math.acos(cosT2 * cosDT2 - sinT2 * sinDT2 * Vector.dot(dV, self.vertex)) self.vertex = A self.vertex.normalize() diff --git a/d3/conv/__init__.py b/d3/model/__init__.py similarity index 100% rename from d3/conv/__init__.py rename to d3/model/__init__.py diff --git a/d3/conv/model.py b/d3/model/basemodel.py similarity index 100% rename from d3/conv/model.py rename to d3/model/basemodel.py diff --git a/d3/conv/obj.py b/d3/model/obj.py similarity index 96% rename from d3/conv/obj.py rename to d3/model/obj.py index 9edd74d..ee515a0 100644 --- a/d3/conv/obj.py +++ b/d3/model/obj.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -from .model import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face +from .basemodel import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face from functools import reduce def is_obj(filename): diff --git a/d3/conv/ply.py b/d3/model/ply.py similarity index 97% rename from d3/conv/ply.py rename to d3/model/ply.py index a0c973d..1f08a12 100644 --- a/d3/conv/ply.py +++ b/d3/model/ply.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -from .model import ModelParser, Exporter, Vertex, Face, FaceVertex +from .basemodel import ModelParser, Exporter, Vertex, Face, FaceVertex def is_ply(filename): return filename[-4:] == '.ply' diff --git a/d3/conv/loadmodel.py b/d3/model/tools.py similarity index 94% rename from d3/conv/loadmodel.py rename to d3/model/tools.py index 74f7f92..9795faa 100644 --- a/d3/conv/loadmodel.py +++ b/d3/model/tools.py @@ -2,6 +2,7 @@ from .obj import is_obj, OBJParser, OBJExporter from .ply import is_ply, PLYParser, PLYExporter +from .basemodel import ModelParser, Exporter def load_model(path): parser = None diff --git a/d3/shader.py b/d3/shader.py new file mode 100644 index 0000000..4d4c835 --- /dev/null +++ b/d3/shader.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +import os +dir_path = os.path.dirname(os.path.realpath(__file__)) + +import OpenGL.GL as gl +import OpenGL.GL.shaders as sh + +class DefaultShader: + + def __init__(self, vertex_path = None, fragment_path = None): + + if vertex_path is None: + vertex_path = dir_path + '/../assets/shaders/shader.vert' + if fragment_path is None: + fragment_path = dir_path + '/../assets/shaders/shader.frag' + + + 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): + 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): + self.program = sh.compileProgram(self.vertex_shader, self.fragment_shader) + + def bind(self): + gl.glUseProgram(self.program) + + def unbind(self): + gl.glUseProgram(0) + pass diff --git a/viewer.py b/viewer.py index aa66099..37a696e 100755 --- a/viewer.py +++ b/viewer.py @@ -12,22 +12,18 @@ from OpenGL.GL import * from OpenGL.GL.shaders import * from OpenGL.GLU import * -from d3.conv.loadmodel import load_model -from d3.conv.model import Vertex +from d3.model.tools import load_model +from d3.geometry import Vector from d3.controls import TrackBallControls from d3.camera import Camera +from d3.shader import DefaultShader WINDOW_WIDTH = 1024 WINDOW_HEIGHT = 768 -x = -0.5 -y = 0.5 -width = 1 -height = 1 - def main(args): - camera = Camera(Vertex(0,0,5), Vertex(0,0,0)) + camera = Camera(Vector(0,0,5), Vector(0,0,0)) controls = TrackBallControls() pygame.init() @@ -50,15 +46,7 @@ def main(args): model = load_model(args.input) model.generate_vbos() - - VERTEX_SHADER = None - FRAGMENT_SHADER = None - with open('assets/shaders/shader.vert') as f: - VERTEX_SHADER = compileShader(f.read(), GL_VERTEX_SHADER) - with open('assets/shaders/shader.frag') as f: - FRAGMENT_SHADER = compileShader(f.read(), GL_FRAGMENT_SHADER) - - SHADER = compileProgram(VERTEX_SHADER, FRAGMENT_SHADER) + shader = DefaultShader() while running: for event in pygame.event.get(): @@ -85,9 +73,9 @@ def main(args): glPushMatrix() controls.apply() - glUseProgram(SHADER) + shader.bind() model.gl_draw() - glUseProgram(0) + shader.unbind() glPopMatrix() glFlush()