1 | #include "dxstdafx.h"
|
---|
2 | #include ".\OceanRenderer.h"
|
---|
3 | #include "GameManager.h"
|
---|
4 |
|
---|
5 | OceanRenderer::OceanRenderer(void) : Renderer()
|
---|
6 | {
|
---|
7 | this->setRenderPriority(20);
|
---|
8 | this->rendererType |= Renderer::RENDERER_OCEAN;
|
---|
9 | }
|
---|
10 |
|
---|
11 | OceanRenderer::~OceanRenderer(void)
|
---|
12 | {
|
---|
13 | this->myScene->manager->releaseEffect(this->OceanEffect);
|
---|
14 | this->OceanEffect = NULL;
|
---|
15 | }
|
---|
16 |
|
---|
17 | void OceanRenderer::init()
|
---|
18 | {
|
---|
19 |
|
---|
20 | }
|
---|
21 |
|
---|
22 | void OceanRenderer::setEffect(ID3DXEffect* effect) {
|
---|
23 | this->OceanEffect = effect;
|
---|
24 |
|
---|
25 | //set technique handles:
|
---|
26 | ShaderTechHandle = OceanEffect->GetTechniqueByName("PS20");
|
---|
27 | ShaderTechHandleDepth = OceanEffect->GetTechniqueByName("PS20_Depth");
|
---|
28 |
|
---|
29 | //matrix handles:
|
---|
30 | WorldViewProjectionHandle = OceanEffect->GetParameterByName(0, "wvpMatrix");
|
---|
31 | WorldHandle = OceanEffect->GetParameterByName(0, "worldMatrix");
|
---|
32 | WorldViewHandle = OceanEffect->GetParameterByName(0, "worldViewMatrix");
|
---|
33 | ViewInverseHandle = OceanEffect->GetParameterByName(0, "viewInverseMatrix");
|
---|
34 |
|
---|
35 | //float handles:
|
---|
36 | TimeHandle = OceanEffect->GetParameterByName(0, "time");
|
---|
37 | RayHandle = OceanEffect->GetParameterByName(0, "forRaytracer");
|
---|
38 |
|
---|
39 | float nearPlane = this->myScene->getActiveCamera()->getNearClipping();
|
---|
40 | float farPlane = this->myScene->getActiveCamera()->getFarClipping();
|
---|
41 | FarPlaneMinusNearPlaneHandle = OceanEffect->GetParameterByName(0, "FarPlaneMinusNearPlane");
|
---|
42 | OceanEffect->SetFloat(FarPlaneMinusNearPlaneHandle, farPlane - nearPlane);
|
---|
43 |
|
---|
44 | this->angle = 0;
|
---|
45 | }
|
---|
46 |
|
---|
47 | void OceanRenderer::render()
|
---|
48 | {
|
---|
49 | this->device->BeginScene();
|
---|
50 | SPTR<Node> node = this->myNode.lock();
|
---|
51 | Object3d *obj = (Object3d *) (node.get());
|
---|
52 | if(obj!=NULL && obj->isModelLoaded()) {
|
---|
53 | //if(this->myScene->getActiveRenderPass() == NULL) {
|
---|
54 | UINT numPasses = 0;
|
---|
55 | switch(this->myScene->getActivePassId()) {
|
---|
56 | case Scene::PASS_NORMAL:
|
---|
57 | // set the technique to use
|
---|
58 | OceanEffect->SetTechnique( ShaderTechHandle );
|
---|
59 |
|
---|
60 | D3DXMatrixMultiply(&this->worldViewMat, &this->myScene->getWorldMatrix(), &this->myScene->getViewMatrix());
|
---|
61 | D3DXMatrixMultiply(&this->worldViewProjMat, &this->worldViewMat, &this->myScene->getProjectionMatrix());
|
---|
62 | D3DXMatrixInverse(&viewInverseMatrix, 0, &this->myScene->getViewMatrix());
|
---|
63 |
|
---|
64 | OceanEffect->SetMatrix(WorldViewHandle, &this->worldViewMat);
|
---|
65 | OceanEffect->SetMatrix(WorldViewProjectionHandle, &this->worldViewProjMat);
|
---|
66 | OceanEffect->SetMatrix(WorldHandle, &this->myScene->getWorldMatrix());
|
---|
67 | OceanEffect->SetMatrix(ViewInverseHandle, &this->viewInverseMatrix);
|
---|
68 |
|
---|
69 | //this->angle += 0.01f;
|
---|
70 | //OceanEffect->SetFloat(TimeHandle, angle);
|
---|
71 | //textur-wellen:
|
---|
72 | this->angle += 0.05f;
|
---|
73 | OceanEffect->SetFloat(TimeHandle, (float)sin(angle / 4.0f));
|
---|
74 |
|
---|
75 | OceanEffect->SetBool(RayHandle, false);
|
---|
76 |
|
---|
77 | OceanEffect->Begin(&numPasses, 0);
|
---|
78 | for(UINT j = 0; j < numPasses; j++)
|
---|
79 | {
|
---|
80 | this->device->SetTexture(0, obj->Textures[0]);
|
---|
81 | // begin effect render pass
|
---|
82 | OceanEffect->BeginPass(j);
|
---|
83 |
|
---|
84 | this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
|
---|
85 | this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
|
---|
86 |
|
---|
87 | this->device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
|
---|
88 | this->device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
|
---|
89 |
|
---|
90 | if(obj->isPMesh()) {
|
---|
91 | (*obj->getProgressiveMesh())->DrawSubset(0);
|
---|
92 | } else {
|
---|
93 | (*obj->getMesh())->DrawSubset(0);
|
---|
94 | }
|
---|
95 |
|
---|
96 | this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
|
---|
97 |
|
---|
98 | // end effect render pass
|
---|
99 | OceanEffect->EndPass();
|
---|
100 | }
|
---|
101 |
|
---|
102 | this->device->SetRenderState(D3DRS_FOGENABLE, FALSE);
|
---|
103 |
|
---|
104 | OceanEffect->End();
|
---|
105 | break;
|
---|
106 | case Scene::PASS_RAYTRACE:
|
---|
107 | // set the technique to use
|
---|
108 | //this->device->SetRenderState( D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_RED );
|
---|
109 | OceanEffect->SetTechnique( ShaderTechHandle );
|
---|
110 |
|
---|
111 | D3DXMatrixMultiply(&this->worldViewMat, &this->myScene->getWorldMatrix(), &this->myScene->getViewMatrix());
|
---|
112 | D3DXMatrixMultiply(&this->worldViewProjMat, &this->worldViewMat, &this->myScene->getProjectionMatrix());
|
---|
113 | D3DXMatrixInverse(&viewInverseMatrix, 0, &this->myScene->getViewMatrix());
|
---|
114 |
|
---|
115 | OceanEffect->SetMatrix(WorldViewHandle, &this->worldViewMat);
|
---|
116 | OceanEffect->SetMatrix(WorldViewProjectionHandle, &this->worldViewProjMat);
|
---|
117 | OceanEffect->SetMatrix(WorldHandle, &this->myScene->getWorldMatrix());
|
---|
118 | OceanEffect->SetMatrix(ViewInverseHandle, &this->viewInverseMatrix);
|
---|
119 |
|
---|
120 | //this->angle += 0.01f;
|
---|
121 | //OceanEffect->SetFloat(TimeHandle, (float)sin(angle / 4.0f));
|
---|
122 | //OceanEffect->SetFloat(TimeHandle, angle);
|
---|
123 | OceanEffect->SetBool(RayHandle, true);
|
---|
124 | OceanEffect->CommitChanges();
|
---|
125 |
|
---|
126 | OceanEffect->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 | OceanEffect->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 | OceanEffect->EndPass();
|
---|
143 | }
|
---|
144 |
|
---|
145 | this->device->SetRenderState(D3DRS_FOGENABLE, FALSE);
|
---|
146 |
|
---|
147 | OceanEffect->End();
|
---|
148 | //this->device->SetRenderState( D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA|D3DCOLORWRITEENABLE_BLUE|D3DCOLORWRITEENABLE_GREEN|D3DCOLORWRITEENABLE_RED );
|
---|
149 | break;
|
---|
150 | case Scene::PASS_DEPTH:
|
---|
151 | // set the technique to use
|
---|
152 | OceanEffect->SetTechnique( ShaderTechHandleDepth );
|
---|
153 | this->device->SetRenderState(D3DRS_FOGENABLE, FALSE);
|
---|
154 | D3DXMatrixMultiply(&this->worldViewMat, &this->myScene->getWorldMatrix(), &this->myScene->getViewMatrix());
|
---|
155 | D3DXMatrixMultiply(&this->worldViewProjMat, &this->worldViewMat, &this->myScene->getProjectionMatrix());
|
---|
156 |
|
---|
157 | OceanEffect->SetMatrix(WorldViewProjectionHandle, &this->worldViewProjMat);
|
---|
158 |
|
---|
159 | D3DXMATRIX worldMat;
|
---|
160 | D3DXMatrixIdentity(&worldMat);
|
---|
161 | this->device->SetTransform( D3DTS_WORLD, &worldMat);
|
---|
162 | this->device->SetTransform( D3DTS_PROJECTION, this->myScene->getActiveCamera()->getProjectionMatrix());
|
---|
163 | this->device->SetTransform( D3DTS_VIEW, this->myScene->getActiveCamera()->getViewMatrix());
|
---|
164 |
|
---|
165 | //this->angle += 0.01f;
|
---|
166 | //OceanEffect->SetFloat(TimeHandle, (float)sin(angle / 4.0f));
|
---|
167 | //OceanEffect->SetFloat(TimeHandle, angle);
|
---|
168 | OceanEffect->SetBool(RayHandle, false);
|
---|
169 | OceanEffect->CommitChanges();
|
---|
170 |
|
---|
171 | OceanEffect->Begin(&numPasses, 0);
|
---|
172 | for(UINT j = 0; j < numPasses; j++)
|
---|
173 | {
|
---|
174 | this->device->SetTexture(0, obj->Textures[0]);
|
---|
175 | // begin effect render pass
|
---|
176 | OceanEffect->BeginPass(j);
|
---|
177 |
|
---|
178 | this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
|
---|
179 |
|
---|
180 | if(obj->isPMesh()) {
|
---|
181 | (*obj->getProgressiveMesh())->DrawSubset(0);
|
---|
182 | } else {
|
---|
183 | (*obj->getMesh())->DrawSubset(0);
|
---|
184 | }
|
---|
185 |
|
---|
186 | // end effect render pass
|
---|
187 | OceanEffect->EndPass();
|
---|
188 | }
|
---|
189 |
|
---|
190 | this->device->SetRenderState(D3DRS_FOGENABLE, FALSE);
|
---|
191 |
|
---|
192 | OceanEffect->End();
|
---|
193 | break;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | this->device->EndScene();
|
---|
197 | }
|
---|
198 |
|
---|
199 | void OceanRenderer::OnLostDevice( void* pUserContext )
|
---|
200 | {
|
---|
201 | HRESULT hr;
|
---|
202 | this->myScene->manager->printToConsole(" OnLostDevice OceanRenderer");
|
---|
203 | V( this->OceanEffect->OnLostDevice() );
|
---|
204 | }
|
---|
205 |
|
---|
206 | void OceanRenderer::OnDestroyDevice( void* pUserContext )
|
---|
207 | {
|
---|
208 | this->myScene->manager->printToConsole(" OnDestroyDevice OceanRenderer");
|
---|
209 | this->myScene->manager->releaseEffect(this->OceanEffect);
|
---|
210 | this->OceanEffect = NULL;
|
---|
211 | }
|
---|
212 |
|
---|
213 | HRESULT OceanRenderer::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
|
---|
214 | {
|
---|
215 | this->device = pd3dDevice;
|
---|
216 | this->OceanEffect = this->myScene->manager->getEffect(GameManager::EFFECT_OCEAN);
|
---|
217 | if(!this->OceanEffect) {
|
---|
218 | this->OceanEffect = this->myScene->manager->loadEffect(GameManager::EFFECT_OCEAN, L"shaders/Ocean.obj");
|
---|
219 | }
|
---|
220 | this->setEffect(this->OceanEffect);
|
---|
221 | return S_OK;
|
---|
222 | }
|
---|
223 |
|
---|
224 | bool OceanRenderer::isLowerThan(Renderer* renderer)
|
---|
225 | {
|
---|
226 | return this->getRenderPriority() < renderer->getRenderPriority();
|
---|
227 | }
|
---|