def run(self) -> None: while True: self.fMan.update() ######## User input ######## self.controller.update() ######## Game logic ######## self.logicalDude.update() self.commander.update() ######## Resource ######## self.resourceManager.update() ######## Render ######## gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glClearBufferfv(gl.GL_COLOR, 0, (0.0, 0.0, 0.0)) gl.glEnable(gl.GL_CULL_FACE) gl.glEnable(gl.GL_DEPTH_TEST) self.renderDude.renderShadow(self.globalStates.winWidth_i, self.globalStates.winHeight_i) self.renderDude.render( self.projectionMatrix, self.camera.getViewMatrix() ) p.display.flip()
def renderTranslucent(self): try: GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, self.translucentFBO) GL.glClearBufferfv(GL.GL_DEPTH, 0, 1) GL_shaders.glUseProgram(self.shaderTranslucent) self.tex1.bind(0) self.model2.bind() GL.glUniformMatrix4fv( self.UNIFORMSTranslucent['my_ModelMatrix'],1, GL.GL_TRUE, self.ModelMatrix.get()) GL.glUniformMatrix4fv( self.UNIFORMSTranslucent['my_ViewMatrix'],1, GL.GL_TRUE, self.ViewMatrix.get()) GL.glUniformMatrix4fv( self.UNIFORMSTranslucent['my_ProjectionMatrix'],1, GL.GL_TRUE, self.ProjectionMatrix.get()) GL.glUniform1i(self.UNIFORMSTranslucent['tex0'], 0) finally: self.model2.unbind() self.tex1.unbind() GL_shaders.glUseProgram(0) GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0)
def update(self, deltaTime: float, winSize: tuple) -> None: self.__sunlight.transformDirection( glm.rotate(glm.mat4(1), 0.5 * deltaTime, (1, 0, 0))) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glClearBufferfv(gl.GL_COLOR, 0, (0.0, 0.0, 0.0, 1.0)) self.__renderAllShadows(winSize) self.__shaderGeneral.activate() projViewMat = self.__progMat * self.__camera.getViewMat() #projViewMat = self.__slights[0].getProjViewMat(); gl.glUniformMatrix4fv(self.__unilocGeneral.uProjViewMat, 1, gl.GL_FALSE, glm.value_ptr(projViewMat)) gl.glUniform3f(self.__unilocGeneral.uViewPos, self.__camera.pos.getPosX(), self.__camera.pos.getPosY(), self.__camera.pos.getPosZ()) gl.glUniform3f(self.__unilocGeneral.uBaseAmbient, 0.1, 0.1, 0.1) gl.glUniform1i(self.__unilocGeneral.uDlightCount, len(self.__dlights)) gl.glUniform1i(self.__unilocGeneral.uPlightCount, len(self.__plights)) gl.glUniform1i(self.__unilocGeneral.uSlightCount, len(self.__slights)) for i, dlight in enumerate(self.__dlights): dlight.sendUniforms(self.__unilocGeneral, i) for i, plight in enumerate(self.__plights): plight.sendUniforms(self.__unilocGeneral, i) for i, slight in enumerate(self.__slights): slight.sendUniforms(self.__unilocGeneral, i) for obj in self.__renderObjects: _renderObjectGeneral(obj, self.__unilocGeneral)
def reset_layer(self, n: int) -> None: """ Reset a particular layer to an empty state. This implies alpha of 0 (transparent) and type of 0 (undrawn) :param n: :return: """ with self.__layer_fbs[n]: GL.glClearBufferfv(GL.GL_COLOR, 0, (0, 255, 0, 0))
def update(self, timeDelta, projectMatrix, viewMatrix, camera: Camera, flashLight, shadowMat, sunLightDirection): a = sunLightDirection.dot(mmath.Vec4(0, -1, -1, 0)) + 0.5 if a < 0.0: a = 0.0 a = a * 2 if a < 0.2: a = 0.2 elif a > 1.0: a = 1.0 if False and 0.0 < a < 0.9: self.sunLightColor = (0.7, 0.3, 0.3) gl.glClearBufferfv(gl.GL_COLOR, 0, (a, a / 2, a / 2, 1.0)) else: self.sunLightColor = (0.5, 0.5, 0.5) gl.glClearBufferfv(gl.GL_COLOR, 0, (a / 2, a / 2, a, 1.0)) self.ambient_t = (0.25, 0.25, 0.25) #self.dynamicLight.r = sin( (time()+1)/2 ) #self.dynamicLight.g = cos( (time()+1)/2 ) #self.dynamicLight.b = sin( (time()+1)/2) * cos((time()+1)/2 ) self.dynamicLight.x = 39 # camera.getWorldXYZ()[0] self.dynamicLight.y = 1 # camera.getWorldXYZ()[1] self.dynamicLight.z = -39 # camera.getWorldXYZ()[2] + -20 x, y, z = camera.getXYZ() y -= 0.5 first = self.spotLights_l[0] first.parent = self.mainLoop.camera a = np.matrix([0, 0, 0, 1], np.float32) * first.getModelMatrix() pos = a.item(0), a.item(1), a.item(2) b = np.matrix([ *first.directionVec3.getXYZ(), 0.0, ], np.float32) * first.getModelMatrix() direction = b.item(0), b.item(1), b.item(2) lightPos_t = tuple() lightColor_t = tuple() lightMaxDistance_t = tuple() lightCount_i = 0 for x in self.pointLights_l: lightPos_t += x.getXYZ() lightColor_t += x.getRGB() lightMaxDistance_t += (x.maxDistance_f, ) lightCount_i += 1 spotLightPos_t = list() spotLightColor_t = tuple() spotLightMaxDistance_t = tuple() spotLightDirection_t = tuple() sportLightCutoff_t = tuple() spotLightCount_i = 0 for x in self.spotLights_l: spotLightPos_t += pos spotLightColor_t += x.getRGB() spotLightMaxDistance_t += (x.maxDistance_f, ) spotLightDirection_t += direction[:3] sportLightCutoff_t += (x.cutoff_f, ) spotLightCount_i += 1 self.boxManager.update( projectMatrix, viewMatrix, camera, self.ambient_t, lightCount_i, lightPos_t, lightColor_t, lightMaxDistance_t, spotLightCount_i, spotLightPos_t, spotLightColor_t, spotLightMaxDistance_t, spotLightDirection_t, sportLightCutoff_t, flashLight, timeDelta, shadowMat, self.depthMap, self.sunLightColor, sunLightDirection) self.loadedModelManager.update( projectMatrix, viewMatrix, camera, self.ambient_t, lightCount_i, lightPos_t, lightColor_t, lightMaxDistance_t, spotLightCount_i, spotLightPos_t, spotLightColor_t, spotLightMaxDistance_t, spotLightDirection_t, sportLightCutoff_t, flashLight, shadowMat, self.depthMap, self.sunLightColor, sunLightDirection) gl.glUseProgram(self.display.program) # Vertex shader gl.glUniformMatrix4fv(5, 1, gl.GL_FALSE, projectMatrix) gl.glUniformMatrix4fv(6, 1, gl.GL_FALSE, viewMatrix) gl.glUniformMatrix4fv(7, 1, gl.GL_FALSE, mmath.identityMat4()) # Fragment shader gl.glUniform3f(8, *camera.getWorldXYZ()) gl.glUniform3f(9, *self.ambient_t) gl.glUniform1i(10, lightCount_i) gl.glUniform3fv(12, lightCount_i, lightPos_t) gl.glUniform3fv(17, lightCount_i, lightColor_t) gl.glUniform1fv(22, lightCount_i, lightMaxDistance_t) if flashLight: gl.glUniform1i(27, spotLightCount_i) gl.glUniform3fv(28, spotLightCount_i, spotLightPos_t) gl.glUniform3fv(33, spotLightCount_i, spotLightColor_t) gl.glUniform1fv(43, spotLightCount_i, spotLightMaxDistance_t) gl.glUniform3fv(38, spotLightCount_i, spotLightDirection_t) gl.glUniform1fv(48, spotLightCount_i, sportLightCutoff_t) else: gl.glUniform1i(27, 0) self.display.update() self.display2.update() gl.glUseProgram(self.surfaceProgram) # Vertex shader gl.glUniformMatrix4fv(5, 1, gl.GL_FALSE, projectMatrix) gl.glUniformMatrix4fv(6, 1, gl.GL_FALSE, viewMatrix) gl.glUniformMatrix4fv(7, 1, gl.GL_FALSE, mmath.identityMat4()) gl.glUniformMatrix4fv(54, 1, gl.GL_FALSE, shadowMat) # Fragment shader gl.glUniform3f(8, *camera.parent.getXYZ()) gl.glUniform3f(9, *self.ambient_t) gl.glUniform1i(10, lightCount_i) gl.glUniform3fv(12, lightCount_i, lightPos_t) gl.glUniform3fv(17, lightCount_i, lightColor_t) gl.glUniform1fv(22, lightCount_i, lightMaxDistance_t) gl.glUniform1i(55, 0) gl.glUniform1i(56, 1) gl.glActiveTexture(gl.GL_TEXTURE1) gl.glBindTexture(gl.GL_TEXTURE_2D, self.depthMap) if flashLight: gl.glUniform1i(27, spotLightCount_i) gl.glUniform3fv(28, spotLightCount_i, spotLightPos_t) gl.glUniform3fv(33, spotLightCount_i, spotLightColor_t) gl.glUniform1fv(43, spotLightCount_i, spotLightMaxDistance_t) gl.glUniform3fv(38, spotLightCount_i, spotLightDirection_t) gl.glUniform1fv(48, spotLightCount_i, sportLightCutoff_t) else: gl.glUniform1i(27, 0) gl.glUniform3f( gl.glGetUniformLocation(self.surfaceProgram, "sunLightColor"), *self.sunLightColor) gl.glUniform3f( gl.glGetUniformLocation(self.surfaceProgram, "sunLightDirection"), *sunLightDirection.getXYZ()) #### self.floor.update() self.wall1.update() self.wall2.update() self.wall3.update() #self.ceiling.update() self.wall4.update() self.grass.update()
def update(self, timeDelta, projectMatrix, viewMatrix, camera:Camera, flashLight, shadowMat): curTime_f = time() / 10 gl.glClearBufferfv(gl.GL_COLOR, 0, ( (sin(curTime_f) + 1) / 4 , (sin(curTime_f) + 1) / 4 , (sin(curTime_f) + 1) / 2, 1.0)) sunLightColor_f = (sin(curTime_f) + 1) / 4 self.sunLightColor = (sunLightColor_f, sunLightColor_f, sunLightColor_f) ambientLightColor_f = (sin(curTime_f) + 1) / 8 self.ambient_t = (ambientLightColor_f, ambientLightColor_f, ambientLightColor_f) self.dynamicLight.r = sin( (time()+1)/2 ) self.dynamicLight.g = cos((time()+1)/2) self.dynamicLight.b = sin((time()+1)/2) * cos((time()+1)/2) x, y, z = camera.getXYZ() y -= 0.5 first = self.spotLights_l[0] first.parent = self.mainLoop.camera a = np.matrix([0, 0, 0, 1], np.float32) * first.getModelMatrix() pos = a.item(0), a.item(1), a.item(2) b = np.matrix([*first.directionVec3.getXYZ(), 0.0,], np.float32) * first.getModelMatrix() direction = b.item(0), b.item(1), b.item(2) lightPos_t = tuple() lightColor_t = tuple() lightMaxDistance_t = tuple() lightCount_i = 0 for x in self.pointLights_l: lightPos_t += x.getXYZ() lightColor_t += x.getRGB() lightMaxDistance_t += (x.maxDistance_f,) lightCount_i += 1 spotLightPos_t = list() spotLightColor_t = tuple() spotLightMaxDistance_t = tuple() spotLightDirection_t = tuple() sportLightCutoff_t = tuple() spotLightCount_i = 0 for x in self.spotLights_l: spotLightPos_t += pos spotLightColor_t += x.getRGB() spotLightMaxDistance_t += (x.maxDistance_f,) spotLightDirection_t += direction[:3] sportLightCutoff_t += (x.cutoff_f,) spotLightCount_i += 1 self.boxManager.update(projectMatrix, viewMatrix, camera, self.ambient_t, lightCount_i, lightPos_t, lightColor_t, lightMaxDistance_t, spotLightCount_i, spotLightPos_t, spotLightColor_t, spotLightMaxDistance_t, spotLightDirection_t, sportLightCutoff_t, flashLight, timeDelta, shadowMat, self.depthMap, self.sunLightColor) self.loadedModelManager.update( projectMatrix, viewMatrix, camera, self.ambient_t, lightCount_i, lightPos_t, lightColor_t, lightMaxDistance_t, spotLightCount_i, spotLightPos_t, spotLightColor_t, spotLightMaxDistance_t, spotLightDirection_t, sportLightCutoff_t, flashLight, self.depthMap, self.sunLightColor ) gl.glUseProgram(self.display.program) # Vertex shader gl.glUniformMatrix4fv(5, 1, gl.GL_FALSE, projectMatrix) gl.glUniformMatrix4fv(6, 1, gl.GL_FALSE, viewMatrix) gl.glUniformMatrix4fv(7, 1, gl.GL_FALSE, mmath.identityMat4()) # Fragment shader gl.glUniform3f(8, *camera.getXYZ()) gl.glUniform3f(9, *self.ambient_t) gl.glUniform1i(10, lightCount_i) gl.glUniform3fv(12, lightCount_i, lightPos_t) gl.glUniform3fv(17, lightCount_i, lightColor_t) gl.glUniform1fv(22, lightCount_i, lightMaxDistance_t) if flashLight: gl.glUniform1i(27, spotLightCount_i) gl.glUniform3fv(28, spotLightCount_i, spotLightPos_t) gl.glUniform3fv(33, spotLightCount_i, spotLightColor_t) gl.glUniform1fv(43, spotLightCount_i, spotLightMaxDistance_t) gl.glUniform3fv(38, spotLightCount_i, spotLightDirection_t) gl.glUniform1fv(48, spotLightCount_i, sportLightCutoff_t) else: gl.glUniform1i(27, 0) self.display.update() self.display2.update() gl.glUseProgram(self.surfaceProgram) # Vertex shader gl.glUniformMatrix4fv(5, 1, gl.GL_FALSE, projectMatrix) gl.glUniformMatrix4fv(6, 1, gl.GL_FALSE, viewMatrix) gl.glUniformMatrix4fv(7, 1, gl.GL_FALSE, mmath.identityMat4()) gl.glUniformMatrix4fv(54, 1, gl.GL_FALSE, shadowMat) # Fragment shader gl.glUniform3f(8, *camera.getXYZ()) gl.glUniform3f(9, *self.ambient_t) gl.glUniform1i(10, lightCount_i) gl.glUniform3fv(12, lightCount_i, lightPos_t) gl.glUniform3fv(17, lightCount_i, lightColor_t) gl.glUniform1fv(22, lightCount_i, lightMaxDistance_t) gl.glUniform1i(55, 0) gl.glUniform1i(56, 1) gl.glActiveTexture(gl.GL_TEXTURE1) gl.glBindTexture(gl.GL_TEXTURE_2D, self.depthMap) if flashLight: gl.glUniform1i(27, spotLightCount_i) gl.glUniform3fv(28, spotLightCount_i, spotLightPos_t) gl.glUniform3fv(33, spotLightCount_i, spotLightColor_t) gl.glUniform1fv(43, spotLightCount_i, spotLightMaxDistance_t) gl.glUniform3fv(38, spotLightCount_i, spotLightDirection_t) gl.glUniform1fv(48, spotLightCount_i, sportLightCutoff_t) else: gl.glUniform1i(27, 0) gl.glUniform3f(gl.glGetUniformLocation(self.surfaceProgram, "sunLightColor"), *self.sunLightColor) #### self.floor.update() self.wall1.update() self.wall2.update() self.wall3.update() #self.ceiling.update() self.wall4.update() self.grass.update()
def render(self): try: # Run first shader program: GL.glEnable(GL.GL_DEPTH_TEST) # Enable for depth test GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, self.fbo) GL.glViewport(0, 0, 512, 512) GL.glClearBufferfv(GL.GL_DEPTH, 0, 1) # last argument: 0 for depth, tuple for color (eg. (1.0, 0.0, 0.0, 1.0) for red etc) # "GL.GL_DEPTH" for depth testing # "GL.GL_COLOR" for color testing GL_shaders.glUseProgram(self.shader_1) GL.glUniform1i(self.UNIFORMS_1['tex0'], self.tex0_ID) self.vbo.bind() self.ibo.bind() try: GL.glEnableVertexAttribArray(0) GL.glEnableVertexAttribArray(1) GL.glVertexAttribPointer( # Vertex data 0, # Attribute 0 in this attribute array 3, # This attribute uses 3 elements GL.GL_FLOAT, # These values are of type "GL_FLOAT" False, # Normalize values? No! 5*4, # bits per row, 4 bits for floats, 6 elements in one row (doubles are 8) self.vbo) # Where in each row does attribute start? A little unintuitive... GL.glVertexAttribPointer( # UV-mapping data 1, 2, GL.GL_FLOAT, False, 5*4, ## OpenGL can calculate this for you, apparently. Try setting it to 0! self.vbo + 3*4) GL.glDrawElements( GL.GL_TRIANGLES, 6, ##TODO: create method that gets number of indices in IBO! GL.GL_UNSIGNED_INT, self.ibo) finally: GL.glDisableVertexAttribArray(0) GL.glDisableVertexAttribArray(1) finally: self.vbo.unbind() self.ibo.unbind() GL_shaders.glUseProgram(0) GL.glDisable(GL.GL_DEPTH_TEST) GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0) GL.glViewport(0, 0, self.width, self.height) try: # Run second shader program: GL.glBindTexture(GL.GL_TEXTURE_2D, self.col_tex) GL_shaders.glUseProgram(self.shader_2) GL.glUniform1i(self.UNIFORMS_2['col_tex'], self.col_tex_ID) self.vbo.bind() self.ibo.bind() try: GL.glEnableVertexAttribArray(0) GL.glEnableVertexAttribArray(1) GL.glVertexAttribPointer( # Vertex data 0, # Attribute 0 in this attribute array 3, # This attribute uses 3 elements GL.GL_FLOAT, # These values are of type "GL_FLOAT" False, # Normalize values? No! 5*4, # bits per row, 4 bits for floats, 6 elements in one row (doubles are 8) self.vbo) # Where in each row does attribute start? A little unintuitive... GL.glVertexAttribPointer( # UV-mapping data 1, 2, GL.GL_FLOAT, False, 5*4, self.vbo + 3*4) GL.glDrawElements( GL.GL_TRIANGLES, 6, ##TODO: create method that gets number of indices in IBO! GL.GL_UNSIGNED_INT, self.ibo) finally: GL.glDisableVertexAttribArray(0) GL.glDisableVertexAttribArray(1) finally: self.vbo.unbind() self.ibo.unbind() GL_shaders.glUseProgram(0) GL.glBindTexture(GL.GL_TEXTURE_2D, 0)