Bit more rendering
This commit is contained in:
@@ -15,14 +15,15 @@ class Chunk:
|
||||
self.current = {}
|
||||
self.original = {}
|
||||
self.onChunkData = Event()
|
||||
self.dirty = False
|
||||
|
||||
self.tiles = {}
|
||||
self.vertexBuffer = VertexBuffer()
|
||||
|
||||
tileIndex = 0
|
||||
for tx in range(CHUNK_WIDTH):
|
||||
for tz in range(CHUNK_DEPTH):
|
||||
for ty in range(CHUNK_HEIGHT):
|
||||
for tz in range(CHUNK_DEPTH):
|
||||
for tx in range(CHUNK_WIDTH):
|
||||
self.tiles[tileIndex] = Tile(self, tx, ty, tz, tileIndex)
|
||||
tileIndex += 1
|
||||
|
||||
@@ -42,8 +43,17 @@ class Chunk:
|
||||
return
|
||||
try:
|
||||
with open(fname, 'r') as f:
|
||||
self.current = json.load(f)
|
||||
self.original = json.loads(json.dumps(self.current)) # Deep copy
|
||||
data = json.load(f)
|
||||
|
||||
if not 'shapes' in data:
|
||||
data['shapes'] = []
|
||||
|
||||
# For each tile.
|
||||
for tile in self.tiles.values():
|
||||
tile.load(data)
|
||||
|
||||
self.tileUpdateVertices()
|
||||
self.dirty = False
|
||||
self.onChunkData.invoke(self.current)
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Failed to load chunk file: {e}")
|
||||
@@ -55,7 +65,7 @@ class Chunk:
|
||||
try:
|
||||
with open(fname, 'w') as f:
|
||||
json.dump(self.current, f, indent=2)
|
||||
self.original = json.loads(json.dumps(self.current)) # Deep copy
|
||||
self.dirty = False
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"Failed to save chunk file: {e}")
|
||||
|
||||
@@ -65,7 +75,8 @@ class Chunk:
|
||||
self.onChunkData.invoke(self.current)
|
||||
|
||||
def isDirty(self):
|
||||
return json.dumps(self.current, sort_keys=True) != json.dumps(self.original, sort_keys=True)
|
||||
return False
|
||||
# return json.dumps(self.current, sort_keys=True) != json.dumps(self.original, sort_keys=True)
|
||||
|
||||
def getFilename(self):
|
||||
if not self.map or not hasattr(self.map, 'getMapDirectory'):
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from dusk.defs import defs
|
||||
|
||||
|
||||
CHUNK_WIDTH = int(defs.get('CHUNK_WIDTH'))
|
||||
CHUNK_HEIGHT = int(defs.get('CHUNK_HEIGHT'))
|
||||
CHUNK_DEPTH = int(defs.get('CHUNK_DEPTH'))
|
||||
@@ -18,4 +17,8 @@ RPG_CAMERA_FOV = float(defs.get('RPG_CAMERA_FOV'))
|
||||
MAP_WIDTH = 5
|
||||
MAP_HEIGHT = 5
|
||||
MAP_DEPTH = 3
|
||||
MAP_CHUNK_COUNT = MAP_WIDTH * MAP_HEIGHT * MAP_DEPTH
|
||||
MAP_CHUNK_COUNT = MAP_WIDTH * MAP_HEIGHT * MAP_DEPTH
|
||||
|
||||
for key in defs.keys():
|
||||
if key.startswith('TILE_SHAPE_'):
|
||||
globals()[key] = int(defs.get(key))
|
||||
@@ -1,41 +1,113 @@
|
||||
from OpenGL.GL import *
|
||||
from editortool.map.mapdefs import TILE_WIDTH, TILE_HEIGHT, TILE_DEPTH, CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_DEPTH
|
||||
from enum import Enum
|
||||
from editortool.map.mapdefs import TILE_WIDTH, TILE_HEIGHT, TILE_DEPTH, CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_DEPTH, TILE_SHAPE_NULL, TILE_SHAPE_FLOOR, TILE_SHAPE_RAMP_NORTH
|
||||
|
||||
def getItem(arr, index, default):
|
||||
if index < len(arr):
|
||||
return arr[index]
|
||||
return default
|
||||
|
||||
class Tile:
|
||||
def __init__(self, chunk, x, y, z):
|
||||
def __init__(self, chunk, x, y, z, tileIndex):
|
||||
self.shape = TILE_SHAPE_NULL
|
||||
|
||||
self.chunk = chunk
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.z = z
|
||||
|
||||
self.index = tileIndex
|
||||
|
||||
self.posX = x * TILE_WIDTH + chunk.x * CHUNK_WIDTH * TILE_WIDTH
|
||||
self.posY = y * TILE_HEIGHT + chunk.y * CHUNK_HEIGHT * TILE_HEIGHT
|
||||
self.posZ = z * TILE_DEPTH + chunk.z * CHUNK_DEPTH * TILE_DEPTH
|
||||
|
||||
def load(self, chunkData):
|
||||
self.shape = getItem(chunkData['shapes'], self.index, TILE_SHAPE_NULL)
|
||||
|
||||
def getBaseTileModel(self):
|
||||
vertices = []
|
||||
indices = []
|
||||
uvs = []
|
||||
colors = []
|
||||
|
||||
if self.shape == TILE_SHAPE_NULL:
|
||||
pass
|
||||
|
||||
elif self.shape == TILE_SHAPE_FLOOR:
|
||||
vertices = [
|
||||
(self.posX, self.posY, self.posZ),
|
||||
(self.posX + TILE_WIDTH, self.posY, self.posZ),
|
||||
(self.posX + TILE_WIDTH, self.posY + TILE_HEIGHT, self.posZ),
|
||||
(self.posX, self.posY + TILE_HEIGHT, self.posZ)
|
||||
]
|
||||
indices = [0, 1, 2, 0, 2, 3]
|
||||
uvs = [ (0, 0), (1, 0), (1, 1), (0, 1) ]
|
||||
colors = [ ( 255, 255, 255, 255 ) ] * 4
|
||||
|
||||
elif self.shape == TILE_SHAPE_RAMP_NORTH:
|
||||
vertices = [
|
||||
(self.posX, self.posY, self.posZ + TILE_DEPTH),
|
||||
(self.posX + TILE_WIDTH, self.posY, self.posZ + TILE_DEPTH),
|
||||
(self.posX + TILE_WIDTH, self.posY + TILE_HEIGHT, self.posZ),
|
||||
(self.posX, self.posY + TILE_HEIGHT, self.posZ)
|
||||
]
|
||||
indices = [0, 1, 2, 0, 2, 3]
|
||||
uvs = [ (0, 0), (1, 0), (1, 1), (0, 1) ]
|
||||
colors = [ ( 255, 0, 0, 255 ) ] * 4
|
||||
|
||||
else:
|
||||
print("Unknown tile shape:", self.shape)
|
||||
pass
|
||||
|
||||
return {
|
||||
'vertices': vertices,
|
||||
'indices': indices,
|
||||
'uvs': uvs,
|
||||
'colors': colors
|
||||
}
|
||||
|
||||
def buffer(self, vertexBuffer):
|
||||
if self.shape == TILE_SHAPE_NULL:
|
||||
return
|
||||
# Old code:
|
||||
# if self.x != 0 or self.y != 0 or self.z != 0:
|
||||
# return # Only buffer the tile at (0,0,0)
|
||||
|
||||
# Center tile.
|
||||
x = self.posX - TILE_WIDTH / 2.0
|
||||
y = self.posY - TILE_HEIGHT / 2.0
|
||||
z = self.posZ - TILE_DEPTH / 2.0
|
||||
w = TILE_WIDTH
|
||||
h = TILE_HEIGHT
|
||||
d = TILE_DEPTH
|
||||
# x = self.posX - TILE_WIDTH / 2.0
|
||||
# y = self.posY - TILE_HEIGHT / 2.0
|
||||
# z = self.posZ - TILE_DEPTH / 2.0
|
||||
# w = TILE_WIDTH
|
||||
# h = TILE_HEIGHT
|
||||
# d = TILE_DEPTH
|
||||
|
||||
# Quad on the XY plane at z
|
||||
vertexBuffer.vertices.extend([
|
||||
x, y, z, # bottom left
|
||||
x + w, y, z, # bottom right
|
||||
x + w, y + h, z, # top right
|
||||
# # Quad on the XY plane at z
|
||||
# vertexBuffer.vertices.extend([
|
||||
# x, y, z, # bottom left
|
||||
# x + w, y, z, # bottom right
|
||||
# x + w, y + h, z, # top right
|
||||
|
||||
x, y, z, # bottom left
|
||||
x + w, y + h, z, # top right
|
||||
x, y + h, z # top left
|
||||
])
|
||||
# x, y, z, # bottom left
|
||||
# x + w, y + h, z, # top right
|
||||
# x, y + h, z # top left
|
||||
# ])
|
||||
|
||||
# New code:
|
||||
baseData = self.getBaseTileModel()
|
||||
|
||||
# Base data is indiced but we need to buffer unindiced data
|
||||
for index in baseData['indices']:
|
||||
verts = baseData['vertices'][index]
|
||||
uv = baseData['uvs'][index]
|
||||
color = baseData['colors'][index]
|
||||
|
||||
vertexBuffer.vertices.extend([
|
||||
verts[0] - (TILE_WIDTH / 2.0),
|
||||
verts[1] - (TILE_HEIGHT / 2.0),
|
||||
verts[2] - (TILE_DEPTH / 2.0)
|
||||
])
|
||||
|
||||
vertexBuffer.colors.extend([
|
||||
color[0] / 255.0,
|
||||
color[1] / 255.0,
|
||||
color[2] / 255.0,
|
||||
color[3] / 255.0
|
||||
])
|
||||
Reference in New Issue
Block a user