[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 |
|
---|
[3070] | 41 | const Vector3 pos = GetWorldCenter();
|
---|
[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 | {
|
---|
[3070] | 50 | if (mLODLevels[i].GetSquaredDistance() > dist)
|
---|
[2841] | 51 | break;
|
---|
[2844] | 52 |
|
---|
| 53 | mCurrentLODLevel = i;
|
---|
[2839] | 54 | }
|
---|
[2848] | 55 | }
|
---|
[2766] | 56 |
|
---|
[2848] | 57 |
|
---|
| 58 | int SceneEntity::GetCurrentLODLevel()
|
---|
| 59 | {
|
---|
| 60 | if (mLODLastUpdated != LODLevel::sFrameId)
|
---|
[2841] | 61 | {
|
---|
[2848] | 62 | mLODLastUpdated = LODLevel::sFrameId;
|
---|
| 63 | UpdateLODs(LODLevel::sViewPoint);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | return mCurrentLODLevel;
|
---|
[2839] | 67 | }
|
---|
[2825] | 68 |
|
---|
[2848] | 69 |
|
---|
[2844] | 70 | void SceneEntity::GetCurrentLODLevel(ShapeContainer::iterator &start,
|
---|
| 71 | ShapeContainer::iterator &end)
|
---|
| 72 | {
|
---|
[2847] | 73 | if (mLODLastUpdated != LODLevel::sFrameId)
|
---|
| 74 | {
|
---|
| 75 | mLODLastUpdated = LODLevel::sFrameId;
|
---|
| 76 | UpdateLODs(LODLevel::sViewPoint);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[3070] | 79 | start = mLODLevels[mCurrentLODLevel].GetShapes().begin();
|
---|
| 80 | end = mLODLevels[mCurrentLODLevel].GetShapes().end();
|
---|
[2844] | 81 | }
|
---|
[2825] | 82 |
|
---|
[2844] | 83 |
|
---|
[3034] | 84 | void SceneEntity::GetLODLevel(int level,
|
---|
[2844] | 85 | ShapeContainer::iterator &start,
|
---|
| 86 | ShapeContainer::iterator &end)
|
---|
[2839] | 87 | {
|
---|
[3070] | 88 | start = mLODLevels[level].GetShapes().begin();
|
---|
| 89 | end = mLODLevels[level].GetShapes().end();
|
---|
[2844] | 90 | }
|
---|
[2839] | 91 |
|
---|
| 92 |
|
---|
[2844] | 93 | void SceneEntity::Render(RenderState *state)
|
---|
| 94 | {
|
---|
| 95 | ShapeContainer::iterator sit, sit_end;
|
---|
[2841] | 96 |
|
---|
[3070] | 97 | if (!mLODLevels.empty())
|
---|
| 98 | GetCurrentLODLevel(sit, sit_end);
|
---|
| 99 | else
|
---|
| 100 | {
|
---|
| 101 | sit = mShapes.begin(); sit_end = mShapes.end();
|
---|
| 102 | }
|
---|
[2844] | 103 |
|
---|
[3071] | 104 | mTransform->Load(state);
|
---|
| 105 |
|
---|
[2844] | 106 | for (; sit != sit_end; ++ sit)
|
---|
| 107 | {
|
---|
[2841] | 108 | (*sit)->Render(state);
|
---|
[2766] | 109 | }
|
---|
[3071] | 110 |
|
---|
| 111 | mTransform->Unload(state);
|
---|
[2766] | 112 | }
|
---|
| 113 |
|
---|
| 114 |
|
---|
[2839] | 115 | void SceneEntity::AddShape(Shape *shape)
|
---|
[2766] | 116 | {
|
---|
[2839] | 117 | mShapes.push_back(shape);
|
---|
[2844] | 118 | mBox.Include(shape->GetBoundingBox());
|
---|
[2847] | 119 |
|
---|
| 120 | mCenter = mBox.Center();
|
---|
[2766] | 121 | }
|
---|
| 122 |
|
---|
| 123 |
|
---|
[2840] | 124 | void SceneEntity::SetTransform(Transform3 *trafo)
|
---|
[2766] | 125 | {
|
---|
| 126 | mTransform = trafo;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 | void SceneEntity::SetLastRendered(int lastRendered)
|
---|
| 131 | {
|
---|
| 132 | mLastRendered = lastRendered;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | int SceneEntity::GetLastRendered() const
|
---|
| 137 | {
|
---|
| 138 | return mLastRendered;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 |
|
---|
[2844] | 142 | int SceneEntity::CountNumTriangles(int lodLevel)
|
---|
[2766] | 143 | {
|
---|
[2842] | 144 | int numTriangles = 0;
|
---|
[2844] | 145 |
|
---|
| 146 | ShapeContainer::iterator sit, sit_end;
|
---|
[2766] | 147 |
|
---|
[2844] | 148 | if (lodLevel == -1)
|
---|
| 149 | {
|
---|
[2848] | 150 | lodLevel = GetCurrentLODLevel();
|
---|
[2844] | 151 | }
|
---|
[2842] | 152 |
|
---|
[3070] | 153 | return mLODLevels[lodLevel].GetNumTriangles();
|
---|
[2766] | 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
[3070] | 157 | AxisAlignedBox3 SceneEntity::GetWorldBoundingBox() const
|
---|
[2844] | 158 | {
|
---|
[2961] | 159 | if (mTransform->IsIdentity()) return mBox;
|
---|
[2844] | 160 |
|
---|
[2957] | 161 | Matrix4x4 mat = mTransform->GetMatrix();
|
---|
| 162 |
|
---|
| 163 | return Transform(mBox, mat);
|
---|
[2844] | 164 | }
|
---|
| 165 |
|
---|
| 166 |
|
---|
| 167 | AxisAlignedBox3 SceneEntity::GetBoundingBox() const
|
---|
| 168 | {
|
---|
| 169 | return mBox;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[2847] | 172 |
|
---|
[3070] | 173 | Vector3 SceneEntity::GetWorldCenter() const
|
---|
[2847] | 174 | {
|
---|
[2957] | 175 | if (mTransform->IsIdentity())
|
---|
| 176 | return mCenter;
|
---|
[2847] | 177 |
|
---|
[3070] | 178 | return mTransform->GetMatrix() * mCenter;
|
---|
[2847] | 179 | }
|
---|
| 180 |
|
---|
| 181 |
|
---|
[2766] | 182 | } |
---|