Bit more rendering
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"tiles": [
|
"shapes": [
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"tiles": [
|
"shapes": [
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ TILE_DEPTH = 16.0
|
|||||||
TILE_SHAPE_NULL = 0
|
TILE_SHAPE_NULL = 0
|
||||||
TILE_SHAPE_FLOOR = 1
|
TILE_SHAPE_FLOOR = 1
|
||||||
TILE_SHAPE_RAMP_SOUTH = 2
|
TILE_SHAPE_RAMP_SOUTH = 2
|
||||||
TILE_SHAPE_RAMP_EAST = 3
|
TILE_SHAPE_RAMP_WEST = 3
|
||||||
TILE_SHAPE_RAMP_NORTH = 4
|
TILE_SHAPE_RAMP_EAST = 4
|
||||||
TILE_SHAPE_RAMP_WEST = 5
|
TILE_SHAPE_RAMP_NORTH = 5
|
||||||
|
|
||||||
RPG_CAMERA_FOV = 70
|
RPG_CAMERA_FOV = 70
|
||||||
RPG_CAMERA_PIXELS_PER_UNIT = 1.0
|
RPG_CAMERA_PIXELS_PER_UNIT = 1.0
|
||||||
|
|||||||
@@ -15,14 +15,15 @@ class Chunk:
|
|||||||
self.current = {}
|
self.current = {}
|
||||||
self.original = {}
|
self.original = {}
|
||||||
self.onChunkData = Event()
|
self.onChunkData = Event()
|
||||||
|
self.dirty = False
|
||||||
|
|
||||||
self.tiles = {}
|
self.tiles = {}
|
||||||
self.vertexBuffer = VertexBuffer()
|
self.vertexBuffer = VertexBuffer()
|
||||||
|
|
||||||
tileIndex = 0
|
tileIndex = 0
|
||||||
for tx in range(CHUNK_WIDTH):
|
for tz in range(CHUNK_DEPTH):
|
||||||
for ty in range(CHUNK_HEIGHT):
|
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)
|
self.tiles[tileIndex] = Tile(self, tx, ty, tz, tileIndex)
|
||||||
tileIndex += 1
|
tileIndex += 1
|
||||||
|
|
||||||
@@ -42,8 +43,17 @@ class Chunk:
|
|||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
with open(fname, 'r') as f:
|
with open(fname, 'r') as f:
|
||||||
self.current = json.load(f)
|
data = json.load(f)
|
||||||
self.original = json.loads(json.dumps(self.current)) # Deep copy
|
|
||||||
|
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)
|
self.onChunkData.invoke(self.current)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError(f"Failed to load chunk file: {e}")
|
raise RuntimeError(f"Failed to load chunk file: {e}")
|
||||||
@@ -55,7 +65,7 @@ class Chunk:
|
|||||||
try:
|
try:
|
||||||
with open(fname, 'w') as f:
|
with open(fname, 'w') as f:
|
||||||
json.dump(self.current, f, indent=2)
|
json.dump(self.current, f, indent=2)
|
||||||
self.original = json.loads(json.dumps(self.current)) # Deep copy
|
self.dirty = False
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError(f"Failed to save chunk file: {e}")
|
raise RuntimeError(f"Failed to save chunk file: {e}")
|
||||||
|
|
||||||
@@ -65,7 +75,8 @@ class Chunk:
|
|||||||
self.onChunkData.invoke(self.current)
|
self.onChunkData.invoke(self.current)
|
||||||
|
|
||||||
def isDirty(self):
|
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):
|
def getFilename(self):
|
||||||
if not self.map or not hasattr(self.map, 'getMapDirectory'):
|
if not self.map or not hasattr(self.map, 'getMapDirectory'):
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
from dusk.defs import defs
|
from dusk.defs import defs
|
||||||
|
|
||||||
|
|
||||||
CHUNK_WIDTH = int(defs.get('CHUNK_WIDTH'))
|
CHUNK_WIDTH = int(defs.get('CHUNK_WIDTH'))
|
||||||
CHUNK_HEIGHT = int(defs.get('CHUNK_HEIGHT'))
|
CHUNK_HEIGHT = int(defs.get('CHUNK_HEIGHT'))
|
||||||
CHUNK_DEPTH = int(defs.get('CHUNK_DEPTH'))
|
CHUNK_DEPTH = int(defs.get('CHUNK_DEPTH'))
|
||||||
@@ -18,4 +17,8 @@ RPG_CAMERA_FOV = float(defs.get('RPG_CAMERA_FOV'))
|
|||||||
MAP_WIDTH = 5
|
MAP_WIDTH = 5
|
||||||
MAP_HEIGHT = 5
|
MAP_HEIGHT = 5
|
||||||
MAP_DEPTH = 3
|
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 OpenGL.GL import *
|
||||||
from editortool.map.mapdefs import TILE_WIDTH, TILE_HEIGHT, TILE_DEPTH, CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_DEPTH
|
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
|
||||||
from enum import Enum
|
|
||||||
|
def getItem(arr, index, default):
|
||||||
|
if index < len(arr):
|
||||||
|
return arr[index]
|
||||||
|
return default
|
||||||
|
|
||||||
class Tile:
|
class Tile:
|
||||||
def __init__(self, chunk, x, y, z):
|
def __init__(self, chunk, x, y, z, tileIndex):
|
||||||
self.shape = TILE_SHAPE_NULL
|
self.shape = TILE_SHAPE_NULL
|
||||||
|
|
||||||
self.chunk = chunk
|
self.chunk = chunk
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.z = z
|
self.z = z
|
||||||
|
self.index = tileIndex
|
||||||
|
|
||||||
self.posX = x * TILE_WIDTH + chunk.x * CHUNK_WIDTH * TILE_WIDTH
|
self.posX = x * TILE_WIDTH + chunk.x * CHUNK_WIDTH * TILE_WIDTH
|
||||||
self.posY = y * TILE_HEIGHT + chunk.y * CHUNK_HEIGHT * TILE_HEIGHT
|
self.posY = y * TILE_HEIGHT + chunk.y * CHUNK_HEIGHT * TILE_HEIGHT
|
||||||
self.posZ = z * TILE_DEPTH + chunk.z * CHUNK_DEPTH * TILE_DEPTH
|
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):
|
def buffer(self, vertexBuffer):
|
||||||
if self.shape == TILE_SHAPE_NULL:
|
if self.shape == TILE_SHAPE_NULL:
|
||||||
return
|
return
|
||||||
|
# Old code:
|
||||||
# if self.x != 0 or self.y != 0 or self.z != 0:
|
# if self.x != 0 or self.y != 0 or self.z != 0:
|
||||||
# return # Only buffer the tile at (0,0,0)
|
# return # Only buffer the tile at (0,0,0)
|
||||||
|
|
||||||
# Center tile.
|
# Center tile.
|
||||||
x = self.posX - TILE_WIDTH / 2.0
|
# x = self.posX - TILE_WIDTH / 2.0
|
||||||
y = self.posY - TILE_HEIGHT / 2.0
|
# y = self.posY - TILE_HEIGHT / 2.0
|
||||||
z = self.posZ - TILE_DEPTH / 2.0
|
# z = self.posZ - TILE_DEPTH / 2.0
|
||||||
w = TILE_WIDTH
|
# w = TILE_WIDTH
|
||||||
h = TILE_HEIGHT
|
# h = TILE_HEIGHT
|
||||||
d = TILE_DEPTH
|
# d = TILE_DEPTH
|
||||||
|
|
||||||
# Quad on the XY plane at z
|
# # Quad on the XY plane at z
|
||||||
vertexBuffer.vertices.extend([
|
# vertexBuffer.vertices.extend([
|
||||||
x, y, z, # bottom left
|
# x, y, z, # bottom left
|
||||||
x + w, y, z, # bottom right
|
# x + w, y, z, # bottom right
|
||||||
x + w, y + h, z, # top right
|
# x + w, y + h, z, # top right
|
||||||
|
|
||||||
x, y, z, # bottom left
|
# x, y, z, # bottom left
|
||||||
x + w, y + h, z, # top right
|
# x + w, y + h, z, # top right
|
||||||
x, y + h, z # top left
|
# 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