From 8d7625a411f315bc9dbacd75b54bb3436b8b66be Mon Sep 17 00:00:00 2001 From: Steffen Temme Date: Sat, 21 Oct 2017 11:10:00 +0200 Subject: [PATCH] Fiddling around with lighting and a Rendermanager --- Lights/LightingManager.py | 38 ++++++++++++++++++++++++++++++-------- Objects/Objects.py | 10 ++++++++-- Objects/Structure.py | 28 +++++++++++++++++++++++++++- fragment.glsl | 5 +++-- main.py | 29 ++++++++++++++++++++--------- 5 files changed, 88 insertions(+), 22 deletions(-) diff --git a/Lights/LightingManager.py b/Lights/LightingManager.py index 4a3a4aa..e5fe629 100644 --- a/Lights/LightingManager.py +++ b/Lights/LightingManager.py @@ -21,18 +21,40 @@ class LightingManager: self.Lights.remove(l) class __Renderstep: - def __init__(self,program, vai): - self.program = program - self.vai = vai + def render(self, projMatrix, geometryRotMatrix): + pass + class __Structurestep(__Renderstep): + def __init__(self, alternateprogramdict, structure): + self.alternateprogramdict = alternateprogramdict + self.structure = structure def __eq__(self, other): if type(other) is type(self): - return self.program == other.program and self.vai == self.vai + return self.alternateprogramdict == other.alternateprogramdict and self.structure == self.structure else: return False + def render(self,projMatrix,geometryRotMatrix): + self.structure.render(projMatrix,geometryRotMatrix,self.alternateprogramdict) + class __Clearstep(__Renderstep): + def render(self, projMatrix, geometryRotMatrix): + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) - def addRenderStep(self,program,vai): - r = self.__Renderstep(program,vai) + class __ActivateDepthmappingStep(__Renderstep): + def __init__(self,light): + self.light = light + + def render(self, projMatrix, geometryRotMatrix): + self.light.prepareForDepthMapping() + + class __DeactivateDepthmappingStep(__Renderstep): + def __init__(self, light): + self.light = light + + def render(self, projMatrix, geometryRotMatrix): + self.light.finishDepthMapping() + + def addRenderStep(self,alternateprogramdict,structure): + r = self.__Renderstep(alternateprogramdict,structure) self.renderSteps.append(r) - def removeRenderStep(self,program,vai): - r = self.__Renderstep(program, vai) + def removeRenderStep(self,alternateprogramdict,structure): + r = self.__Renderstep(alternateprogramdict,structure) self.renderSteps.remove(r) \ No newline at end of file diff --git a/Objects/Objects.py b/Objects/Objects.py index cf06f42..8331af8 100644 --- a/Objects/Objects.py +++ b/Objects/Objects.py @@ -8,7 +8,13 @@ class Object: return True def __init__(self): self.pos = np.zeros((3)) - self.color = np.zeros((3)) + self.color = np.zeros((3,)) self.programmId = -1 def translate(self,M): - self.pos = self.pos * M \ No newline at end of file + self.pos = np.array((np.concatenate((self.pos, [1])) * M)[0,0:3])[0] + return self + def setColor(self,R,G,B): + self.color[0] = R + self.color[1] = G + self.color[2] = B + return self \ No newline at end of file diff --git a/Objects/Structure.py b/Objects/Structure.py index e510ad7..b7d660f 100644 --- a/Objects/Structure.py +++ b/Objects/Structure.py @@ -8,6 +8,8 @@ from OpenGL.GLU import * from OpenGL.GL import * import numpy as np +from OpenGL.extensions import alternate + def check_error(message): gl_error = glGetError() @@ -140,4 +142,28 @@ class Structure: check_error("Rendering problem") glBindVertexArray(0) - glUseProgram(0) \ No newline at end of file + 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 + +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 render(self,projMatrix,geometryRotMatrix,alternateprograms = None): + for (structure, M, R) in self.Structures: + structure.buildvertexArrays() + 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 \ No newline at end of file diff --git a/fragment.glsl b/fragment.glsl index 1161280..f1dd73c 100644 --- a/fragment.glsl +++ b/fragment.glsl @@ -21,6 +21,7 @@ const float specFactor = 1.0; const float shininess = 16.0; const float screenGamma = 2.2; const float pitl = 2*3.14159265359 / 16.0; +const float circlelength = 700.0; void main() { @@ -37,7 +38,7 @@ void main() //int visible = int(!(texture(ShadowMaps,lightpos[i].xy/2 + vec2(0.5,0.5)).z < (lightpos[i].z))); float visible = 0; for(int j = 0; j < 4; j++){ - vec2 offs = vec2(cos(j*4*pitl),sin(j*4*pitl)) / 700; + vec2 offs = vec2(cos(j*4*pitl),sin(j*4*pitl)) / circlelength; visible += float(!(texture(ShadowMaps[i],lightviewpos[i].xy + offs).x <= (lightviewpos[i].z - 0.0005*tan(acos(dot(normal,-normalize(lightpos[i] - pos.xyz))))))) * 1.0/16.0; } if(visible == 0.25) @@ -45,7 +46,7 @@ void main() else visible = 0; for(int j = 0; j < 16; j++){ - vec2 offs = vec2(cos(j*pitl),sin(j*pitl)) / 700; + vec2 offs = vec2(cos(j*pitl),sin(j*pitl)) / circlelength; visible += float(!(texture(ShadowMaps[i],lightviewpos[i].xy + offs).x <= (lightviewpos[i].z - 0.0005*tan(acos(dot(normal,-normalize(lightpos[i] - pos.xyz))))))) * 1.0/16.0; } colorLinear += (visible * 0.5 + 0.5) *(lambertian * diffuseFactor * colorin * lightColor[i] + specular * specFactor*colorin); diff --git a/main.py b/main.py index 1265a31..610ed3f 100644 --- a/main.py +++ b/main.py @@ -22,6 +22,7 @@ import time name = b'ball_glut' struct = None +cstruct = None program_id = 0 program2_id = 0 program3_id = 0 @@ -33,9 +34,9 @@ opening = 45 l = Light() def main(): lm = LightingManager() - lm.addRenderStep(0,0) - lm.addRenderStep(1,1) - lm.removeRenderStep(0,0) + #lm.addRenderStep(0,0) + #lm.addRenderStep(1,1) + #lm.removeRenderStep(0,0) glutInit(sys.argv) w = width @@ -125,11 +126,21 @@ def main(): #glAttachShader(program_id, l.FragmentShaderId) glLinkProgram(program_id) - global struct + global struct, cstruct struct = Structure() - struct.addShape(program_id,c) + struct.addShape(program_id,Cube().translate(translate(0, 0, 1)).setColor(1, 1, 0)) + struct.addShape(program_id, Cube().translate(translate(1, 1, 0)).setColor(0, 0, 1)) + struct.addShape(program_id, Cube().translate(translate(1, 0, 0)).setColor(0, 1, 0)) + struct.addShape(program_id, Cube().translate(translate(1, -1, 0)).setColor(1, 0, 0)) + struct.addShape(program_id, Cube().translate(translate(0, -1, 0)).setColor(0, 1, 0)) + struct.addShape(program_id, Cube().translate(translate(-1, -1, 0)).setColor(0, 0, 1)) + struct.addShape(program_id, Cube().translate(translate(-1, 0, 0)).setColor(0, 1, 1)) + struct.addShape(program_id, Cube().translate(translate(-1, 1, 0)).setColor(1, 1, 1)) + struct.addShape(program_id, Cube().translate(translate(0, 1, 0)).setColor(0, 0, 1)) struct.buildvertexArrays() struct.clearVertexArrays() + cstruct = CompoundStructure() + cstruct.addStructure(struct) program3_id = l.getDepthProgram(vertex_shader_id,Cube.GeometryShaderId) @@ -287,8 +298,8 @@ ry = 0 def render(program_id,projMatrix,vai,x,y,z,alternateprograms = None): - global struct - struct.render(translate(x, y, z) * rotate(0,ry,0,True) * projMatrix,rotate(rx, 0, 0),alternateprograms) + global struct, cstruct + cstruct.render(translate(x, y, z) * rotate(0,ry,0,True) * projMatrix,rotate(rx, 0, 0),alternateprograms) '''glUseProgram(program_id) @@ -366,12 +377,12 @@ def display(): glUniformMatrix4fv(lightProjModelViewMatrix,1,GL_FALSE,np.array(newMat)) glUniform1iv(numLights, 1, 1) glUniform3fv(lightpos, 1, [-cx,-cy,-5]) - glUniform3fv(lightcolorid, 1, [4,1,1]) + glUniform3fv(lightcolorid, 1, [1,1,1]) texID = glGetUniformLocation(program_id, 'ShadowMaps') glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_2D, l.DepthBuffer) - glUniform1i(texID, 0) + glUniform1iv(texID,1, 0) render(program_id,projMatrix,vai,0,0,-10)