-
Notifications
You must be signed in to change notification settings - Fork 18
/
protruded_displ_mapping_geo_tbn.py
180 lines (152 loc) · 6.67 KB
/
protruded_displ_mapping_geo_tbn.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
# Parallax Occlusion Mapping in GLSL [http://sunandblackcat.com/tipFullView.php?topicid=28]
# Followup: Normal Mapping Without Precomputed Tangents [http://www.thetenthplanet.de/archives/1180]
import os
import sys
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir, os.pardir))
os.chdir(os.path.dirname(os.path.abspath(__file__)))
import numpy
from PIL import Image
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
# MyLibOGL import
from MyLibOGL.math import mat
from MyLibOGL.ogl import shader
from MyLibOGL.ogl import vertex
from MyLibOGL.glut import window
def ReadTexture(filename):
# 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)
return image
def CreateTexture(image, textureUnit, internal_channels, linear, repeat):
imageData = numpy.array(list(image.getdata()), numpy.uint8)
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)
internal_formats = [GL_R8, GL_RG8, GL_RGB8, GL_RGBA8]
glTexImage2D(GL_TEXTURE_2D, 0, internal_formats[internal_channels], image.size[0], image.size[1],0, GL_RGB, GL_UNSIGNED_BYTE, imageData)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR if linear else GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR if linear else GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT if repeat else GL_CLAMP_TO_EDGE)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT if repeat else GL_CLAMP_TO_EDGE)
image.close()
return textureObj
def CreateTextureFromFile(filename, textureUnit, repeat):
image = ReadTexture(filename)
textureObj = CreateTexture(image, textureUnit, 3, True, repeat)
return textureObj
def CreateHeightMapFromFile(filename, textureUnit, repeat):
image = ReadTexture(filename)
textureObj = CreateTexture(image, textureUnit, 1, True, repeat)
return textureObj
def CreateConeMapFromFile(filename, textureUnit, repeat):
image = ReadTexture(filename)
textureObj = CreateTexture(image, textureUnit, 2, False, repeat)
return textureObj
class MyWindow(window.CameraWindow):
def __init__( self, cx, cy, multisample=True ):
super().__init__(cx, cy, multisample)
def _InitCamera_(self):
camera = super()._InitCamera_()
#camera.fov_y = 120
camera.pos = (0, -3.0, 0)
return camera
# draw event
def OnDraw(self):
# set up projection matrix
prjMat = self.Perspective()
# set up view matrix
viewMat = self.LookAt()
#glEnable(GL_CULL_FACE) # TODO $$$
glFrontFace(GL_CCW) # GL_CCW, GL_CW
#glCullFace(GL_BACK) # GL_FRONT GL_BACK, GL_FRONT_AND_BACK
glCullFace(GL_FRONT)
# set up attributes and shader program
glEnable( GL_DEPTH_TEST )
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
progDraw.Use()
modelMat = mat.IdentityMat44()
modelMat = self.AutoModelMatrix()
#modelMat = mat.RotateX( modelMat, 60.0*math.pi/180.0 )
#modelMat = mat.RotateY( modelMat, -10.0*math.pi/180.0 )
clip = 0.4
progDraw.SetUniforms( {
b"u_projectionMat44" : self.Perspective(),
b"u_viewMat44" : self.LookAt(),
b"u_modelMat44" : modelMat,
b"u_lightDir" : [-1.0, -0.5, -2.0],
b"u_ambient" : 0.2,
b"u_diffuse" : 0.8,
b"u_specular" : 0.8,
b"u_shininess" : 10.0,
b"u_texture" : 0,
b"u_displacement_map" : 1,
b"u_normal_map" : 2,
b"u_displacement_scale" : 0.25,
b"u_parallax_quality" : [0.5, 1.0],
b"u_clipPlane" : [-1.0, 1.0, -0.2, clip * 1.7321]
} )
# draw object
cubeVAO.DrawIB( GL_TRIANGLES )
def AddToBuffer( buffer, data, count=1 ):
for inx_c in range(0, count):
#buffer.extend( data )
buffer += data
#for inx_s in range(0, len(data)): buffer.append( data[inx_s] )
# create window
wnd = MyWindow( 800, 600, True )
# define cube vertex array opject
cubePts = [
(-1.0, -1.0, 1.0), ( 1.0, -1.0, 1.0), ( 1.0, 1.0, 1.0), (-1.0, 1.0, 1.0),
(-1.0, -1.0, -1.0), ( 1.0, -1.0, -1.0), ( 1.0, 1.0, -1.0), (-1.0, 1.0, -1.0) ]
cubeCol = [ [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] ]
cubeHlpInx = [ 0, 1, 2, 3, 1, 5, 6, 2, 5, 4, 7, 6, 4, 0, 3, 7, 3, 2, 6, 7, 1, 0, 4, 5 ]
cubePosData = []
for inx in cubeHlpInx: AddToBuffer( cubePosData, cubePts[inx] )
cubeNVData = []
for inx_nv in range(0, len(cubeHlpInx) // 4):
nv = [0.0, 0.0, 0.0]
for inx_p in range(0, 4):
for inx_s in range(0, 3): nv[inx_s] += cubePts[ cubeHlpInx[inx_nv*4 + inx_p] ][inx_s]
AddToBuffer( cubeNVData, nv, 4 )
cubeColData = []
for inx_col in range(0, 6):
AddToBuffer( cubeColData, cubeCol[inx_col % len(cubeCol)], 4 )
cubeUVData = []
for inx in range(0, 6):
cubeUVData += [0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0]
cubeIndices = []
for inx in range(0, 6):
for inx_s in [0, 1, 2, 0, 2, 3]: cubeIndices.append( inx * 4 + inx_s )
cubeVAO = vertex.MeshBuffer()
cubeVAO.DefineVA( [
( [ (0, 3, 0) ], 0, cubePosData ),
( [ (1, 3, 0) ], 0, cubeNVData ),
( [ (2, 3, 0) ], 0, cubeColData ),
( [ (3, 2, 0) ], 0, cubeUVData ),
] )
cubeVAO.DefineIB( cubeIndices )
# load, compile and link shader
progDraw = shader.ShaderProgram(
[ ('resource/shader/displacement_map.vert', GL_VERTEX_SHADER),
('resource/shader/displacement_map.geom', GL_GEOMETRY_SHADER),
('resource/shader/displacement_map.frag', GL_FRAGMENT_SHADER) ] )
#('resource/shader/displacement_map_debug.frag', GL_FRAGMENT_SHADER) ] )
#texture objects
textureObj = CreateTextureFromFile('../../../../resource/texture/example_1_texture.bmp', 0, False)
displacementmapObj = CreateConeMapFromFile('../../../../resource/texture/example_1_conemap.bmp', 1, False)
# start main loop
wnd.Run()
# clean up
del cubeVAO