浏览代码

Fixing bugs

Panagiotis Christopoulos Charitos 11 年之前
父节点
当前提交
3331abe207
共有 5 个文件被更改,包括 118 次插入124 次删除
  1. 21 12
      include/anki/collision/Frustum.h
  2. 3 3
      shaders/IsLp.frag.glsl
  3. 90 101
      src/collision/Frustum.cpp
  4. 1 1
      src/renderer/Tiler.cpp
  5. 3 7
      testapp/Main.cpp

+ 21 - 12
include/anki/collision/Frustum.h

@@ -111,7 +111,7 @@ protected:
 	/// @}
 
 	/// Used to check against the frustum
-	Array<Plane, (U)PlaneType::COUNT> m_planes;
+	Array<Plane, (U)PlaneType::COUNT> m_planesL;
 	Array<Plane, (U)PlaneType::COUNT> m_planesW;
 
 	/// Keep the transformation.
@@ -122,8 +122,10 @@ protected:
 
 	/// Called when a viewing variable changes. It recalculates the planes and
 	/// the other variables.
-	/// @note It's const because it must be called on const methods.
-	virtual void recalculate(Bool planes, Bool other) = 0;
+	virtual void recalculate() = 0;
+
+	/// Called when there is a change in the transformation.
+	virtual void onTransform() = 0;
 
 	/// Update if dirty
 	void update() const;
@@ -190,7 +192,8 @@ public:
 
 	const Array<Vec4, 5>& getPoints() const
 	{
-		return m_points;
+		update();
+		return m_pointsW;
 	}
 
 	/// Copy
@@ -216,14 +219,17 @@ private:
 
 	/// @name Shape
 	/// @{
-	Array<Vec4, 5> m_points;
+	Array<Vec4, 5> m_pointsW;
+	Array<Vec4, 4> m_pointsL; ///< Don't need the eye point.
 	ConvexHullShape m_hull;
 	/// @}
 
 	/// Implements Frustum::recalculate. Recalculates:
 	/// @li planes
 	/// @li line segments
-	void recalculate(Bool planes, Bool other) override;
+	void recalculate() override;
+
+	void onTransform() override;
 };
 
 /// Frustum shape for orthographic cameras
@@ -235,7 +241,7 @@ public:
 
 	/// Copy
 	OrthographicFrustum(const OrthographicFrustum& b)
-		: OrthographicFrustum()
+	:	OrthographicFrustum()
 	{
 		*this = b;
 	}
@@ -243,7 +249,7 @@ public:
 	/// Set all
 	OrthographicFrustum(F32 left, F32 right, F32 near,
 		F32 far, F32 top, F32 bottom)
-		: OrthographicFrustum()
+	:	OrthographicFrustum()
 	{
 		setAll(left, right, near, far, top, bottom);
 	}
@@ -304,7 +310,8 @@ public:
 	/// Needed for debug drawing
 	const Obb& getObb() const
 	{
-		return m_obb;
+		update();
+		return m_obbW;
 	}
 
 	/// Copy
@@ -329,11 +336,13 @@ private:
 
 	/// @name Shape
 	/// @{
-	Obb m_obb; ///< Including shape
+	Obb m_obbL, m_obbW; ///< Including shape
 	/// @}
 
-	/// Implements Frustum::recalculate. Recalculate @a m_planes and @a m_obb
-	void recalculate(Bool planes, Bool other) override;
+	/// Implements Frustum::recalculate. Recalculate @a m_planesL and @a m_obb
+	void recalculate() override;
+
+	void onTransform() override;
 };
 /// @}
 

+ 3 - 3
shaders/IsLp.frag.glsl

@@ -345,10 +345,10 @@ void main()
 		* vec3(0.01, 0.001, 0.001);
 #endif
 
-#if 1
-	if(spotTexLightsCount != 0)
+#if 0
+	if(pointLightsCount != 0)
 	{
-		out_color = vec3(0.5, 0.0, 0.0);
+		out_color = vec3(0.5 / float(pointLightsCount));
 	}
 #endif
 }

+ 90 - 101
src/collision/Frustum.cpp

@@ -6,7 +6,6 @@
 #include "anki/collision/Frustum.h"
 #include "anki/collision/LineSegment.h"
 #include "anki/collision/Aabb.h"
