Exemple #1
0
//==============================================================================
void Camera::onMoveComponentUpdate(MoveComponent& move)
{
	// Frustum
	FrustumComponent& fr = getComponent<FrustumComponent>();
	fr.markTransformForUpdate();
	fr.getFrustum().resetTransform(move.getWorldTransform());

	// Spatial
	SpatialComponent& sp = getComponent<SpatialComponent>();
	sp.markForUpdate();
	sp.setSpatialOrigin(move.getWorldTransform().getOrigin());
}
//==============================================================================
void SceneDebugDrawer::draw(SceneNode& node)
{
	MoveComponent* mv = node.tryGetComponent<MoveComponent>();
	if(mv)
	{
		m_dbg->setModelMatrix(Mat4(mv->getWorldTransform()));
	}
	else
	{
		m_dbg->setModelMatrix(Mat4::getIdentity());
	}

	FrustumComponent* fr = node.tryGetComponent<FrustumComponent>();
	if(fr)
	{
		draw(*fr);
	}

	Error err = node.iterateComponentsOfType<SpatialComponent>(
		[&](SpatialComponent& sp) -> Error
	{
		draw(sp);
		return ErrorCode::NONE;
	});

	PortalSectorComponent* ps = node.tryGetComponent<PortalSectorComponent>();
	if(ps)
	{
		draw(*ps);
	}

	(void)err;
}
void OccluderNode::onMoveComponentUpdate(MoveComponent& movec)
{
	const Transform& trf(movec.getWorldTransform());
	U count = m_vertsL.getSize();
	while(count--)
	{

		m_vertsW[count] = trf.transform(m_vertsL[count]);
	}

	getComponent<OccluderComponent>().setVertices(&m_vertsW[0], m_vertsW.getSize(), sizeof(m_vertsW[0]));
}
Exemple #4
0
Error Dbg::run(RenderingContext& ctx)
{
	ANKI_ASSERT(m_enabled);

	if(!m_initialized)
	{
		ANKI_CHECK(lazyInit());
		m_initialized = true;
	}

	CommandBufferPtr& cmdb = ctx.m_commandBuffer;
	cmdb->beginRenderPass(m_fb);
	cmdb->setViewport(0, 0, m_r->getWidth(), m_r->getHeight());

	FrustumComponent& camFrc = *ctx.m_frustumComponent;
	SceneNode& cam = camFrc.getSceneNode();
	m_drawer->prepareFrame(cmdb);
	m_drawer->setViewProjectionMatrix(camFrc.getViewProjectionMatrix());
	m_drawer->setModelMatrix(Mat4::getIdentity());
	// m_drawer->drawGrid();

	SceneGraph& scene = cam.getSceneGraph();

	SceneDebugDrawer sceneDrawer(m_drawer);
	camFrc.getVisibilityTestResults().iterateAll([&](SceneNode& node) {
		if(&node == &cam)
		{
			return;
		}

		// Set position
		MoveComponent* mv = node.tryGetComponent<MoveComponent>();
		if(mv)
		{
			m_drawer->setModelMatrix(Mat4(mv->getWorldTransform()));
		}
		else
		{
			m_drawer->setModelMatrix(Mat4::getIdentity());
		}

		// Spatial
		if(m_flags.get(DbgFlag::SPATIAL_COMPONENT))
		{
			Error err = node.iterateComponentsOfType<SpatialComponent>([&](SpatialComponent& sp) -> Error {
				sceneDrawer.draw(sp);
				return ErrorCode::NONE;
			});
			(void)err;
		}

		// Frustum
		if(m_flags.get(DbgFlag::FRUSTUM_COMPONENT))
		{
			Error err = node.iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& frc) -> Error {
				if(&frc != &camFrc)
				{
					sceneDrawer.draw(frc);
				}
				return ErrorCode::NONE;
			});
			(void)err;
		}

		// Sector/portal
		if(m_flags.get(DbgFlag::SECTOR_COMPONENT))
		{
			Error err = node.iterateComponentsOfType<SectorComponent>([&](SectorComponent& psc) -> Error {
				sceneDrawer.draw(psc);
				return ErrorCode::NONE;
			});

			err = node.iterateComponentsOfType<PortalComponent>([&](PortalComponent& psc) -> Error {
				sceneDrawer.draw(psc);
				return ErrorCode::NONE;
			});
			(void)err;
		}

		// Decal
		if(m_flags.get(DbgFlag::DECAL_COMPONENT))
		{
			Error err = node.iterateComponentsOfType<DecalComponent>([&](DecalComponent& psc) -> Error {
				sceneDrawer.draw(psc);
				return ErrorCode::NONE;
			});
			(void)err;
		}
	});

	if(m_flags.get(DbgFlag::PHYSICS))
	{
		PhysicsDebugDrawer phyd(m_drawer);

		m_drawer->setModelMatrix(Mat4::getIdentity());
		phyd.drawWorld(scene.getPhysicsWorld());
	}

