1 | #include "dxstdafx.h"
|
---|
2 | #include ".\simplemeshrenderer.h"
|
---|
3 | #include "GameManager.h"
|
---|
4 | #include "Sprite.h"
|
---|
5 |
|
---|
6 | SimpleMeshRenderer::SimpleMeshRenderer(void) : Renderer()
|
---|
7 | {
|
---|
8 | this->effect = NULL;
|
---|
9 | this->rayEffect = NULL;
|
---|
10 | this->alphaBlending = false;
|
---|
11 | this->setRenderPriority(30);
|
---|
12 | this->rendererType |= Renderer::RENDERER_SIMPLEMESH;
|
---|
13 | }
|
---|
14 |
|
---|
15 | SimpleMeshRenderer::~SimpleMeshRenderer(void)
|
---|
16 | {
|
---|
17 | this->myScene->manager->releaseEffect(this->effect);
|
---|
18 | this->myScene->manager->releaseEffect(this->rayEffect);
|
---|
19 | this->effect = NULL;
|
---|
20 | this->rayEffect = NULL;
|
---|
21 | }
|
---|
22 |
|
---|
23 |
|
---|
24 | void SimpleMeshRenderer::init()
|
---|
25 | {
|
---|
26 | this->effect = this->myScene->manager->getEffect(GameManager::EFFECT_DEPTHIMP);
|
---|
27 | if(!this->effect) {
|
---|
28 | this->effect = this->myScene->manager->loadEffect(GameManager::EFFECT_DEPTHIMP, L"shaders/depthImp.obj");
|
---|
29 | }
|
---|
30 | this->rayEffect = this->myScene->manager->getEffect(GameManager::EFFECT_RAYTRACE);
|
---|
31 | if(!this->rayEffect) {
|
---|
32 | this->rayEffect = this->myScene->manager->loadEffect(GameManager::EFFECT_RAYTRACE, L"shaders/RayTraceEffects.obj");
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | void SimpleMeshRenderer::render()
|
---|
38 | {
|
---|
39 | switch(this->myScene->getActivePassId()) {
|
---|
40 | case Scene::PASS_NORMAL:
|
---|
41 | this->defaultPass();
|
---|
42 | break;
|
---|
43 | case Scene::PASS_DEPTH:
|
---|
44 | this->depthPass();
|
---|
45 | break;
|
---|
46 | case Scene::PASS_RAYTRACE:
|
---|
47 | this->rayPass();
|
---|
48 | break;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | void SimpleMeshRenderer::defaultPass()
|
---|
53 | {
|
---|
54 | HRESULT hr = 0;
|
---|
55 | SPTR<Node> node = this->myNode.lock();
|
---|
56 | if(node->isA(Scene::NODE_SPRITE)) {
|
---|
57 | return;
|
---|
58 | }
|
---|
59 |
|
---|
60 | Object3d *obj = (Object3d *) (node.get());
|
---|
61 | if(obj!=NULL && obj->isModelLoaded()) {
|
---|
62 | ID3DXEffect* fadeEffect;
|
---|
63 | UINT cPass;
|
---|
64 | this->device->BeginScene();
|
---|
65 |
|
---|
66 |
|
---|
67 | fadeEffect = this->myScene->manager->getEffect(GameManager::EFFECT_FADE);
|
---|
68 | V( fadeEffect->SetFloat("alpha", obj->softKillAlpha) );
|
---|
69 | V( fadeEffect->SetMatrix("g_mWorldViewProjection", &this->myScene->activeRenderPass->getWorldViewProjectionMatrix(obj->getWorldMatrix())) );
|
---|
70 | D3DXMATRIX modelViewIT;
|
---|
71 | D3DXMATRIX invModelView;
|
---|
72 |
|
---|
73 | modelViewIT = *obj->getWorldMatrix();
|
---|
74 |
|
---|
75 | D3DXMatrixInverse(&invModelView, NULL, &modelViewIT);
|
---|
76 | D3DXMATRIX trans;
|
---|
77 | Vector absPos = obj->getAbsolutePosition();
|
---|
78 | D3DXMatrixTranslation(&trans, -absPos.x, -absPos.y, -absPos.z);
|
---|
79 | D3DXMatrixMultiply(&modelViewIT, &modelViewIT, &trans);
|
---|
80 | D3DXMatrixTranspose(&modelViewIT, &modelViewIT);
|
---|
81 |
|
---|
82 | Vector lightDir;
|
---|
83 | Vector eyeDir;
|
---|
84 | Vector halfVector;
|
---|
85 | lightDir = ((this->myScene->getSunDirection()) * -1);
|
---|
86 | eyeDir = this->myScene->getActiveCamera()->getAbsolutePosition() - obj->getAbsolutePosition();
|
---|
87 | halfVector = lightDir + eyeDir;
|
---|
88 | halfVector.normalize();
|
---|
89 | lightDir.normalize();
|
---|
90 |
|
---|
91 | V( fadeEffect->SetMatrix("g_mWorldViewIT", &modelViewIT) );
|
---|
92 | V( fadeEffect->SetVector("lightDir", &lightDir));
|
---|
93 | V( fadeEffect->SetVector("halfVector", &halfVector) );
|
---|
94 | V( fadeEffect->CommitChanges() );
|
---|
95 | V( fadeEffect->SetTechnique("fadeObject") );
|
---|
96 | V( fadeEffect->Begin( &cPass, 0 ) );
|
---|
97 | V( fadeEffect->BeginPass( 0 ) );
|
---|
98 |
|
---|
99 | if(obj->doingSoftKill) {
|
---|
100 | this->device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
|
---|
101 | this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
|
---|
102 | this->device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
|
---|
103 | this->device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
|
---|
104 | }
|
---|
105 | if (obj->Materials.size() == 0) {
|
---|
106 | this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
|
---|
107 | this->device->SetRenderState( D3DRS_LIGHTING, FALSE );
|
---|
108 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
|
---|
109 | if(obj->isPMesh()) {
|
---|
110 | (*obj->getProgressiveMesh())->DrawSubset(0);
|
---|
111 | } else {
|
---|
112 | (*obj->getMesh())->DrawSubset(0);
|
---|
113 | }
|
---|
114 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
---|
115 | } else {
|
---|
116 | this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW);
|
---|
117 | this->device->SetRenderState( D3DRS_LIGHTING, FALSE );
|
---|
118 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
---|
119 |
|
---|
120 | // bilinear texture filtering:
|
---|
121 | this->device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
|
---|
122 | this->device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
|
---|
123 | this->device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
|
---|
124 |
|
---|
125 | for(UINT i = 0; i < obj->Materials.size(); i++) {
|
---|
126 |
|
---|
127 | V( fadeEffect->SetFloatArray("ambient", (float*) (&obj->Materials[i].Ambient), 4) );
|
---|
128 | V( fadeEffect->SetFloatArray("diffuse", (float*) (&obj->Materials[i].Diffuse), 4) );
|
---|
129 | V( fadeEffect->SetFloatArray("specular", (float*) (&obj->Materials[i].Specular), 4) );
|
---|
130 | V( fadeEffect->SetFloat("power", obj->Materials[i].Power) );
|
---|
131 | V( fadeEffect->SetTexture("objTexture", obj->Textures[i]) );
|
---|
132 | V( fadeEffect->CommitChanges() );
|
---|
133 |
|
---|
134 | if(obj->isPMesh()) {
|
---|
135 | (*obj->getProgressiveMesh())->DrawSubset(i);
|
---|
136 | } else {
|
---|
137 | (*obj->getMesh())->DrawSubset(i);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | //deactivate textures
|
---|
143 | this->device->SetTexture(0, 0);
|
---|
144 |
|
---|
145 | V( fadeEffect->EndPass() );
|
---|
146 | V( fadeEffect->End() );
|
---|
147 | if(obj->doingSoftKill) {
|
---|
148 | this->device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
|
---|
149 | this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | this->device->SetTransform(D3DTS_WORLD, &this->myScene->getIdentityMatrix());
|
---|
153 | this->device->SetTransform(D3DTS_VIEW, &this->myScene->getIdentityMatrix());
|
---|
154 | this->device->SetTransform(D3DTS_PROJECTION, &this->myScene->getIdentityMatrix());
|
---|
155 | this->device->EndScene();
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | void SimpleMeshRenderer::depthPass()
|
---|
160 | {
|
---|
161 | SPTR<Node> node = this->myNode.lock();
|
---|
162 | Object3d *obj = (Object3d *) (node.get());
|
---|
163 | UINT cPass;
|
---|
164 | HRESULT hr;
|
---|
165 | if(obj!=NULL && obj->isModelLoaded()) {
|
---|
166 | V( this->effect->SetMatrix("g_mWorldViewProjection", &this->myScene->activeRenderPass->getWorldViewProjectionMatrix(obj->getWorldMatrix())) );
|
---|
167 | V( this->effect->SetTechnique("DepthPass") );
|
---|
168 | V( this->effect->CommitChanges() );
|
---|
169 | this->device->BeginScene();
|
---|
170 | V( this->effect->Begin( &cPass, 0 ) );
|
---|
171 | V( this->effect->BeginPass( 0 ) );
|
---|
172 | if (obj->Materials.size() == 0) {
|
---|
173 | this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
|
---|
174 | this->device->SetRenderState( D3DRS_LIGHTING, FALSE );
|
---|
175 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
|
---|
176 | if(obj->isPMesh()) {
|
---|
177 | (*obj->getProgressiveMesh())->DrawSubset(0);
|
---|
178 | } else {
|
---|
179 | (*obj->getMesh())->DrawSubset(0);
|
---|
180 | }
|
---|
181 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
---|
182 | } else {
|
---|
183 | this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW);
|
---|
184 | this->device->SetRenderState( D3DRS_LIGHTING, TRUE );
|
---|
185 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
---|
186 | if(this->alphaBlending) {
|
---|
187 | this->device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
|
---|
188 | this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
|
---|
189 | this->device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
|
---|
190 | this->device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
|
---|
191 | }
|
---|
192 |
|
---|
193 | // bilinear texture filtering:
|
---|
194 | this->device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
|
---|
195 | this->device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
|
---|
196 | this->device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
|
---|
197 |
|
---|
198 | for(UINT i = 0; i < obj->Materials.size(); i++) {
|
---|
199 | this->device->SetMaterial( &obj->Materials[i] );
|
---|
200 | this->device->SetTexture(0, obj->Textures[i]);
|
---|
201 | if(obj->isPMesh()) {
|
---|
202 | (*obj->getProgressiveMesh())->DrawSubset(i);
|
---|
203 | } else {
|
---|
204 | (*obj->getMesh())->DrawSubset(i);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | if(this->alphaBlending) {
|
---|
208 | this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
|
---|
209 | this->device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
|
---|
210 | }
|
---|
211 |
|
---|
212 | //deactivate textures
|
---|
213 | this->device->SetTexture(0, 0);
|
---|
214 | }
|
---|
215 | V( this->effect->EndPass() );
|
---|
216 | V( this->effect->End() );
|
---|
217 | this->device->EndScene();
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | void SimpleMeshRenderer::rayPass()
|
---|
222 | {
|
---|
223 | SPTR<Node> node = this->myNode.lock();
|
---|
224 | Object3d *obj = (Object3d *) (node.get());
|
---|
225 | UINT cPass;
|
---|
226 | HRESULT hr;
|
---|
227 | this->device->BeginScene();
|
---|
228 | if(obj!=NULL && obj->isModelLoaded()) {
|
---|
229 | V( this->rayEffect->SetMatrix("g_mWorldViewProjection", &this->myScene->activeRenderPass->getWorldViewProjectionMatrix(obj->getWorldMatrix())) );
|
---|
230 | V( this->rayEffect->SetMatrix("g_mWorldView", &this->myScene->activeRenderPass->getWorldViewMatrix(obj->getWorldMatrix())) );
|
---|
231 | V( this->rayEffect->SetTechnique("ColorDistance") );
|
---|
232 | V( this->rayEffect->CommitChanges() );
|
---|
233 | V( this->rayEffect->Begin( &cPass, 0 ) );
|
---|
234 | V( this->rayEffect->BeginPass( 0 ) );
|
---|
235 |
|
---|
236 | if (obj->Materials.size() == 0) {
|
---|
237 | this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
|
---|
238 | this->device->SetRenderState( D3DRS_LIGHTING, FALSE );
|
---|
239 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
|
---|
240 | if(obj->isPMesh()) {
|
---|
241 | (*obj->getProgressiveMesh())->DrawSubset(0);
|
---|
242 | } else {
|
---|
243 | (*obj->getMesh())->DrawSubset(0);
|
---|
244 | }
|
---|
245 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
---|
246 | } else {
|
---|
247 | this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW);
|
---|
248 | this->device->SetRenderState( D3DRS_LIGHTING, TRUE );
|
---|
249 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
---|
250 |
|
---|
251 | if(this->alphaBlending) {
|
---|
252 | this->device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
|
---|
253 | this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
|
---|
254 | this->device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
|
---|
255 | this->device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
|
---|
256 | }
|
---|
257 |
|
---|
258 | // bilinear texture filtering:
|
---|
259 | this->device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
|
---|
260 | this->device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
|
---|
261 | this->device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
|
---|
262 |
|
---|
263 | for(UINT i = 0; i < obj->Materials.size(); i++) {
|
---|
264 | this->device->SetMaterial( &obj->Materials[i] );
|
---|
265 | this->device->SetTexture(0, obj->Textures[i]);
|
---|
266 | V( this->rayEffect->SetTexture( "g_txCurrentTexture", obj->Textures[i]) );
|
---|
267 | V( this->rayEffect->CommitChanges() );
|
---|
268 | if(obj->isPMesh()) {
|
---|
269 | (*obj->getProgressiveMesh())->DrawSubset(i);
|
---|
270 | } else {
|
---|
271 | (*obj->getMesh())->DrawSubset(i);
|
---|
272 | }
|
---|
273 | }
|
---|
274 | if(this->alphaBlending) {
|
---|
275 | this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
|
---|
276 | this->device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
|
---|
277 | }
|
---|
278 | //deactivate textures
|
---|
279 | this->device->SetTexture(0, 0);
|
---|
280 | }
|
---|
281 | V( this->rayEffect->EndPass() );
|
---|
282 | V( this->rayEffect->End() );
|
---|
283 | this->device->EndScene();
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | void SimpleMeshRenderer::enableAlphaBlending(bool _alpha)
|
---|
288 | {
|
---|
289 | this->alphaBlending = _alpha;
|
---|
290 | }
|
---|
291 |
|
---|
292 | void SimpleMeshRenderer::OnLostDevice( void* pUserContext )
|
---|
293 | {
|
---|
294 | HRESULT hr;
|
---|
295 | this->myScene->manager->printToConsole(" OnLostDevice SimpleMeshRenderer");
|
---|
296 | V( this->effect->OnLostDevice() );
|
---|
297 | V( this->rayEffect->OnLostDevice() );
|
---|
298 | }
|
---|
299 |
|
---|
300 | void SimpleMeshRenderer::OnDestroyDevice( void* pUserContext )
|
---|
301 | {
|
---|
302 | this->myScene->manager->printToConsole(" OnDestroyDevice SimpleMeshRenderer");
|
---|
303 | this->myScene->manager->releaseEffect(this->effect);
|
---|
304 | this->myScene->manager->releaseEffect(this->rayEffect);
|
---|
305 | this->effect = NULL;
|
---|
306 | this->rayEffect = NULL;
|
---|
307 | }
|
---|
308 |
|
---|
309 | HRESULT SimpleMeshRenderer::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
|
---|
310 | {
|
---|
311 | this->device = pd3dDevice;
|
---|
312 | this->effect = this->myScene->manager->getEffect(GameManager::EFFECT_DEPTHIMP);
|
---|
313 | if(!this->effect) {
|
---|
314 | this->effect = this->myScene->manager->loadEffect(GameManager::EFFECT_DEPTHIMP, L"shaders/depthImp.obj");
|
---|
315 | }
|
---|
316 | this->rayEffect = this->myScene->manager->getEffect(GameManager::EFFECT_RAYTRACE);
|
---|
317 | if(!this->rayEffect) {
|
---|
318 | this->rayEffect = this->myScene->manager->loadEffect(GameManager::EFFECT_RAYTRACE, L"shaders/RayTraceEffects.obj");
|
---|
319 | }
|
---|
320 | return NULL;
|
---|
321 | }
|
---|
322 |
|
---|
323 |
|
---|
324 | bool SimpleMeshRenderer::isLowerThan(Renderer* renderer)
|
---|
325 | {
|
---|
326 | SPTR<Node> node1 = this->myNode.lock();
|
---|
327 | Object3d *myObj = (Object3d *) (node1.get());
|
---|
328 | SPTR<Node> node2 = renderer->myNode.lock();
|
---|
329 | Object3d *otherObj = (Object3d *) (node2.get());
|
---|
330 | if(this->getRenderPriority()<renderer->getRenderPriority()) {
|
---|
331 | return true;
|
---|
332 | } else {
|
---|
333 | if(this->getRenderPriority()==renderer->getRenderPriority()) {
|
---|
334 | if(myObj->doingSoftKill && !otherObj->doingSoftKill) {
|
---|
335 | return false;
|
---|
336 | }
|
---|
337 | if(!myObj->doingSoftKill && otherObj->doingSoftKill) {
|
---|
338 | return true;
|
---|
339 | }
|
---|
340 | return myObj->camDistance>otherObj->camDistance;
|
---|
341 | } else {
|
---|
342 | return false;
|
---|
343 | }
|
---|
344 | }
|
---|
345 | } |
---|