-#include <limits>
 
 namespace anki {
 
@@ -20,7 +19,7 @@ Frustum& Frustum::operator=(const Frustum& b)
 	ANKI_ASSERT(m_type == b.m_type);
 	m_near = b.m_near;
 	m_far = b.m_far;
-	m_planes = b.m_planes;
+	m_planesL = b.m_planesL;
 	m_planesW = b.m_planesW;
 	m_trf = b.m_trf;
 	m_frustumDirty = b.m_frustumDirty;
@@ -74,27 +73,8 @@ Bool Frustum::insideFrustum(const CollisionShape& b)
 //==============================================================================
 void Frustum::transform(const Transform& trf)
 {
-	m_trf = m_trf.combineTransformations(trf);
-
-	if(m_frustumDirty)
-	{
-		// Update everything
-		updateInternal();
-	}
-	else
-	{
-		// Update only the other shapes
-		recalculate(false, true);
-
-		// Transform the compound
-		CompoundShape::transform(m_trf);
-
-		// Transform the planes
-		for(U i = 0; i < m_planes.getSize(); ++i)
-		{
-			m_planesW[i] = m_planes[i].getTransformed(m_trf);
-		}
-	}
+	Transform trfa = m_trf.combineTransformations(trf);
+	resetTransform(trfa);
 }
 
 //==============================================================================
@@ -109,16 +89,13 @@ void Frustum::resetTransform(const Transform& trf)
 	}
 	else
 	{
-		// Update only the other shapes
-		recalculate(false, true);
-	
-		// Transform the compound
-		CompoundShape::transform(m_trf);
+		// Inform the child about the change
+		onTransform();
 
 		// Transform the planes
-		for(U i = 0; i < m_planes.getSize(); ++i)
+		for(U i = 0; i < m_planesL.getSize(); ++i)
 		{
-			m_planesW[i] = m_planes[i].getTransformed(m_trf);
+			m_planesW[i] = m_planesL[i].getTransformed(m_trf);
 		}
 	}
 }
@@ -138,15 +115,15 @@ void Frustum::updateInternal()
 {
 	ANKI_ASSERT(m_frustumDirty);
 	m_frustumDirty = false;	
-	recalculate(true, true);
+	recalculate();
 
-	// Transform the compound
-	CompoundShape::transform(m_trf);
+	// Transform derived
+	onTransform();
 
 	// Transform the planes
-	for(U i = 0; i < m_planes.getSize(); ++i)
+	for(U i = 0; i < m_planesL.getSize(); ++i)
 	{
-		m_planesW[i] = m_planes[i].getTransformed(m_trf);
+		m_planesW[i] = m_planesL[i].getTransformed(m_trf);
 	}
 }
 
@@ -159,7 +136,7 @@ PerspectiveFrustum::PerspectiveFrustum()
 :	Frustum(Type::PERSPECTIVE)
 {
 	addShape(&m_hull);
-	m_hull.initStorage(&m_points[0], m_points.getSize());
+	m_hull.initStorage(&m_pointsW[0], m_pointsW.getSize());
 }
 
 //==============================================================================
@@ -168,52 +145,63 @@ PerspectiveFrustum& PerspectiveFrustum::operator=(const PerspectiveFrustum& b)
 	Frustum::operator=(b);
 	m_fovX = b.m_fovX;
 	m_fovY = b.m_fovY;
-	m_points = b.m_points;
+	m_pointsL = b.m_pointsL;
+	m_pointsW = b.m_pointsW;
 	return *this;
 }
 
 //==============================================================================
-void PerspectiveFrustum::recalculate(Bool planes, Bool other)
+void PerspectiveFrustum::onTransform()
 {
-	if(planes)
+	// Eye
+	m_pointsW[0] = m_trf.getOrigin();
+
+	for(U i = 1; i < 5; ++i)
 	{
-		F32 c, s; // cos & sine
-
-		sinCos(getPi<F32>() + m_fovX / 2.0, s, c);
-		// right
-		m_planes[(U)PlaneType::RIGHT] = Plane(Vec4(c, 0.0, s, 0.0), 0.0);
-		// left
-		m_planes[(U)PlaneType::LEFT] = Plane(Vec4(-c, 0.0, s, 0.0), 0.0);
-
-		sinCos((getPi<F32>() + m_fovY) * 0.5, s, c);
-		// bottom
-		m_planes[(U)PlaneType::BOTTOM] = Plane(Vec4(0.0, s, c, 0.0), 0.0);
-		// top
-		m_planes[(U)PlaneType::TOP] = Plane(Vec4(0.0, -s, c, 0.0), 0.0);
-
-		// near
-		m_planes[(U)PlaneType::NEAR] = Plane(Vec4(0.0, 0.0, -1.0, 0.0), m_near);
-		// far
-		m_planes[(U)PlaneType::FAR] = Plane(Vec4(0.0, 0.0, 1.0, 0.0), -m_far);
+		m_pointsW[i] = m_trf.transform(m_pointsL[i - 1]);
 	}
+}
 
