forked from JUnderCover/Star-data-with-PyOpenGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.py
41 lines (33 loc) · 1.76 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
from OpenGL.GL import glGetAttribLocation, glUseProgram, \
glGetUniformLocation, glUniformMatrix4fv, \
GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, GL_FALSE
from OpenGL.GL.shaders import compileShader, compileProgram, glDeleteShader
class Shader(object):
def __init__(self, vertexShaderSource, fragmentShaderSource):
vertexFile = open(vertexShaderSource, 'r')
fragmentFile = open(fragmentShaderSource, 'r')
vertexShader = compileShader(vertexFile.read(), GL_VERTEX_SHADER)
fragmentShader = compileShader(fragmentFile.read(), GL_FRAGMENT_SHADER)
self.__shader = compileProgram(vertexShader, fragmentShader)
vertexFile.close()
fragmentFile.close()
glDeleteShader(vertexShader)
glDeleteShader(fragmentShader)
def getAttributes(self):
#the current attributes are position (Vec 3), color (Vec 3) and point size (float)
#These are inputs to the vertex shader.
#Each row of data must be structured in this order
return glGetAttribLocation(self.__shader, "position"),glGetAttribLocation(self.__shader, "vcolor"), glGetAttribLocation(self.__shader, "psize")
def use(self):
glUseProgram(self.__shader)
def setModelMatrix(self, model):
modelLoc = glGetUniformLocation(self.__shader, "model")
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, model)
def setViewMatrix(self, view):
viewLoc = glGetUniformLocation(self.__shader, "view")
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, view)
def setProjectionMatrix(self, projection):
projectionLoc = glGetUniformLocation(self.__shader, "projection")
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, projection)
if __name__ == "__main__":
print("Trying to run Shader class definition?!")