1 #include "..\include\raMain.h" 19 m_nVertices = m_width * m_depth;
20 m_nIndices = 6 * (m_width - 1) * (m_depth - 1);
24 for(
int i = 0; i < 3;i++)
26 m_pVertices[i] = NULL;
34 for(
int n = 0; n < 3; n++)
40 m_lastAnimationTimeStamp = 0;
46 for(
int i = 0; i < 3; i++)
58 D3D11_INPUT_ELEMENT_DESC layout[] =
60 {
"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,
61 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
62 {
"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT,
63 1, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
64 {
"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,
65 2, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
73 pTechnique->GetPassByIndex(0)->GetDesc(&PassDesc);
83 for(
int i = 0; i < 3; i++)
95 D3D11_BUFFER_DESC bufferDesc[3];
96 D3D11_SUBRESOURCE_DATA InitData[3];
98 for(
int n = 0; n < 3; n++)
100 bufferDesc[n].Usage = D3D11_USAGE_DEFAULT;
101 bufferDesc[n].BindFlags = D3D11_BIND_VERTEX_BUFFER;
102 bufferDesc[n].CPUAccessFlags = 0;
103 bufferDesc[n].MiscFlags = 0;
105 InitData[n].SysMemPitch = 0;
106 InitData[n].SysMemSlicePitch = 0;
109 bufferDesc[0].ByteWidth = m_nVertices *
sizeof(D3DXVECTOR3);
110 InitData[0].pSysMem = m_pVertices[0];
111 m_dx->
GetDevice()->CreateBuffer(&bufferDesc[0], &InitData[0], &m_pVB[0]);
113 bufferDesc[1].ByteWidth = m_nVertices *
sizeof(D3DXVECTOR3);
114 InitData[1].pSysMem = m_pNormals;
115 m_dx->
GetDevice()->CreateBuffer(&bufferDesc[1], &InitData[1], &m_pVB[1]);
117 bufferDesc[2].ByteWidth = m_nVertices *
sizeof(D3DXVECTOR2);
118 InitData[2].pSysMem = m_pTexcoords;
119 m_dx->
GetDevice()->CreateBuffer(&bufferDesc[2], &InitData[2], &m_pVB[2]);
128 for (
int z = 0; z < m_depth; z++)
130 for (
int x = 0; x < m_width; x++)
132 for (
int b = 0; b < 3; b++)
134 m_pVertices[b][n].
x = (float) x - m_width / 2;
135 m_pVertices[b][n].
y = 0.0f;
136 m_pVertices[b][n].
z = (float) z - m_depth / 2;
138 m_pTexcoords[n].
x = x / (m_width - 1.0f);
139 m_pTexcoords[n].
y = z / (m_depth - 1.0f);
142 m_pNormals[n].
x = 0.0f;
143 m_pNormals[n].
y = 1.0f;
144 m_pNormals[n].
z = 0.0f;
155 D3D11_BUFFER_DESC bufferDesc;
156 bufferDesc.ByteWidth = m_nIndices *
sizeof(
UINT16);
157 bufferDesc.Usage = D3D11_USAGE_DEFAULT;
158 bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
159 bufferDesc.CPUAccessFlags = 0;
160 bufferDesc.MiscFlags = 0;
162 D3D11_SUBRESOURCE_DATA InitData;
163 InitData.pSysMem = m_pIndices;
164 m_dx->
GetDevice()->CreateBuffer( &bufferDesc, &InitData, &m_pIB);
169 m_pIndices =
new UINT16[m_nIndices];
172 for(
int z = 0; z < m_depth - 1; z++)
174 for(
int x = 0; x < m_width - 1; x++)
176 m_pIndices[n + 0] = (
UINT16) (z * m_width + x);
177 m_pIndices[n + 1] = (
UINT16) ((z + 1) * m_width + x);
178 m_pIndices[n + 2] = (
UINT16) (z * m_width + (x + 1));
179 m_pIndices[n + 3] = (
UINT16) ((z + 1) * m_width + x);
180 m_pIndices[n + 4] = (
UINT16) ((z + 1) * m_width + (x + 1));
181 m_pIndices[n + 5] = (
UINT16) (z * m_width + (x + 1));
204 Strides[0] =
sizeof(D3DXVECTOR3);
205 Strides[1] =
sizeof(D3DXVECTOR3);
206 Strides[2] =
sizeof(D3DXVECTOR2);
213 pTechnique->GetDesc( &techDesc );
215 for(UINT p = 0; p < techDesc.
Passes; ++p )
225 const float C = 0.3f;
226 const float D = 0.4f;
227 const float U = 0.05f;
228 const float T = 0.13f;
230 m_lastFrameTime = fElapsedTime;
231 m_lastTimeStamp += fElapsedTime;
233 while (m_lastAnimationTimeStamp <= m_lastTimeStamp)
235 m_curBuffNo = (m_curBuffNo + 1) % 3;
237 double TERM1 = (4.0f - 8.0f * C * C * T * T / (D * D)) / (U * T + 2);
238 double TERM2 = (U * T - 2.0f) / (U * T + 2.0f);
239 double TERM3 = (2.0f * C * C * T * T / (D * D)) / (U * T + 2);
240 for(
int z = 1; z < m_depth - 1; z++)
243 int row = z * m_width;
244 int rowup = (z - 1) * m_width;
245 int rowdown = (z + 1) * m_width;
246 for(
int x = 1; x < m_width - 1; x++)
248 m_pVertices[m_curBuffNo][row + x].
y = (float)(
249 TERM1 * (
float)m_pVertices[(m_curBuffNo + 2) % 3][row+x].y
250 + TERM2 * (
float)m_pVertices[(m_curBuffNo + 1) % 3][row+x].y
251 + TERM3 * ((
float)m_pVertices[(m_curBuffNo + 2) % 3][row+x-1].y
252 + (
float)m_pVertices[(m_curBuffNo + 2) % 3][row+x+1].y
253 + (
float)m_pVertices[(m_curBuffNo + 2) % 3][rowup+x].y
254 + (
float)m_pVertices[(m_curBuffNo + 2) % 3][rowdown+x].y));
258 m_lastAnimationTimeStamp += (1.0f / ANIMATIONS_PER_SECOND);
264 m_pVertices[m_curBuffNo], 0, 0);
271 void raWater::CalculateNormals()
274 raVector3* buf = m_pVertices[m_curBuffNo];
276 D3DXVECTOR3* pNormals =
new D3DXVECTOR3[m_nVertices];
277 for(UINT n = 0; n < m_nVertices; n++)
279 pNormals[n] = D3DXVECTOR3(0, 0, 0);
283 for(UINT i = 0; i < m_nIndices / 3; i++)
285 UINT16 i0 = m_pIndices[n++];
286 UINT16 i1 = m_pIndices[n++];
287 UINT16 i2 = m_pIndices[n++];
288 D3DXVECTOR3 v0 = buf[i0];
289 D3DXVECTOR3 v1 = buf[i1];
290 D3DXVECTOR3 v2 = buf[i2];
291 D3DXVECTOR3 diff1 = v2 - v1 ;
292 D3DXVECTOR3 diff2 = v0 - v1 ;
294 D3DXVec3Cross(&fn, &diff1, &diff2);
295 pNormals[(int)i0] += fn ;
296 pNormals[(int)i1] += fn;
297 pNormals[(int)i2] += fn;
301 for(z = 0;z < m_depth;z++)
303 for(x = 0;x < m_width;x++)
305 D3DXVec3Normalize((D3DXVECTOR3*)&m_pNormals[n], &pNormals[n]);
316 depth = depth * m_lastFrameTime * ANIMATIONS_PER_SECOND ;
317 _PREP(depth, x, y, 0, 0);
318 _PREP(depth, x, y, 0, 1);
319 _PREP(depth, x, y, 1, 0);
320 _PREP(depth, x, y, 1, 1);
325 for (
int x = 0; x < m_width; x++)
327 _PREP(depth, (
float)x, 1, 0, 0);
333 for (
int z = 0; z < m_depth; z++)
342 int vertex = (int)(z + addz) * m_width + (int)(x + addx);
343 float diffz = (float)(z - floor(z + addz));
344 float diffx = (float)(x - floor(x + addx));
345 float dist = (float)(sqrt(diffz * diffz + diffx * diffx));
346 float power = 1 - dist;
347 if (power < 0) power = 0;
348 m_pVertices[m_curBuffNo][vertex].
y += depth * power;
ID3D11DeviceContext * GetImmediateContext(void)
#define SAFE_DELETE_ARRAY(p)
virtual bool RenderMesh(LPCSTR techniqueName="")
interface ID3DX11EffectTechnique ID3DX11EffectTechnique
ID3D11Device * GetDevice(void)
virtual bool Update(float fTime, float fRunTime)
virtual void SetupIndices()
SIZE_T IAInputSignatureSize
virtual bool CreateVertexLayout()
virtual void CreateVertexBuffer()
ID3D11InputLayout * m_pVertexLayout
raWater(raSmartPointer< raDirectX > dx, int width, int depth, raMaterial *mat)
raMaterial ** m_pMaterials
virtual void SetupVertices()
void _PREP(float depth, float x, float y, int addx, int addy)
virtual void CreateIndexBuffer()
RAPI float raGetRandFloat(float min, float max)
void push(float x, float y, float depth)
raSmartPointer< raDirectX > m_dx
ID3DX11EffectTechnique * GetEffectTechnique(LPCSTR techniqueName="")