-	if(other)
+//==============================================================================
+void PerspectiveFrustum::recalculate()
+{
+	// Planes
+	//
+	F32 c, s; // cos & sine
+
+	sinCos(getPi<F32>() + m_fovX / 2.0, s, c);
+	// right
+	m_planesL[(U)PlaneType::RIGHT] = Plane(Vec4(c, 0.0, s, 0.0), 0.0);
+	// left
+	m_planesL[(U)PlaneType::LEFT] = Plane(Vec4(-c, 0.0, s, 0.0), 0.0);
+
+	sinCos((getPi<F32>() + m_fovY) * 0.5, s, c);
+	// bottom
+	m_planesL[(U)PlaneType::BOTTOM] = Plane(Vec4(0.0, s, c, 0.0), 0.0);
+	// top
+	m_planesL[(U)PlaneType::TOP] = Plane(Vec4(0.0, -s, c, 0.0), 0.0);
+
+	// near
+	m_planesL[(U)PlaneType::NEAR] = Plane(Vec4(0.0, 0.0, -1.0, 0.0), m_near);
+	// far
+	m_planesL[(U)PlaneType::FAR] = Plane(Vec4(0.0, 0.0, 1.0, 0.0), -m_far);
+
+	// Points
+	//
+	Vec4 eye = Vec4(0.0, 0.0, -m_near, 0.0);
+	for(Vec4& p : m_pointsL)
 	{
-		Vec4 eye = Vec4(0.0, 0.0, -m_near, 0.0);
-		for(Vec4& p : m_points)
-		{
-			p = eye;
-		}
+		p = eye;
+	}
 
-		F32 x = m_far / tan((getPi<F32>() - m_fovX) / 2.0);
-		F32 y = tan(m_fovY / 2.0) * m_far;
-		F32 z = -m_far;
+	F32 x = m_far / tan((getPi<F32>() - m_fovX) / 2.0);
+	F32 y = tan(m_fovY / 2.0) * m_far;
+	F32 z = -m_far;
 
-		m_points[1] += Vec4(x, y, z - m_near, 0.0); // top right
-		m_points[2] += Vec4(-x, y, z - m_near, 0.0); // top left
-		m_points[3] += Vec4(-x, -y, z - m_near, 0.0); // bot left
-		m_points[4] += Vec4(x, -y, z - m_near, 0.0); // bot right
-	}
+	m_pointsL[0] += Vec4(x, y, z - m_near, 0.0); // top right
+	m_pointsL[1] += Vec4(-x, y, z - m_near, 0.0); // top left
+	m_pointsL[2] += Vec4(-x, -y, z - m_near, 0.0); // bot left
+	m_pointsL[3] += Vec4(x, -y, z - m_near, 0.0); // bot right
 }
 
 //==============================================================================
@@ -272,7 +260,7 @@ Mat4 PerspectiveFrustum::calculateProjectionMatrix() const
 OrthographicFrustum::OrthographicFrustum()
 :	Frustum(Type::ORTHOGRAPHIC)
 {
-	addShape(&m_obb);
+	addShape(&m_obbW);
 }
 
 //==============================================================================
@@ -284,7 +272,8 @@ OrthographicFrustum& OrthographicFrustum::operator=(
 	m_right = b.m_right;
 	m_top = b.m_top;
 	m_bottom = b.m_bottom;
-	m_obb = b.m_obb;
+	m_obbL = b.m_obbL;
+	m_obbW = b.m_obbW;
 	return *this;
 }
 
@@ -322,36 +311,36 @@ Mat4 OrthographicFrustum::calculateProjectionMatrix() const
 }
 
 //==============================================================================
