def update(self): self.fManager.update() gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) for event in p.event.get(): if event.type == pl.QUIT: p.quit() sys.exit(0) elif event.type == pl.VIDEORESIZE: self.winSize_t = (event.dict['w'], event.dict['h']) self.onResize() self.controller.update(self.fManager.getFrameDelta()) hor, ver = self.camera.getWorldDegree() viewMatrix = mmath.translateMat4( *self.camera.getWorldXYZ(), -1) * mmath.rotateMat4( hor, 0, 1, 0) * mmath.rotateMat4(ver, 1, 0, 0) # viewMatrix = mmath.translateMat4(*self.camera.getWorldXYZ(), -1) * mmath.getlookatMat4( mmath.Vec4(*self.camera.getWorldXYZ(),1), mmath.Vec4(0,0,0,1), mmath.Vec4(0, 1, 0, 0) ) if self.worldLoaded_b: lightProjection = mmath.orthoMat4(-400.0, 400.0, -300.0, 300.0, -300.0, 300.0) else: lightProjection = mmath.orthoMat4(-75.0, 75.0, -75.0, 75.0, -75.0, 75.0) sunLightDirection = mmath.Vec4(0, 1, 0.5, 0).normalize() sunLightDirection = sunLightDirection.transform( mmath.rotateMat4(time() * 10 % 360, 0, 0, -1)) # lightView = mmath.translateMat4(0, 10, 0, -1) * mmath.rotateMat4(0, 0, 1, 0) * mmath.rotateMat4(-30, 1, 0, 0) lightView = mmath.getlookatMat4(sunLightDirection, mmath.Vec4(0, 0, 0, 0), mmath.Vec4(0, 1, 0, 0)) self.shadowMap.startRenderOn(lightProjection, lightView) self.level.drawForShadow(self.fManager.getFrameDelta()) self.shadowMap.finishRenderOn() self.onResize() self.level.update(self.fManager.getFrameDelta(), self.projectMatrix, viewMatrix, self.camera, self.flashLight_b, lightView * lightProjection, sunLightDirection) if True: self.drawText((-0.95, 0.9, 0), "FPS : {}".format(self.fManager.getFPS()[0])) self.drawText( (-0.95, 0.8, 0), "Pos : {:.2f}, {:.2f}, {:.2f}".format(*self.camera.pos_l)) self.drawText( (-0.95, 0.7, 0), "Looking : {:.2f}, {:.2f}".format(self.camera.lookHorDeg_f, self.camera.lookVerDeg_f)) p.display.flip()
def __init__(self, minPos: VEC34, maxPos: VEC34): x0, y0, z0 = minPos.getXYZ() x1, y1, z1 = maxPos.getXYZ() xS = x0 if x0 < x1 else x1 yS = y0 if y0 < y1 else y1 zS = z0 if z0 < z1 else z1 xB = x0 if x0 >= x1 else x1 yB = y0 if y0 >= y1 else y1 zB = z0 if z0 >= z1 else z1 self.min = mmath.Vec4(xS, yS, zS, 1) self.max = mmath.Vec4(xB, yB, zB, 1)
def __init__(self, p0: VEC34, p1: VEC34, p2: VEC34, parent: "mainpy.Actor" = None, initPos_l: list = None): super().__init__(parent, initPos_l) if isinstance(p0, mmath.Vec3): p0 = mmath.Vec4(p0, 1.0) if isinstance(p1, mmath.Vec3): p1 = mmath.Vec4(p1, 1.0) if isinstance(p2, mmath.Vec3): p2 = mmath.Vec4(p2, 1.0) self.points_l = [p0, p1, p2] self.plane = Plane.from3Dots(p0, p1, p2)
def hitCheckPlanePlane(pln0: "Plane", pln1: "Plane"): dot = (pln0.a + pln1.a) + (pln0.b + pln1.b) + (pln0.c + pln1.c) if (dot >= 1.0) or (dot <= -1.0): return False # When two planes are parallel. vec = mmath.Vec4(pln0.a, pln0.b, pln0.c, 0).cross(mmath.Vec4(pln1.a, pln1.b, pln1.c, 0)) if vec.z != 0.0: pos = mmath.Vec4((pln0.b * pln1.d - pln1.b * pln0.d) / vec.z, (pln1.a * pln0.d + pln0.a * pln1.d) / vec.z, 0, 1) elif vec.y != 0.0: pos = mmath.Vec4((pln1.c * pln0.d + pln0.c * pln1.d) / vec.y, 0.0, (pln0.a * pln1.d + pln1.a * pln0.d) / vec.y, 1.0) else: pos = mmath.Vec4(0.0, (pln0.c * pln1.d - pln1.c * pln0.d) / vec.x, (pln1.b * pln0.d - pln0.b * pln1.d) / vec.x, 1.0) return True, Segment(*pos.getXYZ(), *vec.getXYZ())
def hitCheckPlaneSegment( pln: "Plane", seg: "Segment", printResult_b: bool = False ) -> Tuple[int, Optional[mmath.Vec4], Optional[float]]: """ return values [0] : Result code, described below. [1] : Meet coord. [2] : Vector's length - (from start point to meet point vector)'s length. -1 : Vector is too short to reach the plane. -2 : Plane and vector are parallel and never gonna meet. 0 : Vector is heading opposite side from plane. -4 : Length is long enough to reach plane but heading wrong way. 1 : They meat. """ dotCheck = (pln.a * seg.posVec4.x + pln.b * seg.posVec4.y + pln.c * seg.posVec4.z) if dotCheck >= 0.0: return -3, None, None l = pln.a * seg.posVec4.x + pln.b * seg.posVec4.y + pln.c * seg.posVec4.z + pln.d if printResult_b: print("\tDistance of plane and segment:", l) vec_l2 = seg.vecVec4.x**2 + seg.vecVec4.y**2 + seg.vecVec4.z**2 if vec_l2 < l**2: return -1, None, None # Vector is too short to reach the plane. vec_l = math.sqrt(vec_l2) if printResult_b: print("\tVector length:", vec_l) # cos 0을 내적을 사용해 구한다. vec = seg.vecVec4 * (1.0 / vec_l) # Normalize dot = vec.dot(mmath.Vec4(-pln.a, -pln.b, -pln.c, 0.0)) if printResult_b: print("\tDot:", dot) if dot == 0.0: return -2, None, None # Plane and vector are parallel and never gonna meet. pln_l = abs(l / dot) if printResult_b: print("\tDistance of start and meet points:", pln_l) hitPosVec = seg.posVec4 + vec * pln_l if pln_l > vec_l: return -4, hitPosVec, vec_l - pln_l # Length is long enough to reach plane but heading wrong way. if dot < 0.0: return 0, hitPosVec, vec_l - pln_l # Vector is heading opposite side from plane. return 1, hitPosVec, vec_l - pln_l
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 getPoint2(self) -> mmath.Vec4: return self.points_l[2] + mmath.Vec4(*self.getWorldXYZ(), 1.0)
def __init__(self, xPos: UNION_NUM, yPos: UNION_NUM, zPos: UNION_NUM, radius_f: UNION_NUM): self.centerPosVec4 = mmath.Vec4(xPos, yPos, zPos, 1.0) self.radius_f = float(radius_f)
def __init__(self, xPos: UNION_NUM, yPos: UNION_NUM, zPos: UNION_NUM, xVec: UNION_NUM, yVec: UNION_NUM, zVec: UNION_NUM): self.posVec4 = mmath.Vec4(xPos, yPos, zPos, 1.0) self.vecVec4 = mmath.Vec4(xVec, yVec, zVec, 0.0)