-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboat.py
296 lines (227 loc) · 10.3 KB
/
boat.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
# Classes used to load and render the boat.
# Author: Joelene Hales
from OpenGL.GL import *
import numpy as np
from helpers import *
class TextureMesh():
""" Class representing a textured triangle mesh.
Vertex attributes and mesh faces are loaded from a PLY file. The mesh's
texture is loaded from a bitmap image file. This data is contained in a
vertex array object and restored to render the mesh. The shader program
includes a geometry shader which shifts the vertex positions up and down.
This creates the effect of the boat bobbing up and down in the water.
Attributes
----------
program_ID : int
Integer ID of shader program.
VAO : int
Integer ID of the vertex array object used to restore state and render
the mesh.
texture_ID : int
Integer ID of generated texture object.
MVP_uniform : int
Integer handle for model view projection matrix uniform variable in
shader program.
texture_uniform : int
Integer handle for the mesh's texture uniform variable in shader program.
time_uniform : int
Integer handle for time uniform variable in shader program.
num_indices : int
Number of indices in the mesh.
Methods
-------
draw(MVP, time):
Renders the texture mesh object.
"""
def __init__(self, ply_file, bitmap_file):
""" Loads the textured triangle mesh and initializes the shader programs
to render it.
Vertex attributes and mesh faces are loaded from a PLY file. The mesh's
texture is loaded from a bitmap image file. This data is contained in a
vertex array object and restored to render the mesh. The shader program
includes a geometry shader which shifts the vertex positions up and
down. This creates the effect of the boat bobbing up and down in the
water.
Parameters
----------
ply_file : str
Filepath to a PLY file.
bitmap_file : str
Filepath to a bitmap image file.
"""
# Read mesh data from PLY file
positions,normals,_,texture_coordinates,indices = read_ply(ply_file)
self.num_indices = len(indices)
# Import shader codes from file
shader_directory = "Shaders/"
vertex_shader_code = open(shader_directory+"boat.vs",
"r").read()
geometry_shader_code = open(shader_directory+"boat.gs", "r").read()
fragment_shader_code = open(shader_directory+"boat.fs", "r").read()
# Create vertex and fragment shaders
vertex_shader_ID = glCreateShader(GL_VERTEX_SHADER)
geometry_shader_ID = glCreateShader(GL_GEOMETRY_SHADER)
fragment_shader_ID = glCreateShader(GL_FRAGMENT_SHADER)
# Set shader source code
glShaderSource(vertex_shader_ID, vertex_shader_code)
glShaderSource(geometry_shader_ID, geometry_shader_code)
glShaderSource(fragment_shader_ID, fragment_shader_code)
# Compile shaders
glCompileShader(vertex_shader_ID)
glCompileShader(geometry_shader_ID)
glCompileShader(fragment_shader_ID)
# Check for compilation errors
if not(glGetShaderiv(vertex_shader_ID, GL_COMPILE_STATUS)):
raise RuntimeError(glGetShaderInfoLog(vertex_shader_ID))
if not(glGetShaderiv(geometry_shader_ID, GL_COMPILE_STATUS)):
raise RuntimeError(glGetShaderInfoLog(geometry_shader_ID))
if not(glGetShaderiv(fragment_shader_ID, GL_COMPILE_STATUS)):
raise RuntimeError(glGetShaderInfoLog(fragment_shader_ID))
# Attach shaders and link shader program
self.program_ID = glCreateProgram()
glAttachShader(self.program_ID, vertex_shader_ID)
glAttachShader(self.program_ID, geometry_shader_ID)
glAttachShader(self.program_ID, fragment_shader_ID)
glLinkProgram(self.program_ID)
# Check for linking error
if not(glGetProgramiv(self.program_ID, GL_LINK_STATUS)):
raise RuntimeError(glGetProgramInfoLog(self.program_ID))
# Unlink shader program
glDetachShader(self.program_ID, vertex_shader_ID)
glDetachShader(self.program_ID, geometry_shader_ID)
glDetachShader(self.program_ID, fragment_shader_ID)
glDeleteShader(vertex_shader_ID)
glDeleteShader(geometry_shader_ID)
glDeleteShader(fragment_shader_ID)
glUseProgram(0)
# Get handle for each uniform variable
self.MVP_uniform = glGetUniformLocation(self.program_ID, "MVP")
self.time_uniform = glGetUniformLocation(self.program_ID, "time")
self.texture_uniform = glGetUniformLocation(self.program_ID, "textureImage")
# Read bitmap images
bitmap_image, texture_width, texture_height = read_bitmap(bitmap_file)
# Generate textures
self.texture_ID = glGenTextures(1)
# Load texture image
glBindTexture(GL_TEXTURE_2D, self.texture_ID)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, texture_width, texture_height, 0, GL_BGRA, GL_UNSIGNED_BYTE, bitmap_image)
glGenerateMipmap(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, 0)
# Create and bind VAO
self.VAO = glGenVertexArrays(1)
glBindVertexArray(self.VAO)
# Create and bind VBO for vertex positions
vertex_VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vertex_VBO)
glBufferData(GL_ARRAY_BUFFER, np.dtype(np.float32).itemsize*len(positions), positions, GL_STATIC_DRAW)
# Set vertex attributes for vertex positions
glEnableVertexAttribArray(0)
glVertexAttribPointer(
0, # Attribute number
3, # Size (Number of components)
GL_FLOAT, # Type
GL_FALSE, # Normalized?
0, # Stride (Byte offset)
None # Offset
)
# Create and bind VBO for texture coordinates
texture_VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, texture_VBO)
glBufferData(GL_ARRAY_BUFFER, np.dtype(np.float32).itemsize*len(texture_coordinates), texture_coordinates, GL_STATIC_DRAW)
# Set vertex attributes for texture coordinates
glEnableVertexAttribArray(1)
glVertexAttribPointer(
1, # Attribute number
2, # Size (Number of components)
GL_FLOAT, # Type
GL_FALSE, # Normalized?
0, # Stride (Byte offset)
None # Offset
)
# Create and bind VBO for vertex normals
normal_VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, normal_VBO)
glBufferData(GL_ARRAY_BUFFER, np.dtype(np.float32).itemsize*len(normals), normals, GL_STATIC_DRAW)
# Set vertex attributes for vertex normals
glEnableVertexAttribArray(0)
glVertexAttribPointer(
2, # Attribute number
3, # Size (Number of components)
GL_FLOAT, # Type
GL_TRUE, # Normalized?
0, # Stride (Byte offset)
None # Offset
)
# Create and bind EBO for face indices
face_EBO = glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, face_EBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, np.dtype(np.uint32).itemsize*self.num_indices,indices, GL_STATIC_DRAW)
glBindVertexArray(0) # Unbind VAO
def draw(self, MVP, time):
""" Renders the texture mesh object.
Parameters
----------
MVP : glm.mat4
Model view projection matrix.
time : float
Time elapsed. Used to calculate the position of the wave at a given
moment in time, to create the effect of the boat bobbing up and down
in the water.
"""
# Enable blending
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
# Bind water texture
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, self.texture_ID)
# Use program and set uniforms
glUseProgram(self.program_ID)
glUniformMatrix4fv(self.MVP_uniform, 1, GL_FALSE, matrix_to_array(MVP, 4))
glUniform1f(self.time_uniform, time)
# Bind VAO to restore captured state
glBindVertexArray(self.VAO)
# Draw triangles
glDrawElements(GL_TRIANGLES, self.num_indices, GL_UNSIGNED_INT, None)
# Unbind VAO and texture, clean up shader program
glBindVertexArray(0)
glBindTexture(GL_TEXTURE_2D, 0)
glUseProgram(0)
glDisable(GL_BLEND)
class Boat():
""" Contains all textured meshes in the boat.
Additional details on how each textured mesh is loaded, stored, and rendered
can be found in the documentation for the TextureMesh class.
Attributes
----------
body : TextureMesh
Textured triangle mesh for the boat's body (hull and mast).
head : TextureMesh
Textured triangle mesh for the boat's head.
eyes : TextureMesh
Textured triangle mesh for the eyes of the boat's head.
Methods
-------
draw(MVP, time):
Renders the boat by rendering the mesh for each individual component of the boat.
"""
def __init__(self):
""" Creates the textured mesh for each component of the boat. """
assets = "Assets/" # Directory containing all assets (PLY files and bitmap images)
# Create textured triangle mesh for each component of the boat
self.body = TextureMesh(assets+"boat.ply", assets+"boat.bmp")
self.head = TextureMesh(assets+"head.ply", assets+"head.bmp")
self.eyes = TextureMesh(assets+"eyes.ply", assets+"eyes.bmp")
def draw(self, MVP, time):
""" Renders the boat by rendering the mesh for each individual component of the boat.
Parameters
----------
MVP : glm.mat4
Model view projection matrix.
time : float
Time elapsed. Used to calculate the position of the wave at a given
moment in time, to create the effect of the boat bobbing up and down
in the water.
"""
self.body.draw(MVP, time)
self.head.draw(MVP, time)
self.eyes.draw(MVP, time)