-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunk.pyx
77 lines (55 loc) · 2.01 KB
/
Chunk.pyx
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
# distutils: language = c++
import cython
cimport numpy as np
import numpy as np
from libc.stdint cimport uint8_t, uint32_t, int64_t
# from libcpp.vector cimport vector
from ChunkCpp cimport ChunkCpp
from constants import *
cdef class Chunk:
# cdef int64_t x, z
# cdef ChunkCpp* chunk
# cdef np.ndarray blocks
# def __cinit__(self):
# pass
# def __init__(self):
# pass
def __init__(self, x, z):
self.x = x
self.z = z
self.chunk = new ChunkCpp(x, z)
self.blocks = np.asarray(<uint8_t[:CHUNK_SIZE_X, :CHUNK_SIZE_Y, :CHUNK_SIZE_Z]>self.chunk.getBuffer())
self.chunk.generate()
def __dealloc__(self):
del self.chunk
@cython.nogil
@staticmethod
cdef Chunk create(ChunkCpp* chunkCpp):
cdef Chunk obj = Chunk.__new__(Chunk)
obj.x = chunkCpp.getX()
obj.z = chunkCpp.getZ()
obj.chunk = chunkCpp
obj.blocks = np.asarray(<uint8_t[:CHUNK_SIZE_X, :CHUNK_SIZE_Y, :CHUNK_SIZE_Z]>chunkCpp.getBuffer())
return obj
def getPos(self):
return (self.x, self.z)
def getBlocks(self):
return self.blocks
def generateMesh(self, nx, px, nz, pz):
self.chunk.generateMesh()
@cython.cdivision(True) # Deactivate division by 0 checking.
def getMesh(self): # parameters: neighporing chunks (negative x, positive x, negative z, positive z)
cdef float* verts = self.chunk.getVertices()
cdef uint32_t* inds = self.chunk.getIndices()
cdef int numVerts = self.chunk.numVertices()
cdef int numInds = self.chunk.numIndices()
# cdef int numQuads = numInds // 6
# prevent 0-size errors
if numVerts == 0:
return np.empty((0)), np.empty((0))
# cdef vector[uint32_t] inds = self.chunk.getIndices(numQuads)
# cdef np.ndarray va = np.ascontiguousarray(verts, dtype=np.float32)
# cdef np.ndarray ia = np.ascontiguousarray(inds, dtype=np.uint32)
cdef np.ndarray va = np.asarray(<np.float32_t[:numVerts]>verts, dtype=np.float32)
cdef np.ndarray ia = np.asarray(<np.uint32_t[:numInds]>inds, dtype=np.uint32)
return va, ia