-
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathtextured-cube.py
More file actions
110 lines (85 loc) · 2.98 KB
/
textured-cube.py
File metadata and controls
110 lines (85 loc) · 2.98 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# -----------------------------------------------------------------------------
# Python and OpenGL for Scientific Visualization
# www.labri.fr/perso/nrougier/python+opengl
# Copyright (c) 2017, Nicolas P. Rougier
# Distributed under the 2-Clause BSD License.
# -----------------------------------------------------------------------------
import numpy as np
from PIL import Image
from glumpy import app, gl, glm, gloo, data
def cube():
vtype = [('position', np.float32, 3),
('texcoord', np.float32, 2)]
itype = np.uint32
# Vertices positions
p = np.array([[1, 1, 1], [-1, 1, 1], [-1, -1, 1], [1, -1, 1],
[1, -1, -1], [1, 1, -1], [-1, 1, -1], [-1, -1, -1]],
dtype=float)
# Texture coords
t = np.array([[0, 0], [0, 1], [1, 1], [1, 0]])
faces_p = [0, 1, 2, 3, 0, 3, 4, 5, 0, 5, 6, 1,
1, 6, 7, 2, 7, 4, 3, 2, 4, 7, 6, 5]
faces_t = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3,
3, 2, 1, 0, 0, 1, 2, 3, 0, 1, 2, 3]
vertices = np.zeros(24, vtype)
vertices['position'] = p[faces_p]
vertices['texcoord'] = t[faces_t]
filled = np.resize(
np.array([0, 1, 2, 0, 2, 3], dtype=itype), 6 * (2 * 3))
filled += np.repeat(4 * np.arange(6, dtype=itype), 6)
vertices = vertices.view(gloo.VertexBuffer)
filled = filled.view(gloo.IndexBuffer)
return vertices, filled
vertex = """
uniform mat4 model; // Model matrix
uniform mat4 view; // View matrix
uniform mat4 projection; // Projection matrix
attribute vec3 position; // Vertex position
attribute vec2 texcoord; // Vertex texture coordinates
varying vec2 v_texcoord; // Interpolated fragment texture coordinates (out)
void main()
{
// Assign varying variables
v_texcoord = texcoord;
// Final position
gl_Position = projection * view * model * vec4(position,1.0);
}
"""
fragment = """
uniform sampler2D texture; // Texture
varying vec2 v_texcoord; // Interpolated fragment texture coordinates (in)
void main()
{
// Final color
gl_FragColor = texture2D(texture, v_texcoord);
}
"""
window = app.Window(width=512, height=512, color=(1,1,1,1))
@window.event
def on_draw(dt):
global phi, theta, duration
window.clear()
gl.glDisable(gl.GL_BLEND)
gl.glEnable(gl.GL_DEPTH_TEST)
cube.draw(gl.GL_TRIANGLES, I)
# Rotate cube
theta += 1.0 # degrees
phi += 1.0 # 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)
V,I = cube()
cube = gloo.Program(vertex, fragment)
cube.bind(V)
cube['texture'] = np.array(Image.open("./crate.png"))
cube['model'] = np.eye(4, dtype=np.float32)
cube['view'] = glm.translation(0, 0, -5)
phi, theta = 40, 30
app.run(framerate=60, framecount=360)