File size: 5,384 Bytes
f92c150 |
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 |
import ctypes
import math
import pyglet.gl as gl
import subchunk
CHUNK_WIDTH = 16
CHUNK_HEIGHT = 128
CHUNK_LENGTH = 16
class Chunk:
def __init__(self, world, chunk_position):
self.world = world
self.modified = False
self.chunk_position = chunk_position
self.position = (
self.chunk_position[0] * CHUNK_WIDTH,
self.chunk_position[1] * CHUNK_HEIGHT,
self.chunk_position[2] * CHUNK_LENGTH)
self.blocks = [[[0
for z in range(CHUNK_LENGTH)]
for y in range(CHUNK_HEIGHT)]
for x in range(CHUNK_WIDTH )]
self.subchunks = {}
for x in range(int(CHUNK_WIDTH / subchunk.SUBCHUNK_WIDTH)):
for y in range(int(CHUNK_HEIGHT / subchunk.SUBCHUNK_HEIGHT)):
for z in range(int(CHUNK_LENGTH / subchunk.SUBCHUNK_LENGTH)):
self.subchunks[(x, y, z)] = subchunk.Subchunk(self, (x, y, z))
# mesh variables
self.mesh_vertex_positions = []
self.mesh_tex_coords = []
self.mesh_shading_values = []
self.mesh_index_counter = 0
self.mesh_indices = []
# create VAO and VBO's
self.vao = gl.GLuint(0)
gl.glGenVertexArrays(1, self.vao)
gl.glBindVertexArray(self.vao)
self.vertex_position_vbo = gl.GLuint(0)
gl.glGenBuffers(1, self.vertex_position_vbo)
self.tex_coord_vbo = gl.GLuint(0)
gl.glGenBuffers(1, self.tex_coord_vbo)
self.shading_values_vbo = gl.GLuint(0)
gl.glGenBuffers(1, self.shading_values_vbo)
self.ibo = gl.GLuint(0)
gl.glGenBuffers(1, self.ibo)
def update_subchunk_meshes(self):
for subchunk_position in self.subchunks:
subchunk = self.subchunks[subchunk_position]
subchunk.update_mesh()
def update_at_position(self, position):
x, y, z = position
lx = int(x % subchunk.SUBCHUNK_WIDTH )
ly = int(y % subchunk.SUBCHUNK_HEIGHT)
lz = int(z % subchunk.SUBCHUNK_LENGTH)
clx, cly, clz = self.world.get_local_position(position)
sx = math.floor(clx / subchunk.SUBCHUNK_WIDTH)
sy = math.floor(cly / subchunk.SUBCHUNK_HEIGHT)
sz = math.floor(clz / subchunk.SUBCHUNK_LENGTH)
self.subchunks[(sx, sy, sz)].update_mesh()
def try_update_subchunk_mesh(subchunk_position):
if subchunk_position in self.subchunks:
self.subchunks[subchunk_position].update_mesh()
if lx == subchunk.SUBCHUNK_WIDTH - 1: try_update_subchunk_mesh((sx + 1, sy, sz))
if lx == 0: try_update_subchunk_mesh((sx - 1, sy, sz))
if ly == subchunk.SUBCHUNK_HEIGHT - 1: try_update_subchunk_mesh((sx, sy + 1, sz))
if ly == 0: try_update_subchunk_mesh((sx, sy - 1, sz))
if lz == subchunk.SUBCHUNK_LENGTH - 1: try_update_subchunk_mesh((sx, sy, sz + 1))
if lz == 0: try_update_subchunk_mesh((sx, sy, sz - 1))
def update_mesh(self):
# combine all the small subchunk meshes into one big chunk mesh
self.mesh_vertex_positions = []
self.mesh_tex_coords = []
self.mesh_shading_values = []
self.mesh_index_counter = 0
self.mesh_indices = []
for subchunk_position in self.subchunks:
subchunk = self.subchunks[subchunk_position]
self.mesh_vertex_positions.extend(subchunk.mesh_vertex_positions)
self.mesh_tex_coords.extend(subchunk.mesh_tex_coords)
self.mesh_shading_values.extend(subchunk.mesh_shading_values)
mesh_indices = [index + self.mesh_index_counter for index in subchunk.mesh_indices]
self.mesh_indices.extend(mesh_indices)
self.mesh_index_counter += subchunk.mesh_index_counter
# send the full mesh data to the GPU and free the memory used client-side (we don't need it anymore)
# don't forget to save the length of 'self.mesh_indices' before freeing
self.mesh_indices_length = len(self.mesh_indices)
self.send_mesh_data_to_gpu()
del self.mesh_vertex_positions
del self.mesh_tex_coords
del self.mesh_shading_values
del self.mesh_indices
def send_mesh_data_to_gpu(self): # pass mesh data to gpu
if not self.mesh_index_counter:
return
gl.glBindVertexArray(self.vao)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vertex_position_vbo)
gl.glBufferData(
gl.GL_ARRAY_BUFFER,
ctypes.sizeof(gl.GLfloat * len(self.mesh_vertex_positions)),
(gl.GLfloat * len(self.mesh_vertex_positions)) (*self.mesh_vertex_positions),
gl.GL_STATIC_DRAW)
gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 0, 0)
gl.glEnableVertexAttribArray(0)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.tex_coord_vbo)
gl.glBufferData(
gl.GL_ARRAY_BUFFER,
ctypes.sizeof(gl.GLfloat * len(self.mesh_tex_coords)),
(gl.GLfloat * len(self.mesh_tex_coords)) (*self.mesh_tex_coords),
gl.GL_STATIC_DRAW)
gl.glVertexAttribPointer(1, 3, gl.GL_FLOAT, gl.GL_FALSE, 0, 0)
gl.glEnableVertexAttribArray(1)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.shading_values_vbo)
gl.glBufferData(
gl.GL_ARRAY_BUFFER,
ctypes.sizeof(gl.GLfloat * len(self.mesh_shading_values)),
(gl.GLfloat * len(self.mesh_shading_values)) (*self.mesh_shading_values),
gl.GL_STATIC_DRAW)
gl.glVertexAttribPointer(2, 1, gl.GL_FLOAT, gl.GL_FALSE, 0, 0)
gl.glEnableVertexAttribArray(2)
gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self.ibo)
gl.glBufferData(
gl.GL_ELEMENT_ARRAY_BUFFER,
ctypes.sizeof(gl.GLuint * self.mesh_indices_length),
(gl.GLuint * self.mesh_indices_length) (*self.mesh_indices),
gl.GL_STATIC_DRAW)
def draw(self):
if not self.mesh_index_counter:
return
gl.glBindVertexArray(self.vao)
gl.glDrawElements(
gl.GL_TRIANGLES,
self.mesh_indices_length,
gl.GL_UNSIGNED_INT,
None) |