Using shaders
This commit is contained in:
parent
160d266aea
commit
c744cdd099
|
@ -0,0 +1,50 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
class Controls:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def apply(self):
|
||||
pass
|
||||
|
||||
def update(self, time = 10):
|
||||
pass
|
||||
|
||||
class TrackBallControls(Controls):
|
||||
def __init__(self):
|
||||
super().__init__(self)
|
||||
self.vertex = Vertex()
|
||||
self.theta = 0
|
||||
|
||||
def apply(self):
|
||||
glRotatef(self.theta * 180 / math.pi, self.vertex.x, self.vertex.y, self.vertex.z)
|
||||
|
||||
def update(self, time = 10):
|
||||
|
||||
if not pygame.mouse.get_pressed()[0]:
|
||||
return
|
||||
|
||||
coeff = 0.001
|
||||
move = pygame.mouse.get_rel()
|
||||
|
||||
dV = Vertex(move[1] * time * coeff, move[0] * time * coeff, 0)
|
||||
dTheta = dV.norm2()
|
||||
|
||||
if abs(dTheta) < 0.00001:
|
||||
return
|
||||
|
||||
dV.normalize()
|
||||
|
||||
cosT2 = math.cos(self.theta / 2)
|
||||
sinT2 = math.sin(self.theta / 2)
|
||||
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)
|
||||
|
||||
self.theta = 2 * math.acos(cosT2 * cosDT2 - sinT2 * sinDT2 * Vertex.dot(dV, self.vertex))
|
||||
|
||||
self.vertex = A
|
||||
self.vertex.normalize()
|
||||
|
||||
|
|
@ -9,12 +9,14 @@ import math
|
|||
|
||||
from pygame.locals import *
|
||||
from OpenGL.GL import *
|
||||
from OpenGL.GL.shaders import *
|
||||
from OpenGL.GLU import *
|
||||
from OpenGL.GLUT import *
|
||||
|
||||
from conv3d.loadmodel import load_model
|
||||
from conv3d.model import Vertex
|
||||
|
||||
from controls import TrackBallControls
|
||||
|
||||
WINDOW_WIDTH = 1024
|
||||
WINDOW_HEIGHT = 768
|
||||
|
@ -24,42 +26,6 @@ y = 0.5
|
|||
width = 1
|
||||
height = 1
|
||||
|
||||
class TrackBallControls:
|
||||
def __init__(self):
|
||||
self.vertex = Vertex()
|
||||
self.theta = 0
|
||||
|
||||
def apply(self):
|
||||
glRotatef(self.theta * 180 / math.pi, self.vertex.x, self.vertex.y, self.vertex.z)
|
||||
|
||||
def update(self, time = 10):
|
||||
|
||||
if not pygame.mouse.get_pressed()[0]:
|
||||
return
|
||||
|
||||
coeff = 0.001
|
||||
move = pygame.mouse.get_rel()
|
||||
|
||||
dV = Vertex(move[1] * time * coeff, move[0] * time * coeff, 0)
|
||||
dTheta = dV.norm2()
|
||||
|
||||
if abs(dTheta) < 0.00001:
|
||||
return
|
||||
|
||||
dV.normalize()
|
||||
|
||||
cosT2 = math.cos(self.theta / 2)
|
||||
sinT2 = math.sin(self.theta / 2)
|
||||
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)
|
||||
|
||||
self.theta = 2 * math.acos(cosT2 * cosDT2 - sinT2 * sinDT2 * Vertex.dot(dV, self.vertex))
|
||||
|
||||
self.vertex = A
|
||||
self.vertex.normalize()
|
||||
|
||||
|
||||
|
||||
def init_frame():
|
||||
|
@ -88,15 +54,22 @@ def main(args):
|
|||
glEnable(GL_BLEND)
|
||||
glClearColor(0, 0, 0, 0)
|
||||
|
||||
glLightfv(GL_LIGHT0, GL_POSITION, [10,5,7])
|
||||
glEnable(GL_LIGHTING)
|
||||
glEnable(GL_LIGHT0)
|
||||
|
||||
running = True
|
||||
|
||||
model = load_model(args.input)
|
||||
model.generate_vbos()
|
||||
|
||||
|
||||
VERTEX_SHADER = None
|
||||
FRAGMENT_SHADER = None
|
||||
with open('shaders/shader.vert') as f:
|
||||
VERTEX_SHADER = compileShader(f.read(), GL_VERTEX_SHADER)
|
||||
with open('shaders/shader.frag') as f:
|
||||
FRAGMENT_SHADER = compileShader(f.read(), GL_FRAGMENT_SHADER)
|
||||
|
||||
print(VERTEX_SHADER, FRAGMENT_SHADER)
|
||||
SHADER = compileProgram(VERTEX_SHADER, FRAGMENT_SHADER)
|
||||
|
||||
while running:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
|
@ -118,7 +91,9 @@ def main(args):
|
|||
|
||||
glPushMatrix()
|
||||
controls.apply()
|
||||
glUseProgram(SHADER)
|
||||
model.gl_draw()
|
||||
glUseProgram(0)
|
||||
glPopMatrix()
|
||||
|
||||
glFlush()
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
varying vec3 fNormal;
|
||||
|
||||
vec3 ambientLight = vec3(0.2,0.2,0.2);
|
||||
vec3 directionnalLight = normalize(vec3(10,5,7));
|
||||
vec3 directionnalLightFactor = vec3(0.5,0.5,0.5);
|
||||
|
||||
|
||||
void main() {
|
||||
|
||||
vec3 ambientFactor = ambientLight;
|
||||
vec3 lambertFactor = max(vec3(0.0,0.0,0.0), dot(directionnalLight, fNormal) * directionnalLightFactor);
|
||||
|
||||
gl_FragColor = vec4(ambientFactor + lambertFactor, 1.0);
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
varying vec3 fNormal;
|
||||
|
||||
void main() {
|
||||
fNormal = gl_Normal;
|
||||
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
|
||||
}
|
Loading…
Reference in New Issue