diff --git a/src/duskdefs.env b/src/duskdefs.env index b115a02..ae57014 100644 --- a/src/duskdefs.env +++ b/src/duskdefs.env @@ -6,6 +6,8 @@ CHUNK_WIDTH = 16 CHUNK_HEIGHT = 16 CHUNK_DEPTH = 4 +# CHUNK_VERTEX_COUNT_MAX = QUAD_VERTEXES * CHUNK_WIDTH * CHUNK_HEIGHT * 4 +CHUNK_VERTEX_COUNT_MAX=6144 TILE_WIDTH = 16.0 TILE_HEIGHT = 16.0 diff --git a/tools/editortool/map/chunk.py b/tools/editortool/map/chunk.py index b2d4320..49e4f2e 100644 --- a/tools/editortool/map/chunk.py +++ b/tools/editortool/map/chunk.py @@ -1,6 +1,10 @@ import json import os 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: def __init__(self, map, x, y, z): @@ -12,6 +16,25 @@ class Chunk: self.original = {} 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): fname = self.getFilename() if not fname or not os.path.exists(fname): @@ -51,3 +74,7 @@ class Chunk: if dir_path is None: return None return f"{dir_path}/{self.x}_{self.y}_{self.z}.json" + + def draw(self): + glColor3f(0.0, 1.0, 1.0) + self.vertexBuffer.draw() \ No newline at end of file diff --git a/tools/editortool/map/glwidget.py b/tools/editortool/map/glwidget.py index aba2a9a..7072255 100644 --- a/tools/editortool/map/glwidget.py +++ b/tools/editortool/map/glwidget.py @@ -31,5 +31,6 @@ class GLWidget(QOpenGLWidget): glViewport(0, 0, w, h) self.parent.camera.setup(w, h) + self.parent.map.draw() self.parent.grid.draw() self.parent.selectBox.draw() \ No newline at end of file diff --git a/tools/editortool/map/map.py b/tools/editortool/map/map.py index c1dbf78..da9d327 100644 --- a/tools/editortool/map/map.py +++ b/tools/editortool/map/map.py @@ -46,7 +46,6 @@ class Map: traceback.print_exc() def updateEditorConfig(self): - print("Updating editor config...") try: mapFileName = self.getMapFilename() config = { @@ -136,4 +135,8 @@ class Map: self.position[0] + x, self.position[1] + y, self.position[2] + z - ) \ No newline at end of file + ) + + def draw(self): + for chunk in self.chunks.values(): + chunk.draw() \ No newline at end of file diff --git a/tools/editortool/map/mapdefs.py b/tools/editortool/map/mapdefs.py index 27a09a2..995a0f2 100644 --- a/tools/editortool/map/mapdefs.py +++ b/tools/editortool/map/mapdefs.py @@ -5,6 +5,7 @@ CHUNK_WIDTH = int(defs.get('CHUNK_WIDTH')) CHUNK_HEIGHT = int(defs.get('CHUNK_HEIGHT')) CHUNK_DEPTH = int(defs.get('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_HEIGHT = float(defs.get('TILE_HEIGHT')) diff --git a/tools/editortool/map/tile.py b/tools/editortool/map/tile.py index ea2d3e2..1f1555c 100644 --- a/tools/editortool/map/tile.py +++ b/tools/editortool/map/tile.py @@ -1,29 +1,39 @@ 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: - def __init__(self): - self.tile_id = 0 + def __init__(self, chunk, x, y, z, tileIndex): + 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 h = TILE_HEIGHT d = TILE_DEPTH - x = x * w - y = y * h - z = z * d + # 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 - # Center tile. - x -= w / 2.0 - y -= h / 2.0 - 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() \ No newline at end of file + x, y, z, # bottom left + x + w, y + h, z, # top right + x, y + h, z # top left + ]) \ No newline at end of file diff --git a/tools/editortool/map/vertexbuffer.py b/tools/editortool/map/vertexbuffer.py new file mode 100644 index 0000000..fc804fe --- /dev/null +++ b/tools/editortool/map/vertexbuffer.py @@ -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 \ No newline at end of file