fixes most bugs
This commit is contained in:
parent
325da79fac
commit
dede00377e
7 changed files with 321 additions and 262 deletions
Objects
|
@ -4,13 +4,16 @@ from OpenGL.GL import *
|
|||
from Objects.Objects import *
|
||||
import numpy as np
|
||||
|
||||
|
||||
class Cube(Object):
|
||||
GeometryShaderId = -1
|
||||
|
||||
def __init__(self):
|
||||
Object.__init__(self)
|
||||
if(Cube.GeometryShaderId == -1):
|
||||
if (Cube.GeometryShaderId == -1):
|
||||
self.initializeShader()
|
||||
def initializeShader(self)->bool:
|
||||
|
||||
def initializeShader(self) -> bool:
|
||||
with open('./Objects/Cube/cube_geometry.glsl', 'r') as f:
|
||||
geometry_shader_string = f.read()
|
||||
Cube.GeometryShaderId = glCreateShader(GL_GEOMETRY_SHADER)
|
||||
|
@ -19,4 +22,4 @@ class Cube(Object):
|
|||
if glGetShaderiv(Cube.GeometryShaderId, GL_COMPILE_STATUS) != GL_TRUE:
|
||||
raise RuntimeError(glGetShaderInfoLog(Cube.GeometryShaderId))
|
||||
return False
|
||||
return True
|
||||
return True
|
||||
|
|
|
@ -1,20 +1,27 @@
|
|||
import numpy as np
|
||||
import typing
|
||||
|
||||
|
||||
class Object:
|
||||
GeometryShaderId = -1
|
||||
def draw(self)->bool:
|
||||
|
||||
def draw(self) -> bool:
|
||||
return False
|
||||
def initializeShader(self)->bool:
|
||||
|
||||
def initializeShader(self) -> bool:
|
||||
return True
|
||||
|
||||
def __init__(self):
|
||||
self.pos = np.zeros((3))
|
||||
self.color = np.zeros((3,))
|
||||
self.programmId = -1
|
||||
def translate(self,M):
|
||||
self.pos = np.array((np.concatenate((self.pos, [1])) * M)[0,0:3])[0]
|
||||
|
||||
def translate(self, M):
|
||||
self.pos = np.array((np.concatenate((self.pos, [1])) * M)[0, 0:3])[0]
|
||||
return self
|
||||
def setColor(self,R,G,B):
|
||||
|
||||
def setColor(self, R, G, B):
|
||||
self.color[0] = R
|
||||
self.color[1] = G
|
||||
self.color[2] = B
|
||||
return self
|
||||
return self
|
||||
|
|
|
@ -22,20 +22,21 @@ def check_error(message):
|
|||
return True
|
||||
return False
|
||||
|
||||
|
||||
class Structure:
|
||||
def __init__(self):
|
||||
self.Objects = {}
|
||||
self.vais = {}
|
||||
self.Matrix = np.identity(4,np.float32)
|
||||
self.Matrix = np.identity(4, np.float32)
|
||||
self.dirty = False
|
||||
|
||||
def addShape(self,program,shape):
|
||||
def addShape(self, program, shape):
|
||||
if not program in self.Objects.keys():
|
||||
self.Objects[program] = []
|
||||
self.Objects[program].append(shape)
|
||||
self.dirty = True
|
||||
|
||||
def removeShape(self,program,shape):
|
||||
def removeShape(self, program, shape):
|
||||
if program in self.Objects.keys():
|
||||
self.Objects[program].remove(shape)
|
||||
if len(self.Objects[program]) == 0:
|
||||
|
@ -51,11 +52,12 @@ class Structure:
|
|||
glEnableClientState(GL_COLOR_ARRAY)
|
||||
self.vais = {}
|
||||
|
||||
for key,objects in self.Objects.items():
|
||||
tvai = GLuint(-1)
|
||||
tpbi = -1
|
||||
tcbi = -1
|
||||
tsbi = -1
|
||||
for key, objects in self.Objects.items():
|
||||
tvai = GLuint(0)
|
||||
tpbi = GLuint(0)
|
||||
tcbi = GLuint(0)
|
||||
tsbi = GLuint(0)
|
||||
|
||||
glGenVertexArrays(1, tvai)
|
||||
glBindVertexArray(tvai)
|
||||
|
||||
|
@ -69,7 +71,7 @@ class Structure:
|
|||
positions.append(o.pos[0])
|
||||
positions.append(o.pos[1])
|
||||
positions.append(o.pos[2])
|
||||
glBufferData(GL_ARRAY_BUFFER, np.asarray(positions), GL_STATIC_DRAW)
|
||||
glBufferData(GL_ARRAY_BUFFER, np.array(positions, dtype=np.float32), GL_STATIC_DRAW)
|
||||
glVertexAttribPointer(vid, 3, GL_FLOAT, GL_FALSE, 0, None)
|
||||
check_error("Could not create position buffer")
|
||||
|
||||
|
@ -80,14 +82,14 @@ class Structure:
|
|||
colors.append(o.color[2])
|
||||
tcbi = glGenBuffers(1)
|
||||
glBindBuffer(GL_ARRAY_BUFFER, tcbi)
|
||||
glBufferData(GL_ARRAY_BUFFER, np.asarray(colors), GL_STATIC_DRAW)
|
||||
glBufferData(GL_ARRAY_BUFFER, np.array(colors, dtype=np.float32), GL_STATIC_DRAW)
|
||||
vc = glGetAttribLocation(key, "MyInColor")
|
||||
if vc != -1:
|
||||
glEnableVertexAttribArray(vc)
|
||||
glVertexAttribPointer(vc, 3, GL_FLOAT, GL_FALSE, 0, None)
|
||||
check_error("Could not create color buffer")
|
||||
|
||||
if hasattr(objects[0],'size'):
|
||||
if hasattr(objects[0], 'size'):
|
||||
sizes = []
|
||||
for o in objects:
|
||||
sizes.append(o.size[0])
|
||||
|
@ -95,7 +97,7 @@ class Structure:
|
|||
sizes.append(o.size[2])
|
||||
tsbi = glGenBuffers(1)
|
||||
glBindBuffer(GL_ARRAY_BUFFER, tsbi)
|
||||
glBufferData(GL_ARRAY_BUFFER, np.asarray(sizes), GL_STATIC_DRAW)
|
||||
glBufferData(GL_ARRAY_BUFFER, np.array(sizes, dtype=np.float32), GL_STATIC_DRAW)
|
||||
vs = glGetAttribLocation(key, "MyInSize")
|
||||
if vs != -1:
|
||||
glEnableVertexAttribArray(vs)
|
||||
|
@ -103,26 +105,26 @@ class Structure:
|
|||
check_error("Could not create size buffer")
|
||||
|
||||
glBindVertexArray(0)
|
||||
self.vais[key] = (tvai,tpbi,tcbi,tsbi)
|
||||
self.vais[key] = (tvai, tpbi, tcbi, tsbi)
|
||||
self.dirty = False
|
||||
|
||||
def clearVertexArrays(self):
|
||||
for key,(a,p,c,s) in self.vais.items():
|
||||
for key, (a, p, c, s) in self.vais.items():
|
||||
if p != -1:
|
||||
glDisableVertexAttribArray(p)
|
||||
glDeleteBuffers(1,[p])
|
||||
glDeleteBuffers(1, [p])
|
||||
if c != -1:
|
||||
glDisableVertexAttribArray(c)
|
||||
glDeleteBuffers(1,[c])
|
||||
glDeleteBuffers(1, [c])
|
||||
if s != -1 and s != GLuint(-1):
|
||||
glDisableVertexAttribArray(s)
|
||||
glDeleteBuffers(1,[s])
|
||||
glDeleteBuffers(1, [s])
|
||||
glDeleteVertexArrays(1, a)
|
||||
check_error("Could not destroy vertex array")
|
||||
|
||||
def render(self,projMatrix,geometryRotMatrix,alternateprograms = None):
|
||||
def render(self, projMatrix, geometryRotMatrix, alternateprograms=None):
|
||||
|
||||
for key,tupel in self.vais.items():
|
||||
for key, tupel in self.vais.items():
|
||||
if alternateprograms == None:
|
||||
program_id = key
|
||||
else:
|
||||
|
@ -150,20 +152,22 @@ class Structure:
|
|||
else:
|
||||
return False
|
||||
|
||||
|
||||
class CompoundStructure:
|
||||
def __init__(self):
|
||||
self.Structures = []
|
||||
|
||||
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))
|
||||
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))
|
||||
|
||||
def render(self,projMatrix,geometryRotMatrix,alternateprograms = None):
|
||||
def render(self, projMatrix, geometryRotMatrix, alternateprograms=None):
|
||||
for (structure, M, R) in self.Structures:
|
||||
structure.buildvertexArrays()
|
||||
structure.render(M*projMatrix, R*geometryRotMatrix, alternateprograms)
|
||||
structure.render(M * projMatrix, R * geometryRotMatrix, alternateprograms)
|
||||
|
||||
def __eq__(self, other):
|
||||
if type(other) is type(self):
|
||||
return self.Structures == other.Structures
|
||||
else:
|
||||
return False
|
||||
return False
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue