Fiddling around with lighting and a Rendermanager

This commit is contained in:
steffen 2017-10-21 11:10:00 +02:00
parent 8dcc04b7b2
commit 8d7625a411
5 changed files with 88 additions and 22 deletions

View file

@ -21,18 +21,40 @@ class LightingManager:
self.Lights.remove(l) self.Lights.remove(l)
class __Renderstep: class __Renderstep:
def __init__(self,program, vai): def render(self, projMatrix, geometryRotMatrix):
self.program = program pass
self.vai = vai class __Structurestep(__Renderstep):
def __init__(self, alternateprogramdict, structure):
self.alternateprogramdict = alternateprogramdict
self.structure = structure
def __eq__(self, other): def __eq__(self, other):
if type(other) is type(self): 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: else:
return False 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): class __ActivateDepthmappingStep(__Renderstep):
r = self.__Renderstep(program,vai) 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) self.renderSteps.append(r)
def removeRenderStep(self,program,vai): def removeRenderStep(self,alternateprogramdict,structure):
r = self.__Renderstep(program, vai) r = self.__Renderstep(alternateprogramdict,structure)
self.renderSteps.remove(r) self.renderSteps.remove(r)

View file

@ -8,7 +8,13 @@ class Object:
return True return True
def __init__(self): def __init__(self):
self.pos = np.zeros((3)) self.pos = np.zeros((3))
self.color = np.zeros((3)) self.color = np.zeros((3,))
self.programmId = -1 self.programmId = -1
def translate(self,M): def translate(self,M):
self.pos = self.pos * M 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

View file

@ -8,6 +8,8 @@ from OpenGL.GLU import *
from OpenGL.GL import * from OpenGL.GL import *
import numpy as np import numpy as np
from OpenGL.extensions import alternate
def check_error(message): def check_error(message):
gl_error = glGetError() gl_error = glGetError()
@ -140,4 +142,28 @@ class Structure:
check_error("Rendering problem") check_error("Rendering problem")
glBindVertexArray(0) glBindVertexArray(0)
glUseProgram(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
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

View file

@ -21,6 +21,7 @@ const float specFactor = 1.0;
const float shininess = 16.0; const float shininess = 16.0;
const float screenGamma = 2.2; const float screenGamma = 2.2;
const float pitl = 2*3.14159265359 / 16.0; const float pitl = 2*3.14159265359 / 16.0;
const float circlelength = 700.0;
void main() 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))); //int visible = int(!(texture(ShadowMaps,lightpos[i].xy/2 + vec2(0.5,0.5)).z < (lightpos[i].z)));
float visible = 0; float visible = 0;
for(int j = 0; j < 4; j++){ 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; 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) if(visible == 0.25)
@ -45,7 +46,7 @@ void main()
else else
visible = 0; visible = 0;
for(int j = 0; j < 16; j++){ 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; 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); colorLinear += (visible * 0.5 + 0.5) *(lambertian * diffuseFactor * colorin * lightColor[i] + specular * specFactor*colorin);

29
main.py
View file

@ -22,6 +22,7 @@ import time
name = b'ball_glut' name = b'ball_glut'
struct = None struct = None
cstruct = None
program_id = 0 program_id = 0
program2_id = 0 program2_id = 0
program3_id = 0 program3_id = 0
@ -33,9 +34,9 @@ opening = 45
l = Light() l = Light()
def main(): def main():
lm = LightingManager() lm = LightingManager()
lm.addRenderStep(0,0) #lm.addRenderStep(0,0)
lm.addRenderStep(1,1) #lm.addRenderStep(1,1)
lm.removeRenderStep(0,0) #lm.removeRenderStep(0,0)
glutInit(sys.argv) glutInit(sys.argv)
w = width w = width
@ -125,11 +126,21 @@ def main():
#glAttachShader(program_id, l.FragmentShaderId) #glAttachShader(program_id, l.FragmentShaderId)
glLinkProgram(program_id) glLinkProgram(program_id)
global struct global struct, cstruct
struct = Structure() 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.buildvertexArrays()
struct.clearVertexArrays() struct.clearVertexArrays()
cstruct = CompoundStructure()
cstruct.addStructure(struct)
program3_id = l.getDepthProgram(vertex_shader_id,Cube.GeometryShaderId) 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): def render(program_id,projMatrix,vai,x,y,z,alternateprograms = None):
global struct global struct, cstruct
struct.render(translate(x, y, z) * rotate(0,ry,0,True) * projMatrix,rotate(rx, 0, 0),alternateprograms) cstruct.render(translate(x, y, z) * rotate(0,ry,0,True) * projMatrix,rotate(rx, 0, 0),alternateprograms)
'''glUseProgram(program_id) '''glUseProgram(program_id)
@ -366,12 +377,12 @@ def display():
glUniformMatrix4fv(lightProjModelViewMatrix,1,GL_FALSE,np.array(newMat)) glUniformMatrix4fv(lightProjModelViewMatrix,1,GL_FALSE,np.array(newMat))
glUniform1iv(numLights, 1, 1) glUniform1iv(numLights, 1, 1)
glUniform3fv(lightpos, 1, [-cx,-cy,-5]) glUniform3fv(lightpos, 1, [-cx,-cy,-5])
glUniform3fv(lightcolorid, 1, [4,1,1]) glUniform3fv(lightcolorid, 1, [1,1,1])
texID = glGetUniformLocation(program_id, 'ShadowMaps') texID = glGetUniformLocation(program_id, 'ShadowMaps')
glActiveTexture(GL_TEXTURE0) glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, l.DepthBuffer) glBindTexture(GL_TEXTURE_2D, l.DepthBuffer)
glUniform1i(texID, 0) glUniform1iv(texID,1, 0)
render(program_id,projMatrix,vai,0,0,-10) render(program_id,projMatrix,vai,0,0,-10)