voxel_engine/world_objects/chunk.py

54 lines
1.4 KiB
Python
Raw Permalink Normal View History

2023-10-19 12:59:58 +02:00
#!/usr/bin/python3
from settings import *
from mesh.chunk import ChunkMesh
class Chunk:
def __init__(self, world, position):
self.app = world.app
self.world = world
self.position = position
self.m_model = self.get_model_matrix()
self.voxels: numpy.array = None
self.mesh: ChunkMesh = None
self.is_empty = True
def get_model_matrix(self):
m_model = glm.translate(glm.mat4(), glm.vec3(self.position) * CHUNK_SIZE)
return m_model
def set_uniform(self):
self.mesh.program["m_model"].write(self.m_model)
def build_mesh(self):
self.mesh = ChunkMesh(self)
def render(self):
self.set_uniform()
self.mesh.render()
def build_voxels(self):
# empty chunk
voxels = numpy.zeros(CHUNK_VOL, dtype="uint8")
# fill chunk
cx, cy, cz = glm.ivec3(self.position) * CHUNK_SIZE
# fill chunk
for x in range(CHUNK_SIZE):
for z in range(CHUNK_SIZE):
wx = x + cx
wz = z + cz
world_height = int(glm.simplex(glm.vec2(wx, wz) * 0.01) * 32 + 32)
local_height = min(world_height - cy, CHUNK_SIZE)
for y in range(local_height):
wy = y + cy
voxels[x + CHUNK_SIZE * z + CHUNK_AREA * y] = wy + 1
if numpy.any(voxels):
self.is_empty = False
return voxels