-
Notifications
You must be signed in to change notification settings - Fork 0
/
test4_tweening.py
103 lines (95 loc) · 2.94 KB
/
test4_tweening.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
from OpenGLContext import testingcontext
BaseContext = testingcontext.getInteractive()
from OpenGL.GL import *
from OpenGL.arrays import vbo
from OpenGLContext.arrays import *
from OpenGL.GL import shaders
from OpenGLContext.events.timer import Timer
class TestContext(BaseContext):
"""Demonstrates the use of attribute types in GLSL"""
def OnInit(self):
"""Initialize the context"""
vertex_shader = shaders.compileShader(
"""
uniform float tween;
attribute vec3 position;
attribute vec3 tweened;
attribute vec3 color;
varying vec4 baseColor;
void main()
{
gl_Position = gl_ModelViewProjectionMatrix
* mix(vec4(position, 1.0), vec4(tweened, 1.0), tween);
baseColor = vec4(color, 1.0);
}
""",
GL_VERTEX_SHADER)
fragment_shader = shaders.compileShader(
"""
varying vec4 baseColor;
void main()
{
gl_FragColor = baseColor;
}
""",
GL_FRAGMENT_SHADER)
self.shader = shaders.compileProgram(vertex_shader, fragment_shader)
self.vbo = vbo.VBO(
array([
[0, 1, 0, 1, 3, 0, 0, 1, 0],
[-1, -1, 0, -1, -1, 0, 1, 1, 0],
[1, -1, 0, 1, -1, 0, 0, 1, 1],
[2, -1, 0, 2, -1, 0, 1, 0, 0],
[4, -1, 0, 4, -1, 0, 0, 1, 0],
[4, 1, 0, 4, 9, 0, 0, 0, 1],
[2, -1, 0, 2, -1, 0, 1, 0, 0],
[4, 1, 0, 1, 3, 0, 0, 0, 1],
[2, 1, 0, 1, -1, 0, 0, 1, 1]
], "f"))
self.position_location = glGetAttribLocation(self.shader, "position")
self.tweened_location = glGetAttribLocation(self.shader, "tweened")
self.color_location = glGetAttribLocation(self.shader, "color")
self.tween_location = glGetUniformLocation(self.shader, "tween")
self.time = Timer(duration=2.0, repeating=1)
self.time.addEventHandler("fraction", self.OnTimerFraction)
self.time.register(self)
self.time.start()
def Render(self, mode=0):
"""Render the geometry for the scene."""
super(TestContext, self).Render(mode)
glUseProgram(self.shader)
glUniform1f(self.tween_location, self.tween_fraction)
try:
self.vbo.bind()
try:
glEnableVertexAttribArray(self.position_location)
glEnableVertexAttribArray(self.tweened_location)
glEnableVertexAttribArray(self.color_location)
stride = 4 * 9
glVertexAttribPointer(
self.position_location,
3, GL_FLOAT, False, stride, self.vbo)
glVertexAttribPointer(
self.tweened_location,
3, GL_FLOAT, False, stride, self.vbo + 12)
glVertexAttribPointer(
self.color_location,
3, GL_FLOAT, False, stride, self.vbo + 24)
glDrawArrays(GL_TRIANGLES, 0, 9)
finally:
self.vbo.unbind()
glDisableVertexAttribArray(self.position_location)
glDisableVertexAttribArray(self.tweened_location)
glDisableVertexAttribArray(self.color_location)
finally:
shaders.glUseProgram(0)
tween_fraction = 0.0
def OnTimerFraction(self, event):
frac = event.fraction()
if frac > 0.5:
frac = 1.0 - frac
frac *= 2
self.tween_fraction = frac
self.triggerRedraw()
if __name__ == "__main__":
TestContext.ContextMainLoop()