1 | #include "dxstdafx.h"
|
---|
2 | #include "OceanEffect.h"
|
---|
3 |
|
---|
4 | #include "waves.h"
|
---|
5 |
|
---|
6 | OceanEffect::OceanEffect(LPDIRECT3DDEVICE9 device)
|
---|
7 | {
|
---|
8 | this->device = device;
|
---|
9 | D3DXCreateSprite( device, &g_pTextSprite );
|
---|
10 | DXUTGetGlobalResourceCache().CreateFont( device, 15, 0, FW_BOLD, 0, FALSE, DEFAULT_CHARSET,
|
---|
11 | OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
|
---|
12 | L"Arial", &g_pFont );
|
---|
13 |
|
---|
14 | nShips = 2;
|
---|
15 |
|
---|
16 | D3DXCreateTextureFromFile(device, L"waves2.dds", &waterTexture);
|
---|
17 | D3DXCreateTextureFromFile(device, L"moonbase3.bmp", &elevationTexture);
|
---|
18 | D3DXCreateCubeTextureFromFile(device, L"CloudyHillsCubemap2.dds", &environmentCube);
|
---|
19 |
|
---|
20 | camera = new CFirstPersonCamera();
|
---|
21 | camera->SetViewParams( &D3DXVECTOR3(0.0f, 0.0f, 4.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f));
|
---|
22 | camera->SetProjParams(D3DX_PI/4, 1.0f, 0.1f, 1000.0f);
|
---|
23 |
|
---|
24 | device->CreateIndexBuffer(sizeof(short) * 3 * 2 * 127 * 127, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &oceanIndexBuffer, NULL);
|
---|
25 | device->CreateVertexBuffer(sizeof(float) * 3 * 128 * 128, D3DUSAGE_WRITEONLY, D3DFVF_XYZ, D3DPOOL_DEFAULT, &oceanVertexBuffer, NULL);
|
---|
26 |
|
---|
27 | short* indexData;
|
---|
28 | oceanIndexBuffer->Lock(0, sizeof(short) * 3 * 2 * 127 * 127, (void**)&indexData, 0);
|
---|
29 |
|
---|
30 | for(int i=0; i<127; i++)
|
---|
31 | for(int j=0; j<127; j++)
|
---|
32 | {
|
---|
33 | indexData[ (i * 127 + j) * 6] = i * 128 + j;
|
---|
34 | indexData[ (i * 127 + j) * 6 + 2] = i * 128 + j + 1;
|
---|
35 | indexData[ (i * 127 + j) * 6 + 1] = (i + 1) * 128 + j;
|
---|
36 | indexData[ (i * 127 + j) * 6 + 4] = i * 128 + j + 1;
|
---|
37 | indexData[ (i * 127 + j) * 6 + 5] = (i + 1) * 128 + j;
|
---|
38 | indexData[ (i * 127 + j) * 6 + 3] = (i + 1) * 128 + j + 1;
|
---|
39 | }
|
---|
40 |
|
---|
41 | oceanIndexBuffer->Unlock();
|
---|
42 |
|
---|
43 | float* vertexData;
|
---|
44 | oceanVertexBuffer->Lock(0, sizeof(float) * 3 * 128 * 128, (void**)&vertexData, 0);
|
---|
45 |
|
---|
46 | for(int i=0; i<128; i++)
|
---|
47 | for(int j=0; j<128; j++)
|
---|
48 | {
|
---|
49 | vertexData[ (i * 128 + j) * 3] = (float)j / 127;
|
---|
50 | vertexData[ (i * 128 + j) * 3 + 1] = (float)0;
|
---|
51 | vertexData[ (i * 128 + j) * 3 + 2] = (float)i/127;
|
---|
52 | }
|
---|
53 |
|
---|
54 | oceanVertexBuffer->Unlock();
|
---|
55 |
|
---|
56 | //creating the effect:
|
---|
57 | LPD3DXBUFFER pBufferErrors = NULL;
|
---|
58 |
|
---|
59 | HRESULT hr = D3DXCreateEffectFromFile(device, L"ocean.fx", NULL, NULL, 0, NULL, &effect, &pBufferErrors);
|
---|
60 |
|
---|
61 | if( FAILED(hr))
|
---|
62 | {
|
---|
63 | LPVOID pCompilErrors = pBufferErrors->GetBufferPointer();
|
---|
64 | MessageBoxA(NULL, (const char*)pCompilErrors, "Fx Compile Error",
|
---|
65 | MB_OK|MB_ICONEXCLAMATION);
|
---|
66 | exit(-1);
|
---|
67 | return;
|
---|
68 | }
|
---|
69 |
|
---|
70 | //loading the x file:
|
---|
71 | hr = D3DXLoadMeshFromX(
|
---|
72 | L"ship_model\\ketch.x",
|
---|
73 | // L"ship_model\\unitsphere.x",
|
---|
74 | D3DXMESH_MANAGED,
|
---|
75 | device,
|
---|
76 | &adjBuffer,
|
---|
77 | &mtrlBuffer,
|
---|
78 | 0,
|
---|
79 | &numMtrls,
|
---|
80 | &ship);
|
---|
81 | LPD3DXMESH clonedShip;
|
---|
82 | ship->CloneMeshFVF(ship->GetOptions(), D3DFVF_XYZ | D3DFVF_NORMAL, device, &clonedShip);
|
---|
83 | ship->Release();
|
---|
84 | ship = clonedShip;
|
---|
85 |
|
---|
86 | if( mtrlBuffer != 0 && numMtrls != 0 )
|
---|
87 | {
|
---|
88 | D3DXMATERIAL* mtrls = (D3DXMATERIAL*)mtrlBuffer->GetBufferPointer();
|
---|
89 |
|
---|
90 | for(int i = 0; i < numMtrls; i++)
|
---|
91 | {
|
---|
92 | mtrls[i].MatD3D.Ambient = mtrls[i].MatD3D.Diffuse;
|
---|
93 |
|
---|
94 | Mtrls.push_back( mtrls[i].MatD3D );
|
---|
95 |
|
---|
96 | if( mtrls[i].pTextureFilename != 0 )
|
---|
97 | {
|
---|
98 | IDirect3DTexture9* tex = 0;
|
---|
99 | D3DXCreateTextureFromFile(
|
---|
100 | device,
|
---|
101 | //mtrls[i].pTextureFilename,
|
---|
102 | NULL,
|
---|
103 | &tex);
|
---|
104 | textures.push_back( tex );
|
---|
105 | }
|
---|
106 | else
|
---|
107 | {
|
---|
108 | textures.push_back( 0 );
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | mtrlBuffer->Release();
|
---|
113 |
|
---|
114 | /*
|
---|
115 | hr = D3DXLoadMeshFromX(L"ship_model\\ketch.x", NULL, device, NULL, NULL, NULL, NULL, &ship);
|
---|
116 | if( FAILED(hr))
|
---|
117 | {
|
---|
118 | MessageBoxA(NULL, (const char*)"Can't load the ship container file", "Loading the Mesh has failed",
|
---|
119 | MB_OK|MB_ICONEXCLAMATION);
|
---|
120 | exit(-1);
|
---|
121 | return;
|
---|
122 | }*/
|
---|
123 |
|
---|
124 | D3DVERTEXELEMENT9 shipElement[] = { {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
|
---|
125 | {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
|
---|
126 | {1, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
|
---|
127 | {1, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
|
---|
128 | {1, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2},
|
---|
129 | D3DDECL_END()};
|
---|
130 |
|
---|
131 | ships[0].direction = D3DXVECTOR2(1,0);
|
---|
132 | ships[0].position = D3DXVECTOR2(0,0);
|
---|
133 |
|
---|
134 | ships[1].direction = D3DXVECTOR2(.7,0.7);
|
---|
135 | ships[1].position = D3DXVECTOR2(50,15);
|
---|
136 |
|
---|
137 | HRESULT hrr = device->CreateVertexDeclaration(shipElement, &shipDeclaration);
|
---|
138 | device->CreateVertexBuffer(MAXSHIPS * sizeof(float) * 3 * 3, D3DUSAGE_DYNAMIC, 0, D3DPOOL_DEFAULT, &instanceVertexBuffer, NULL);
|
---|
139 |
|
---|
140 | ship->GetVertexBuffer(&shipVertexBuffer);
|
---|
141 | ship->GetIndexBuffer(&shipIndexBuffer);
|
---|
142 |
|
---|
143 | applyShip();
|
---|
144 |
|
---|
145 | //optimizing the mesh:
|
---|
146 | //ship->OptimizeInplace(D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE | D3DXMESHOPT_STRIPREORDER, NULL, NULL, NULL, NULL);
|
---|
147 |
|
---|
148 |
|
---|
149 | D3DXMATRIX rotationMatrix, translationMatrix;
|
---|
150 | D3DXMatrixRotationX(&rotationMatrix, 4.7123); //rotating 270 degrees
|
---|
151 | D3DXMatrixTranslation(&translationMatrix, 0, 1, 0.0); //translating one unit up
|
---|
152 | D3DXMatrixScaling(&downScale, .01,.01,.01);
|
---|
153 | D3DXMatrixMultiply(&finalWorld, &rotationMatrix, &downScale);
|
---|
154 | D3DXMatrixMultiply(&finalWorld, &finalWorld, &translationMatrix);
|
---|
155 |
|
---|
156 |
|
---|
157 | //applying the model transformations
|
---|
158 | /* D3DXMATRIX rotationMatrix, translationMatrix, ssm;
|
---|
159 |
|
---|
160 | D3DXMatrixIdentity(&finalWorld);
|
---|
161 | D3DXMatrixTranslation(&translationMatrix, 0, 0.6, 0.0); //translating one unit up
|
---|
162 | D3DXMatrixMultiply(&finalWorld, &finalWorld, &translationMatrix);
|
---|
163 | double fp = 0.1, bp = 2.1;
|
---|
164 | D3DXMATRIX sphereToShip(1.0f, 0.0f, 0.0f, 0.0f,
|
---|
165 | 0.0f, (bp + fp)/(bp-fp), 0.0f, 1.0f
|
---|
166 | ,
|
---|
167 | 0.0f, 0.0f, 1.0f, 0.0f,
|
---|
168 | 0.0f, -2.0 * bp * fp / (bp-fp), 0.0f, 0.0f);
|
---|
169 | D3DXMatrixMultiply(&finalWorld, &finalWorld, &sphereToShip);
|
---|
170 | D3DXMatrixTranslation(&translationMatrix, 0, -1.0, 0.0);
|
---|
171 | D3DXMatrixMultiply(&finalWorld, &finalWorld, &translationMatrix);
|
---|
172 | D3DXMatrixScaling(&ssm, 8.0, 10.0, 1.0);
|
---|
173 | D3DXMatrixMultiply(&finalWorld, &finalWorld, &ssm);*/
|
---|
174 |
|
---|
175 | }
|
---|
176 |
|
---|
177 | OceanEffect::~OceanEffect(void)
|
---|
178 | {
|
---|
179 | if(effect != NULL) effect->Release();
|
---|
180 | if(shipVertexBuffer != NULL) shipVertexBuffer->Release();
|
---|
181 | if(shipIndexBuffer != NULL) shipIndexBuffer->Release();
|
---|
182 | if(oceanIndexBuffer != NULL)oceanIndexBuffer->Release();
|
---|
183 | if(oceanVertexBuffer != NULL) oceanVertexBuffer->Release();
|
---|
184 | if(environmentCube != NULL) environmentCube->Release();
|
---|
185 | if(waterTexture != NULL) waterTexture->Release();
|
---|
186 | if(elevationTexture != NULL) elevationTexture->Release();
|
---|
187 | if(ship != NULL) ship->Release();
|
---|
188 | if(shipDeclaration != NULL) shipDeclaration->Release();
|
---|
189 | if(instanceVertexBuffer != NULL) instanceVertexBuffer->Release();
|
---|
190 | if(camera != NULL) delete camera;
|
---|
191 | if(adjBuffer != NULL) adjBuffer->Release();
|
---|
192 | g_pTextSprite->Release();
|
---|
193 | g_pFont->Release();
|
---|
194 | }
|
---|
195 |
|
---|
196 | void OceanEffect::render()
|
---|
197 | {
|
---|
198 | device->SetRenderState(D3DRS_CULLMODE, NULL);
|
---|
199 | //device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
|
---|
200 | device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DXCOLOR(0.0,.3,0.7,0), 1.0f, NULL);
|
---|
201 | device->BeginScene();
|
---|
202 |
|
---|
203 | //displaying the FPS
|
---|
204 | CDXUTTextHelper txtHelper( g_pFont, g_pTextSprite, 15 );
|
---|
205 | txtHelper.Begin();
|
---|
206 | txtHelper.SetInsertionPos( 5, 5 );
|
---|
207 | txtHelper.SetForegroundColor( D3DXCOLOR( 0.0f, 5.0f, 1.0f, 1.0f ) );
|
---|
208 | txtHelper.DrawTextLine( DXUTGetFrameStats(true) ); // Show FPS
|
---|
209 | txtHelper.DrawTextLine( DXUTGetDeviceStats() );
|
---|
210 |
|
---|
211 | txtHelper.SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
|
---|
212 | txtHelper.End();
|
---|
213 |
|
---|
214 |
|
---|
215 | effect->SetTechnique("ocean");
|
---|
216 | UINT nPasses;
|
---|
217 | effect->Begin(&nPasses, 0);
|
---|
218 | effect->BeginPass(0);
|
---|
219 |
|
---|
220 | device->SetIndices(oceanIndexBuffer);
|
---|
221 | device->SetStreamSource(0, oceanVertexBuffer, 0, 3 * sizeof(float));
|
---|
222 | device->SetFVF(D3DFVF_XYZ);
|
---|
223 |
|
---|
224 | effect->SetFloat("time", time);
|
---|
225 | const D3DXVECTOR3* ep = camera->GetEyePt();
|
---|
226 | HRESULT hr = effect->SetFloatArray("eyePosition", (float*)ep,3);
|
---|
227 | effect->SetTexture("environmentCubeTexture", environmentCube);
|
---|
228 | effect->SetTexture("waterTexture", waterTexture);
|
---|
229 |
|
---|
230 | D3DXMATRIX viewProjMatrix;
|
---|
231 | D3DXMatrixMultiply(&viewProjMatrix, camera->GetViewMatrix(), camera->GetProjMatrix());
|
---|
232 | effect->SetMatrix("viewProjMatrix", &viewProjMatrix);
|
---|
233 |
|
---|
234 | D3DXVECTOR3 pushBack = *camera->GetWorldAhead();
|
---|
235 | float maxDistortion = 0.0;
|
---|
236 | for(int iWave=0; iWave<NWAVES; iWave++)
|
---|
237 | maxDistortion += wave[iWave].amplitude * (1.0 + wave[iWave].deformation);
|
---|
238 | pushBack *= - maxDistortion * 1.44;
|
---|
239 |
|
---|
240 | D3DXMATRIX inverseViewProjMatrix;
|
---|
241 | D3DXMatrixInverse(&inverseViewProjMatrix, NULL, &viewProjMatrix);
|
---|
242 | D3DXMATRIX pushEyeBackMatrix;
|
---|
243 | D3DXMatrixTranslation(&pushEyeBackMatrix, pushBack.x, pushBack.y, pushBack.z);
|
---|
244 | D3DXMatrixMultiply(&inverseViewProjMatrix, &inverseViewProjMatrix, &pushEyeBackMatrix);
|
---|
245 | D3DXVECTOR4 worldCorner0,worldCorner1,worldCorner2,worldCorner3, transVector;
|
---|
246 |
|
---|
247 | transVector=D3DXVECTOR4(-1,-1,0,1);
|
---|
248 | D3DXVec4Transform(&worldCorner0, &transVector ,&inverseViewProjMatrix);
|
---|
249 |
|
---|
250 | transVector=D3DXVECTOR4(1,-1,0,1);
|
---|
251 | D3DXVec4Transform(&worldCorner1, &transVector ,&inverseViewProjMatrix);
|
---|
252 |
|
---|
253 | transVector=D3DXVECTOR4(-1,1,0,1);
|
---|
254 | D3DXVec4Transform(&worldCorner2, &transVector ,&inverseViewProjMatrix);
|
---|
255 |
|
---|
256 | transVector=D3DXVECTOR4(1,1,0,1);
|
---|
257 | D3DXVec4Transform(&worldCorner3, &transVector ,&inverseViewProjMatrix);
|
---|
258 |
|
---|
259 | D3DXVECTOR3 worldCornerCar0 = D3DXVECTOR3(worldCorner0.x/worldCorner0.w, worldCorner0.y/worldCorner0.w, worldCorner0.z/worldCorner0.w);
|
---|
260 | D3DXVECTOR3 worldCornerCar1 = D3DXVECTOR3(worldCorner1.x/worldCorner1.w, worldCorner1.y/worldCorner1.w, worldCorner1.z/worldCorner1.w);
|
---|
261 | D3DXVECTOR3 worldCornerCar2 = D3DXVECTOR3(worldCorner2.x/worldCorner2.w, worldCorner2.y/worldCorner2.w, worldCorner2.z/worldCorner2.w);
|
---|
262 | D3DXVECTOR3 worldCornerCar3 = D3DXVECTOR3(worldCorner3.x/worldCorner3.w, worldCorner3.y/worldCorner3.w, worldCorner3.z/worldCorner3.w);
|
---|
263 |
|
---|
264 | D3DXVECTOR3 epPushedBack = *ep + pushBack;
|
---|
265 | effect->SetFloatArray("eyePositionPushedBack", (float*)&epPushedBack,3);
|
---|
266 | worldCornerCar0 = worldCornerCar0 - epPushedBack;
|
---|
267 | D3DXVec3Normalize(&worldCornerCar0, &worldCornerCar0);
|
---|
268 | worldCornerCar1 = worldCornerCar1 - epPushedBack;
|
---|
269 | D3DXVec3Normalize(&worldCornerCar1, &worldCornerCar1);
|
---|
270 | worldCornerCar2 = worldCornerCar2 - epPushedBack;
|
---|
271 | D3DXVec3Normalize(&worldCornerCar2, &worldCornerCar2);
|
---|
272 | worldCornerCar3 = worldCornerCar3 - epPushedBack;
|
---|
273 | D3DXVec3Normalize(&worldCornerCar3, &worldCornerCar3);
|
---|
274 |
|
---|
275 | if(ep->y > 0)
|
---|
276 | {
|
---|
277 | if(worldCornerCar2.y > 0 && worldCornerCar0.y < 0) worldCornerCar2 += (worldCornerCar0 - worldCornerCar2) * (worldCornerCar2.y - worldCornerCar0.y / 40.0) / (-worldCornerCar0.y + worldCornerCar2.y);
|
---|
278 | if(worldCornerCar3.y > 0 && worldCornerCar1.y < 0) worldCornerCar3 += (worldCornerCar1 - worldCornerCar3) * (worldCornerCar3.y - worldCornerCar1.y / 40.0) / (-worldCornerCar1.y + worldCornerCar3.y);
|
---|
279 | // if(worldCornerCar2.y > 0 && worldCornerCar0.y < 0) worldCornerCar2.y = worldCornerCar0.y / 40.0;
|
---|
280 | // if(worldCornerCar3.y > 0 && worldCornerCar1.y < 0) worldCornerCar3.y = worldCornerCar1.y / 40.0;
|
---|
281 | }
|
---|
282 | else
|
---|
283 | {
|
---|
284 | if(worldCornerCar2.y < 0 && worldCornerCar0.y > 0) worldCornerCar0.y = worldCornerCar2.y / 40.0;
|
---|
285 | if(worldCornerCar3.y < 0 && worldCornerCar1.y > 0) worldCornerCar1.y = worldCornerCar3.y / 40.0;
|
---|
286 | }
|
---|
287 | /* if(worldCornerCar0.y > horizon) worldCornerCar0.y = horizon;
|
---|
288 | if(worldCornerCar1.y > horizon) worldCornerCar1.y = horizon;
|
---|
289 | if(worldCornerCar2.y > horizon) worldCornerCar2.y = horizon;
|
---|
290 | if(worldCornerCar3.y > horizon) worldCornerCar3.y = horizon;*/
|
---|
291 |
|
---|
292 | /* worldCornerCar0 = epPushedBack - (epPushedBack.y/worldCornerCar0.y) * worldCornerCar0;
|
---|
293 | worldCornerCar1 = epPushedBack - (epPushedBack.y/worldCornerCar1.y) * worldCornerCar1;
|
---|
294 | worldCornerCar2 = epPushedBack - (epPushedBack.y/worldCornerCar2.y) * worldCornerCar2;
|
---|
295 | worldCornerCar3 = epPushedBack - (epPushedBack.y/worldCornerCar3.y) * worldCornerCar3;*/
|
---|
296 |
|
---|
297 | effect->SetFloatArray("worldCornerCar0", (float*)&worldCornerCar0,3);
|
---|
298 | effect->SetFloatArray("worldCornerCar1", (float*)&worldCornerCar1,3);
|
---|
299 | effect->SetFloatArray("worldCornerCar2", (float*)&worldCornerCar2,3);
|
---|
300 | effect->SetFloatArray("worldCornerCar3", (float*)&worldCornerCar3,3);
|
---|
301 |
|
---|
302 |
|
---|
303 |
|
---|
304 | //setting the scale parameter:
|
---|
305 | D3DXMatrixMultiply(&shipWorldViewProjMatrix, &finalWorld, &viewProjMatrix);
|
---|
306 | effect->SetMatrix("shipWorldMatrix", &finalWorld);
|
---|
307 | effect->SetMatrix("shipViewProjMatrix", &viewProjMatrix);
|
---|
308 | D3DXVECTOR4 lightPosition = D3DXVECTOR4(10,10,0,0);
|
---|
309 | effect->SetVector("lightPosition", &lightPosition);
|
---|
310 |
|
---|
311 | //set ship positions for reflections
|
---|
312 |
|
---|
313 | for(int iShip=0; iShip<nShips; iShip++)
|
---|
314 | {
|
---|
315 | D3DXVECTOR3 oceanNormal;
|
---|
316 | D3DXVec3Cross(&oceanNormal, &shipLocations[iShip].tangent, &shipLocations[iShip].binormal);
|
---|
317 | D3DXMATRIX tangentToWorld(
|
---|
318 | shipLocations[iShip].binormal.x, shipLocations[iShip].binormal.y, shipLocations[iShip].binormal.z, 0,
|
---|
319 | oceanNormal.x, oceanNormal.y, oceanNormal.z, 0,
|
---|
320 | shipLocations[iShip].tangent.x, shipLocations[iShip].tangent.y, shipLocations[iShip].tangent.z, 0,
|
---|
321 | shipLocations[iShip].shipPos.x, shipLocations[iShip].shipPos.y, shipLocations[iShip].shipPos.z, 1);
|
---|
322 | D3DXMatrixTranspose(&tangentToWorld, &tangentToWorld);
|
---|
323 | D3DXMATRIX worldToTangent;
|
---|
324 | D3DXMatrixInverse(&worldToTangent, NULL, &tangentToWorld);
|
---|
325 |
|
---|
326 | char fxvarname[256];
|
---|
327 | sprintf(fxvarname, "shipLocations[%d]", iShip);
|
---|
328 | HRESULT hrx = effect->SetMatrix(fxvarname, &worldToTangent);
|
---|
329 | }
|
---|
330 |
|
---|
331 | effect->CommitChanges();
|
---|
332 | device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 128 * 128, 0, 127 * 127*2);
|
---|
333 |
|
---|
334 | effect->EndPass();
|
---|
335 | effect->End();
|
---|
336 |
|
---|
337 | /* effect->SetTechnique("terrain");
|
---|
338 | effect->Begin(&nPasses, 0);
|
---|
339 | effect->BeginPass(0);
|
---|
340 |
|
---|
341 | device->SetIndices(oceanIndexBuffer);
|
---|
342 | device->SetStreamSource(0, oceanVertexBuffer, 0, 3 * sizeof(float));
|
---|
343 | device->SetFVF(D3DFVF_XYZ);
|
---|
344 |
|
---|
345 | effect->SetTexture("elevationTexture", elevationTexture);
|
---|
346 |
|
---|
347 | effect->SetFloatArray("eyeDirection", (float*)camera->GetWorldAhead(),3);
|
---|
348 | effect->SetFloat("fp", 0.1f);
|
---|
349 | effect->SetFloat("bp", 1000.0f);
|
---|
350 |
|
---|
351 | effect->CommitChanges();
|
---|
352 | device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 128 * 128, 0, 127 * 127*2);
|
---|
353 |
|
---|
354 | effect->EndPass();
|
---|
355 | effect->End();*/
|
---|
356 |
|
---|
357 | hr = effect->SetTechnique("ship");
|
---|
358 | effect->Begin(&nPasses, 0);
|
---|
359 | effect->BeginPass(0);
|
---|
360 |
|
---|
361 | device->SetStreamSourceFreq(0, D3DSTREAMSOURCE_INDEXEDDATA | nShips);
|
---|
362 | int sss = ship->GetNumBytesPerVertex();
|
---|
363 | device->SetStreamSource(0, shipVertexBuffer, 0, ship->GetNumBytesPerVertex());
|
---|
364 | device->SetStreamSourceFreq(1, (unsigned)(D3DSTREAMSOURCE_INSTANCEDATA | 1));
|
---|
365 | device->SetStreamSource(1, instanceVertexBuffer, 0, sizeof(float) * 3 * 3);
|
---|
366 | device->SetVertexDeclaration(shipDeclaration);
|
---|
367 | device->SetIndices(shipIndexBuffer);
|
---|
368 |
|
---|
369 | device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, ship->GetNumVertices(), 0, ship->GetNumFaces());
|
---|
370 |
|
---|
371 | effect->EndPass();
|
---|
372 | effect->End();
|
---|
373 |
|
---|
374 |
|
---|
375 | device->EndScene();
|
---|
376 |
|
---|
377 | }
|
---|
378 |
|
---|
379 | void OceanEffect::move(double time, double elapsedTime)
|
---|
380 | {
|
---|
381 | camera->FrameMove(elapsedTime * 10.0);
|
---|
382 | for(int i=0; i< nShips; i++)
|
---|
383 | {
|
---|
384 | ships[i].Move(elapsedTime);
|
---|
385 | }
|
---|
386 | this->time = (float)time;
|
---|
387 | applyShip();
|
---|
388 | }
|
---|
389 |
|
---|
390 | void OceanEffect::message(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
---|
391 | {
|
---|
392 | camera->HandleMessages(hWnd, uMsg, wParam, lParam);
|
---|
393 | if(uMsg == WM_KEYDOWN)
|
---|
394 | {
|
---|
395 | switch(wParam)
|
---|
396 | {
|
---|
397 | case 'U':
|
---|
398 | ships[0].accelerate = true;
|
---|
399 | ships[0].brake = false;
|
---|
400 | break;
|
---|
401 | case 'J':
|
---|
402 | ships[0].accelerate = false;
|
---|
403 | ships[0].brake = true;
|
---|
404 | break;
|
---|
405 | case 'H':
|
---|
406 | ships[0].turnLeft = true;
|
---|
407 | ships[0].turnRight = false;
|
---|
408 | break;
|
---|
409 | case 'K':
|
---|
410 | ships[0].turnLeft = false;
|
---|
411 | ships[0].turnRight = true;
|
---|
412 | break;
|
---|
413 | }
|
---|
414 | }
|
---|
415 | if(uMsg == WM_KEYUP)
|
---|
416 | {
|
---|
417 | switch(wParam)
|
---|
418 | {
|
---|
419 | case 'U' :
|
---|
420 | ships[0].accelerate = false;
|
---|
421 | break;
|
---|
422 | case 'J':
|
---|
423 | ships[0].brake = false;
|
---|
424 | break;
|
---|
425 | case 'H':
|
---|
426 | ships[0].turnLeft = false;
|
---|
427 | break;
|
---|
428 | case 'K':
|
---|
429 | ships[0].turnRight = false;
|
---|
430 | break;
|
---|
431 | }
|
---|
432 | }
|
---|
433 |
|
---|
434 | }
|
---|
435 |
|
---|
436 | void OceanEffect::applyShip()
|
---|
437 | {
|
---|
438 | OceanEffect::ShipInstanceData* tempShips;
|
---|
439 | instanceVertexBuffer->Lock(0, MAXSHIPS * sizeof(float) * 3 * 3, (void**) &tempShips, 0);
|
---|
440 | for(int si=0; si<MAXSHIPS;si++)
|
---|
441 | {
|
---|
442 | D3DXVECTOR3 shipPos = D3DXVECTOR3(ships[si].position.x, 0, ships[si].position.y);
|
---|
443 | D3DXVECTOR3 du = D3DXVECTOR3(1, 0, 0);
|
---|
444 | D3DXVECTOR3 dv = D3DXVECTOR3(0, 0, 1);
|
---|
445 |
|
---|
446 | for (int i=0;i<NWAVES;i++)
|
---|
447 | {
|
---|
448 | float phase = (ships[si].position.x * wave[i].direction.x + ships[si].position.y * wave[i].direction.y) / wave[i].wavelength / 6.28 + time * sqrt(1.5915 * wave[i].wavelength);
|
---|
449 | shipPos += evaluateWave(wave[i],phase);
|
---|
450 | D3DXVECTOR3 da = evaluateWaveDerivative(wave[i], phase);
|
---|
451 | du += da * wave[i].direction.x / wave[i].wavelength / 6.28;
|
---|
452 | dv += da * wave[i].direction.y / wave[i].wavelength / 6.28;
|
---|
453 | }
|
---|
454 |
|
---|
455 | D3DXVec3Normalize(&du, &du);
|
---|
456 | D3DXVec3Normalize(&dv, &dv);
|
---|
457 |
|
---|
458 | shipLocations[si].binormal = tempShips[si].binormal = ships[si].direction.x * du + ships[si].direction.y * dv;
|
---|
459 | shipLocations[si].tangent = tempShips[si].tangent = ships[si].direction.x * dv - ships[si].direction.y * du;
|
---|
460 | shipLocations[si].shipPos = tempShips[si].shipPos = shipPos;
|
---|
461 | }
|
---|
462 | instanceVertexBuffer->Unlock();
|
---|
463 | }
|
---|
464 |
|
---|