World Chunk

This commit is contained in:
zomseffen 2020-09-12 12:39:51 +02:00
parent 24662a43a9
commit 5168061d1b
3 changed files with 90 additions and 4 deletions

View file

@ -0,0 +1,6 @@
from Lights.Lights import Light
from OpenGL.GL import *
from MatrixStuff.Transformations import *
class Spotlight(Light):
pass

View file

@ -10,6 +10,9 @@ from OpenGL.GL import *
import numpy as np import numpy as np
from OpenGL.extensions import alternate from OpenGL.extensions import alternate
from Objects.Objects import Object
from MatrixStuff.Transformations import translate
def check_error(message): def check_error(message):
gl_error = glGetError() gl_error = glGetError()
@ -23,7 +26,12 @@ def check_error(message):
return False return False
class Structure: class Renderable:
def render(self, projMatrix, geometryRotMatrix, alternateprograms=None):
pass
class Structure(Renderable):
def __init__(self): def __init__(self):
self.Objects = {} self.Objects = {}
self.vais = {} self.vais = {}
@ -153,7 +161,7 @@ class Structure:
return False return False
class CompoundStructure: class CompoundStructure(Renderable):
def __init__(self): def __init__(self):
self.Structures = [] self.Structures = []
@ -171,3 +179,75 @@ class CompoundStructure:
return self.Structures == other.Structures return self.Structures == other.Structures
else: else:
return False 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)

View file

@ -19,7 +19,7 @@ from Objects.Cube.Cube import *
from Objects.Cuboid.Cuboid import * from Objects.Cuboid.Cuboid import *
from Objects.Structure import * from Objects.Structure import *
from MatrixStuff.Transformations import * from MatrixStuff.Transformations import *
from Lights.Lights import * from Lights.Spotlight.Spotlight import Spotlight
from Lights.LightingManager import * from Lights.LightingManager import *
import numpy as np import numpy as np
import time import time
@ -36,7 +36,7 @@ frames = 0
width = 1920 width = 1920
height = 1080 height = 1080
opening = 45 opening = 45
l = Light() l = Spotlight()
def main(): def main():