forked from peterderivaz/pyopengles
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path03_Hello_Moving_Triangle.py
140 lines (109 loc) · 3.72 KB
/
03_Hello_Moving_Triangle.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
#!/usr/bin/env python
from pyopengles import *
#Boilerplate code to allow for non-blocking reading the stdin
import termios, fcntl, sys, os, select
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = oldterm[:]
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
ctx = EGL()
vertex_shader = """
attribute vec4 vPosition;
uniform float rotation;
uniform vec3 move;
mat4 translate(float x, float y, float z)
{
return mat4(
vec4(1.0, 0.0, 0.0, 0.0),
vec4(0.0, 1.0, 0.0, 0.0),
vec4(0.0, 0.0, 1.0, 0.0),
vec4(x, y, z, 1.0)
);
}
mat4 rotate_z(float theta)
{
return mat4(
vec4( cos(theta), sin(theta), 0.0, 0.0),
vec4(-sin(theta), cos(theta), 0.0, 0.0),
vec4( 0.0, 0.0, 1.0, 0.0),
vec4( 0.0, 0.0, 0.0, 1.0)
);
}
void main()
{
gl_Position = translate(move.x, move.y, move.z)
* rotate_z(rotation)
* vPosition;
}
"""
fragment_shader = """
precision mediump float;
void main()
{
gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );
}
"""
binding = ((0, 'vPosition'),)
program = ctx.get_program(vertex_shader, fragment_shader, binding)
opengles.glClearColor(eglfloat(0.1), eglfloat(0.1), eglfloat(0.1),eglfloat(1.0))
triangle_vertices = eglfloats(( -0.866, -0.5, 1.0,
0.0, 1.0, 1.0,
0.866, -0.5, 1.0 ))
# Use the program object
opengles.glUseProgram ( program )
# Set the Viewport: (NB openegl, not opengles)
openegl.glViewport(0,0,ctx.width, ctx.height)
# Find the location of the 'uniform' rotation:
rot_loc = opengles.glGetUniformLocation(program, "rotation")
move_loc = opengles.glGetUniformLocation(program, "move")
rotation = 0.0
move = [0.0, 0.0, 0.0]
print "Controls: \nm - Rotate clockwise, n - rotate counter-clockwise, \n"
print "Movement: a - left, d - right, w - up, s - down\n\nq - Quit\n\nStarts in 3 seconds..."
time.sleep(3)
try:
running = True
while(running):
# Clear the color buffer
opengles.glClear ( GL_COLOR_BUFFER_BIT )
# Update then draw:
# is there a keypress to read?
r, w, e = select.select([fd], [], [], 0.02)
if r:
c = sys.stdin.read(1)
if c.lower() == "m":
# rotate
rotation -= 0.05
elif c.lower() == "n":
# rotate
rotation += 0.05
elif c.lower() == "a":
# move left
move[0] = move[0] - 0.1
elif c.lower() == "d":
# move right
move[0] = move[0] + 0.1
elif c.lower() == "s":
# move down
move[1] -= 0.1
elif c.lower() == "w":
# move up
move[1] += 0.1
elif c.lower() == "q":
# quit
running = False
# set rotation:
opengles.glUniform1f(rot_loc, eglfloat(rotation))
# set translation
opengles.glUniform3f(move_loc, eglfloat( move[0] ), eglfloat(move[1]), eglfloat(move[2]))
# Load the vertex data
opengles.glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, triangle_vertices )
opengles.glEnableVertexAttribArray ( 0 )
opengles.glDrawArrays ( GL_TRIANGLES, 0, 3 )
openegl.eglSwapBuffers(ctx.display, ctx.surface)
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)