forked from glumpy/glumpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform-trackball.py
72 lines (60 loc) · 1.7 KB
/
transform-trackball.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
# -----------------------------------------------------------------------------
# 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
from glumpy.transforms import Trackball, Position
vertex = """
uniform vec4 u_color;
attribute vec3 position;
attribute vec4 color;
varying vec4 v_color;
void main()
{
v_color = u_color * color;
gl_Position = <transform>;
}
"""
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):
window.clear()
# Filled cube
gl.glDisable(gl.GL_BLEND)
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glEnable(gl.GL_POLYGON_OFFSET_FILL)
cube['u_color'] = 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['u_color'] = 0, 0, 0, 1
cube.draw(gl.GL_LINES, outline)
gl.glDepthMask(gl.GL_TRUE)
# Build cube data
V, I, O = colorcube()
vertices = V.view(gloo.VertexBuffer)
faces = I.view(gloo.IndexBuffer)
outline = O.view(gloo.IndexBuffer)
cube = gloo.Program(vertex, fragment)
cube.bind(vertices)
cube['transform'] = Trackball(Position("position"))
window.attach(cube['transform'])
# OpenGL initalization
gl.glEnable(gl.GL_DEPTH_TEST)
gl.glPolygonOffset(1, 1)
gl.glEnable(gl.GL_LINE_SMOOTH)
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
# Run
app.run()