forked from glumpy/glumpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrain.py
92 lines (79 loc) · 2.59 KB
/
brain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -----------------------------------------------------------------------------
# Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
import numpy as np
from glumpy import app, gl, gloo, data, log
from glumpy.transforms import Trackball, Position
vertex = """
uniform mat4 m_model;
uniform mat4 m_view;
uniform mat4 m_normal;
attribute vec3 position;
attribute vec3 normal;
varying vec3 v_normal;
varying vec3 v_position;
void main()
{
gl_Position = <transform>;
vec4 P = m_view * m_model* vec4(position, 1.0);
v_position = P.xyz / P.w;
v_normal = vec3(m_normal * vec4(normal,0.0));
}
"""
fragment = """
varying vec3 v_normal;
varying vec3 v_position;
const vec3 light_position = vec3(1.0,1.0,1.0);
const vec3 ambient_color = vec3(0.1, 0.0, 0.0);
const vec3 diffuse_color = vec3(0.75, 0.125, 0.125);
const vec3 specular_color = vec3(1.0, 1.0, 1.0);
const float shininess = 128.0;
const float gamma = 2.2;
void main()
{
vec3 normal= normalize(v_normal);
vec3 light_direction = normalize(light_position - v_position);
float lambertian = max(dot(light_direction,normal), 0.0);
float specular = 0.0;
if (lambertian > 0.0)
{
vec3 view_direction = normalize(-v_position);
vec3 half_direction = normalize(light_direction + view_direction);
float specular_angle = max(dot(half_direction, normal), 0.0);
specular = pow(specular_angle, shininess);
}
vec3 color_linear = ambient_color +
lambertian * diffuse_color +
specular * specular_color;
vec3 color_gamma = pow(color_linear, vec3(1.0/gamma));
gl_FragColor = vec4(color_gamma, 1.0);
}
"""
log.info("Loading brain mesh")
vertices,indices = data.get("brain.obj")
brain = gloo.Program(vertex, fragment)
brain.bind(vertices)
trackball = Trackball(Position("position"))
brain['transform'] = trackball
trackball.theta, trackball.phi, trackball.zoom = 80, -135, 15
window = app.Window(width=1024, height=768)
def update():
model = brain['transform']['model'].reshape(4,4)
view = brain['transform']['view'].reshape(4,4)
brain['m_view'] = view
brain['m_model'] = model
brain['m_normal'] = np.array(np.matrix(np.dot(view, model)).I.T)
@window.event
def on_draw(dt):
window.clear()
brain.draw(gl.GL_TRIANGLES)
@window.event
def on_mouse_drag(x, y, dx, dy, button):
update()
@window.event
def on_init():
gl.glEnable(gl.GL_DEPTH_TEST)
update()
window.attach(brain['transform'])
app.run()