-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon3dlighting.py
317 lines (285 loc) · 7.49 KB
/
common3dlighting.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#Based on https://github.com/Habitats/uni/blob/master/img_processing/project/graphics_modern2.py
from __future__ import print_function
import OpenGL
from OpenGL.GL import *
import OpenGL.GL as gl
from OpenGL.GL.shaders import *
from OpenGL.GLU import *
from OpenGL.arrays import vbo
import numpy as np
import math, sys, time
import transutils
strVS = """
#version 330 core
in vec3 aVert;
in vec3 aVertNormal;
in vec4 aColor;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
out vec4 vCol;
out vec3 fragNormal;
out vec3 fragWorld;
void main() {
// set position
vec4 fragWorldTmp = uMVMatrix * vec4(aVert, 1.0);
gl_Position = uPMatrix * fragWorldTmp;
fragWorld = vec3(fragWorldTmp);
vCol = aColor;
fragNormal = aVertNormal;
}
"""
strFS = """
#version 330 core
in vec4 vCol;
in vec3 fragNormal;
in vec3 fragWorld;
out vec4 fragColor;
uniform mat4 uMVMatrix;
uniform vec3 lightPos;
void main() {
//calculate normal in world coordinates
mat3 normalMatrix = transpose(inverse(mat3(uMVMatrix)));
vec3 normal = normalize(normalMatrix * fragNormal);
//calculate the vector from this pixels surface to the light source
vec3 surfaceToLight = lightPos - fragWorld;
//calculate the cosine of the angle of incidence
float brightness = dot(normal, surfaceToLight) / (length(surfaceToLight) * length(normal));
brightness = clamp(brightness, 0, 1);
// use vertex color
fragColor = vCol * brightness;
}
"""
# initialization
def init():
versionString = gl.glGetString(gl.GL_VERSION).split(b" ")
openglVersionString = versionString[0]
openglVersionNums = list(map(int, openglVersionString.split(b".")))
if openglVersionNums[0] < 3 or (openglVersionNums[0] == 3 and openglVersionNums[1] < 3):
exit("Requires opengl 3.3 or better, you have {0}".format(openglVersionString))
out = {}
# create shader
vs = compileShader(strVS, GL_VERTEX_SHADER)
fs = compileShader(strFS, GL_FRAGMENT_SHADER)
program = compileProgram(vs, fs)
glUseProgram(program)
pMatrixUniform = glGetUniformLocation(program, 'uPMatrix')
mvMatrixUniform = glGetUniformLocation(program, "uMVMatrix")
lightPosUniform = glGetUniformLocation(program, 'lightPos')
# attributes
vertIndex = glGetAttribLocation(program, "aVert")
colorIndex = glGetAttribLocation(program, "aColor")
normalIndex = glGetAttribLocation(program, "aVertNormal")
# define quad vertices
s = 0.9
quadV = [
#Front
- s, s, -s,
- s, -s, -s,
s, s, -s,
s, s, -s,
- s, -s, -s,
s, -s, -s,
#Back
- s, s, s,
- s, -s, s,
s, s, s,
s, s, s,
- s, -s, s,
s, -s, s,
#Left
s, - s, s,
s, - s, -s,
s, s, s,
s, s, s,
s, - s, -s,
s, s, -s,
#Right
-s, - s, s,
-s, - s, -s,
-s, s, s,
-s, s, s,
-s, - s, -s,
-s, s, -s,
#Top
- s, -s, s,
- s, -s, -s,
s, -s, s,
s, -s, s,
- s, -s, -s,
s, -s, -s,
#Bottom
- s, s, s,
- s, s, -s,
s, s, s,
s, s, s,
- s, s, -s,
s, s, -s,
]
vertexBuffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer)
vertexData = np.array(quadV, np.float32)
glBufferData(GL_ARRAY_BUFFER, 4 * len(vertexData), vertexData, GL_STATIC_DRAW)
# define normals
s = 1.
quadN = [
#Front
0, 0, -s,
0, 0, -s,
0, 0, -s,
0, 0, -s,
0, 0, -s,
0, 0, -s,
#Back
0, 0, s,
0, 0, s,
0, 0, s,
0, 0, s,
0, 0, s,
0, 0, s,
#Left
s, 0, 0,
s, 0, 0,
s, 0, 0,
s, 0, 0,
s, 0, 0,
s, 0, 0,
#Right
-s, 0, 0,
-s, 0, 0,
-s, 0, 0,
-s, 0, 0,
-s, 0, 0,
-s, 0, 0,
#Top
0, -s, 0,
0, -s, 0,
0, -s, 0,
0, -s, 0,
0, -s, 0,
0, -s, 0,
#Bottom
0, s, 0,
0, s, 0,
0, s, 0,
0, s, 0,
0, s, 0,
0, s, 0,
]
normalBuffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer)
normalData = np.array(quadN, np.float32)
glBufferData(GL_ARRAY_BUFFER, 4 * len(normalData), normalData, GL_STATIC_DRAW)
# define vertex colours
vcol = [
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 1.0, 1.0, 1.0,
0.0, 1.0, 1.0, 1.0,
0.0, 1.0, 1.0, 1.0,
0.0, 1.0, 1.0, 1.0,
0.0, 1.0, 1.0, 1.0,
0.0, 1.0, 1.0, 1.0,
1.0, 0.0, 1.0, 1.0,
1.0, 0.0, 1.0, 1.0,
1.0, 0.0, 1.0, 1.0,
1.0, 0.0, 1.0, 1.0,
1.0, 0.0, 1.0, 1.0,
1.0, 0.0, 1.0, 1.0,
1.0, 1.0, 0.0, 1.0,
1.0, 1.0, 0.0, 1.0,
1.0, 1.0, 0.0, 1.0,
1.0, 1.0, 0.0, 1.0,
1.0, 1.0, 0.0, 1.0,
1.0, 1.0, 0.0, 1.0,
]
colBuffer = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, colBuffer)
colData = np.array(vcol, np.float32)
glBufferData(GL_ARRAY_BUFFER, 4 * len(colData), colData, GL_STATIC_DRAW)
out = {'program': program,
'pMatrixUniform': pMatrixUniform,
'mvMatrixUniform': mvMatrixUniform,
'colorIndex': colorIndex,
'vertIndex': vertIndex,
'vertexBuffer': vertexBuffer,
'colBuffer': colBuffer}
out['lightPosUniform'] = lightPosUniform
out['normalIndex'] = normalIndex
out['normalBuffer'] = normalBuffer
return out
def draw(params, aspect):
program = params['program']
pMatrixUniform = params['pMatrixUniform']
mvMatrixUniform = params['mvMatrixUniform']
colorIndex = params['colorIndex']
vertIndex = params['vertIndex']
vertexBuffer = params['vertexBuffer']
colBuffer = params['colBuffer']
lightPosUniform = params['lightPosUniform']
normalIndex = params['normalIndex']
normalBuffer = params['normalBuffer']
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_DEPTH_TEST)
# build projection matrix
fov = math.radians(45.0)
f = 1.0 / math.tan(fov / 2.0)
zN, zF = (0.1, 100.0)
a = aspect
pMatrix = np.array([f / a, 0.0, 0.0, 0.0,
0.0, f, 0.0, 0.0,
0.0, 0.0, (zF + zN) / (zN - zF), -1.0,
0.0, 0.0, 2.0 * zF * zN / (zN - zF), 0.0], np.float32)
# modelview matrix
identityMvMatrix = np.array([[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]])
test = time.time()
numPi = int(test / math.pi)
mvMatrix = identityMvMatrix
mvRotate1 = transutils.rotMatrix(time.time()*20., 1., 0., 0., )
mvMatrix = np.dot(mvRotate1, mvMatrix)
mvRotate2 = transutils.rotMatrix(time.time()*5., 0., 1., 0.)
mvMatrix = np.dot(mvRotate2, mvMatrix)
mvMatrix[3,2] = -5. #Translate
mvMatrix = np.array(mvMatrix.reshape((16,)), np.float32)
# use shader
glUseProgram(program)
# set proj matrix
glUniformMatrix4fv(pMatrixUniform, 1, GL_FALSE, pMatrix)
# set modelview matrix
glUniformMatrix4fv(mvMatrixUniform, 1, GL_FALSE, mvMatrix)
# set lighting
glUniform3fv(lightPosUniform, 1, np.array([2., 2., 0.], np.float32))
#enable arrays
glEnableVertexAttribArray(vertIndex)
glEnableVertexAttribArray(colorIndex)
glEnableVertexAttribArray(normalIndex)
# set buffers
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer)
glVertexAttribPointer(vertIndex, 3, GL_FLOAT, GL_FALSE, 0, None)
glBindBuffer(GL_ARRAY_BUFFER, colBuffer)
glVertexAttribPointer(colorIndex, 4, GL_FLOAT, GL_FALSE, 0, None)
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer)
glVertexAttribPointer(normalIndex, 3, GL_FLOAT, GL_FALSE, 0, None)
# draw
glDrawArrays(GL_TRIANGLES, 0, 36)
# disable arrays
glDisableVertexAttribArray(vertIndex)
glDisableVertexAttribArray(colorIndex)