1 | #include "RenderQueue.h"
|
---|
2 | #include "SceneEntity.h"
|
---|
3 | #include "Geometry.h"
|
---|
4 | #include "Texture.h"
|
---|
5 | #include "Material.h"
|
---|
6 | #include "Camera.h"
|
---|
7 | #include "Shape.h"
|
---|
8 | #include "RenderState.h"
|
---|
9 | #include "Transform3.h"
|
---|
10 |
|
---|
11 |
|
---|
12 | using namespace std;
|
---|
13 |
|
---|
14 |
|
---|
15 | namespace CHCDemoEngine
|
---|
16 | {
|
---|
17 |
|
---|
18 | RenderQueue::RenderQueue():
|
---|
19 | mState(NULL),
|
---|
20 | mNumEntries(0)
|
---|
21 | {
|
---|
22 | }
|
---|
23 |
|
---|
24 |
|
---|
25 | RenderQueue::RenderQueue(RenderState *state):
|
---|
26 | mState(state),
|
---|
27 | mNumEntries(0)
|
---|
28 | {
|
---|
29 | }
|
---|
30 |
|
---|
31 |
|
---|
32 | RenderQueue::~RenderQueue()
|
---|
33 | {
|
---|
34 | for (size_t i = 0; i < mBuckets.size(); ++ i)
|
---|
35 | {
|
---|
36 | DEL_PTR(mBuckets[i]);
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 |
|
---|
41 | bool RenderQueue::FitsInBucket(Shape *shape, size_t idx) const
|
---|
42 | {
|
---|
43 | Technique *tech = shape->GetMaterial()->GetTechnique(mState->GetRenderTechnique());
|
---|
44 |
|
---|
45 | // test if entity belongs to this bucket
|
---|
46 | if (tech->IsAlphaTestEnabled() != mBuckets[idx]->mAlphaTestEnabled) { return false; }
|
---|
47 | if (tech->IsCullFaceEnabled() != mBuckets[idx]->mCullFaceEnabled) { return false; }
|
---|
48 | if (tech->IsColorWriteEnabled() != mBuckets[idx]->mColorWriteEnabled) { return false; }
|
---|
49 | if (tech->IsLightingEnabled() != mBuckets[idx]->mLightingEnabled) { return false; }
|
---|
50 | if (tech->IsDepthWriteEnabled() != mBuckets[idx]->mDepthWriteEnabled) { return false; }
|
---|
51 |
|
---|
52 | //if (tech->IsColorWriteEnabled()) cout << "x";
|
---|
53 |
|
---|
54 | const bool hasTexture = (tech->GetTexture() != NULL);
|
---|
55 |
|
---|
56 | if (hasTexture != mBuckets[idx]->mHasTexture) { return false; }
|
---|
57 |
|
---|
58 | if (hasTexture)
|
---|
59 | {
|
---|
60 | if (tech->GetTexture()->GetWidth() != mBuckets[idx]->mTexWidth) { return false; }
|
---|
61 | if (tech->GetTexture()->GetHeight() != mBuckets[idx]->mTexHeight) {return false; }
|
---|
62 | if (tech->GetTexture()->GetFormat() != mBuckets[idx]->mTexFormat) { return false; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | //if (!tech->GetFragmentProgram() && mBuckets[idx]->mHasFragmentProgram) { return false; }
|
---|
66 | //if (!tech->GetVertexProgram() && mBuckets[idx]->mHasVertexProgram) { return false; }
|
---|
67 | if (tech->GetFragmentProgram() != mBuckets[idx]->mFragmentProgram) { return false; }
|
---|
68 | if (tech->GetVertexProgram() != mBuckets[idx]->mVertexProgram) { return false; }
|
---|
69 |
|
---|
70 | return true;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | void RenderQueue::Enqueue(SceneEntity *entity)
|
---|
75 | {
|
---|
76 | ShapeContainer::iterator sit, sit_end;
|
---|
77 |
|
---|
78 | entity->GetCurrentLODLevel(sit, sit_end);
|
---|
79 |
|
---|
80 | for (; sit != sit_end; ++ sit)
|
---|
81 | {
|
---|
82 | Enqueue(*sit, entity->GetTransform());
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | void RenderQueue::Enqueue(Shape *shape, Transform3 *trafo)
|
---|
88 | {
|
---|
89 | Technique *tech = shape->GetMaterial()->GetTechnique(mState->GetRenderTechnique());
|
---|
90 | RenderQueueBucket *bucket = tech->mRenderQueueBucket;
|
---|
91 |
|
---|
92 | ++ mNumEntries;
|
---|
93 |
|
---|
94 | if (!bucket)
|
---|
95 | {
|
---|
96 | bool bucketFound = false;
|
---|
97 |
|
---|
98 | size_t i = 0;
|
---|
99 |
|
---|
100 | for (; i < mBuckets.size(); ++ i)
|
---|
101 | {
|
---|
102 | if (bucketFound = FitsInBucket(shape, i))
|
---|
103 | break;
|
---|
104 | }
|
---|
105 |
|
---|
106 | // create new bucket
|
---|
107 | if (!bucketFound)
|
---|
108 | {
|
---|
109 | RenderQueueBucket *bucket = new RenderQueueBucket();
|
---|
110 | //bucket->mMinDistance = -1;
|
---|
111 |
|
---|
112 | bucket->mAlphaTestEnabled = tech->IsAlphaTestEnabled();
|
---|
113 | bucket->mCullFaceEnabled = tech->IsCullFaceEnabled();
|
---|
114 |
|
---|
115 | bucket->mColorWriteEnabled = tech->IsColorWriteEnabled();
|
---|
116 | bucket->mLightingEnabled = tech->IsLightingEnabled();
|
---|
117 | bucket->mDepthWriteEnabled = tech->IsDepthWriteEnabled();
|
---|
118 |
|
---|
119 |
|
---|
120 | Texture *tex = tech->GetTexture();
|
---|
121 |
|
---|
122 | if (tex != NULL)
|
---|
123 | {
|
---|
124 | bucket->mHasTexture = true;
|
---|
125 |
|
---|
126 | bucket->mTexWidth = tex->GetWidth();
|
---|
127 | bucket->mTexHeight = tex->GetHeight();
|
---|
128 |
|
---|
129 | bucket->mTexFormat = tex->GetFormat();
|
---|
130 | }
|
---|
131 | else
|
---|
132 | {
|
---|
133 | bucket->mHasTexture = false;
|
---|
134 |
|
---|
135 | bucket->mTexWidth = 0;
|
---|
136 | bucket->mTexHeight = 0;
|
---|
137 | bucket->mTexFormat = 0;
|
---|
138 | }
|
---|
139 |
|
---|
140 | // bucket->mHasVertexProgram = (tech->GetVertexProgram() != NULL);
|
---|
141 | // bucket->mHasFragmentProgram = (tech->GetFragmentProgram() != NULL);
|
---|
142 | bucket->mVertexProgram = tech->GetVertexProgram();
|
---|
143 | bucket->mFragmentProgram = tech->GetFragmentProgram();
|
---|
144 |
|
---|
145 | mBuckets.push_back(bucket);
|
---|
146 | //cout << "increased #buckets to " << (int)mBuckets.size() << endl;
|
---|
147 | }
|
---|
148 |
|
---|
149 | bucket = mBuckets[i];
|
---|
150 | tech->mRenderQueueBucket = bucket;
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (bucket->IsEmpty())
|
---|
154 | {
|
---|
155 | // add bucket to the list of currently active buckets that will be rendered
|
---|
156 | // assume that the incoming nodes are ordered by distance
|
---|
157 | // => active buckets are sorted by distance
|
---|
158 | mActiveBuckets.push_back(bucket);
|
---|
159 | }
|
---|
160 |
|
---|
161 | bucket->mShapes.push_back(ShapePair(shape, trafo));
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | void RenderQueue::Clear()
|
---|
166 | {
|
---|
167 | for (size_t i = 0; i < mActiveBuckets.size(); ++ i)
|
---|
168 | {
|
---|
169 | mActiveBuckets[i]->mShapes.clear();
|
---|
170 | }
|
---|
171 |
|
---|
172 | mNumEntries = 0;
|
---|
173 | mActiveBuckets.clear();
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | void RenderQueue::SetRenderState(RenderState *state)
|
---|
178 | {
|
---|
179 | mState = state;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | void RenderQueue::Render()
|
---|
184 | {
|
---|
185 | // render all buckets
|
---|
186 | for (size_t i = 0; i < mActiveBuckets.size(); ++ i)
|
---|
187 | {
|
---|
188 | ShapePairArray::const_iterator sit, sit_end = mActiveBuckets[i]->mShapes.end();
|
---|
189 |
|
---|
190 | for (sit = mActiveBuckets[i]->mShapes.begin(); sit != sit_end; ++ sit)
|
---|
191 | {
|
---|
192 | ShapePair s = *sit;
|
---|
193 |
|
---|
194 | Shape *shape = s.first;
|
---|
195 | Transform3 *t = s.second;
|
---|
196 |
|
---|
197 | t->Load(mState);
|
---|
198 | shape->Render(mState);
|
---|
199 | t->Unload(mState);
|
---|
200 | }
|
---|
201 | }
|
---|
202 | //cout << "active: " << (int)mActiveBuckets.size() << endl;
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | void RenderQueue::Apply()
|
---|
207 | {
|
---|
208 | Render();
|
---|
209 | Clear();
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | } |
---|