-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube_v4.py
125 lines (87 loc) · 3.23 KB
/
cube_v4.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Show spinning cube using VBO's, and transforms, and texturing.
"""
import numpy as np
from vispy import app, gl, oogl
import vispy_io as io # Because vispy 0.1.0 lacks some data files
from transforms import perspective, translate, rotate
VERT_CODE = """
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_projection;
attribute vec3 a_position;
attribute vec2 a_texcoord;
varying vec2 v_texcoord;
void main()
{
v_texcoord = a_texcoord;
gl_Position = u_projection * u_view * u_model * vec4(a_position,1.0);
//gl_Position = vec4(a_position,1.0);
}
"""
FRAG_CODE = """
uniform sampler2D u_texture;
varying vec2 v_texcoord;
void main()
{
float ty = v_texcoord.y;
float tx = sin(ty*20.0)*0.05 + v_texcoord.x;
gl_FragColor = texture2D(u_texture, vec2(tx, ty));
}
"""
# Read cube data
positions, faces, normals, texcoords = io.read_mesh('cube.obj')
colors = np.random.uniform(0,1,positions.shape).astype('float32')
faces_buffer = oogl.ElementBuffer(faces.astype(np.uint16))
class Canvas(app.Canvas):
def __init__(self, **kwargs):
app.Canvas.__init__(self, **kwargs)
self.geometry = 0, 0, 400, 400
self.program = oogl.ShaderProgram( oogl.VertexShader(VERT_CODE),
oogl.FragmentShader(FRAG_CODE) )
# Set attributes
self.program.attributes['a_position'] = oogl.VertexBuffer(positions)
self.program.attributes['a_texcoord'] = oogl.VertexBuffer(texcoords)
self.program.uniforms['u_texture'] = oogl.Texture2D(io.cat())
# Handle transformations
self.init_transforms()
self.timer = app.Timer(1.0/60)
self.timer.connect(self.update_transforms)
self.timer.start()
def on_initialize(self, event):
gl.glClearColor(1,1,1,1)
gl.glEnable(gl.GL_DEPTH_TEST)
def on_resize(self, event):
width, height = event.size
gl.glViewport(0, 0, width, height)
self.projection = perspective( 45.0, width/float(height), 2.0, 10.0 )
self.program.uniforms['u_projection'] = self.projection
def on_paint(self, event):
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
with self.program as prog:
prog.draw_elements(gl.GL_TRIANGLES, faces_buffer)
# Swap buffers
self.swap_buffers()
def init_transforms(self):
self.view = np.eye(4,dtype=np.float32)
self.model = np.eye(4,dtype=np.float32)
self.projection = np.eye(4,dtype=np.float32)
self.theta = 0
self.phi = 0
translate(self.view, 0,0,-5)
self.program.uniforms['u_model'] = self.model
self.program.uniforms['u_view'] = self.view
def update_transforms(self,event):
self.theta += .5
self.phi += .5
self.model = np.eye(4, dtype=np.float32)
rotate(self.model, self.theta, 0,0,1)
rotate(self.model, self.phi, 0,1,0)
self.program.uniforms['u_model'] = self.model
self.update()
if __name__ == '__main__':
c = Canvas()
c.show()
app.run()