-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaderProgramBackup.py
174 lines (146 loc) · 6 KB
/
shaderProgramBackup.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
from OpenGL.GL import (
#imports that are no longer used:
#glVertex3fv,
shaders, GL_VERTEX_SHADER, GL_FRAGMENT_SHADER,
glUseProgram,
glLoadIdentity, glTranslatef, glRotatef,
glBegin, glEnd, glClear,
GL_LINES, GL_QUADS, GL_TRIANGLES, GL_STATIC_DRAW,
GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_ARRAY_BUFFER,
GL_FLOAT, GLfloat,
glDrawArrays, glBufferData, glBufferSubData,
glGenVertexArrays, glBindVertexArray, glDeleteVertexArrays,
glEnableVertexAttribArray, glDisableVertexAttribArray,
glGenBuffers, glBindBuffer, glDeleteBuffers,
glVertexAttribPointer
)
from OpenGL.GLU import gluPerspective
from exceptions import *
import ctypes
class shaderManager(): #context manager to ensure resource cleanup
def __init__(self, parent, viewPort):
self.parent = parent
self.viewPort = viewPort
def __enter__(self):
self._shader = shader(self.parent, self.viewPort)
return self._shader
def __exit__(self, *args):
self._shader.crashCleanup()
class shader():
def __init__(self, parent, viewPort):
self.parent = parent
self.viewPort = viewPort
self.sizeX = 100
self.sizeY = 100
#drawing data
self.rotateDegreeV = 0
self.rotateDegreeH = 0
self.zoomLevel = -5
self.vOffset = 0
self.shapes = []
#legalShapes = [cube]
#shader data
self.vertPath = 'vertexShader.glsl'
self.fragPath = 'fragmentShader.glsl'
self.vertProg = None
self.fragProg = None
self.vertCode = ''
self.fragCode = ''
self.vertShader = None
self.fragShader = None
#crash data
self.crashFlag = False
self.viewPort.initializeGL = self.initGL
self.viewPort.paintGL = self.paintGL
def addShapes(self, *newShapes):
for newShape in newShapes:
#for s in self.legalShapes:
# if not type(newShape) is s:
# raise invalidShapeError("Shape {} is not valid.".format(s))
# return
self.shapes.append(newShape)
def update(self):
self.viewPort.update()
def resize(self, x, y):
self.sizeX = x
self.sizeY = y
self.viewPort.resizeGL(self.sizeX, self.sizeY)
def navigate(self, hVal = 0, vVal = 0, zVal = 0):
self.rotateDegreeH += hVal
self.rotateDegreeV += vVal
self.zoomLevel += zVal
def initGL(self):
self.vertexData = [-1, -1, 0,
1, -1, 0,
0, 1, 0]
self.vertexArrayID = glGenVertexArrays(1)
glBindVertexArray(self.vertexArrayID)
self.attrID = 0
self.vertexBuffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer)
arrayType = GLfloat * len(self.vertexData)
#initialize data for the buffer
target = GL_ARRAY_BUFFER
size = len(self.vertexData) * ctypes.sizeof(ctypes.c_float)
data = arrayType(*self.vertexData)
usage = GL_STATIC_DRAW
glBufferData(target, size, data, usage)
glVertexAttribPointer(self.attrID, 3, GL_FLOAT, False, 0, None)
glEnableVertexAttribArray(self.attrID)
#access the code for the vertex and fragment shaders
with open(self.vertPath, 'r') as vertProg:
self.vertCode = vertProg.read()
with open(self.fragPath, 'r') as fragProg:
self.fragCode = fragProg.read()
#compile those shaders
self.vertShader = shaders.compileShader(self.vertCode, GL_VERTEX_SHADER)
self.fragShader = shaders.compileShader(self.fragCode, GL_FRAGMENT_SHADER)
self.shader = shaders.compileProgram(self.vertShader, self.fragShader)
glUseProgram(self.shader)
#paintGL uses shape objects, such as cube() or mesh(). Shape objects require the following:
#a list named 'vertices' - This list is a list of points, from which edges and faces are drawn.
#a list named 'wires' - This list is a list of tuples which refer to vertices, dictating where to draw wires.
#a list named 'facets' - This list is a list of tuples which refer to vertices, ditating where to draw facets.
#a bool named 'render' - This bool is used to dictate whether or not to draw the shape.
#a bool named 'drawWires' - This bool is used to dictate whether wires should be drawn.
#a bool named 'drawFaces' - This bool is used to dictate whether facets should be drawn.
def paintGL(self):
if self.crashFlag: #run cleanup operations
glUseProgram(0)
glDisableVertexAttribArray(self.attrID)
glDeleteBuffers(1,[self.vertexBuffer])
glDeleteVertexArrays(1, [self.vertexArrayID])
glLoadIdentity()
gluPerspective(45, self.sizeX / self.sizeY, 0.1, 110.0) #set perspective
glTranslatef(0, 0, self.zoomLevel)
glRotatef(self.rotateDegreeV + self.vOffset, 1, 0, 0)
glRotatef(self.rotateDegreeH, 0, 0, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
self.vertexData = [
-1, 1, 0,
0, -1, 0,
1, 1, 0
]
arrayType = GLfloat * len(self.vertexData)
target = GL_ARRAY_BUFFER
offset = 0
size = len(self.vertexData) * ctypes.sizeof(ctypes.c_float)
data = arrayType(*self.vertexData)
glBufferSubData(target, offset, size, data)
glDrawArrays(GL_TRIANGLES, 0, 3)
#for s in self.shapes:
# if s.drawWires:
# glBegin(GL_LINES)
# for w in s.wires:
# for v in w:
# glVertex3fv(s.vertices[v])
# glEnd()
# if s.drawFaces:
# glBegin(GL_QUADS)
# for f in s.facets:
# for v in f:
# glVertex3fv(s.vertices[v])
#glEnd()
def crashCleanup(self):
self.crashFlag = True
self.viewPort.update()