[2766] | 1 | #include "SceneEntity.h"
|
---|
| 2 | #include "Geometry.h"
|
---|
| 3 | #include "Material.h"
|
---|
[2769] | 4 | #include "RenderState.h"
|
---|
[2840] | 5 | #include "Shape.h"
|
---|
[2844] | 6 | #include "Transform3.h"
|
---|
| 7 | #include "Camera.h"
|
---|
| 8 |
|
---|
| 9 | #include "glInterface.h"
|
---|
[2818] | 10 | #include <Cg/cg.h>
|
---|
| 11 | #include <Cg/cgGL.h>
|
---|
[2766] | 12 |
|
---|
| 13 |
|
---|
[2842] | 14 | using namespace std;
|
---|
| 15 |
|
---|
| 16 |
|
---|
[2776] | 17 | namespace CHCDemoEngine
|
---|
[2766] | 18 | {
|
---|
| 19 |
|
---|
[2865] | 20 | bool SceneEntity::sUseLODs = true;
|
---|
[2842] | 21 |
|
---|
[2865] | 22 |
|
---|
[2844] | 23 | SceneEntity::SceneEntity(Transform3 *trafo):
|
---|
[2847] | 24 | mTransform(trafo), mCurrentLODLevel(0), mLODLastUpdated(-1)
|
---|
[2766] | 25 | {
|
---|
[2844] | 26 | mBox.Initialize();
|
---|
[2766] | 27 | }
|
---|
| 28 |
|
---|
| 29 |
|
---|
[2792] | 30 | SceneEntity::~SceneEntity()
|
---|
| 31 | {
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 |
|
---|
[2847] | 35 | void SceneEntity::UpdateLODs(const Vector3 &viewPoint)
|
---|
[2766] | 36 | {
|
---|
[2865] | 37 | mCurrentLODLevel = 0;
|
---|
| 38 |
|
---|
| 39 | if (!sUseLODs) return;
|
---|
| 40 |
|
---|
[2847] | 41 | const Vector3 pos = GetCenter();
|
---|
[2766] | 42 |
|
---|
[2853] | 43 | const float dist = SqrDistance(pos, viewPoint);
|
---|
[2841] | 44 |
|
---|
[2853] | 45 | // note: we assume that lods are ordered from smallest distance to largest
|
---|
[2847] | 46 | const int l = (int)mLODLevels.size();
|
---|
[2839] | 47 |
|
---|
[2844] | 48 | for (int i = 0; i < l; ++ i)
|
---|
[2766] | 49 | {
|
---|
[2844] | 50 | LODLevel *lod = mLODLevels[i];
|
---|
[2841] | 51 |
|
---|
[2853] | 52 | if (lod->GetSquaredDistance() > dist)
|
---|
[2841] | 53 | break;
|
---|
[2844] | 54 |
|
---|
| 55 | mCurrentLODLevel = i;
|
---|
[2839] | 56 | }
|
---|
[2848] | 57 | }
|
---|
[2766] | 58 |
|
---|
[2848] | 59 |
|
---|
| 60 | int SceneEntity::GetCurrentLODLevel()
|
---|
| 61 | {
|
---|
| 62 | if (mLODLastUpdated != LODLevel::sFrameId)
|
---|
[2841] | 63 | {
|
---|
[2848] | 64 | mLODLastUpdated = LODLevel::sFrameId;
|
---|
| 65 | UpdateLODs(LODLevel::sViewPoint);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | return mCurrentLODLevel;
|
---|
[2839] | 69 | }
|
---|
[2825] | 70 |
|
---|
[2848] | 71 |
|
---|
[2844] | 72 | void SceneEntity::GetCurrentLODLevel(ShapeContainer::iterator &start,
|
---|
| 73 | ShapeContainer::iterator &end)
|
---|
| 74 | {
|
---|
[2847] | 75 | if (mLODLastUpdated != LODLevel::sFrameId)
|
---|
| 76 | {
|
---|
| 77 | mLODLastUpdated = LODLevel::sFrameId;
|
---|
| 78 | UpdateLODs(LODLevel::sViewPoint);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[2848] | 81 | start = mLODLevels[mCurrentLODLevel]->GetShapes().begin();
|
---|
| 82 | end = mLODLevels[mCurrentLODLevel]->GetShapes().end();
|
---|
[2844] | 83 | }
|
---|
[2825] | 84 |
|
---|
[2844] | 85 |
|
---|
[3034] | 86 | void SceneEntity::GetLODLevel(int level,
|
---|
[2844] | 87 | ShapeContainer::iterator &start,
|
---|
| 88 | ShapeContainer::iterator &end)
|
---|
[2839] | 89 | {
|
---|
[2844] | 90 | LODLevel *lod = mLODLevels[level];
|
---|
[2839] | 91 |
|
---|
[2848] | 92 | start = lod->GetShapes().begin();
|
---|
| 93 | end = lod->GetShapes().end();
|
---|
[2844] | 94 | }
|
---|
[2839] | 95 |
|
---|
| 96 |
|
---|
[2844] | 97 | void SceneEntity::Render(RenderState *state)
|
---|
| 98 | {
|
---|
| 99 | ShapeContainer::iterator sit, sit_end;
|
---|
[2841] | 100 |
|
---|
[2847] | 101 | GetCurrentLODLevel(sit, sit_end);
|
---|
[2844] | 102 |
|
---|
| 103 | for (; sit != sit_end; ++ sit)
|
---|
| 104 | {
|
---|
[2841] | 105 | (*sit)->Render(state);
|
---|
[2766] | 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 |
|
---|
[2839] | 110 | void SceneEntity::AddShape(Shape *shape)
|
---|
[2766] | 111 | {
|
---|
[2839] | 112 | mShapes.push_back(shape);
|
---|
[2844] | 113 | mBox.Include(shape->GetBoundingBox());
|
---|
[2847] | 114 |
|
---|
| 115 | mCenter = mBox.Center();
|
---|
[2766] | 116 | }
|
---|
| 117 |
|
---|
| 118 |
|
---|
[2840] | 119 | void SceneEntity::SetTransform(Transform3 *trafo)
|
---|
[2766] | 120 | {
|
---|
| 121 | mTransform = trafo;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | void SceneEntity::SetLastRendered(int lastRendered)
|
---|
| 126 | {
|
---|
| 127 | mLastRendered = lastRendered;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 | int SceneEntity::GetLastRendered() const
|
---|
| 132 | {
|
---|
| 133 | return mLastRendered;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
[2844] | 137 | int SceneEntity::CountNumTriangles(int lodLevel)
|
---|
[2766] | 138 | {
|
---|
[2842] | 139 | int numTriangles = 0;
|
---|
[2844] | 140 |
|
---|
| 141 | ShapeContainer::iterator sit, sit_end;
|
---|
[2766] | 142 |
|
---|
[2844] | 143 | if (lodLevel == -1)
|
---|
| 144 | {
|
---|
[2848] | 145 | lodLevel = GetCurrentLODLevel();
|
---|
[2844] | 146 | }
|
---|
[2842] | 147 |
|
---|
[2848] | 148 | return mLODLevels[lodLevel]->GetNumTriangles();
|
---|
[2766] | 149 | }
|
---|
| 150 |
|
---|
| 151 |
|
---|
[2844] | 152 | AxisAlignedBox3 SceneEntity::GetTransformedBoundingBox() const
|
---|
| 153 | {
|
---|
[2961] | 154 | if (mTransform->IsIdentity()) return mBox;
|
---|
[2844] | 155 |
|
---|
[2957] | 156 | Matrix4x4 mat = mTransform->GetMatrix();
|
---|
| 157 |
|
---|
| 158 | return Transform(mBox, mat);
|
---|
[2844] | 159 | }
|
---|
| 160 |
|
---|
| 161 |
|
---|
| 162 | AxisAlignedBox3 SceneEntity::GetBoundingBox() const
|
---|
| 163 | {
|
---|
| 164 | return mBox;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[2847] | 167 |
|
---|
| 168 | Vector3 SceneEntity::GetCenter() const
|
---|
| 169 | {
|
---|
[2957] | 170 | if (mTransform->IsIdentity())
|
---|
| 171 | return mCenter;
|
---|
[2847] | 172 |
|
---|
[2957] | 173 | Matrix4x4 mat = mTransform->GetMatrix();
|
---|
[2847] | 174 |
|
---|
[2957] | 175 | return mat * mCenter;
|
---|
[2847] | 176 | }
|
---|
| 177 |
|
---|
| 178 |
|
---|
[2766] | 179 | } |
---|