1 | #include "dxstdafx.h"
|
---|
2 | #include ".\TerrainRenderer.h"
|
---|
3 | #include "GameManager.h"
|
---|
4 |
|
---|
5 | TerrainRenderer::TerrainRenderer(void) : Renderer()
|
---|
6 | {
|
---|
7 | this->setRenderPriority(10);
|
---|
8 | this->rendererType |= Renderer::RENDERER_TERRAIN;
|
---|
9 | }
|
---|
10 |
|
---|
11 | TerrainRenderer::~TerrainRenderer(void)
|
---|
12 | {
|
---|
13 | this->myScene->manager->releaseEffect(this->TerrainEffect);
|
---|
14 | this->TerrainEffect = NULL;
|
---|
15 | }
|
---|
16 |
|
---|
17 | void TerrainRenderer::init()
|
---|
18 | {
|
---|
19 |
|
---|
20 | }
|
---|
21 |
|
---|
22 | void TerrainRenderer::setEffect(ID3DXEffect* effect) {
|
---|
23 | this->TerrainEffect = effect;
|
---|
24 |
|
---|
25 | //set technique handles:
|
---|
26 | ShaderTechHandle = TerrainEffect->GetTechniqueByName("TerrainShader");
|
---|
27 | ShaderTechHandleRay = TerrainEffect->GetTechniqueByName("TerrainShaderRay");
|
---|
28 | ShaderTechHandleDepth = TerrainEffect->GetTechniqueByName("TerrainShaderDepth");
|
---|
29 |
|
---|
30 | //new texture handles:
|
---|
31 | //shader:
|
---|
32 | WorldViewProjectionHandle = TerrainEffect->GetParameterByName(0, "g_mWorldViewProjection");
|
---|
33 | WorldViewHandle = TerrainEffect->GetParameterByName(0, "g_mWorldView");
|
---|
34 |
|
---|
35 | float nearPlane = this->myScene->getActiveCamera()->getNearClipping();
|
---|
36 | float farPlane = this->myScene->getActiveCamera()->getFarClipping();
|
---|
37 | FarPlaneMinusNearPlaneHandle = TerrainEffect->GetParameterByName(0, "FarPlaneMinusNearPlane");
|
---|
38 | TerrainEffect->SetFloat(FarPlaneMinusNearPlaneHandle, farPlane - nearPlane);
|
---|
39 | }
|
---|
40 |
|
---|
41 | void TerrainRenderer::render()
|
---|
42 | {
|
---|
43 | this->device->BeginScene();
|
---|
44 | SPTR<Node> node = this->myNode.lock();
|
---|
45 | Object3d *obj = (Object3d *) (node.get());
|
---|
46 | if(obj!=NULL && obj->isModelLoaded()) {
|
---|
47 | //Habe das von this->myScene->getActiveRenderPass() == NULL auf switch cases umgebaut..
|
---|
48 | //if(this->myScene->getActiveRenderPass() == NULL) {
|
---|
49 | UINT numPasses = 0;
|
---|
50 | switch(this->myScene->getActivePassId()) {
|
---|
51 | case Scene::PASS_NORMAL:
|
---|
52 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
---|
53 |
|
---|
54 | // set the technique to use
|
---|
55 | TerrainEffect->SetTechnique( ShaderTechHandle );
|
---|
56 |
|
---|
57 | D3DXMatrixMultiply(&this->worldViewMat, &this->myScene->getWorldMatrix(), &this->myScene->getViewMatrix());
|
---|
58 | D3DXMatrixMultiply(&this->worldViewProjMat, &this->worldViewMat, &this->myScene->getProjectionMatrix());
|
---|
59 | TerrainEffect->SetMatrix(WorldViewProjectionHandle, &this->worldViewProjMat);
|
---|
60 |
|
---|
61 |
|
---|
62 | TerrainEffect->Begin(&numPasses, 0);
|
---|
63 | for(UINT j = 0; j < numPasses; j++)
|
---|
64 | {
|
---|
65 | this->device->SetTexture(0, obj->Textures[0]);
|
---|
66 | // begin effect render pass
|
---|
67 | TerrainEffect->BeginPass(j);
|
---|
68 |
|
---|
69 | this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
|
---|
70 |
|
---|
71 | if(obj->isPMesh()) {
|
---|
72 | (*obj->getProgressiveMesh())->DrawSubset(0);
|
---|
73 | } else {
|
---|
74 | (*obj->getMesh())->DrawSubset(0);
|
---|
75 | }
|
---|
76 |
|
---|
77 | // end effect render pass
|
---|
78 | TerrainEffect->EndPass();
|
---|
79 | }
|
---|
80 | TerrainEffect->End();
|
---|
81 | break;
|
---|
82 | case Scene::PASS_RAYTRACE:
|
---|
83 | //this->device->SetRenderState( D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_RED );
|
---|
84 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
---|
85 |
|
---|
86 | // set the technique to use
|
---|
87 | TerrainEffect->SetTechnique( ShaderTechHandleRay );
|
---|
88 |
|
---|
89 | D3DXMatrixMultiply(&this->worldViewMat, &this->myScene->getWorldMatrix(), &this->myScene->getViewMatrix());
|
---|
90 | TerrainEffect->SetMatrix(WorldViewHandle, &this->worldViewMat);
|
---|
91 | D3DXMatrixMultiply(&this->worldViewProjMat, &this->worldViewMat, &this->myScene->getProjectionMatrix());
|
---|
92 | TerrainEffect->SetMatrix(WorldViewProjectionHandle, &this->worldViewProjMat);
|
---|
93 |
|
---|
94 | TerrainEffect->Begin(&numPasses, 0);
|
---|
95 | for(UINT j = 0; j < numPasses; j++)
|
---|
96 | {
|
---|
97 | this->device->SetTexture(0, obj->Textures[0]);
|
---|
98 | // begin effect render pass
|
---|
99 | TerrainEffect->BeginPass(j);
|
---|
100 |
|
---|
101 | this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
|
---|
102 |
|
---|
103 | if(obj->isPMesh()) {
|
---|
104 | (*obj->getProgressiveMesh())->DrawSubset(0);
|
---|
105 | } else {
|
---|
106 | (*obj->getMesh())->DrawSubset(0);
|
---|
107 | }
|
---|
108 |
|
---|
109 | // end effect render pass
|
---|
110 | TerrainEffect->EndPass();
|
---|
111 | }
|
---|
112 | TerrainEffect->End();
|
---|
113 | //this->device->SetRenderState( D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA|D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_RED );
|
---|
114 |
|
---|
115 | break;
|
---|
116 | case Scene::PASS_DEPTH: //habs im fx file noch nicht umgestellt
|
---|
117 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
---|
118 |
|
---|
119 | // set the technique to use
|
---|
120 | TerrainEffect->SetTechnique( ShaderTechHandleDepth );
|
---|
121 |
|
---|
122 | D3DXMatrixMultiply(&this->worldViewMat, &this->myScene->getWorldMatrix(), &this->myScene->getViewMatrix());
|
---|
123 | D3DXMatrixMultiply(&this->worldViewProjMat, &this->worldViewMat, &this->myScene->getProjectionMatrix());
|
---|
124 | TerrainEffect->SetMatrix(WorldViewProjectionHandle, &this->worldViewProjMat);
|
---|
125 |
|
---|
126 | TerrainEffect->Begin(&numPasses, 0);
|
---|
127 | for(UINT j = 0; j < numPasses; j++)
|
---|
128 | {
|
---|
129 | this->device->SetTexture(0, obj->Textures[0]);
|
---|
130 | // begin effect render pass
|
---|
131 | TerrainEffect->BeginPass(j);
|
---|
132 |
|
---|
133 | this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
|
---|
134 |
|
---|
135 | if(obj->isPMesh()) {
|
---|
136 | (*obj->getProgressiveMesh())->DrawSubset(0);
|
---|
137 | } else {
|
---|
138 | (*obj->getMesh())->DrawSubset(0);
|
---|
139 | }
|
---|
140 |
|
---|
141 | // end effect render pass
|
---|
142 | TerrainEffect->EndPass();
|
---|
143 | }
|
---|
144 | TerrainEffect->End();
|
---|
145 | break;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | this->device->EndScene();
|
---|
149 | }
|
---|
150 |
|
---|
151 | void TerrainRenderer::OnLostDevice( void* pUserContext )
|
---|
152 | {
|
---|
153 | HRESULT hr;
|
---|
154 | this->myScene->manager->printToConsole(" OnLostDevice TerrainRenderer");
|
---|
155 | V( this->TerrainEffect->OnLostDevice() );
|
---|
156 | }
|
---|
157 |
|
---|
158 | void TerrainRenderer::OnDestroyDevice( void* pUserContext )
|
---|
159 | {
|
---|
160 | this->myScene->manager->printToConsole(" OnDestroyDevice TerrainRenderer");
|
---|
161 | //SAFE_RELEASE(this->TerrainEffect);
|
---|
162 | this->myScene->manager->releaseEffect(this->TerrainEffect);
|
---|
163 | this->TerrainEffect = NULL;
|
---|
164 | }
|
---|
165 |
|
---|
166 | HRESULT TerrainRenderer::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
|
---|
167 | {
|
---|
168 | this->device = pd3dDevice;
|
---|
169 | this->TerrainEffect = this->myScene->manager->getEffect(GameManager::EFFECT_TERRAIN);
|
---|
170 | if(!this->TerrainEffect) {
|
---|
171 | this->TerrainEffect = this->myScene->manager->loadEffect(GameManager::EFFECT_TERRAIN, L"shaders/terrain.obj");
|
---|
172 | }
|
---|
173 | this->setEffect(this->TerrainEffect);
|
---|
174 | return S_OK;
|
---|
175 | }
|
---|
176 |
|
---|
177 | bool TerrainRenderer::isLowerThan(Renderer* renderer)
|
---|
178 | {
|
---|
179 | return this->getRenderPriority() < renderer->getRenderPriority();
|
---|
180 | } |
---|