forked from glumpy/glumpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgloo-cube.py
85 lines (69 loc) · 1.98 KB
/
gloo-cube.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
# -----------------------------------------------------------------------------
# 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, glm, gloo
from glumpy.geometry import colorcube
vertex = """
uniform vec4 ucolor;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
attribute vec3 position;
attribute vec4 color;
varying vec4 v_color;
void main()
{
v_color = ucolor * color;
gl_Position = projection * view * model * vec4(position,1.0);
}
"""
fragment = """
varying vec4 v_color;
void main()
{
gl_FragColor = v_color;
}
"""
window = app.Window(width=1024, height=1024, color=(0.30, 0.30, 0.35, 1.00))
@window.event
def on_draw(dt):
global phi, theta, duration
window.clear()
# Filled cube
gl.glDisable(gl.GL_BLEND)
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glEnable(gl.GL_POLYGON_OFFSET_FILL)
cube['ucolor'] = 1, 1, 1, 1
cube.draw(gl.GL_TRIANGLES, faces)
# Outlined cube
gl.glDisable(gl.GL_POLYGON_OFFSET_FILL)
gl.glEnable(gl.GL_BLEND)
gl.glDepthMask(gl.GL_FALSE)
cube['ucolor'] = 0, 0, 0, 1
cube.draw(gl.GL_LINES, outline)
gl.glDepthMask(gl.GL_TRUE)
# Make cube rotate
theta += 0.5 # degrees
phi += 0.5 # degrees
model = np.eye(4, dtype=np.float32)
glm.rotate(model, theta, 0, 0, 1)
glm.rotate(model, phi, 0, 1, 0)
cube['model'] = model
@window.event
def on_resize(width, height):
cube['projection'] = glm.perspective(45.0, width / float(height), 2.0, 100.0)
@window.event
def on_init():
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glPolygonOffset(1, 1)
gl.glEnable(gl.GL_LINE_SMOOTH)
gl.glLineWidth(0.75)
vertices, faces, outline = colorcube()
cube = gloo.Program(vertex, fragment)
cube.bind(vertices)
cube['model'] = np.eye(4, dtype=np.float32)
cube['view'] = glm.translation(0, 0, -5)
phi, theta = 0, 0
app.run()