123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- #pragma once
- #include "plane3d.h"
- #include "vector3d.h"
- #include "line3d.h"
- #include "aabbox3d.h"
- #include "matrix4.h"
- #include "EVideoTypes.h"
- namespace irr
- {
- namespace scene
- {
- struct SViewFrustum
- {
- enum VFPLANES
- {
-
- VF_FAR_PLANE = 0,
-
- VF_NEAR_PLANE,
-
- VF_LEFT_PLANE,
-
- VF_RIGHT_PLANE,
-
- VF_BOTTOM_PLANE,
-
- VF_TOP_PLANE,
-
- VF_PLANE_COUNT
- };
-
- SViewFrustum() :
- BoundingRadius(0.f), FarNearDistance(0.f) {}
-
- SViewFrustum(const SViewFrustum &other);
-
-
- SViewFrustum(const core::matrix4 &mat, bool zClipFromZero);
-
-
- inline void setFrom(const core::matrix4 &mat, bool zClipFromZero);
-
-
- void transform(const core::matrix4 &mat);
-
- core::vector3df getFarLeftUp() const;
-
- core::vector3df getFarLeftDown() const;
-
- core::vector3df getFarRightUp() const;
-
- core::vector3df getFarRightDown() const;
-
- core::vector3df getNearLeftUp() const;
-
- core::vector3df getNearLeftDown() const;
-
- core::vector3df getNearRightUp() const;
-
- core::vector3df getNearRightDown() const;
-
- const core::aabbox3d<f32> &getBoundingBox() const;
-
- inline void recalculateBoundingBox();
-
- float getBoundingRadius() const;
-
- core::vector3df getBoundingCenter() const;
-
- void setFarNearDistance(float distance);
-
- core::matrix4 &getTransform(video::E_TRANSFORMATION_STATE state);
-
- const core::matrix4 &getTransform(video::E_TRANSFORMATION_STATE state) const;
-
-
- bool clipLine(core::line3d<f32> &line) const;
-
- core::vector3df cameraPosition;
-
- core::plane3d<f32> planes[VF_PLANE_COUNT];
-
- core::aabbox3d<f32> boundingBox;
- private:
-
- enum E_TRANSFORMATION_STATE_FRUSTUM
- {
- ETS_VIEW = 0,
- ETS_PROJECTION = 1,
- ETS_COUNT_FRUSTUM
- };
-
- inline void recalculateBoundingSphere();
-
- core::matrix4 Matrices[ETS_COUNT_FRUSTUM];
- float BoundingRadius;
- float FarNearDistance;
- core::vector3df BoundingCenter;
- };
- inline SViewFrustum::SViewFrustum(const SViewFrustum &other)
- {
- cameraPosition = other.cameraPosition;
- boundingBox = other.boundingBox;
- u32 i;
- for (i = 0; i < VF_PLANE_COUNT; ++i)
- planes[i] = other.planes[i];
- for (i = 0; i < ETS_COUNT_FRUSTUM; ++i)
- Matrices[i] = other.Matrices[i];
- BoundingRadius = other.BoundingRadius;
- FarNearDistance = other.FarNearDistance;
- BoundingCenter = other.BoundingCenter;
- }
- inline SViewFrustum::SViewFrustum(const core::matrix4 &mat, bool zClipFromZero)
- {
- setFrom(mat, zClipFromZero);
- }
- inline void SViewFrustum::transform(const core::matrix4 &mat)
- {
- for (u32 i = 0; i < VF_PLANE_COUNT; ++i)
- mat.transformPlane(planes[i]);
- mat.transformVect(cameraPosition);
- recalculateBoundingBox();
- }
- inline core::vector3df SViewFrustum::getFarLeftUp() const
- {
- core::vector3df p;
- planes[scene::SViewFrustum::VF_FAR_PLANE].getIntersectionWithPlanes(
- planes[scene::SViewFrustum::VF_TOP_PLANE],
- planes[scene::SViewFrustum::VF_LEFT_PLANE], p);
- return p;
- }
- inline core::vector3df SViewFrustum::getFarLeftDown() const
- {
- core::vector3df p;
- planes[scene::SViewFrustum::VF_FAR_PLANE].getIntersectionWithPlanes(
- planes[scene::SViewFrustum::VF_BOTTOM_PLANE],
- planes[scene::SViewFrustum::VF_LEFT_PLANE], p);
- return p;
- }
- inline core::vector3df SViewFrustum::getFarRightUp() const
- {
- core::vector3df p;
- planes[scene::SViewFrustum::VF_FAR_PLANE].getIntersectionWithPlanes(
- planes[scene::SViewFrustum::VF_TOP_PLANE],
- planes[scene::SViewFrustum::VF_RIGHT_PLANE], p);
- return p;
- }
- inline core::vector3df SViewFrustum::getFarRightDown() const
- {
- core::vector3df p;
- planes[scene::SViewFrustum::VF_FAR_PLANE].getIntersectionWithPlanes(
- planes[scene::SViewFrustum::VF_BOTTOM_PLANE],
- planes[scene::SViewFrustum::VF_RIGHT_PLANE], p);
- return p;
- }
- inline core::vector3df SViewFrustum::getNearLeftUp() const
- {
- core::vector3df p;
- planes[scene::SViewFrustum::VF_NEAR_PLANE].getIntersectionWithPlanes(
- planes[scene::SViewFrustum::VF_TOP_PLANE],
- planes[scene::SViewFrustum::VF_LEFT_PLANE], p);
- return p;
- }
- inline core::vector3df SViewFrustum::getNearLeftDown() const
- {
- core::vector3df p;
- planes[scene::SViewFrustum::VF_NEAR_PLANE].getIntersectionWithPlanes(
- planes[scene::SViewFrustum::VF_BOTTOM_PLANE],
- planes[scene::SViewFrustum::VF_LEFT_PLANE], p);
- return p;
- }
- inline core::vector3df SViewFrustum::getNearRightUp() const
- {
- core::vector3df p;
- planes[scene::SViewFrustum::VF_NEAR_PLANE].getIntersectionWithPlanes(
- planes[scene::SViewFrustum::VF_TOP_PLANE],
- planes[scene::SViewFrustum::VF_RIGHT_PLANE], p);
- return p;
- }
- inline core::vector3df SViewFrustum::getNearRightDown() const
- {
- core::vector3df p;
- planes[scene::SViewFrustum::VF_NEAR_PLANE].getIntersectionWithPlanes(
- planes[scene::SViewFrustum::VF_BOTTOM_PLANE],
- planes[scene::SViewFrustum::VF_RIGHT_PLANE], p);
- return p;
- }
- inline const core::aabbox3d<f32> &SViewFrustum::getBoundingBox() const
- {
- return boundingBox;
- }
- inline void SViewFrustum::recalculateBoundingBox()
- {
- boundingBox.reset(getNearLeftUp());
- boundingBox.addInternalPoint(getNearRightUp());
- boundingBox.addInternalPoint(getNearLeftDown());
- boundingBox.addInternalPoint(getNearRightDown());
- boundingBox.addInternalPoint(getFarRightUp());
- boundingBox.addInternalPoint(getFarLeftDown());
- boundingBox.addInternalPoint(getFarRightDown());
- boundingBox.addInternalPoint(getFarLeftUp());
-
- recalculateBoundingSphere();
- }
- inline float SViewFrustum::getBoundingRadius() const
- {
- return BoundingRadius;
- }
- inline core::vector3df SViewFrustum::getBoundingCenter() const
- {
- return BoundingCenter;
- }
- inline void SViewFrustum::setFarNearDistance(float distance)
- {
- FarNearDistance = distance;
- }
- inline void SViewFrustum::setFrom(const core::matrix4 &mat, bool zClipFromZero)
- {
-
- planes[VF_LEFT_PLANE].Normal.X = mat[3] + mat[0];
- planes[VF_LEFT_PLANE].Normal.Y = mat[7] + mat[4];
- planes[VF_LEFT_PLANE].Normal.Z = mat[11] + mat[8];
- planes[VF_LEFT_PLANE].D = mat[15] + mat[12];
-
- planes[VF_RIGHT_PLANE].Normal.X = mat[3] - mat[0];
- planes[VF_RIGHT_PLANE].Normal.Y = mat[7] - mat[4];
- planes[VF_RIGHT_PLANE].Normal.Z = mat[11] - mat[8];
- planes[VF_RIGHT_PLANE].D = mat[15] - mat[12];
-
- planes[VF_TOP_PLANE].Normal.X = mat[3] - mat[1];
- planes[VF_TOP_PLANE].Normal.Y = mat[7] - mat[5];
- planes[VF_TOP_PLANE].Normal.Z = mat[11] - mat[9];
- planes[VF_TOP_PLANE].D = mat[15] - mat[13];
-
- planes[VF_BOTTOM_PLANE].Normal.X = mat[3] + mat[1];
- planes[VF_BOTTOM_PLANE].Normal.Y = mat[7] + mat[5];
- planes[VF_BOTTOM_PLANE].Normal.Z = mat[11] + mat[9];
- planes[VF_BOTTOM_PLANE].D = mat[15] + mat[13];
-
- planes[VF_FAR_PLANE].Normal.X = mat[3] - mat[2];
- planes[VF_FAR_PLANE].Normal.Y = mat[7] - mat[6];
- planes[VF_FAR_PLANE].Normal.Z = mat[11] - mat[10];
- planes[VF_FAR_PLANE].D = mat[15] - mat[14];
-
- if (zClipFromZero) {
- planes[VF_NEAR_PLANE].Normal.X = mat[2];
- planes[VF_NEAR_PLANE].Normal.Y = mat[6];
- planes[VF_NEAR_PLANE].Normal.Z = mat[10];
- planes[VF_NEAR_PLANE].D = mat[14];
- } else {
-
- planes[VF_NEAR_PLANE].Normal.X = mat[3] + mat[2];
- planes[VF_NEAR_PLANE].Normal.Y = mat[7] + mat[6];
- planes[VF_NEAR_PLANE].Normal.Z = mat[11] + mat[10];
- planes[VF_NEAR_PLANE].D = mat[15] + mat[14];
- }
-
- u32 i;
- for (i = 0; i != VF_PLANE_COUNT; ++i) {
- const f32 len = -core::reciprocal_squareroot(
- planes[i].Normal.getLengthSQ());
- planes[i].Normal *= len;
- planes[i].D *= len;
- }
-
- recalculateBoundingBox();
- }
- inline core::matrix4 &SViewFrustum::getTransform(video::E_TRANSFORMATION_STATE state)
- {
- u32 index = 0;
- switch (state) {
- case video::ETS_PROJECTION:
- index = SViewFrustum::ETS_PROJECTION;
- break;
- case video::ETS_VIEW:
- index = SViewFrustum::ETS_VIEW;
- break;
- default:
- break;
- }
- return Matrices[index];
- }
- inline const core::matrix4 &SViewFrustum::getTransform(video::E_TRANSFORMATION_STATE state) const
- {
- u32 index = 0;
- switch (state) {
- case video::ETS_PROJECTION:
- index = SViewFrustum::ETS_PROJECTION;
- break;
- case video::ETS_VIEW:
- index = SViewFrustum::ETS_VIEW;
- break;
- default:
- break;
- }
- return Matrices[index];
- }
- inline bool SViewFrustum::clipLine(core::line3d<f32> &line) const
- {
- bool wasClipped = false;
- for (u32 i = 0; i < VF_PLANE_COUNT; ++i) {
- if (planes[i].classifyPointRelation(line.start) == core::ISREL3D_FRONT) {
- line.start = line.start.getInterpolated(line.end,
- 1.f - planes[i].getKnownIntersectionWithLine(line.start, line.end));
- wasClipped = true;
- }
- if (planes[i].classifyPointRelation(line.end) == core::ISREL3D_FRONT) {
- line.end = line.start.getInterpolated(line.end,
- 1.f - planes[i].getKnownIntersectionWithLine(line.start, line.end));
- wasClipped = true;
- }
- }
- return wasClipped;
- }
- inline void SViewFrustum::recalculateBoundingSphere()
- {
-
- const float shortlen = (getNearLeftUp() - getNearRightUp()).getLength();
- const float longlen = (getFarLeftUp() - getFarRightUp()).getLength();
- const float farlen = FarNearDistance;
- const float fartocenter = (farlen + (shortlen - longlen) * (shortlen + longlen) / (4 * farlen)) / 2;
- const float neartocenter = farlen - fartocenter;
- BoundingCenter = cameraPosition + -planes[VF_NEAR_PLANE].Normal * neartocenter;
-
- core::vector3df dir[8];
- dir[0] = getFarLeftUp() - BoundingCenter;
- dir[1] = getFarRightUp() - BoundingCenter;
- dir[2] = getFarLeftDown() - BoundingCenter;
- dir[3] = getFarRightDown() - BoundingCenter;
- dir[4] = getNearRightDown() - BoundingCenter;
- dir[5] = getNearLeftDown() - BoundingCenter;
- dir[6] = getNearRightUp() - BoundingCenter;
- dir[7] = getNearLeftUp() - BoundingCenter;
- u32 i = 0;
- float diam[8] = {0.f};
- for (i = 0; i < 8; ++i)
- diam[i] = dir[i].getLengthSQ();
- float longest = 0;
- for (i = 0; i < 8; ++i) {
- if (diam[i] > longest)
- longest = diam[i];
- }
- BoundingRadius = sqrtf(longest);
- }
- }
- }
|