/
texture-cube.py
152 lines (123 loc) · 4.79 KB
/
texture-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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# -----------------------------------------------------------------------------
# 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
def cube():
vtype = [('a_position', np.float32, 3), ('a_texcoord', np.float32, 2),
('a_normal', np.float32, 3), ('a_color', np.float32, 4)]
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)
# Face Normals
n = np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0],
[-1, 0, 1], [0, -1, 0], [0, 0, -1]])
# Vertice colors
c = np.array([[0, 1, 1, 1], [0, 0, 1, 1], [0, 0, 0, 1], [0, 1, 0, 1],
[1, 1, 0, 1], [1, 1, 1, 1], [1, 0, 1, 1], [1, 0, 0, 1]])
# 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_c = [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_n = [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 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['a_position'] = p[faces_p]
vertices['a_normal'] = n[faces_n]
vertices['a_color'] = c[faces_c]
vertices['a_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)
outline = np.resize(
np.array([0, 1, 1, 2, 2, 3, 3, 0], dtype=itype), 6 * (2 * 4))
outline += np.repeat(4 * np.arange(6, dtype=itype), 8)
vertices = vertices.view(gloo.VertexBuffer)
filled = filled.view(gloo.IndexBuffer)
outline = outline.view(gloo.IndexBuffer)
return vertices, filled, outline
def checkerboard(grid_num=8, grid_size=32):
""" Checkerboard pattern """
row_even = grid_num // 2 * [0, 1]
row_odd = grid_num // 2 * [1, 0]
Z = np.row_stack(grid_num // 2 * (row_even, row_odd)).astype(np.uint8)
return 255 * Z.repeat(grid_size, axis=0).repeat(grid_size, axis=1)
vertex = """
uniform mat4 u_model; // Model matrix
uniform mat4 u_view; // View matrix
uniform mat4 u_projection; // Projection matrix
attribute vec4 a_color; // Vertex color
attribute vec3 a_position; // Vertex position
attribute vec2 a_texcoord; // Vertex texture coordinates
varying vec4 v_color; // Interpolated fragment color (out)
varying vec2 v_texcoord; // Interpolated fragment texture coordinates (out)
void main()
{
// Assign varying variables
v_color = a_color;
v_texcoord = a_texcoord;
// Final position
gl_Position = u_projection * u_view * u_model * vec4(a_position,1.0);
}
"""
fragment = """
uniform vec4 u_color; // Global color
uniform sampler2D u_texture; // Texture
varying vec4 v_color; // Interpolated fragment color (in)
varying vec2 v_texcoord; // Interpolated fragment texture coordinates (in)
void main()
{
// Get texture color
vec4 t_color = vec4(vec3(texture2D(u_texture, v_texcoord).r), 1.0);
// Final color
gl_FragColor = u_color * t_color * mix(v_color, t_color, 0.25);
}
"""
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['u_color'] = 1, 1, 1, 1
cube.draw(gl.GL_TRIANGLES, I)
# 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, O)
gl.glDepthMask(gl.GL_TRUE)
# Rotate cube
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['u_model'] = model
@window.event
def on_resize(width, height):
cube['u_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)
V,I,O = cube()
cube = gloo.Program(vertex, fragment)
cube.bind(V)
cube['u_texture'] = checkerboard()
cube['u_model'] = np.eye(4, dtype=np.float32)
cube['u_view'] = glm.translation(0, 0, -5)
phi, theta = 40, 30
app.run()