Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Waves.h"
- #include "vertex.h"
- #include <algorithm>
- #include <vector>
- #include "StdAfx.h"
- #include "Level.h"
- Waves::Waves(Level* pLevel)
- : m_NumRows(0), m_NumCols(0), m_NumVertices(0), m_NumFaces(0),
- m_K1(0.0f), m_K2(0.0f), m_K3(0.0f), m_TimeStep(0.0f), m_SpatialStep(0.0f),
- m_PrevSolution(0), m_CurrSolution(0), m_Normals(0), m_VB(0), m_IB(0),mLightType(0),
- m_pLevel(pLevel)
- {
- D3DXMatrixIdentity(&mWavesWorld);
- D3DXMatrixIdentity(&mView);
- D3DXMatrixIdentity(&mProj);
- D3DXMatrixIdentity(&mWVP);
- }
- Waves::~Waves(void)
- {
- delete[] m_PrevSolution;
- delete[] m_CurrSolution;
- delete[] m_Normals;
- SafeRelease(m_VB);
- SafeRelease(m_IB);
- SafeDelete(m_FX);
- }
- void Waves::init(DWORD m, DWORD n, float dx, float dt, float speed, float damping,D3DXVECTOR3 pos)
- {
- m_NumRows = m;
- m_NumCols = n;
- m_NumVertices = m*n;
- m_NumFaces = (m-1) * (n-1) *2;
- m_TimeStep = dt;
- m_SpatialStep = dx;
- float d = damping * dt + 2.0f;
- float e = (speed * speed) * (dt * dt) / (dx * dx);
- m_K1 = (damping*dt-2.0f) / d;
- m_K2 = (4.0f - 8.0f*e) / d;
- m_K3 = (2.0f*e) / d;
- m_PrevSolution = new D3DXVECTOR3[m * n];
- m_CurrSolution = new D3DXVECTOR3[m * n];
- m_Normals = new D3DXVECTOR3[m * n];
- float halfWidth = (n-1) * dx * 0.5f;
- float halfDepth = (m-1) * dx * 0.5f;
- // grid vertices genereren in het systeem geh
- for(DWORD i = 0; i < m; ++i)
- {
- float z = halfDepth - i*dx;
- for(DWORD j = 0; j < n; ++j)
- {
- float x = -halfWidth + j * dx;
- m_PrevSolution[i * n + j] = D3DXVECTOR3(pos.x + x, pos.y,pos.z + z);
- m_CurrSolution[i * n + j] = D3DXVECTOR3(pos.x + x, pos.y,pos.z+ z);
- m_Normals[i * n + j] = D3DXVECTOR3(0.0f, 1.0f,0.0f);
- }
- }
- D3D10_BUFFER_DESC v_bd;
- v_bd.Usage = D3D10_USAGE_DYNAMIC;
- v_bd.ByteWidth = sizeof(Vertex) * m_NumVertices;
- v_bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
- v_bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
- v_bd.MiscFlags = 0;
- HR(m_pLevel->GetDevice()->CreateBuffer(&v_bd,0,&m_VB));
- //creër de index buffer, de index buffer is vast, dus we moeten deze maar een keer creëren en plaatsen
- vector<DWORD> indices(m_NumFaces * 3);
- int k = 0;
- for(DWORD i = 0; i < m-1; ++i)
- {
- for(DWORD j = 0;j<n-1; ++j)
- {
- indices[k] = i * n + j;
- indices[k+1] = i * n + j + 1;
- indices[k+2] = (i + 1)* n + j;
- indices[k+3] = (i + 1) * n + j;
- indices[k+4] = i * n + j + 1;
- indices[k+5] = (i + 1) * n + j + 1;
- k+=6; //volgende quad
- }
- }
- mLights[0].dir = D3DXVECTOR3(0.57735f, -0.57735f, 0.57735f);
- mLights[0].ambient = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
- mLights[0].diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
- mLights[0].specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
- buildFX();
- buildVertexLayouts();
- D3D10_BUFFER_DESC ibd;
- ibd.Usage = D3D10_USAGE_IMMUTABLE;
- ibd.ByteWidth = sizeof(DWORD) * m_NumFaces * 3;
- ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
- ibd.CPUAccessFlags = 0;
- ibd.MiscFlags = 0;
- D3D10_SUBRESOURCE_DATA iInitData;
- iInitData.pSysMem = &indices[0];
- HR(m_pLevel->GetDevice()->CreateBuffer(&ibd,&iInitData,&m_IB));
- }
- void Waves::update(float dt)
- {
- static float t = 0;
- //tijd verhogen
- t += dt;
- // simulatie alleen updaten op bepaald tijdstip
- if( t >= m_TimeStep)
- {
- //Only update interior points; we use zero boundary conditions.
- for(DWORD i = 1; i < m_NumRows-1; ++i)
- {
- for(DWORD j = 1; j < m_NumCols-1;++j)
- {
- // After this update we will be discarding the old previous
- // buffer, so overwrite that buffer with the new update.
- // Note how we can do this inplace (read/write to same element)
- // because we won't need prev_ij again and the assignment happens last.
- // Note j indexes x and i indexes z: h(x_j, z_i, t_k)
- // Moreover, out +z axis goes "down"; this is just to
- // keep consistent with our row indices going down.
- m_PrevSolution[i*m_NumCols+j].y =
- m_K1*m_PrevSolution[i*m_NumCols+j].y +
- m_K2*m_CurrSolution[i*m_NumCols+j].y +
- m_K3*(m_CurrSolution[(i+1)*m_NumCols+j].y +
- m_CurrSolution[(i-1)*m_NumCols+j].y +
- m_CurrSolution[i*m_NumCols+j+1].y +
- m_CurrSolution[i*m_NumCols+j-1].y);
- }
- }
- // We just overwrite the previous buffer with the new data, so
- // this data needs to become the current solution and the old
- // current solution becomes the new previous solution.
- swap(m_PrevSolution, m_CurrSolution);
- t = 0.0f; // reset tijd
- // Compute normals using finite difference scheme.
- for(DWORD i = 1; i < m_NumRows-1; ++i)
- {
- for(DWORD j = 1; j < m_NumCols-1; ++j)
- {
- float l = m_CurrSolution[i*m_NumCols+j-1].y;
- float r = m_CurrSolution[i*m_NumCols+j+1].y;
- float t = m_CurrSolution[(i-1)*m_NumCols+j-1].y;
- float b = m_CurrSolution[(i+1)*m_NumCols+j+1].y;
- m_Normals[i*m_NumCols+j].x = -r+l;
- m_Normals[i*m_NumCols+j].y = 2*m_SpatialStep;
- m_Normals[i*m_NumCols+j].z = b-t;
- D3DXVec3Normalize(&m_Normals[i*m_NumCols+j], &m_Normals[i*m_NumCols+j]);
- }
- }
- float xTrans = 20000;
- float yTrans = 20000;
- float zTrans = 20000;
- D3DXMatrixTranslation(&mWavesWorld, xTrans, yTrans, zTrans);
- // Update the vertex buffer with the new solution.
- Vertex* v = 0;
- HR(m_VB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&v ));
- for(DWORD i = 0; i < m_NumVertices; ++i)
- {
- v[i].pos = m_CurrSolution[i];
- v[i].diffuse = D3DXCOLOR(0.0f, 0.0f, v[i].pos.z / 10, 1.0f);
- v[i].spec = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
- v[i].normal = m_Normals[i];
- v;
- }
- m_VB->Unmap();
- }
- }
- void Waves::disturb()
- {
- // Don't disturb boundaries.
- //assert(i > 1 && i < mNumRows-2);
- //assert(j > 1 && j < mNumCols-2);
- DWORD i = 1 + rand() % 119;
- DWORD j = 1 + rand() % 119;
- float r = RandF(0.1f, 0.2f);
- //float r = RandF(1.0f, 2.0f);
- float halfMag = 0.5f* r;
- m_CurrSolution[i*m_NumCols+j].y += r;
- m_CurrSolution[i*m_NumCols+j+1].y += halfMag;
- m_CurrSolution[i*m_NumCols+j-1].y += halfMag;
- m_CurrSolution[(i+1)*m_NumCols+j].y += halfMag;
- m_CurrSolution[(i-1)*m_NumCols+j].y += halfMag;
- }
- void Waves::draw(GraphCamera *pGraphCamera)
- {
- m_pGraphCamera = pGraphCamera;
- UINT stride = sizeof(Vertex);
- UINT offset = 0;
- m_pLevel->GetDevice()->IASetVertexBuffers(0,1,&m_VB, &stride, &offset);
- m_pLevel->GetDevice()->IASetIndexBuffer(m_IB, DXGI_FORMAT_R32_UINT, 0);
- m_pLevel->GetDevice()->DrawIndexed(m_NumFaces*3, 0, 0);
- m_pLevel->GetDevice()->IASetInputLayout(m_VertexLayout);
- m_pLevel->GetDevice()->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
- D3D10_TECHNIQUE_DESC techDesc;
- m_Tech->GetDesc( &techDesc );
- D3DXVECTOR3 temp = m_pGraphCamera->GetPos();
- mfxEyePosVar->SetRawValue(&temp, 0, sizeof(D3DXVECTOR3));
- mfxLightVar->SetRawValue(&mLights[mLightType], 0, sizeof(Light));
- mfxLightType->SetInt(mLightType);
- for(UINT i = 0; i < techDesc.Passes; ++i)
- {
- ID3D10EffectPass* pass = m_Tech->GetPassByIndex(i);
- mWVP = mWavesWorld*mView*mProj;
- mfxWVPVar->SetMatrix((float*)&mWVP);
- mfxWorldVar->SetMatrix((float*)&mWavesWorld);
- pass->Apply(0);
- }
- }
- void Waves::buildFX()
- {
- DWORD shaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
- #if defined( DEBUG ) || defined( _DEBUG )
- shaderFlags |= D3D10_SHADER_DEBUG;
- shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION;
- #endif
- ID3D10Blob* compilationErrors = 0;
- HRESULT hr = 0;
- hr = D3DX10CreateEffectFromFile(_T("./Effect/Waves.fx"), 0, 0,
- "fx_4_0", shaderFlags, 0, m_pLevel->GetDevice(), 0, 0, &m_FX, &compilationErrors, 0);
- if(FAILED(hr))
- {
- if( compilationErrors )
- {
- MessageBoxA(0, (char*)compilationErrors->GetBufferPointer(), 0, 0);
- SafeRelease(compilationErrors);
- }
- DXTrace(__FILE__, (DWORD)__LINE__, hr, L"D3DX10CreateEffectFromFile", true);
- }
- m_Tech = m_FX->GetTechniqueByName("LightTech");
- mfxWVPVar = m_FX->GetVariableByName("gWVP")->AsMatrix();
- mfxWorldVar = m_FX->GetVariableByName("gWorld")->AsMatrix();
- mfxEyePosVar = m_FX->GetVariableByName("gEyePosW");
- mfxLightVar = m_FX->GetVariableByName("gLight");
- mfxLightType = m_FX->GetVariableByName("gLightType")->AsScalar();
- }
- void Waves::buildVertexLayouts()
- {
- // Create the vertex input layout.
- D3D10_INPUT_ELEMENT_DESC vertexDesc[] =
- {
- {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
- {"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0},
- {"DIFFUSE", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0},
- {"SPECULAR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 40, D3D10_INPUT_PER_VERTEX_DATA, 0}
- };
- // Create the input layout
- D3D10_PASS_DESC PassDesc;
- m_Tech->GetPassByIndex(0)->GetDesc(&PassDesc);
- HR(m_pLevel->GetDevice()->CreateInputLayout(vertexDesc, 4, PassDesc.pIAInputSignature,
- PassDesc.IAInputSignatureSize, &m_VertexLayout));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement