forked from peterderivaz/pyopengles
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathenv_glsl.py
224 lines (181 loc) · 6.39 KB
/
env_glsl.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from pyopengles import *
import os.path, pyinotify
file_changed = 0
# File watcher for live updates
class FileWatcher(object):
def __init__(self, file_list):
self.file_list = set([os.path.abspath(f) for f in file_list])
self.watch_dirs = set([os.path.dirname(f) for f in self.file_list])
self.file_changed = 0
def handler(self, event):
if event.pathname in self.file_list:
self.file_changed = 1
#print "Event: ", event.maskname, ", triggered by: ", event.pathname
def start(self):
handler = self.handler
class EventHandler(pyinotify.ProcessEvent):
def process_default(self, event):
handler(event)
wm = pyinotify.WatchManager() # Watch Manager
mask = pyinotify.IN_MODIFY
ev = EventHandler()
self.notifier = pyinotify.ThreadedNotifier(wm, ev)
for watch_this in self.watch_dirs:
wm.add_watch(watch_this, mask, rec=True)
self.notifier.start()
def stop(self):
self.notifier.stop()
_v_src = """attribute vec3 position;
void main() {
gl_Position = vec4( position, 1.0 );
}"""
example_frag = """precision mediump float;
uniform float time_ms;
uniform vec2 mouse;
uniform vec2 resolution;
void main( void ) {
float mouse_charge = 10.;
vec3 charges[10];
charges[0] = vec3(0,0,10);
charges[1] = vec3(0.1,0.1,-10);
const int N = 2;
vec2 position = gl_FragCoord.xy / resolution.xy - resolution.xy/2.0;
position.x = (gl_FragCoord.x - resolution.x/2.0)/resolution.x;
position.y = (gl_FragCoord.y - resolution.y/2.0)/resolution.x;
float r = distance(position.xy,vec2(mouse.x - 0.5, (mouse.y - 0.5)*resolution.y/resolution.x));
float s = mouse_charge/(r*r);
for(int i=0; i < N; i++){
r = distance(position.xy,charges[i].xy);
s += charges[i].z / (r*r);
}
gl_FragColor = vec4(fract(-log(s)*10.),fract(-log(s)*1.),fract(-log(s)/10.),1.);
}"""
e = EGL(alpha_flags=1<<16) # fullscreen, RGBA, alpha-PREMULT flag on
#e=EGL(pref_width = 640, pref_height=480)
surface_tris = eglfloats( ( - 1.0, - 1.0, 1.0,
- 1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
- 1.0, - 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, - 1.0, 1.0 ) ) # 2 tris
def draw(programObject,time_ms, m, r, Vbo):
opengles.glClear ( GL_COLOR_BUFFER_BIT )
location = opengles.glGetUniformLocation(programObject, "time")
opengles.glUniform1f(location, eglfloat(time_ms))
try:
e._check_glerror()
except GLError, error:
print "Error setting time uniform var"
print error
location = opengles.glGetUniformLocation(programObject, "mouse")
opengles.glUniform2f(location, eglfloat(float(m.x) / r[0].value), eglfloat(float(m.y) / r[1].value))
try:
e._check_glerror()
except GLError, error:
print "Error setting mouse uniform var"
print error
location = opengles.glGetUniformLocation(programObject, "resolution")
opengles.glUniform2f(location, r[0], r[1])
try:
e._check_glerror()
except GLError, error:
print "Error setting resolution uniform var"
print error
opengles.glBindBuffer(GL_ARRAY_BUFFER, Vbo)
opengles.glEnableVertexAttribArray(0);
opengles.glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*4, 0)
# Draws a non-indexed triangle array
opengles.glDrawArrays ( GL_TRIANGLE_STRIP, 0, 6 ) # 2 tris
opengles.glBindBuffer(GL_ARRAY_BUFFER, 0)
e._check_glerror()
openegl.eglSwapBuffers(e.display, e.surface)
time.sleep(0.02)
def run_shader(frag_shader):
try:
try:
m = start_mouse()
except Exception, err:
print err
class FakeM():
def __init__(self, x,y):
self.x = x
self.y = y
self.finished = False
m = FakeM(400,300)
_v_src = """attribute vec3 position;
void main() {
gl_Position = vec4( position, 1.0 );
}"""
vertexShader = e.load_shader(_v_src, GL_VERTEX_SHADER )
fragmentShader = e.load_shader(frag_shader, GL_FRAGMENT_SHADER)
# Create the program object
programObject = opengles.glCreateProgram ( )
opengles.glAttachShader ( programObject, vertexShader )
opengles.glAttachShader ( programObject, fragmentShader )
e._check_glerror()
opengles.glBindAttribLocation ( programObject, 0, "position" )
e._check_glerror()
# Link the program
opengles.glLinkProgram ( programObject )
e._check_glerror()
# Check the link status
if not (e._check_Linked_status(programObject)):
print "Couldn't link the shaders to the program object. Check the bindings and shader sourcefiles."
raise Exception
opengles.glClearColor ( eglfloat(0.3), eglfloat(0.3), eglfloat(0.5), eglfloat(1.0) )
e._check_glerror()
opengles.glUseProgram( programObject )
e._check_glerror()
# Make a VBO buffer obj
Vbo = eglint()
opengles.glGenBuffers(1, ctypes.byref(Vbo))
e._check_glerror()
opengles.glBindBuffer(GL_ARRAY_BUFFER, Vbo)
e._check_glerror()
# Set the buffer's data
opengles.glBufferData(GL_ARRAY_BUFFER, 4 * 6 * 3, surface_tris, GL_STATIC_DRAW)
e._check_glerror()
# Unbind the VBO
opengles.glBindBuffer(GL_ARRAY_BUFFER, 0)
e._check_glerror()
# render loop
start = time.time()
r = (eglfloat(e.width.value), eglfloat(e.height.value))
while(1):
draw(programObject, (time.time() - start), m, r, Vbo)
time.sleep(0.02)
if fw.file_changed:
break
del programObject, vertexShader, fragmentShader
except KeyboardInterrupt:
print "Finishing"
m.finished = True
opengles.glClearColor ( eglfloat(0.0), eglfloat(0.0), eglfloat(0.0), eglfloat(0.0) )
opengles.glClear ( GL_COLOR_BUFFER_BIT )
openegl.eglSwapBuffers(e.display, e.surface)
return
except Exception, error:
print "Error - finishing"
opengles.glClearColor ( eglfloat(0.0), eglfloat(0.0), eglfloat(0.0), eglfloat(0.0) )
opengles.glClear ( GL_COLOR_BUFFER_BIT )
openegl.eglSwapBuffers(e.display, e.surface)
m.finished = True
raise error
if __name__ == "__main__":
import sys
if len(sys.argv) == 2:
fw=FileWatcher([sys.argv[1]])
fw.start()
try:
while(True):
fw.file_changed=0
glsl_file = open(sys.argv[1], "r")
frag = glsl_file.read()
glsl_file.close()
try:
run_shader(frag)
except:
time.sleep(3)
except IOError:
print "No such file"
fw.stop()