Shader class, some cleaning

This commit is contained in:
Thomas FORGIONE
2016-11-25 14:56:37 +01:00
parent da582944ea
commit c6537a8f8c
10 changed files with 62 additions and 32 deletions

View File

@@ -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(

View File

@@ -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()

View File

@@ -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):

View File

@@ -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'

View File

@@ -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

41
d3/shader.py Normal file
View File

@@ -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