Finally ready to merge the two tool codebases
This commit is contained in:
@@ -6,6 +6,8 @@
|
|||||||
CHUNK_WIDTH = 16
|
CHUNK_WIDTH = 16
|
||||||
CHUNK_HEIGHT = 16
|
CHUNK_HEIGHT = 16
|
||||||
CHUNK_DEPTH = 4
|
CHUNK_DEPTH = 4
|
||||||
|
# CHUNK_VERTEX_COUNT_MAX = QUAD_VERTEXES * CHUNK_WIDTH * CHUNK_HEIGHT * 4
|
||||||
|
CHUNK_VERTEX_COUNT_MAX=6144
|
||||||
|
|
||||||
TILE_WIDTH = 16.0
|
TILE_WIDTH = 16.0
|
||||||
TILE_HEIGHT = 16.0
|
TILE_HEIGHT = 16.0
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from dusk.event import Event
|
from dusk.event import Event
|
||||||
|
from editortool.map.mapdefs import CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_DEPTH, CHUNK_VERTEX_COUNT_MAX
|
||||||
|
from editortool.map.tile import Tile
|
||||||
|
from editortool.map.vertexbuffer import VertexBuffer
|
||||||
|
from OpenGL.GL import *
|
||||||
|
|
||||||
class Chunk:
|
class Chunk:
|
||||||
def __init__(self, map, x, y, z):
|
def __init__(self, map, x, y, z):
|
||||||
@@ -12,6 +16,25 @@ class Chunk:
|
|||||||
self.original = {}
|
self.original = {}
|
||||||
self.onChunkData = Event()
|
self.onChunkData = Event()
|
||||||
|
|
||||||
|
self.tiles = {}
|
||||||
|
self.vertexBuffer = VertexBuffer()
|
||||||
|
|
||||||
|
tileIndex = 0
|
||||||
|
for tx in range(CHUNK_WIDTH):
|
||||||
|
for ty in range(CHUNK_HEIGHT):
|
||||||
|
for tz in range(CHUNK_DEPTH):
|
||||||
|
self.tiles[tileIndex] = Tile(self, tx, ty, tz, tileIndex)
|
||||||
|
tileIndex += 1
|
||||||
|
|
||||||
|
# Update vertices
|
||||||
|
self.tileUpdateVertices()
|
||||||
|
|
||||||
|
def tileUpdateVertices(self):
|
||||||
|
self.vertexBuffer.clear()
|
||||||
|
for tile in self.tiles.values():
|
||||||
|
tile.buffer(self.vertexBuffer)
|
||||||
|
self.vertexBuffer.buildData()
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
fname = self.getFilename()
|
fname = self.getFilename()
|
||||||
if not fname or not os.path.exists(fname):
|
if not fname or not os.path.exists(fname):
|
||||||
@@ -51,3 +74,7 @@ class Chunk:
|
|||||||
if dir_path is None:
|
if dir_path is None:
|
||||||
return None
|
return None
|
||||||
return f"{dir_path}/{self.x}_{self.y}_{self.z}.json"
|
return f"{dir_path}/{self.x}_{self.y}_{self.z}.json"
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
glColor3f(0.0, 1.0, 1.0)
|
||||||
|
self.vertexBuffer.draw()
|
||||||
@@ -31,5 +31,6 @@ class GLWidget(QOpenGLWidget):
|
|||||||
|
|
||||||
glViewport(0, 0, w, h)
|
glViewport(0, 0, w, h)
|
||||||
self.parent.camera.setup(w, h)
|
self.parent.camera.setup(w, h)
|
||||||
|
self.parent.map.draw()
|
||||||
self.parent.grid.draw()
|
self.parent.grid.draw()
|
||||||
self.parent.selectBox.draw()
|
self.parent.selectBox.draw()
|
||||||
@@ -46,7 +46,6 @@ class Map:
|
|||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
def updateEditorConfig(self):
|
def updateEditorConfig(self):
|
||||||
print("Updating editor config...")
|
|
||||||
try:
|
try:
|
||||||
mapFileName = self.getMapFilename()
|
mapFileName = self.getMapFilename()
|
||||||
config = {
|
config = {
|
||||||
@@ -136,4 +135,8 @@ class Map:
|
|||||||
self.position[0] + x,
|
self.position[0] + x,
|
||||||
self.position[1] + y,
|
self.position[1] + y,
|
||||||
self.position[2] + z
|
self.position[2] + z
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
for chunk in self.chunks.values():
|
||||||
|
chunk.draw()
|
||||||
@@ -5,6 +5,7 @@ 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'))
|
||||||
CHUNK_TILE_COUNT = CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH
|
CHUNK_TILE_COUNT = CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_DEPTH
|
||||||
|
CHUNK_VERTEX_COUNT_MAX = int(defs.get('CHUNK_VERTEX_COUNT_MAX'))
|
||||||
|
|
||||||
TILE_WIDTH = float(defs.get('TILE_WIDTH'))
|
TILE_WIDTH = float(defs.get('TILE_WIDTH'))
|
||||||
TILE_HEIGHT = float(defs.get('TILE_HEIGHT'))
|
TILE_HEIGHT = float(defs.get('TILE_HEIGHT'))
|
||||||
|
|||||||
@@ -1,29 +1,39 @@
|
|||||||
from OpenGL.GL import *
|
from OpenGL.GL import *
|
||||||
from editortool.map.mapdefs import TILE_WIDTH, TILE_HEIGHT, TILE_DEPTH
|
from editortool.map.mapdefs import TILE_WIDTH, TILE_HEIGHT, TILE_DEPTH, CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_DEPTH
|
||||||
|
|
||||||
class Tile:
|
class Tile:
|
||||||
def __init__(self):
|
def __init__(self, chunk, x, y, z, tileIndex):
|
||||||
self.tile_id = 0
|
self.tileId = 0
|
||||||
|
self.chunk = chunk
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.z = z
|
||||||
|
|
||||||
|
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 draw(self, x, y, z):
|
def buffer(self, vertexBuffer):
|
||||||
|
if self.z != 0:
|
||||||
|
return
|
||||||
|
# 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
|
w = TILE_WIDTH
|
||||||
h = TILE_HEIGHT
|
h = TILE_HEIGHT
|
||||||
d = TILE_DEPTH
|
d = TILE_DEPTH
|
||||||
|
|
||||||
x = x * w
|
# Quad on the XY plane at z
|
||||||
y = y * h
|
vertexBuffer.vertices.extend([
|
||||||
z = z * d
|
x, y, z, # bottom left
|
||||||
|
x + w, y, z, # bottom right
|
||||||
|
x + w, y + h, z, # top right
|
||||||
|
|
||||||
# Center tile.
|
x, y, z, # bottom left
|
||||||
x -= w / 2.0
|
x + w, y + h, z, # top right
|
||||||
y -= h / 2.0
|
x, y + h, z # top left
|
||||||
z -= d / 2.0
|
])
|
||||||
|
|
||||||
# Draw the tile as a flat square on the X-Y plane at depth z.
|
|
||||||
glColor3f(1.0, 0.0, 0.0) # Red color
|
|
||||||
glBegin(GL_QUADS)
|
|
||||||
glVertex3f(x, y, z) # Bottom-left
|
|
||||||
glVertex3f(x + w, y, z) # Bottom-right
|
|
||||||
glVertex3f(x + w, y + h, z) # Top-right
|
|
||||||
glVertex3f(x, y + h, z) # Top-left
|
|
||||||
glEnd()
|
|
||||||
80
tools/editortool/map/vertexbuffer.py
Normal file
80
tools/editortool/map/vertexbuffer.py
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
from OpenGL.GL import *
|
||||||
|
import array
|
||||||
|
|
||||||
|
class VertexBuffer:
|
||||||
|
def __init__(self, componentsPerVertex=3):
|
||||||
|
self.componentsPerVertex = componentsPerVertex
|
||||||
|
self.vertices = []
|
||||||
|
self.colors = []
|
||||||
|
self.uvs = []
|
||||||
|
self.data = None
|
||||||
|
self.colorData = None
|
||||||
|
self.uvData = None
|
||||||
|
|
||||||
|
def buildData(self):
|
||||||
|
hasColors = len(self.colors) > 0
|
||||||
|
hasUvs = len(self.uvs) > 0
|
||||||
|
|
||||||
|
vertexCount = len(self.vertices) // self.componentsPerVertex
|
||||||
|
|
||||||
|
dataList = []
|
||||||
|
colorList = []
|
||||||
|
uvList = []
|
||||||
|
|
||||||
|
for i in range(vertexCount):
|
||||||
|
vStart = i * self.componentsPerVertex
|
||||||
|
dataList.extend(self.vertices[vStart:vStart+self.componentsPerVertex])
|
||||||
|
|
||||||
|
if hasColors:
|
||||||
|
cStart = i * 4 # Assuming RGBA
|
||||||
|
colorList.extend(self.colors[cStart:cStart+4])
|
||||||
|
|
||||||
|
if hasUvs:
|
||||||
|
uStart = i * 2 # Assuming UV
|
||||||
|
uvList.extend(self.uvs[uStart:uStart+2])
|
||||||
|
|
||||||
|
self.data = array.array('f', dataList)
|
||||||
|
|
||||||
|
if hasColors:
|
||||||
|
self.colorData = array.array('f', colorList)
|
||||||
|
else:
|
||||||
|
self.colorData = None
|
||||||
|
|
||||||
|
if hasUvs:
|
||||||
|
self.uvData = array.array('f', uvList)
|
||||||
|
else:
|
||||||
|
self.uvData = None
|
||||||
|
|
||||||
|
def draw(self, mode=GL_TRIANGLES, count=-1):
|
||||||
|
if count == -1:
|
||||||
|
count = len(self.data) // self.componentsPerVertex
|
||||||
|
|
||||||
|
if count == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
glEnableClientState(GL_VERTEX_ARRAY)
|
||||||
|
glVertexPointer(self.componentsPerVertex, GL_FLOAT, 0, self.data.tobytes())
|
||||||
|
|
||||||
|
if self.colorData:
|
||||||
|
glEnableClientState(GL_COLOR_ARRAY)
|
||||||
|
glColorPointer(4, GL_FLOAT, 0, self.colorData.tobytes())
|
||||||
|
|
||||||
|
if self.uvData:
|
||||||
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
|
||||||
|
glTexCoordPointer(2, GL_FLOAT, 0, self.uvData.tobytes())
|
||||||
|
|
||||||
|
glDrawArrays(mode, 0, count)
|
||||||
|
|
||||||
|
glDisableClientState(GL_VERTEX_ARRAY)
|
||||||
|
if self.colorData:
|
||||||
|
glDisableClientState(GL_COLOR_ARRAY)
|
||||||
|
if self.uvData:
|
||||||
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY)
|
||||||
|
|
||||||
|
def clear(self):
|
||||||
|
self.vertices = []
|
||||||
|
self.colors = []
|
||||||
|
self.uvs = []
|
||||||
|
self.data = array.array('f')
|
||||||
|
self.colorData = None
|
||||||
|
self.uvData = None
|
||||||
Reference in New Issue
Block a user