This repository was archived by the owner on Jul 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshaders.py
More file actions
86 lines (79 loc) · 3.02 KB
/
shaders.py
File metadata and controls
86 lines (79 loc) · 3.02 KB
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
from OpenGL.GL import *
ProgramFiles = ['simple.glsl']
Programs = {
"BareBones" : {
"GL_VERTEX_SHADER" : "simple.xforms simple.vert",
"GL_GEOMETRY_SHADER" : "simple.xforms simple.geom",
"GL_FRAGMENT_SHADER" : "simple.xforms simple.frag"
}
}
class Attribs:
POSITION = 0
NORMAL = 1
def load_shaders(glslFiles = ProgramFiles, attribs = Attribs, programMap = Programs):
"""Parse a series of simple text files, each of which is composed
series of shader snippets. The given 'attribs' class defines an
enumeration of attribute slots to bind (aka semantics).
In each text files, lines starting with two dash characters start a new
shader snippet with the given name. For example:
-- S1
uniform float foo;
-- Prefix
#version 150
-- S2
uniform float bar;
"""
# First parse the file to populate 'snippetMap'
class ParserState:
HEADER = 0
SOURCE = 1
parserState = ParserState.HEADER
snippetMap = {}
for glslFile in glslFiles:
snippetPrefix = glslFile.split('.')[0] + '.'
for line in open(glslFile):
if line.startswith('--'):
if parserState is ParserState.HEADER:
parserState = ParserState.SOURCE
elif len(source):
snippetMap[snippetName] = ''.join(source)
snippetName = snippetPrefix + line[2:].strip()
source = []
continue
if parserState is ParserState.HEADER:
pass
else:
source.append(line)
if len(source):
snippetMap[snippetName] = ''.join(source)
# Now, glue together the strings and feed them to OpenGL
stagePrefix = "#version 150\n"
programs = {}
for key, val in programMap.items():
programHandle = glCreateProgram()
for stageName, snippetList in val.items():
snippets = map(snippetMap.get, snippetList.split())
stageSource = stagePrefix + ''.join(snippets)
stage = getattr(OpenGL.GL, stageName)
sHandle = glCreateShader(stage)
glShaderSource(sHandle, stageSource)
glCompileShader(sHandle)
success = glGetShaderiv(sHandle, GL_COMPILE_STATUS)
if not success:
print 'Error in', stageName, snippetList
infolog = glGetShaderInfoLog(sHandle)
raise SystemExit(infolog)
glAttachShader(programHandle, sHandle)
for attrib in dir(attribs):
if attrib.startswith('__'):
continue
slot = getattr(attribs, attrib)
name = attrib[0] + attrib[1:].lower()
glBindAttribLocation(programHandle, slot, name)
glLinkProgram(programHandle)
success = glGetProgramiv(programHandle, GL_LINK_STATUS);
if not success:
infolog = glGetProgramInfoLog(programHandle)
raise SystemExit(infolog)
programs[key] = programHandle
return programs