source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp @ 3120

Revision 3120, 3.4 KB checked in by mattausch, 16 years ago (diff)

worked on filtering now trying to reduce flickering (have to reorder ssao function quite much

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