#if 0
	{
		m_drawer->setViewProjectionMatrix(camFrc.getViewProjectionMatrix());
		m_drawer->setModelMatrix(Mat4::getIdentity());
		CollisionDebugDrawer cd(m_drawer);
		Mat4 proj = camFrc.getProjectionMatrix();

		m_drawer->setViewProjectionMatrix(camFrc.getViewProjectionMatrix());

		Sphere s(Vec4(1.2, 2.0, -1.1, 0.0), 2.1);

		s.accept(cd);

		Transform trf = scene.findSceneNode("light0").getComponent<MoveComponent>().getWorldTransform();
		Vec4 rayOrigin = trf.getOrigin();
		Vec3 rayDir = -trf.getRotation().getZAxis().getNormalized();
		m_drawer->setModelMatrix(Mat4::getIdentity());
		m_drawer->drawLine(rayOrigin.xyz(), rayOrigin.xyz() + rayDir.xyz() * 10.0, Vec4(1.0, 1.0, 1.0, 1.0));

		Array<Vec4, 2> intersectionPoints;
		U intersectionPointCount;
		s.intersectsRay(rayDir.xyz0(), rayOrigin, intersectionPoints, intersectionPointCount);
		for(U i = 0; i < intersectionPointCount; ++i)
		{
			m_drawer->drawLine(Vec3(0.0), intersectionPoints[i].xyz(), Vec4(0.0, 1.0, 0.0, 1.0));
		}
	}
#endif

#if 0
	{
		Clusterer c;
		c.init(getAllocator(), 16, 12, 30);

		const FrustumComponent& frc = scene.findSceneNode("cam0").getComponent<FrustumComponent>();
		const MoveComponent& movc = scene.findSceneNode("cam0").getComponent<MoveComponent>();

		ClustererPrepareInfo pinf;
		pinf.m_viewMat = frc.getViewMatrix();
		pinf.m_projMat = frc.getProjectionMatrix();
		pinf.m_camTrf = frc.getFrustum().getTransform();
		c.prepare(m_r->getThreadPool(), pinf);

		class DD : public ClustererDebugDrawer
		{
		public:
			DebugDrawer* m_d;

			void operator()(const Vec3& lineA, const Vec3& lineB, const Vec3& color)
			{
				m_d->drawLine(lineA, lineB, color.xyz1());
			}
		};

		DD dd;
		dd.m_d = m_drawer;

		CollisionDebugDrawer cd(m_drawer);

		Sphere s(Vec4(1.0, 0.1, -1.2, 0.0), 1.2);
		PerspectiveFrustum fr(toRad(25.), toRad(35.), 0.1, 5.);
		fr.transform(Transform(Vec4(0., 1., 0., 0.), Mat3x4::getIdentity(), 1.0));

		m_drawer->setModelMatrix(Mat4(movc.getWorldTransform()));
		// c.debugDraw(dd);

		if(frc.getFrustum().insideFrustum(fr))
		{
			ClustererTestResult rez;
			c.initTestResults(getAllocator(), rez);
			Aabb sbox;
			fr.computeAabb(sbox);
			c.binPerspectiveFrustum(fr, sbox, rez);
			//c.bin(s, sbox, rez);

			c.debugDrawResult(rez, dd);
		}

		m_drawer->setColor(Vec4(1.0, 1.0, 0.0, 1.0));
		frc.getFrustum().accept(cd);
		fr.accept(cd);
	}
#endif

#if 0
	{
		CollisionDebugDrawer cd(m_drawer);

		Array<Vec3, 4> poly;
		poly[0] = Vec3(0.0, 0.0, 0.0);
		poly[1] = Vec3(2.5, 0.0, 0.0);

		Mat4 trf(Vec4(147.392776, -12.132728, 16.607138, 1.0),
			Mat3(Euler(toRad(45.0), toRad(0.0), toRad(120.0))),
			1.0);

		Array<Vec3, 4> polyw;
		polyw[0] = trf.transform(poly[0]);
		polyw[1] = trf.transform(poly[1]);

		m_drawer->setModelMatrix(Mat4::getIdentity());
		m_drawer->drawLine(polyw[0], polyw[1], Vec4(1.0));

		Vec4 p0 = camFrc.getViewMatrix() * polyw[0].xyz1();
		p0.w() = 0.0;
		Vec4 p1 = camFrc.getViewMatrix() * polyw[1].xyz1();
		p1.w() = 0.0;

		Vec4 r = p1 - p0;
		r.normalize();

		Vec4 a = camFrc.getProjectionMatrix() * p0.xyz1();
		a /= a.w();

		Vec4 i;
		if(r.z() > 0)
		{
			// Plane near(Vec4(0, 0, -1, 0), camFrc.getFrustum().getNear() +
			// 0.001);
			// Bool in = near.intersectRay(p0, r * 100000.0, i);
			i.z() = -camFrc.getFrustum().getNear();
			F32 t = (i.z() - p0.z()) / r.z();
			i.x() = p0.x() + t * r.x();
			i.y() = p0.y() + t * r.y();

			i = camFrc.getProjectionMatrix() * i.xyz1();
			i /= i.w();
		}
		else
		{
			i = camFrc.getProjectionMatrix() * (r * 100000.0).xyz1();
			i /= i.w();
		}

		/*r *= 0.01;
		Vec4 b = polyw[0].xyz0() + r;
		b = camFrc.getViewProjectionMatrix() * b.xyz1();
		Vec4 d = b / b.w();*/

		m_drawer->setViewProjectionMatrix(Mat4::getIdentity());
		m_drawer->drawLine(
			Vec3(a.xy(), 0.1), Vec3(i.xy(), 0.1), Vec4(1.0, 0, 0, 1));
	}
#endif

	m_drawer->finishFrame();
	cmdb->endRenderPass();
	return ErrorCode::NONE;
}