From 5168061d1b90a12e45cd3dad49ac85d236b7c343 Mon Sep 17 00:00:00 2001 From: zomseffen Date: Sat, 12 Sep 2020 12:39:51 +0200 Subject: [PATCH] World Chunk --- Lights/Spotlight/Spotlight.py | 6 +++ Objects/Structure.py | 84 ++++++++++++++++++++++++++++++++++- main.py | 4 +- 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/Lights/Spotlight/Spotlight.py b/Lights/Spotlight/Spotlight.py index e69de29..e0e1570 100644 --- a/Lights/Spotlight/Spotlight.py +++ b/Lights/Spotlight/Spotlight.py @@ -0,0 +1,6 @@ +from Lights.Lights import Light +from OpenGL.GL import * +from MatrixStuff.Transformations import * + +class Spotlight(Light): + pass diff --git a/Objects/Structure.py b/Objects/Structure.py index f1e10cb..0697fb2 100644 --- a/Objects/Structure.py +++ b/Objects/Structure.py @@ -10,6 +10,9 @@ from OpenGL.GL import * import numpy as np from OpenGL.extensions import alternate +from Objects.Objects import Object +from MatrixStuff.Transformations import translate + def check_error(message): gl_error = glGetError() @@ -23,7 +26,12 @@ def check_error(message): return False -class Structure: +class Renderable: + def render(self, projMatrix, geometryRotMatrix, alternateprograms=None): + pass + + +class Structure(Renderable): def __init__(self): self.Objects = {} self.vais = {} @@ -153,7 +161,7 @@ class Structure: return False -class CompoundStructure: +class CompoundStructure(Renderable): def __init__(self): self.Structures = [] @@ -171,3 +179,75 @@ class CompoundStructure: return self.Structures == other.Structures else: return False + + +class WorldChunk(Renderable): + def __init__(self, width, length, height): + assert width > 0, 'Width must be greater than 0' + assert length > 0, 'length must be greater than 0' + assert height > 0, 'height must be greater than 0' + self.visible = [] + self.content = [] + self.entities = [] + + self.width = width + self.length = length + self.height = height + + for x in range(width): + self.content.append([]) + self.visible.append([]) + for y in range(length): + self.content[x].append([]) + self.visible[x].append([]) + for z in range(height): + self.content[x][y].append(None) + self.visible[x][y].append(4) + + def put_object(self, x: int, y: int, z: int, new_object: Object): + assert 0 <= x < self.width, 'Put out of bounds for x coordinate! Must be between 0 and %i' % self.width + assert 0 <= y < self.length, 'Put out of bounds for y coordinate! Must be between 0 and %i' % self.length + assert 0 <= z < self.height, 'Put out of bounds for z coordinate! Must be between 0 and %i' % self.height + self.content[x][y][z] = new_object + + change = -1 if new_object is not None else 1 + visible_carry_over = [] + if x + 1 >= self.width: + visible_carry_over.append((1, 0, 0, change)) + else: + self.visible[x + 1][y][z] += change + if x - 1 < 0: + visible_carry_over.append((-1, 0, 0, change)) + else: + self.visible[x - 1][y][z] += change + + if y + 1 >= self.length: + visible_carry_over.append((0, 1, 0, change)) + else: + self.visible[x][y + 1][z] += change + if y - 1 < 0: + visible_carry_over.append((0, -1, 0, change)) + else: + self.visible[x][y - 1][z] += change + + if z + 1 >= self.height: + visible_carry_over.append((0, 0, 1, change)) + else: + self.visible[x][y][z + 1] += change + if z - 1 < 0: + visible_carry_over.append((0, 0, -1, change)) + else: + self.visible[x][y][z - 1] += change + + return visible_carry_over + + def render(self, projMatrix, geometryRotMatrix, alternateprograms=None): + for x in range(self.width): + for y in range(self.length): + for z in range(self.height): + if self.visible[x][y][z] > 0 and self.content[x][y][z] is not None: + self.content[x][y][z].render(translate(x, y, z) * projMatrix, + geometryRotMatrix, alternateprograms) + + for entity in self.entities: + entity.render(projMatrix, geometryRotMatrix, alternateprograms) diff --git a/main.py b/main.py index d76acef..03eede3 100644 --- a/main.py +++ b/main.py @@ -19,7 +19,7 @@ from Objects.Cube.Cube import * from Objects.Cuboid.Cuboid import * from Objects.Structure import * from MatrixStuff.Transformations import * -from Lights.Lights import * +from Lights.Spotlight.Spotlight import Spotlight from Lights.LightingManager import * import numpy as np import time @@ -36,7 +36,7 @@ frames = 0 width = 1920 height = 1080 opening = 45 -l = Light() +l = Spotlight() def main():