-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathshader.py
121 lines (104 loc) · 3.84 KB
/
shader.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
#!/usr/bin/env python
import os, time, sys, urllib, re
# http://eyalarubas.com/python-subproc-nonblock.html
from subprocess import Popen, PIPE
from time import sleep
from fcntl import fcntl, F_GETFL, F_SETFL
from os import O_NONBLOCK, read
class Shader:
COMMAND='glslViewer'
TMP_DIR='/tmp/'
process = {}
cout = {}
wait_time = 0.0001
time = 0.
delta = 0.
fps = 0
generated_file = ''
def __init__(self, filename, options = {}):
# Compose a shader file
if 'template' in options:
self.generated_file = filename
shader_file = open(filename, "w")
with open(options['template']) as f:
if 'pragmas' in options:
for line in f:
matches = re.findall('\\#pragma tangram:\\s+([\\w|]*)\\n', line)
if len(matches):
if options['pragmas'].has_key(matches[0]):
shader_file.write(options['pragmas'][matches[0]])
else:
shader_file.write(line)
else:
# TODO: make sense of this case
for line in f:
shader_file.write(line)
shader_file.close()
# compose and excecute a glslViewer command
cmd = [self.COMMAND]
cmd.append(filename)
if options.has_key('size'):
cmd.append('-w')
cmd.append(str(options['size']))
cmd.append('-h')
cmd.append(str(options['size']))
if options.has_key('headless'):
cmd.append('--headless')
if options.has_key('output'):
cmd.append('-o')
cmd.append(options['output'])
tex_counter=0
if options.has_key('textures'):
for uniform_name in options['textures'].keys():
url = options['textures'][uniform_name]
ext = os.path.splitext(url)[1]
img_path = self.TMP_DIR + str(tex_counter) + ext
http = urllib.URLopener()
http.retrieve(url, img_path)
cmd.append('-' + uniform_name)
cmd.append(img_path);
# print 'EXE ', ' '.join(cmd)
self.process = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)
flags = fcntl(self.process.stdout, F_GETFL) # get current self.process.stdout flags
fcntl(self.process.stdout, F_SETFL, flags | O_NONBLOCK)
# self.cout = NonBlockingStreamReader(self.process)
def read(self):
while True:
try:
return read(self.process.stdout.fileno(), 1024).rstrip()
except OSError:
return 'none'
def isFinish(self):
return self.process.poll() is not None
def getTime(self):
self.process.stdin.write('time\n')
sleep(self.wait_time)
answer = self.read()
if answer:
if answer.replace('.','',1).isdigit():
self.time = float(answer)
return self.time
def getDelta(self):
self.process.stdin.write('delta\n')
sleep(self.wait_time)
answer = self.read()
if answer:
if answer.replace('.','',1).isdigit():
self.delta = float(answer)
return self.delta
def getFPS(self):
return self.process.stdin.write('fps\n')
sleep(self.wait_time)
answer = self.read()
if answer:
if answer.replace('.','',1).isdigit():
self.fps = float(answer)
return self.fps
def stop(self):
self.process.stdin.write('exit\n')
while not self.isFinish():
sleep(1)
# print "Finish"
# self.process.kill()
# if len(self.generated_file):
# os.remove(self.generated_file)