-void OrthographicFrustum::recalculate(Bool planes, Bool other)
+void OrthographicFrustum::recalculate()
 {
-	if(planes)
-	{
-		// Planes
-		m_planes[(U)PlaneType::LEFT] = 
-			Plane(Vec4(1.0, 0.0, 0.0, 0.0), m_left);
-		m_planes[(U)PlaneType::RIGHT] = 
-			Plane(Vec4(-1.0, 0.0, 0.0, 0.0), -m_right);
-
-		m_planes[(U)PlaneType::NEAR] = 
-			Plane(Vec4(0.0, 0.0, -1.0, 0.0), m_near);
-		m_planes[(U)PlaneType::FAR] = 
-			Plane(Vec4(0.0, 0.0, 1.0, 0.0), -m_far);
-		m_planes[(U)PlaneType::TOP] = 
-			Plane(Vec4(0.0, -1.0, 0.0, 0.0), -m_top);
-		m_planes[(U)PlaneType::BOTTOM] = 
-			Plane(Vec4(0.0, 1.0, 0.0, 0.0), m_bottom);
-	}
+	// Planes
+	m_planesL[(U)PlaneType::LEFT] = 
+		Plane(Vec4(1.0, 0.0, 0.0, 0.0), m_left);
+	m_planesL[(U)PlaneType::RIGHT] = 
+		Plane(Vec4(-1.0, 0.0, 0.0, 0.0), -m_right);
+
+	m_planesL[(U)PlaneType::NEAR] = 
+		Plane(Vec4(0.0, 0.0, -1.0, 0.0), m_near);
+	m_planesL[(U)PlaneType::FAR] = 
+		Plane(Vec4(0.0, 0.0, 1.0, 0.0), -m_far);
+	m_planesL[(U)PlaneType::TOP] = 
+		Plane(Vec4(0.0, -1.0, 0.0, 0.0), -m_top);
+	m_planesL[(U)PlaneType::BOTTOM] = 
+		Plane(Vec4(0.0, 1.0, 0.0, 0.0), m_bottom);
+
+	// OBB
+	Vec4 c((m_right + m_left) * 0.5, 
+		(m_top + m_bottom) * 0.5, 
+		-(m_far + m_near) * 0.5,
+		0.0);
+	Vec4 e = Vec4(m_right, m_top, -m_far, 0.0) - c;
+	m_obbL = Obb(c, Mat3x4::getIdentity(), e);
+}
 
-	if(other)
-	{
-		// OBB
-		Vec4 c((m_right + m_left) * 0.5, 
-			(m_top + m_bottom) * 0.5, 
-			-(m_far + m_near) * 0.5,
-			0.0);
-		Vec4 e = Vec4(m_right, m_top, -m_far, 0.0) - c;
-		m_obb = Obb(c, Mat3x4::getIdentity(), e);
-	}
+//==============================================================================
+void OrthographicFrustum::onTransform()
+{
+	m_obbW = m_obbL.getTransformed(m_trf);
 }
 
 } // end namespace anki

+ 1 - 1
src/renderer/Tiler.cpp

@@ -226,7 +226,7 @@ Bool Tiler::test(
 
 	// Call the recursive function or the fast path
 	U count = 0;
-#if 0
+#if 1
 	if(isa<Sphere>(cs))
 	{
 		testFastSphere(dcast<const Sphere&>(cs), visible, count);

+ 3 - 7
testapp/Main.cpp

@@ -82,11 +82,6 @@ Error init()
 		1.0));
 #endif
 
-	cam->getComponent<MoveComponent>().
-		setLocalTransform(Transform(Vec4(0.0, 0, 10, 0),
-		Mat3x4::getIdentity(),
-		1.0));
-
 	// lights
 #if 0
 	Vec3 lpos(-24.0, 0.1, -10.0);
@@ -250,7 +245,7 @@ Error init()
 
 	if(0)
 	{
-		err = scene.newSceneNode<SpotLight>("plight0", spot);
+		err = scene.newSceneNode<SpotLight>("horse", spot);
 		if(err) return err;
 
 		lightc = spot->tryGetComponent<LightComponent>();
@@ -260,6 +255,7 @@ Error init()
 
 		move = spot->tryGetComponent<MoveComponent>();
 		move->setLocalOrigin(Vec4(1.0, 2.0, 0.2, 0.0));
+		move->setLocalRotation(Mat3x4(Axisang(toRad(90.0), Vec3(0, 1, 0))));
 	}
 
 #if 1
@@ -533,7 +529,7 @@ Error initSubsystems(int argc, char* argv[])
 
 	//config.set("maxTextureSize", 256);
 
-	config.set("fullscreenDesktopResolution", true);
+	config.set("fullscreenDesktopResolution", false);
 	config.set("debugContext", false);
 
 	app = new App;