|
10 | 10 | from pyglet.gl import *
|
11 | 11 |
|
12 | 12 | class Shader:
|
13 |
| - # vert, frag and geom take arrays of source strings |
14 |
| - # the arrays will be concattenated into one string by OpenGL |
15 |
| - def __init__(self, vert = [], frag = [], geom = []): |
16 |
| - # create the program handle |
17 |
| - self.handle = glCreateProgram() |
18 |
| - # we are not linked yet |
19 |
| - self.linked = False |
20 |
| - |
21 |
| - # create the vertex shader |
22 |
| - self.createShader(vert, GL_VERTEX_SHADER) |
23 |
| - # create the fragment shader |
24 |
| - self.createShader(frag, GL_FRAGMENT_SHADER) |
25 |
| - # the geometry shader will be the same, once pyglet supports the extension |
26 |
| - # self.createShader(frag, GL_GEOMETRY_SHADER_EXT) |
27 |
| - |
28 |
| - # attempt to link the program |
29 |
| - self.link() |
30 |
| - |
31 |
| - def createShader(self, strings, type): |
32 |
| - count = len(strings) |
33 |
| - # if we have no source code, ignore this shader |
34 |
| - if count < 1: |
35 |
| - return |
36 |
| - |
37 |
| - # create the shader handle |
38 |
| - shader = glCreateShader(type) |
39 |
| - |
40 |
| - # convert the source strings into a ctypes pointer-to-char array, and upload them |
41 |
| - # this is deep, dark, dangerous black magick - don't try stuff like this at home! |
42 |
| - src = (c_char_p * count)(*strings) |
43 |
| - glShaderSource(shader, count, cast(pointer(src), POINTER(POINTER(c_char))), None) |
44 |
| - |
45 |
| - # compile the shader |
46 |
| - glCompileShader(shader) |
47 |
| - |
48 |
| - temp = c_int(0) |
49 |
| - # retrieve the compile status |
50 |
| - glGetShaderiv(shader, GL_COMPILE_STATUS, byref(temp)) |
51 |
| - |
52 |
| - # if compilation failed, print the log |
53 |
| - if not temp: |
54 |
| - # retrieve the log length |
55 |
| - glGetShaderiv(shader, GL_INFO_LOG_LENGTH, byref(temp)) |
56 |
| - # create a buffer for the log |
57 |
| - buffer = create_string_buffer(temp.value) |
58 |
| - # retrieve the log text |
59 |
| - glGetShaderInfoLog(shader, temp, None, buffer) |
60 |
| - # print the log to the console |
61 |
| - print buffer.value |
62 |
| - else: |
63 |
| - # all is well, so attach the shader to the program |
64 |
| - glAttachShader(self.handle, shader); |
65 |
| - |
66 |
| - def link(self): |
67 |
| - # link the program |
68 |
| - glLinkProgram(self.handle) |
69 |
| - |
70 |
| - temp = c_int(0) |
71 |
| - # retrieve the link status |
72 |
| - glGetProgramiv(self.handle, GL_LINK_STATUS, byref(temp)) |
73 |
| - |
74 |
| - # if linking failed, print the log |
75 |
| - if not temp: |
76 |
| - # retrieve the log length |
77 |
| - glGetProgramiv(self.handle, GL_INFO_LOG_LENGTH, byref(temp)) |
78 |
| - # create a buffer for the log |
79 |
| - buffer = create_string_buffer(temp.value) |
80 |
| - # retrieve the log text |
81 |
| - glGetProgramInfoLog(self.handle, temp, None, buffer) |
82 |
| - # print the log to the console |
83 |
| - print buffer.value |
84 |
| - else: |
85 |
| - # all is well, so we are linked |
86 |
| - self.linked = True |
87 |
| - |
88 |
| - def bind(self): |
89 |
| - # bind the program |
90 |
| - glUseProgram(self.handle) |
91 |
| - |
92 |
| - def unbind(self): |
93 |
| - # unbind whatever program is currently bound - not necessarily this program, |
94 |
| - # so this should probably be a class method instead |
95 |
| - glUseProgram(0) |
96 |
| - |
97 |
| - # upload a floating point uniform |
98 |
| - # this program must be currently bound |
99 |
| - def uniformf(self, name, *vals): |
100 |
| - # check there are 1-4 values |
101 |
| - if len(vals) in range(1, 5): |
102 |
| - # select the correct function |
103 |
| - { 1 : glUniform1f, |
104 |
| - 2 : glUniform2f, |
105 |
| - 3 : glUniform3f, |
106 |
| - 4 : glUniform4f |
107 |
| - # retrieve the uniform location, and set |
108 |
| - }[len(vals)](glGetUniformLocation(self.handle, name), *vals) |
109 |
| - |
110 |
| - # upload an integer uniform |
111 |
| - # this program must be currently bound |
112 |
| - def uniformi(self, name, *vals): |
113 |
| - # check there are 1-4 values |
114 |
| - if len(vals) in range(1, 5): |
115 |
| - # select the correct function |
116 |
| - { 1 : glUniform1i, |
117 |
| - 2 : glUniform2i, |
118 |
| - 3 : glUniform3i, |
119 |
| - 4 : glUniform4i |
120 |
| - # retrieve the uniform location, and set |
121 |
| - }[len(vals)](glGetUniformLocation(self.handle, name), *vals) |
122 |
| - |
123 |
| - # upload a uniform matrix |
124 |
| - # works with matrices stored as lists, |
125 |
| - # as well as euclid matrices |
126 |
| - def uniform_matrixf(self, name, mat): |
127 |
| - # obtian the uniform location |
128 |
| - loc = glGetUniformLocation(self.Handle, name) |
129 |
| - # uplaod the 4x4 floating point matrix |
130 |
| - glUniformMatrix4fv(loc, 1, False, (c_float * 16)(*mat)) |
| 13 | + default_vert = """ |
| 14 | +void main() |
| 15 | +{ |
| 16 | + gl_Position = ftransform(); |
| 17 | +} |
| 18 | +""" |
| 19 | + |
| 20 | + default_frag = """ |
| 21 | +varying vec3 normal, lightDir0, lightDir1, eyeVec; |
| 22 | +
|
| 23 | +void main (void) |
| 24 | +{ |
| 25 | + vec4 final_color = |
| 26 | + (gl_FrontLightModelProduct.sceneColor * gl_FrontMaterial.ambient) + |
| 27 | + (gl_LightSource[0].ambient * gl_FrontMaterial.ambient) + |
| 28 | + (gl_LightSource[1].ambient * gl_FrontMaterial.ambient); |
| 29 | +
|
| 30 | + gl_FragColor = final_color; |
| 31 | +} |
| 32 | +""" |
| 33 | + |
| 34 | + # vert, frag and geom take arrays of source strings |
| 35 | + # the arrays will be concattenated into one string by OpenGL |
| 36 | + def __init__(self, vert = None, frag = None, geom = None): |
| 37 | + # create the program handle |
| 38 | + self.handle = glCreateProgram() |
| 39 | + # we are not linked yet |
| 40 | + self.linked = False |
| 41 | + |
| 42 | + # create the vertex shader |
| 43 | + self.createShader(vert, GL_VERTEX_SHADER) |
| 44 | + # create the fragment shader |
| 45 | + self.createShader(frag, GL_FRAGMENT_SHADER) |
| 46 | + # the geometry shader will be the same, once pyglet supports the extension |
| 47 | + # self.createShader(frag, GL_GEOMETRY_SHADER_EXT) |
| 48 | + |
| 49 | + # attempt to link the program |
| 50 | + self.link() |
| 51 | + |
| 52 | + def createShader(self, strings, type): |
| 53 | + if strings == None: |
| 54 | + return |
| 55 | + |
| 56 | + count = len(strings) |
| 57 | + # if we have no source code, ignore this shader |
| 58 | + if count < 1: |
| 59 | + return |
| 60 | + |
| 61 | + # create the shader handle |
| 62 | + shader = glCreateShader(type) |
| 63 | + |
| 64 | + # convert the source strings into a ctypes pointer-to-char array, and upload them |
| 65 | + # this is deep, dark, dangerous black magick - don't try stuff like this at home! |
| 66 | + src = (c_char_p * count)(*strings) |
| 67 | + glShaderSource(shader, count, cast(pointer(src), POINTER(POINTER(c_char))), None) |
| 68 | + |
| 69 | + # compile the shader |
| 70 | + glCompileShader(shader) |
| 71 | + |
| 72 | + temp = c_int(0) |
| 73 | + # retrieve the compile status |
| 74 | + glGetShaderiv(shader, GL_COMPILE_STATUS, byref(temp)) |
| 75 | + |
| 76 | + # if compilation failed, print the log |
| 77 | + if not temp: |
| 78 | + # retrieve the log length |
| 79 | + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, byref(temp)) |
| 80 | + # create a buffer for the log |
| 81 | + buffer = create_string_buffer(temp.value) |
| 82 | + # retrieve the log text |
| 83 | + glGetShaderInfoLog(shader, temp, None, buffer) |
| 84 | + # print the log to the console |
| 85 | + print buffer.value |
| 86 | + else: |
| 87 | + # all is well, so attach the shader to the program |
| 88 | + glAttachShader(self.handle, shader); |
| 89 | + |
| 90 | + def link(self): |
| 91 | + # link the program |
| 92 | + glLinkProgram(self.handle) |
| 93 | + |
| 94 | + temp = c_int(0) |
| 95 | + # retrieve the link status |
| 96 | + glGetProgramiv(self.handle, GL_LINK_STATUS, byref(temp)) |
| 97 | + |
| 98 | + # if linking failed, print the log |
| 99 | + if not temp: |
| 100 | + # retrieve the log length |
| 101 | + glGetProgramiv(self.handle, GL_INFO_LOG_LENGTH, byref(temp)) |
| 102 | + # create a buffer for the log |
| 103 | + buffer = create_string_buffer(temp.value) |
| 104 | + # retrieve the log text |
| 105 | + glGetProgramInfoLog(self.handle, temp, None, buffer) |
| 106 | + # print the log to the console |
| 107 | + print buffer.value |
| 108 | + else: |
| 109 | + # all is well, so we are linked |
| 110 | + self.linked = True |
| 111 | + |
| 112 | + def bind(self): |
| 113 | + # bind the program |
| 114 | + glUseProgram(self.handle) |
| 115 | + |
| 116 | + def unbind(self): |
| 117 | + # unbind whatever program is currently bound - not necessarily this program, |
| 118 | + # so this should probably be a class method instead |
| 119 | + glUseProgram(0) |
| 120 | + |
| 121 | + # upload a floating point uniform |
| 122 | + # this program must be currently bound |
| 123 | + def uniformf(self, name, *vals): |
| 124 | + # check there are 1-4 values |
| 125 | + if len(vals) in range(1, 5): |
| 126 | + # select the correct function |
| 127 | + { 1 : glUniform1f, |
| 128 | + 2 : glUniform2f, |
| 129 | + 3 : glUniform3f, |
| 130 | + 4 : glUniform4f |
| 131 | + # retrieve the uniform location, and set |
| 132 | + }[len(vals)](glGetUniformLocation(self.handle, name), *vals) |
| 133 | + |
| 134 | + # upload an integer uniform |
| 135 | + # this program must be currently bound |
| 136 | + def uniformi(self, name, *vals): |
| 137 | + # check there are 1-4 values |
| 138 | + if len(vals) in range(1, 5): |
| 139 | + # select the correct function |
| 140 | + { 1 : glUniform1i, |
| 141 | + 2 : glUniform2i, |
| 142 | + 3 : glUniform3i, |
| 143 | + 4 : glUniform4i |
| 144 | + # retrieve the uniform location, and set |
| 145 | + }[len(vals)](glGetUniformLocation(self.handle, name), *vals) |
| 146 | + |
| 147 | + # upload a uniform matrix |
| 148 | + # works with matrices stored as lists, |
| 149 | + # as well as euclid matrices |
| 150 | + def uniform_matrixf(self, name, mat): |
| 151 | + # obtian the uniform location |
| 152 | + loc = glGetUniformLocation(self.Handle, name) |
| 153 | + # uplaod the 4x4 floating point matrix |
| 154 | + glUniformMatrix4fv(loc, 1, False, (c_float * 16)(*mat)) |
0 commit comments