示例#1
0
    def drawForShadow(self):
        gl.glBindVertexArray(self.vao)
        gl.glBindTexture(gl.GL_TEXTURE_2D, self.textureId)

        gl.glUniformMatrix4fv(3, 1, gl.GL_FALSE, mmath.identityMat4())

        #gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE)
        gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6)
示例#2
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()