/
core.py
583 lines (376 loc) · 13.4 KB
/
core.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
"""Core graphics module."""
from __future__ import print_function
from OpenGL.GL import GLfloat
from OpenGL.GL import glBindBuffer
from OpenGL.GL import glBufferData
from OpenGL.GL import glEnableVertexAttribArray
from OpenGL.GL import glDisableVertexAttribArray
from OpenGL.GL import glVertexAttribPointer
from OpenGL.GL import glDrawArrays
from OpenGL.GL import glPolygonMode
from OpenGL.GL import glEnable
from OpenGL.GL import glDisable
from OpenGL.GL import GL_ARRAY_BUFFER
from OpenGL.GL import GL_STATIC_DRAW
from OpenGL.GL import GL_FLOAT
from OpenGL.GL import GL_FALSE
from OpenGL.GL import GL_TRIANGLES
from OpenGL.GL import GL_CULL_FACE
from OpenGL.GL import GL_FRONT_AND_BACK
from OpenGL.GL import GL_LINE
from OpenGL.GL import GL_FILL
from OpenGL.GL import GL_POINTS
from multiprocessing import Lock
from multiprocessing.sharedctypes import Array
from multiprocessing.sharedctypes import copy
from ctypes import c_float
import collections
import multiprocessing as mp
import time
import logging
import os
# import pprint
import random
import graphics
# from decorators import print_time
# from decorators import print_pid
log = logging.getLogger(__name__)
# mpl = mp.log_to_stderr(5)
# multiprocessing infrastructure
####################################
lock = Lock()
size = 250000
shared_array = Array(c_float, size, lock=lock)
def long_func(chunk_data):
print("{}, PID: {} ({})".format(
"long_func", os.getpid(), os.getppid()))
time.sleep(random.randint(1, 9))
positions = generate_vbo_blocks(chunk_data)
return chunk_data, positions
def gl_vertexes_mp(chunk_id, chunk_vertexes):
"""MP wrapper."""
shared_array.get_lock().acquire()
raw_array = shared_array.get_obj()
index = 0
for value in chunk_vertexes:
raw_array[index] = value
index += 1
return chunk_id
def generate_gl_vertexes(chunk_vertexes):
"""Generate vertex data.
Args:
chunk_vertexes (list): vertexes of chunk
"""
gl_vertexes = (GLfloat * len(chunk_vertexes))(*chunk_vertexes)
return gl_vertexes
def vertexes_mp(chunk_id, positions):
"""MP wrapper."""
vertexes = generate_vertexes(positions)
return chunk_id, vertexes
def generate_vertexes(positions):
vertexes = []
for position in positions:
vertexes.extend(graphics.GraphicBlock.get_vertexes(position))
return vertexes
def positions_mp(chunk_data):
"""MP wrapper."""
positions = generate_vbo_blocks(chunk_data)
return chunk_data, positions
def generate_vbo_blocks(chunk_data):
"""Generate blocks data.
Args:
chunk_data (Chunk): chunk data
"""
blocks_positions = []
for rel_pos, block in chunk_data.blocks.items():
block_position = (
chunk_data.position.x + rel_pos[0],
rel_pos[1],
chunk_data.position.z + rel_pos[2]
)
if block is not None:
blocks_positions.append(block_position)
return blocks_positions
# multiprocessing end
######################################
def generate_vbo(chunk_data):
"""Generate VBO object.
Args:
chunk_data (Chunk): chunk data
Return:
VboData: VBO data object
"""
positions = generate_vbo_blocks(chunk_data)
chunk_vertexes = generate_vertexes(positions)
gl_vertexes = generate_gl_vertexes(chunk_vertexes)
chunk_vbo = graphics.VboData(chunk_data.chunk_id)
chunk_vbo.vertexes_count = len(gl_vertexes)
glBindBuffer(GL_ARRAY_BUFFER, chunk_vbo.name)
glBufferData(
GL_ARRAY_BUFFER,
len(gl_vertexes) * 4,
gl_vertexes,
GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, 0)
return chunk_vbo
class VboCreator(object):
"""Create VBO data object."""
def __init__(self, vbo_list, workers=2):
self.orig_list = vbo_list
self.active_tasks = []
self.active_subtasks = {}
self.prepared_vbos = {}
self.ready_vbos = collections.deque()
self.vbo_parts = {
# "vbo_id": {
# "parts1": (1, 2,),
# "parts2": ("a", "b", "d"),
# "parts3": None,
# },
}
self.pool = mp.Pool(workers)
def add_task(self, chunk_id):
log.debug("New VboCreator task: {}".format(chunk_id))
self.active_tasks.append(chunk_id)
def task_exists(self, chunk_id):
if chunk_id in self.active_tasks:
return True
else:
return False
def create_subtasks(self, vbo_id):
self.active_subtasks[vbo_id] = {
"positions": None,
"vertexes": None,
"gl_vertexes": None,
}
def set_subtask_state(self, vbo_id, subtask, state):
self.active_subtasks[vbo_id][subtask] = state
def create_parts(self, vbo_id):
self.vbo_parts[vbo_id] = {
"positions": None,
"vertexes": None,
"gl_vertexes": None,
}
def delete_parts(self, vbo_id):
del self.vbo_parts[vbo_id]
def add_parts(self, vbo_id, section, data):
self.vbo_parts[vbo_id][section] = data
def add_ready_vbo(self, uid):
self.ready_vbos.append(uid)
def check_parts(self):
# TODO: refactoring
log.debug("Active VboCreator tasks: {}".format(len(self.active_tasks)))
for vbo_id, value in self.vbo_parts.items():
all_parts = []
for part_name, data in value.items():
if data:
if (part_name == "positions"
and not self.active_subtasks[vbo_id]["vertexes"]):
positions = data
self.pool.apply_async(
vertexes_mp,
args=(vbo_id, positions),
callback=self.vertexes_done
)
self.set_subtask_state(vbo_id, "vertexes", "running")
all_parts.append(False)
elif (part_name == "vertexes"
and not self.active_subtasks[vbo_id]["gl_vertexes"]): # NOQA
vertexes = data
self.pool.apply_async(
gl_vertexes_mp,
args=(vbo_id, vertexes),
callback=self.gl_vertexes_done
)
self.set_subtask_state(vbo_id, "gl_vertexes", "running") # NOQA
all_parts.append(False)
else:
all_parts.append(True)
else:
all_parts.append(False)
# print(all_parts)
if all(all_parts):
if vbo_id not in self.ready_vbos:
self.add_ready_vbo(vbo_id)
def create(self, chunk_data):
chunk_id = chunk_data.chunk_id
if self.task_exists(chunk_id):
return
self.add_task(chunk_id)
self.create_subtasks(chunk_id)
self.create_parts(chunk_id)
self.pool.apply_async(
positions_mp,
args=(chunk_data,),
callback=self.positions_done
)
self.set_subtask_state(chunk_id, "positions", "running")
def build_ready_vbos(self):
if len(self.ready_vbos) > 0:
new_vbo = self.ready_vbos.popleft()
self.build_vbo(
new_vbo,
self.vbo_parts[new_vbo]["vertexes"],
self.vbo_parts[new_vbo]["gl_vertexes"],
)
self.delete_parts(new_vbo)
log.debug("VboCreator task {} done.".format(new_vbo))
def build_vbo(self, uid, vertexes, gl_vertexes):
chunk_vertexes = vertexes
gl_vertexes = gl_vertexes
vertexes_count = len(chunk_vertexes)
chunk_vbo = graphics.VboData(uid)
chunk_vbo.vertexes_count = vertexes_count
glBindBuffer(GL_ARRAY_BUFFER, chunk_vbo.name)
glBufferData(
GL_ARRAY_BUFFER,
vertexes_count * 4,
gl_vertexes,
GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, 0)
self.active_tasks.remove(uid)
self.orig_list.append(chunk_vbo)
def wait_for_procs(self):
self.pool.close()
self.pool.join()
def positions_done(self, arg):
log.debug("positions done")
chunk_data = arg[0]
positions = arg[1]
self.add_parts(chunk_data.chunk_id, "positions", positions)
self.set_subtask_state(chunk_data.chunk_id, "positions", "done")
def vertexes_done(self, arg):
log.debug("vertexes done")
uid = arg[0]
vertexes = arg[1]
self.add_parts(uid, "vertexes", vertexes)
self.set_subtask_state(uid, "vertexes", "done")
def gl_vertexes_done(self, arg):
log.debug("gl_vertexes done")
uid = arg
c_array = copy(shared_array.get_obj())
shared_array.get_lock().release()
self.add_parts(uid, "gl_vertexes", c_array)
self.set_subtask_state(uid, "gl_vertexes", "done")
def update(self):
self.check_parts()
self.build_ready_vbos()
class Renderer(object):
"""Render world."""
def __init__(self, world, configuration=None):
log.debug("Renderer initializing...")
self.world = world
self.visibility = 22
self.chunk_gen_distance = self.visibility * 1.2
# VboData list for vertex buffer objects
self.vbos = []
self.vbo_creator = VboCreator(self.vbos, workers=2)
# external configuration
self.configuration = configuration.get_values()
self.update_configuration()
log.debug("Renderer initialized.")
# def print_info(self, point):
# """Development info method."""
#
# # print(self.world.find_necessary_chunks(point, 10))
# pass
def update_configuration(self):
"""Update configuration of renderer."""
if not self.configuration:
return
self.visibility = int(self.configuration["visibility"])
def ground_collision(self, point):
"""Return ground collision value as boolean.
Args:
point (Point): Point for collision check.
Return:
boolean
"""
return self.world.collision(point)
def prepare_new_chunks(self, position):
"""Generate new chunks around the position.
Args:
position (Point): Centre.
"""
self.world.generate_chunks(position, self.chunk_gen_distance)
def print_visibility(self):
"""Print visibility for all chunks in world."""
for position, chunk in self.world.chunks.items():
print(position, chunk.visible)
def check_visibility(self, point):
"""Check and set visibility for chunks.
Args:
point (Point): Centre.
"""
self.world.set_visibility(point, self.visibility)
def set_visibility(self):
"""Set visibility for VBOs according to world data."""
for position, chunk in self.world.chunks.items():
for vbo in self.vbos:
if vbo.chunk_id == chunk.chunk_id:
vbo.render = chunk.visible
def data_update(self):
self.vbo_creator.update()
self.world.update_chunks()
def create_vbos(self):
"""Create necessary VBOs."""
for position, chunk in self.world.chunks.items():
if self.vbo_exists(chunk.chunk_id):
pass
else:
self.vbo_creator.create(chunk)
def vbo_exists(self, chunk_id):
"""Check VBO existence for the chunk ID.
Args:
chunk_id (str): Chunk ID.
Return:
boolean: True if VBO data exists.
"""
for vbo in self.vbos:
if vbo.chunk_id == chunk_id:
return True
return False
def prepare_world(self):
"""Fill buffer objects with data."""
block_counter = 0
for pos, chunk in self.world.chunks.items():
chunk_vbo = generate_vbo(chunk)
self.vbos.append(chunk_vbo)
print("=" * 40)
print("World info")
print("Chunks: {}".format(len(self.vbos)))
print("Blocks: {}".format(
len(self.vbos) *
self.world.chunk_type.size *
self.world.chunk_type.size *
self.world.chunk_type.height))
print("Rendered blocks: {}".format(block_counter))
print("+" * 40)
print("")
def render(self):
"""Render game world."""
for vbo in self.vbos:
if vbo.render:
glBindBuffer(GL_ARRAY_BUFFER, vbo.name)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
glDrawArrays(
GL_TRIANGLES,
0,
vbo.vertexes_count)
glDisableVertexAttribArray(0)
glBindBuffer(GL_ARRAY_BUFFER, 0)
@staticmethod
def set_lines():
"""Set OpenGL lines rendering."""
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
glDisable(GL_CULL_FACE)
@staticmethod
def set_fill():
"""Set OpenGL fill rendering."""
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glEnable(GL_CULL_FACE)
@staticmethod
def set_points():
"""Set OpenGL points rendering."""
glPolygonMode(GL_FRONT_AND_BACK, GL_POINTS)