-
Notifications
You must be signed in to change notification settings - Fork 18
/
glut_opengl_shader_glm_cube_parallax.py
378 lines (299 loc) · 13.9 KB
/
glut_opengl_shader_glm_cube_parallax.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# Adding Shadows to Parallax Occlusion Map
# https://stackoverflow.com/questions/55089830/adding-shadows-to-parallax-occlusion-map/55091654#55091654
import os
import math
import ctypes
import numpy as np
import glm
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GL.shaders import *
from OpenGL.arrays import *
from ctypes import c_void_p
from PIL import Image
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../resource/texture'))
def ReadTexture(filename, textureUnit):
# PIL can open BMP, EPS, FIG, IM, JPEG, MSP, PCX, PNG, PPM
# and other file types. We convert into a texture using GL.
print('trying to open', filename)
try:
image = Image.open(filename)
except IOError as ex:
print('IOError: failed to open texture file ', filename)
sys.exit()
return -1
print('opened file: size=', image.size, 'format=', image.format)
imageData = np.array(list(image.getdata()), np.uint8)
format = GL_RGB if imageData.shape[1] == 3 else GL_RGBA
glActiveTexture( GL_TEXTURE0 + textureUnit )
textureObj = glGenTextures( 1 )
glPixelStorei(GL_UNPACK_ALIGNMENT, 4)
glBindTexture(GL_TEXTURE_2D, textureObj)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.size[0], image.size[1],0, format, GL_UNSIGNED_BYTE, imageData)
#glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
#glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
#glGenerateMipmap(GL_TEXTURE_2D)
image.close()
return textureObj
class MyWindow:
__caption = 'OpenGL Window'
__vp_size = [800, 600]
__vp_valid = False
__glut_wnd = None
__glsl_vert = """
#version 450 core
layout (location = 0) in vec3 a_pos;
layout (location = 1) in vec3 a_nv;
layout (location = 2) in vec3 a_tv;
layout (location = 3) in vec2 a_uv;
layout (location = 4) in vec4 a_col;
out vec3 o_worldPos;
out vec2 o_texCoord;
out vec4 o_color;
out vec3 world_normal;
out vec3 world_tangent;
uniform mat4 u_proj;
uniform mat4 u_view;
uniform mat4 u_model;
void main()
{
mat4 model_view = u_model;
mat3 normal = transpose(inverse(mat3(model_view)));
vec4 view_pos = model_view * vec4(a_pos.xyz, 1.0);
o_worldPos = view_pos.xyz;
world_normal = normal * a_nv;
world_tangent = normal * a_tv;
o_texCoord = a_uv;
o_color = a_col;
gl_Position = u_proj * u_view * view_pos;
}
"""
__glsl_frag = """
#version 450 core
out vec4 fragColor;
in vec3 o_worldPos;
in vec2 o_texCoord;
in vec4 o_color;
in vec3 world_normal;
in vec3 world_tangent;
layout (binding = 0) uniform sampler2D diffuseMap;
layout (binding = 1) uniform sampler2D normalMap;
layout (binding = 2) uniform sampler2D heightMap;
uniform vec3 light_pos;
uniform vec3 viewPosition;
uniform float heightScale;
void main()
{
vec4 color = texture(diffuseMap, o_texCoord);
vec3 N = normalize(world_normal);
vec3 L = normalize(light_pos- o_worldPos);
vec3 V = -normalize(o_worldPos - viewPosition);
float ka = 0.1;
float kd = max(0.0, dot(N, L)) * 0.9;
fragColor = vec4(color.rgb * (ka + kd), color.a);
}
"""
__glsl_parallax_frag = """
#version 450 core
out vec4 fragColor;
in vec3 o_worldPos;
in vec2 o_texCoord;
in vec4 o_color;
in vec3 world_normal;
in vec3 world_tangent;
layout (binding = 0) uniform sampler2D diffuseMap;
layout (binding = 1) uniform sampler2D normalMap;
layout (binding = 2) uniform sampler2D heightMap;
uniform vec3 light_pos;
uniform vec3 viewPosition;
uniform float heightScale;
vec2 ParallaxMapping (vec2 texCoord, vec3 viewDir)
{
if ( viewDir.z >= 0.0 )
return texCoord;
float minLayers = 1;
float maxLayers = 32;
float numLayers = mix(maxLayers, minLayers, abs(dot(vec3(0.0, 0.0, 1.0), viewDir)));
float layerDepth = 1.0 / numLayers;
float currentLayerDepth = 0;
vec2 P = viewDir.xy / viewDir.z * heightScale;
vec2 deltaTexCoords = P / numLayers;
vec2 currentTexCoords = texCoord;
float currentDepthMapValue = texture(heightMap, currentTexCoords).r;
while (currentLayerDepth < currentDepthMapValue)
{
currentTexCoords -= deltaTexCoords;
currentDepthMapValue = texture(heightMap, currentTexCoords).r;
currentLayerDepth += layerDepth;
}
vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
float afterDepth = currentDepthMapValue - currentLayerDepth;
float beforeDepth = texture(heightMap, prevTexCoords).r - currentLayerDepth + layerDepth;
float weight = afterDepth / (afterDepth - beforeDepth);
vec2 finalTexCoords = prevTexCoords * weight + currentTexCoords * (1.0 - weight);
return finalTexCoords;
}
// FUNCTION I ADDED FOR SHADOW CALCULATION
float ShadowCalc(vec2 texCoord, vec3 lightDir)
{
if ( lightDir.z >= 0.0 )
return 0.0;
float minLayers = 1;
float maxLayers = 32;
float numLayers = mix(maxLayers, minLayers, abs(dot(vec3(0.0, 0.0, 1.0), lightDir)));
vec2 currentTexCoords = texCoord;
float currentDepthMapValue = texture(heightMap, currentTexCoords).r;
float currentLayerDepth = currentDepthMapValue;
float layerDepth = 1.0 / numLayers;
vec2 P = lightDir.xy / lightDir.z * heightScale;
vec2 deltaTexCoords = P / numLayers;
while (currentLayerDepth <= currentDepthMapValue && currentLayerDepth > 0.0)
{
currentTexCoords += deltaTexCoords;
currentDepthMapValue = texture(heightMap, currentTexCoords).r;
currentLayerDepth -= layerDepth;
}
float r = currentLayerDepth > currentDepthMapValue ? 0.0 : 1.0;
return r;
}
void main()
{
mat3 TBN_norm = transpose(mat3(normalize(world_tangent),
normalize(cross(world_normal, world_tangent)),
normalize(world_normal)));
vec3 viewDir = TBN_norm * normalize(o_worldPos - viewPosition);
vec3 lightDir = TBN_norm * normalize(o_worldPos - light_pos);
vec2 currentTex = ParallaxMapping(o_texCoord, viewDir);
if (currentTex.x > 1.0 || currentTex.y > 1.0 || currentTex.x < 0.0 || currentTex.y < 0.0)
discard;
vec3 normal = texture(normalMap, currentTex).rgb;
normal = normalize(normal * 2.0 - 1.0);
float dc = max(0.0, dot(-lightDir, normal));
// STUFF I ADDED FOR SHADOWS
float shadow = dc > 0.0 ? ShadowCalc(currentTex, lightDir) : 0.0;
fragColor = shadow * dc * texture(diffuseMap, currentTex);
}
"""
__program = None
__vao = None
__vbo = None
__no_vert = 0
def __init__(self, w, h):
self.__vp_size = [w, h]
glutInit()
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutInitWindowSize(self.__vp_size[0], self.__vp_size[1])
__glut_wnd = glutCreateWindow(self.__caption)
texObj = ReadTexture('woodtiles.jpg', 0)
nmTexObj = ReadTexture('toy_box_normal.png', 1);
hmTexObj = ReadTexture('toy_box_disp.png', 2);
print(texObj, nmTexObj, hmTexObj)
self.__program = compileProgram(
compileShader( self.__glsl_vert, GL_VERTEX_SHADER ),
compileShader( self.__glsl_parallax_frag, GL_FRAGMENT_SHADER ),
)
self.___attrib = { a : glGetAttribLocation (self.__program, a) for a in ['a_pos', 'a_nv', 'a_col'] }
print(self.___attrib)
self.___uniform = { u : glGetUniformLocation (self.__program, u) for u in ['u_model', 'u_view', 'u_proj', 'viewPosition', 'light_pos', 'heightScale'] }
print(self.___uniform)
v = [ -1,-1,1, 1,-1,1, 1,1,1, -1,1,1, -1,-1,-1, 1,-1,-1, 1,1,-1, -1,1,-1 ]
c = [ 1.0, 0.0, 0.0, 1.0, 0.5, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 ]
nv = [ 0,0,1, 1,0,0, 0,0,-1, -1,0,0, 0,1,0, 0,-1,0 ]
tv = [ 1,0,0, 0,0,-1, -1,0,0, 0,0,1, 1,0,0, -1,0,0 ]
t = [ 0, 0, 1, 0, 1, 1, 0, 1]
e = [ 0,1,2,3, 1,5,6,2, 5,4,7,6, 4,0,3,7, 3,2,6,7, 1,0,4,5 ]
attr_array = []
index_array = []
for si in range(6):
temp = []
for vi in range(6):
ci = [0, 1, 2, 0, 2, 3][vi]
i = si*4+ci
temp.append(i)
index_array.append(temp)
for ci in range(4):
i = si*4+ci
attr_array.extend( [ v[e[i]*3], v[e[i]*3+1], v[e[i]*3+2] ] )
attr_array.extend( [ nv[si*3], nv[si*3+1], nv[si*3+2] ] )
attr_array.extend( [ tv[si*3], tv[si*3+1], tv[si*3+2] ] )
attr_array.extend( [ t[ci*2], t[ci*2+1] ] )
attr_array.extend( [ c[si*3], c[si*3+1], c[si*3+2], 1 ] );
self.__no_vert = len(attr_array) // 10
vertex_attributes = np.array(attr_array, dtype=np.float32)
#vertex_attributes = (ctypes.c_float * len(attr_array))(*attr_array)
self.n_indices = len(index_array)*6
#indices = (ctypes.c_uint32 * self.n_indices)(*index_array)
indices = np.array(index_array, dtype=np.uint32)
self.__vbo = glGenBuffers(2)
glBindBuffer(GL_ARRAY_BUFFER, self.__vbo[0])
glBufferData(GL_ARRAY_BUFFER, vertex_attributes, GL_STATIC_DRAW)
self.__vao = glGenVertexArrays(1)
glBindVertexArray(self.__vao)
self.__ibo = glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.__ibo)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW)
glVertexAttribPointer(0, 3, GL_FLOAT, False, 15*vertex_attributes.itemsize, None)
glEnableVertexAttribArray(0)
glVertexAttribPointer(1, 3, GL_FLOAT, False, 15*vertex_attributes.itemsize, c_void_p(3*4))
glEnableVertexAttribArray(1)
glVertexAttribPointer(2, 3, GL_FLOAT, False, 15*vertex_attributes.itemsize, c_void_p(6*4))
glEnableVertexAttribArray(2)
glVertexAttribPointer(3, 2, GL_FLOAT, False, 15*vertex_attributes.itemsize, c_void_p(9*4))
glEnableVertexAttribArray(3)
glVertexAttribPointer(4, 4, GL_FLOAT, False, 15*vertex_attributes.itemsize, c_void_p(11*4))
glEnableVertexAttribArray(4)
glEnable(GL_DEPTH_TEST)
glUseProgram(self.__program)
glutReshapeFunc(self.__reshape)
glutDisplayFunc(self.__mainloop)
def run(self):
self.__starttime = 0
self.__starttime = self.elapsed_ms()
glutMainLoop()
def elapsed_ms(self):
return glutGet(GLUT_ELAPSED_TIME) - self.__starttime
def __reshape(self, w, h):
self.__vp_valid = False
def __mainloop(self):
if not self.__vp_valid:
self.__vp_size = [glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT)]
self.__vp_valid = True
glViewport(0, 0, self.__vp_size[0], self.__vp_size[1])
proj = glm.mat4(1)
view = glm.mat4(1)
model = glm.mat4(1)
aspect = self.__vp_size[0]/self.__vp_size[1]
proj = glm.perspective(glm.radians(75.0), aspect, 0.1, 10.0)
heightScale = 0.1
light_pos = glm.vec3(-30,-10,0)
view_pos = glm.vec3(0,-3,0)
view = glm.lookAt(view_pos, glm.vec3(0, 0, 0), glm.vec3(0,0,1))
speed = 0.2
angle1 = self.elapsed_ms() * speed * math.pi * 2 / 5000.0
angle2 = self.elapsed_ms() * speed * math.pi * 2 / 7333.0
#model = glm.rotate(model, angle1, glm.vec3(1, 0, 0))
#model = glm.rotate(model, angle2, glm.vec3(0, 1, 0))
model = glm.rotate(model, angle2, glm.vec3(0, 0, 1))
angle = 82 + 10 * math.sin(self.elapsed_ms() / 250.0)
#model = glm.rotate(model, glm.radians(angle), glm.vec3(0, 0, 1))
model = glm.rotate(model, glm.radians(10), glm.vec3(0, 1, 0))
glUniformMatrix4fv(self.___uniform['u_proj'], 1, GL_FALSE, glm.value_ptr(proj) )
glUniformMatrix4fv(self.___uniform['u_view'], 1, GL_FALSE, glm.value_ptr(view) )
glUniformMatrix4fv(self.___uniform['u_model'], 1, GL_FALSE, glm.value_ptr(model) )
glUniform3fv(self.___uniform['viewPosition'], 1, glm.value_ptr(view_pos) )
glUniform3fv(self.___uniform['light_pos'], 1, glm.value_ptr(light_pos) )
glUniform1f(self.___uniform['heightScale'], heightScale )
glClearColor(0.2, 0.3, 0.3, 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
#glDrawArrays(GL_TRIANGLES, 0, self.__no_vert)
glDrawElements(GL_TRIANGLES, self.n_indices, GL_UNSIGNED_INT, None)
glutSwapBuffers()
glutPostRedisplay()
window = MyWindow(800, 600)
window.run()