Panagiotis Christopoulos Charitos před 13 roky
rodič
revize
acb9ddb377
2 změnil soubory, kde provedl 118 přidání a 5 odebrání
  1. 8 0
      src/collision/Frustum.cpp
  2. 110 5
      src/renderer/Is.cpp

+ 8 - 0
src/collision/Frustum.cpp

@@ -123,11 +123,19 @@ void PerspectiveFrustum::recalculate()
 	// left
 	planes[FP_LEFT] = Plane(Vec3(-c, 0.0, s), 0.0);
 
+#if 0
 	Math::sinCos((3 * Math::PI - fovY) * 0.5, s, c);
 	// top
 	planes[FP_TOP] = Plane(Vec3(0.0, s, c), 0.0);
 	// bottom
 	planes[FP_BOTTOM] = Plane(Vec3(0.0, -s, c), 0.0);
+#else
+	Math::sinCos((Math::PI + fovY) * 0.5, s, c);
+	// bottom
+	planes[FP_BOTTOM] = Plane(Vec3(0.0, s, c), 0.0);
+	// top
+	planes[FP_TOP] = Plane(Vec3(0.0, -s, c), 0.0);
+#endif
 
 	// near
 	planes[FP_NEAR] = Plane(Vec3(0.0, 0.0, -1.0), near);

+ 110 - 5
src/renderer/Is.cpp

@@ -246,15 +246,120 @@ Bool Is::cullLight(const PointLight& plight, const Tile& tile)
 }
 
 //==============================================================================
-void Is::updateAllTilesPlanes(const PerspectiveCamera& pcam)
+void Is::updateAllTilesPlanes(const PerspectiveCamera& cam)
 {
-	//F32 fovXFragment = cam.getFov
+	// The algorithm is almost the same as the recalculation of planes for
+	// PerspectiveFrustum class
 
-	for(U j = 0; j < TILES_Y_COUNT; j++)
+	// Algorithm works for even number of tiles per dimention
+	ANKI_ASSERT(TILES_X_COUNT % 2 == 0 && TILES_Y_COUNT % 2 == 0);
+
+	F32 fovX = cam.getFovX();
+	F32 fovY = cam.getFovY();
+
+	// Opts
+	F32 piAddfovXDiv2 = Math::PI + fovX / 2.0;
+	F32 piAddFovYDiv2 = (Math::PI + fovY) * 0.5;
+
+	// This is X the angle of the tile frustums
+	F32 fovXFragment = fovX / TILES_X_COUNT;
+	// See above
+	F32 fovYFragment = fovY / TILES_Y_COUNT;
+
+	for(U j = 0; j < TILES_Y_COUNT / 2; j++)
 	{
-		for(U i = 0; i < TILES_X_COUNT; i++)
+		for(U i = 0; i < TILES_X_COUNT / 2; i++)
 		{
-	
+			F32 c, s; // cos & sin
+
+			// Calc planes for one of those tiles:
+			// +-+-+-+-+
+			// | | | | |
+			// +-+-+-+-+
+			// | | | | |
+			// +-+-+-+-+
+			// |x|x| | |
+			// +-+-+-+-+
+			// |x|x| | |
+			// +-+-+-+-+
+			Array<Plane, 6>& planes = tiles[j][i].planes;
+			
+			Math::sinCos(piAddfovXDiv2 - i * fovXFragment, s, c);
+			// right
+			planes[Frustum::FP_RIGHT] = Plane(Vec3(c, 0.0, s), 0.0);
+			// left
+			planes[Frustum::FP_LEFT] = Plane(Vec3(-c, 0.0, s), 0.0);
+
+			Math::sinCos(piAddFovYDiv2 - j * fovYFragment, s, c);
+			// bottom
+			planes[Frustum::FP_BOTTOM] = Plane(Vec3(0.0, s, c), 0.0);
+			// top
+			planes[Frustum::FP_TOP] = Plane(Vec3(0.0, -s, c), 0.0);
+
+			// Mirror planes for those tiles:
+			// +-+-+-+-+
+			// | | | | |
+			// +-+-+-+-+
+			// | | | | |
+			// +-+-+-+-+
+			// | | |x|x|
+			// +-+-+-+-+
+			// | | |x|x|
+			// +-+-+-+-+
+			Array<Plane, 6>& planes2 = tiles[j][TILES_X_COUNT - i - 1].planes;
+
+			planes2[Frustum::FP_RIGHT] = planes[Frustum::FP_LEFT];
+			planes2[Frustum::FP_RIGHT].getNormal().x() = 
+				-planes2[Frustum::FP_RIGHT].getNormal().x();
+
+			planes2[Frustum::FP_LEFT] = planes[Frustum::FP_RIGHT];
+			planes2[Frustum::FP_LEFT].getNormal().x() = 
+				-planes2[Frustum::FP_LEFT].getNormal().x();
+
+			planes2[Frustum::FP_BOTTOM] = planes[Frustum::FP_BOTTOM];
+			planes2[Frustum::FP_TOP] = planes[Frustum::FP_TOP];
+
+			// Mirror planes for those tiles:
+			// +-+-+-+-+
+			// |x|x| | |
+			// +-+-+-+-+
+			// |x|x| | |
+			// +-+-+-+-+
+			// | | | | |
+			// +-+-+-+-+
+			// | | | | |
+			// +-+-+-+-+
+			Array<Plane, 6>& planes3 = tiles[TILES_Y_COUNT - j - 1][i].planes;
+
+			planes3[Frustum::FP_RIGHT] = planes[Frustum::FP_RIGHT];
+			planes3[Frustum::FP_LEFT] = planes[Frustum::FP_RIGHT];
+
+			planes3[Frustum::FP_BOTTOM] = planes[Frustum::FP_TOP];
+			planes3[Frustum::FP_BOTTOM].getNormal().y() = 
+				-planes3[Frustum::FP_BOTTOM].getNormal().y();
+
+			planes3[Frustum::FP_TOP] = planes[Frustum::FP_BOTTOM];
+			planes3[Frustum::FP_TOP].getNormal().y() = 
+				-planes3[Frustum::FP_TOP].getNormal().y();
+
+			// Mirror planes for those tiles:
+			// +-+-+-+-+
+			// | | |x|x|
+			// +-+-+-+-+
+			// | | |x|x|
+			// +-+-+-+-+
+			// | | | | |
+			// +-+-+-+-+
+			// | | | | |
+			// +-+-+-+-+
+			Array<Plane, 6>& planes4 = 
+				tiles[TILES_Y_COUNT - j - 1][TILES_X_COUNT - i - 1].planes;
+
+			planes4[Frustum::FP_RIGHT] = planes2[Frustum::FP_RIGHT];
+			planes4[Frustum::FP_LEFT] = planes2[Frustum::FP_RIGHT];
+
+			planes4[Frustum::FP_BOTTOM] = planes3[Frustum::FP_BOTTOM];
+			planes4[Frustum::FP_TOP] = planes3[Frustum::FP_TOP];
 		}
 	}
 }