def __init__(self): self.program = compileProgram(VisualizeDepthShaderVert, None, VisualizeDepthShaderFrag)
def createShader(self): """ creates shader for this segment. first the combination of used shaders is generated. this result has a string representation. if a shader with the same string representation was generated before, then the allready compiled shader is used, else a new one created. """ vertData = ShaderData(); geomData = ShaderData(); fragData = ShaderData() # texture counter self.nextTexID = self.app.textureCounter # texture list textures = [] # material texture colors maybe mapped to multiple targets, # we dont wanna iterate over the material textures in the create function, # so the relation will be saved in this table. matTextures = {} baseShaders = self.createBaseShaders(textures) # add material textures for i in range(len(self.material.textures)): tex = self.material.textures[i] texUnit = self.nextTexID; self.nextTexID += 1 texData = (tex.glID, GL_TEXTURE_2D, "sampler2D", "Texture%d" % texUnit, texUnit) textures.append(texData) for mapTo in tex.mapTo: try: matTextures[mapTo].append((tex, texData)) except: matTextures[mapTo] = [(tex, texData)] # add shadow map textures i = 0 for light in self.lights: shadowMapArray = light.shadowMapArray if shadowMapArray!=None: texUnit = self.nextTexID; self.nextTexID += 1 texData = (shadowMapArray.texture.glID, GL_TEXTURE_2D_ARRAY, shadowMapArray.textureType, "shadowMap%d" % i, texUnit) textures.append(texData) i += 1 hasLight = bool(self.enabledLights) if hasLight: # uv and light shaders make only sense when light is enabled lightShaders = self.createLightShaders(textures, matTextures) norShaders = self.createNorShaders(textures, matTextures) uvShaders = self.createUVShaders(textures, matTextures) vertexShaders = self.createVertexShaders(textures, matTextures) colorShaders = self.createColorShaders(textures, matTextures) if self.isReflecting(): self.reflectionHandler = self.createReflectionHandler(self.app, self, colorShaders, textures) # get multiple vertex shader functions self._addShaders(baseShaders[VERT_SHADER], vertData) self._addShaders(vertexShaders[VERT_SHADER], vertData) if hasLight: self._addShaders(lightShaders[VERT_SHADER], vertData) self._addShaders(norShaders[VERT_SHADER], vertData) self._addShaders(uvShaders[VERT_SHADER], vertData) self._addShaders(colorShaders[VERT_SHADER], vertData) # get multiple geometry shader functions self._addShaders(baseShaders[GEOM_SHADER], geomData) self._addShaders(vertexShaders[GEOM_SHADER], geomData) # get multiple fragment shader functions self._addShaders(uvShaders[FRAG_SHADER], fragData) self._addShaders(baseShaders[FRAG_SHADER], fragData) self._addShaders(vertexShaders[FRAG_SHADER], fragData) self._addShaders(colorShaders[FRAG_SHADER], fragData) if hasLight: self._addShaders(norShaders[FRAG_SHADER], fragData) self._addShaders(lightShaders[FRAG_SHADER], fragData) # add texture sampler as uniform for (_, _, type, name, _) in textures: fragData.functions[0].addUniform(type=type, name=name) if vertData.functions == []: vertData.functions.append(VertShaderFunc(name=None)) self.addShaderAttributes(vertData.functions[0]) # combine all shaders into one (vertShader, geomShader, fragShader) = \ combineShader(vertData.functions, geomData.functions, fragData.functions) # setup main function if fragShader!=None: for var in fragData.localVars.keys(): (type, val) = fragData.localVars[var] fragShader.addLocalVar(type, var, val) for exp in fragData.exports.keys(): fragShader.addExport(exp, fragData.exports[exp]) if geomShader!=None: for var in geomData.localVars.keys(): (type, val) = geomData.localVars[var] geomShader.addLocalVar(type, var, val) for exp in geomData.exports.keys(): geomShader.addExport(exp, geomData.exports[exp]) if vertShader!=None: for var in vertData.localVars.keys(): (type, val) = vertData.localVars[var] vertShader.addLocalVar(type, var, val) for exp in vertData.exports.keys(): vertShader.addExport(exp, vertData.exports[exp]) # get shader string representation shaderStr = "" for shader in [vertShader, geomShader, fragShader]: if shader==None: continue print shader.code() shaderStr += str(shader) + "\n" try: # try to use allready compiled shader (self.shaderProgram, self.shader) = self.segmentShaders[shaderStr] return except: pass if fragShader==None and geomShader==None and vertShader==None: self.shaderProgram = -1 # self.shader handles enabling/disabling the shader self.shader = ShaderWrapper() else: # ready to compile... self.shaderProgram = compileProgram( vertShader, geomShader, fragShader) # self.shader handles enabling/disabling the shader self.shader = SegmentShader(self, self.shaderProgram, textures) self.segmentShaders[shaderStr] = (self.shaderProgram, self.shader)