[1212] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of the GameTools Project
|
---|
| 4 | http://www.gametools.org
|
---|
| 5 |
|
---|
| 6 | Author: Martin Szydlowski
|
---|
| 7 | -----------------------------------------------------------------------------
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include "OgreKdTreeCamera.h"
|
---|
| 11 |
|
---|
| 12 | namespace Ogre
|
---|
| 13 | {
|
---|
| 14 |
|
---|
| 15 | KdTreeCamera::KdTreeCamera(const String& name, SceneManager *sm):
|
---|
| 16 | Camera(name, sm),
|
---|
| 17 | mEnhancedVisMode(true)
|
---|
| 18 | {
|
---|
| 19 | //getVisibility = getVisibilityEnhanced;
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | KdTreeCamera::~KdTreeCamera()
|
---|
| 23 | {
|
---|
| 24 |
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | void KdTreeCamera::setEnhancedVisMode(bool enhanced)
|
---|
| 28 | {
|
---|
| 29 | mEnhancedVisMode = enhanced;
|
---|
| 30 | //if (mEnhancedVisMode)
|
---|
| 31 | // getVisibility = getVisibilityEnhanced;
|
---|
| 32 | //else
|
---|
| 33 | // getVisibility = getVisibilitySimple;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | bool KdTreeCamera::getEnhancedVisMode()
|
---|
| 37 | {
|
---|
| 38 | return mEnhancedVisMode;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | //-----------------------------------------------------------------------
|
---|
| 42 | // enhanced vis calculation - borrowed from OctreeCamera - indicate when entire box inside the frustum
|
---|
| 43 | KdTreeCamera::NodeVisibility KdTreeCamera::getVisibilityEnhanced(const AxisAlignedBox& box) const
|
---|
| 44 | {
|
---|
| 45 | // Null boxes always invisible
|
---|
| 46 | if ( box.isNull() )
|
---|
| 47 | return KDNV_NONE;
|
---|
| 48 |
|
---|
| 49 | // Make any pending updates to the calculated frustum planes
|
---|
| 50 | updateFrustumPlanes();
|
---|
| 51 |
|
---|
| 52 | // Get corners of the box
|
---|
| 53 | const Vector3* pCorners = box.getAllCorners();
|
---|
| 54 |
|
---|
| 55 | // For each plane, see if all points are on the negative side
|
---|
| 56 | // If so, object is not visible.
|
---|
| 57 | // If one or more are, it's partial.
|
---|
| 58 | // If all aren't, full
|
---|
| 59 |
|
---|
| 60 | int corners[ 8 ] = {0, 4, 3, 5, 2, 6, 1, 7};
|
---|
| 61 |
|
---|
| 62 | int planes[ 6 ] = {FRUSTUM_PLANE_TOP, FRUSTUM_PLANE_BOTTOM,
|
---|
| 63 | FRUSTUM_PLANE_LEFT, FRUSTUM_PLANE_RIGHT,
|
---|
| 64 | FRUSTUM_PLANE_FAR, FRUSTUM_PLANE_NEAR };
|
---|
| 65 |
|
---|
| 66 | bool all_inside = true;
|
---|
| 67 |
|
---|
| 68 | for ( int plane = 0; plane < 6; ++plane )
|
---|
| 69 | {
|
---|
| 70 |
|
---|
| 71 | // Skip far plane if infinite view frustum
|
---|
| 72 | if (mFarDist == 0 && planes[ plane ] == FRUSTUM_PLANE_FAR)
|
---|
| 73 | continue;
|
---|
| 74 |
|
---|
| 75 | bool all_outside = true;
|
---|
| 76 |
|
---|
| 77 | float distance = 0;
|
---|
| 78 |
|
---|
| 79 | for ( int corner = 0; corner < 8; ++corner )
|
---|
| 80 | {
|
---|
| 81 | distance = mFrustumPlanes[ planes[ plane ] ].getDistance( pCorners[ corners[ corner ] ] );
|
---|
| 82 | all_outside = all_outside && ( distance < 0 );
|
---|
| 83 | all_inside = all_inside && ( distance >= 0 );
|
---|
| 84 |
|
---|
| 85 | if ( !all_outside && !all_inside )
|
---|
| 86 | break;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | if ( all_outside )
|
---|
| 90 | return KDNV_NONE;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | if ( all_inside )
|
---|
| 94 | return KDNV_FULL;
|
---|
| 95 | else
|
---|
| 96 | return KDNV_PART;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | //-----------------------------------------------------------------------
|
---|
| 100 | // simple vis calculation - does the same as isVisible, only different return type
|
---|
| 101 | KdTreeCamera::NodeVisibility KdTreeCamera::getVisibilitySimple(const AxisAlignedBox& box) const
|
---|
| 102 | {
|
---|
| 103 | // dummy
|
---|
| 104 | FrustumPlane* culledBy = 0;
|
---|
| 105 | if (mCullFrustum)
|
---|
| 106 | {
|
---|
| 107 | return mCullFrustum->isVisible(box, culledBy) ? KDNV_PART : KDNV_NONE;
|
---|
| 108 | }
|
---|
| 109 | else
|
---|
| 110 | {
|
---|
| 111 | return Frustum::isVisible(box, culledBy) ? KDNV_PART : KDNV_NONE;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | //KdTreeCamera::NodeVisibility KdTreeCamera::getVisibility(const AxisAlignedBox& box) const
|
---|
| 116 | //{
|
---|
| 117 | // // dummy
|
---|
| 118 | // FrustumPlane* culledBy = 0;
|
---|
| 119 | // if (mCullFrustum)
|
---|
| 120 | // {
|
---|
| 121 | // return mCullFrustum->isVisible(box, culledBy) ? KDNV_PART : KDNV_NONE;
|
---|
| 122 | // }
|
---|
| 123 | // else
|
---|
| 124 | // {
|
---|
| 125 | // return Frustum::isVisible(box, culledBy) ? KDNV_PART : KDNV_NONE;
|
---|
| 126 | // }
|
---|
| 127 | //}
|
---|
| 128 | } // namespace Ogre |
---|