VoxelEngine/Objects/Structure.py

165 lines
6.4 KiB
Python
Raw Permalink Normal View History

2017-08-27 12:51:26 +02:00
from wsgiref.validate import check_errors
from OpenGL.GL.ARB.vertex_array_object import glDeleteVertexArrays
from OpenGL.GL.framebufferobjects import glBindFramebuffer
from OpenGL.GLUT import *
import OpenGL.GLUT.freeglut
from OpenGL.GLU import *
from OpenGL.GL import *
import numpy as np
from OpenGL.extensions import alternate
2020-09-12 12:39:51 +02:00
from Objects.Objects import Object
2020-11-08 10:20:23 +01:00
from Objects.Renderable import Renderable
2020-09-12 12:39:51 +02:00
class Structure(Renderable):
2017-08-27 12:51:26 +02:00
def __init__(self):
self.Objects = {}
self.vais = {}
self.dirty = False
2020-07-19 10:41:08 +02:00
def addShape(self, program, shape):
2017-08-27 12:51:26 +02:00
if not program in self.Objects.keys():
self.Objects[program] = []
self.Objects[program].append(shape)
self.dirty = True
2020-07-19 10:41:08 +02:00
def removeShape(self, program, shape):
2017-08-27 12:51:26 +02:00
if program in self.Objects.keys():
self.Objects[program].remove(shape)
if len(self.Objects[program]) == 0:
self.Objects.pop(program)
self.dirty = True
def buildvertexArrays(self):
if self.dirty:
self.clearVertexArrays()
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY)
glEnableClientState(GL_COLOR_ARRAY)
self.vais = {}
2020-07-19 10:41:08 +02:00
for key, objects in self.Objects.items():
tvai = GLuint(0)
tpbi = GLuint(0)
tcbi = GLuint(0)
tsbi = GLuint(0)
2020-11-08 10:20:23 +01:00
num = len(objects)
2020-07-19 10:41:08 +02:00
2017-08-27 12:51:26 +02:00
glGenVertexArrays(1, tvai)
glBindVertexArray(tvai)
vid = glGetAttribLocation(key, "in_position")
glEnableVertexAttribArray(vid)
tpbi = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, tpbi)
positions = []
for o in objects:
positions.append(o.pos[0])
positions.append(o.pos[1])
positions.append(o.pos[2])
2020-07-19 10:41:08 +02:00
glBufferData(GL_ARRAY_BUFFER, np.array(positions, dtype=np.float32), GL_STATIC_DRAW)
2017-08-27 12:51:26 +02:00
glVertexAttribPointer(vid, 3, GL_FLOAT, GL_FALSE, 0, None)
2020-11-08 10:20:23 +01:00
self.check_error("Could not create position buffer")
2017-08-27 12:51:26 +02:00
colors = []
for o in objects:
colors.append(o.color[0])
colors.append(o.color[1])
colors.append(o.color[2])
tcbi = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, tcbi)
2020-07-19 10:41:08 +02:00
glBufferData(GL_ARRAY_BUFFER, np.array(colors, dtype=np.float32), GL_STATIC_DRAW)
2017-08-27 12:51:26 +02:00
vc = glGetAttribLocation(key, "MyInColor")
if vc != -1:
glEnableVertexAttribArray(vc)
glVertexAttribPointer(vc, 3, GL_FLOAT, GL_FALSE, 0, None)
2020-11-08 10:20:23 +01:00
self.check_error("Could not create color buffer")
2017-08-27 12:51:26 +02:00
2020-07-19 10:41:08 +02:00
if hasattr(objects[0], 'size'):
2017-08-27 12:51:26 +02:00
sizes = []
for o in objects:
sizes.append(o.size[0])
sizes.append(o.size[1])
sizes.append(o.size[2])
tsbi = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, tsbi)
2020-07-19 10:41:08 +02:00
glBufferData(GL_ARRAY_BUFFER, np.array(sizes, dtype=np.float32), GL_STATIC_DRAW)
2017-08-27 12:51:26 +02:00
vs = glGetAttribLocation(key, "MyInSize")
if vs != -1:
glEnableVertexAttribArray(vs)
glVertexAttribPointer(vs, 3, GL_FLOAT, GL_FALSE, 0, None)
2020-11-08 10:20:23 +01:00
self.check_error("Could not create size buffer")
2017-08-27 12:51:26 +02:00
glBindVertexArray(0)
2020-11-08 10:20:23 +01:00
self.vais[key] = (tvai, tpbi, tcbi, tsbi, num)
2017-08-27 12:51:26 +02:00
self.dirty = False
def clearVertexArrays(self):
2020-11-08 10:20:23 +01:00
temp = dict(self.vais)
self.vais = {}
for key, vertex_array_ids in temp.items():
if vertex_array_ids[1] != -1 and vertex_array_ids[1] != GLuint(-1):
glDeleteBuffers(1, [vertex_array_ids[1]])
self.check_error("Could not destroy buffer")
if vertex_array_ids[2] != -1 and vertex_array_ids[2] != GLuint(-1):
glDeleteBuffers(1, [vertex_array_ids[2]])
self.check_error("Could not destroy buffer")
if vertex_array_ids[3] != -1 and vertex_array_ids[3] != GLuint(-1):
glDeleteBuffers(1, [vertex_array_ids[3]])
self.check_error("Could not destroy buffer")
glDeleteVertexArrays(1, vertex_array_ids[0])
self.check_error("Could not destroy vertex array")
2020-07-19 10:41:08 +02:00
def render(self, projMatrix, geometryRotMatrix, alternateprograms=None):
2020-11-08 10:20:23 +01:00
self.buildvertexArrays()
for key, vertex_array_ids in self.vais.items():
if alternateprograms == None:
program_id = key
else:
assert key in alternateprograms
program_id = alternateprograms[key]
glUseProgram(program_id)
2020-11-08 10:20:23 +01:00
self.check_error("Renderingprogram is not initialized!")
projection = glGetUniformLocation(program_id, 'projModelViewMatrix')
rot = glGetUniformLocation(program_id, 'rotMatrix')
glUniformMatrix4fv(projection, 1, GL_FALSE, np.array(projMatrix))
glUniformMatrix3fv(rot, 1, GL_FALSE, np.array(geometryRotMatrix))
2020-11-08 10:20:23 +01:00
glBindVertexArray(vertex_array_ids[0])
glDrawArrays(GL_POINTS, 0, vertex_array_ids[4])
self.check_error("Rendering problem")
glBindVertexArray(0)
glUseProgram(0)
def __eq__(self, other):
if type(other) is type(self):
return self.vais == other.vais and self.Objects == other.Objects
else:
return False
2020-07-19 10:41:08 +02:00
2020-09-12 12:39:51 +02:00
class CompoundStructure(Renderable):
def __init__(self):
self.Structures = []
2020-07-19 10:41:08 +02:00
def addStructure(self, structure: Structure, M: np.matrix = np.identity(4, np.float),
R: np.matrix = np.identity(3, np.float)):
self.Structures.append((structure, M, R))
2020-07-19 10:41:08 +02:00
def render(self, projMatrix, geometryRotMatrix, alternateprograms=None):
for (structure, M, R) in self.Structures:
2020-07-19 10:41:08 +02:00
structure.render(M * projMatrix, R * geometryRotMatrix, alternateprograms)
def __eq__(self, other):
if type(other) is type(self):
return self.Structures == other.Structures
else:
2020-11-08 10:20:23 +01:00
return False