Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.82 KB | None | 0 0
  1. #include "Waves.h"
  2. #include "vertex.h"
  3. #include <algorithm>
  4. #include <vector>
  5. #include "StdAfx.h"
  6. #include "Level.h"
  7.  
  8. Waves::Waves(Level* pLevel)
  9. : m_NumRows(0), m_NumCols(0), m_NumVertices(0), m_NumFaces(0),
  10.   m_K1(0.0f), m_K2(0.0f), m_K3(0.0f), m_TimeStep(0.0f), m_SpatialStep(0.0f),
  11.   m_PrevSolution(0), m_CurrSolution(0), m_Normals(0), m_VB(0), m_IB(0),mLightType(0),
  12.   m_pLevel(pLevel)
  13. {
  14.     D3DXMatrixIdentity(&mWavesWorld);
  15.     D3DXMatrixIdentity(&mView);
  16.     D3DXMatrixIdentity(&mProj);
  17.     D3DXMatrixIdentity(&mWVP);
  18. }
  19. Waves::~Waves(void)
  20. {
  21.     delete[] m_PrevSolution;
  22.     delete[] m_CurrSolution;
  23.     delete[] m_Normals;
  24.  
  25.     SafeRelease(m_VB);
  26.     SafeRelease(m_IB);
  27.     SafeDelete(m_FX);
  28. }
  29. void Waves::init(DWORD m, DWORD n, float dx, float dt, float speed, float damping,D3DXVECTOR3 pos)
  30. {
  31.     m_NumRows = m;
  32.     m_NumCols = n;
  33.  
  34.     m_NumVertices = m*n;
  35.     m_NumFaces = (m-1) * (n-1) *2;
  36.  
  37.     m_TimeStep = dt;
  38.     m_SpatialStep = dx;
  39.  
  40.     float d = damping * dt + 2.0f;
  41.     float e = (speed * speed) * (dt * dt) / (dx * dx);
  42.     m_K1 = (damping*dt-2.0f) / d;
  43.     m_K2 = (4.0f - 8.0f*e) / d;
  44.     m_K3 = (2.0f*e) / d;
  45.  
  46.     m_PrevSolution = new D3DXVECTOR3[m * n];
  47.     m_CurrSolution = new D3DXVECTOR3[m * n];
  48.     m_Normals = new D3DXVECTOR3[m * n];
  49.  
  50.     float halfWidth = (n-1) * dx * 0.5f;
  51.     float halfDepth = (m-1) * dx * 0.5f;
  52.  
  53.     // grid vertices genereren in het systeem geh
  54.     for(DWORD i = 0; i < m; ++i)
  55.     {
  56.         float z = halfDepth - i*dx;
  57.  
  58.         for(DWORD j = 0; j < n; ++j)
  59.         {
  60.             float x = -halfWidth + j * dx;
  61.  
  62.             m_PrevSolution[i * n + j] = D3DXVECTOR3(pos.x + x, pos.y,pos.z + z);
  63.             m_CurrSolution[i * n + j] = D3DXVECTOR3(pos.x + x, pos.y,pos.z+ z);
  64.             m_Normals[i * n + j] = D3DXVECTOR3(0.0f, 1.0f,0.0f);
  65.         }
  66.     }  
  67.  
  68.     D3D10_BUFFER_DESC v_bd;
  69.     v_bd.Usage = D3D10_USAGE_DYNAMIC;
  70.     v_bd.ByteWidth = sizeof(Vertex) * m_NumVertices;
  71.     v_bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
  72.     v_bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
  73.     v_bd.MiscFlags = 0;
  74.  
  75.     HR(m_pLevel->GetDevice()->CreateBuffer(&v_bd,0,&m_VB));
  76.  
  77.     //creër de index buffer, de index buffer is vast, dus we moeten deze maar een keer creëren en plaatsen
  78.  
  79.     vector<DWORD> indices(m_NumFaces * 3);
  80.  
  81.     int k = 0;
  82.  
  83.     for(DWORD i = 0; i < m-1; ++i)
  84.     {
  85.         for(DWORD j = 0;j<n-1; ++j)
  86.         {
  87.             indices[k] = i * n + j;
  88.             indices[k+1] = i * n + j + 1;
  89.             indices[k+2] = (i + 1)* n + j;
  90.  
  91.             indices[k+3] = (i + 1) * n + j;
  92.             indices[k+4] = i * n + j + 1;
  93.             indices[k+5] = (i + 1) * n + j + 1;
  94.  
  95.             k+=6; //volgende quad
  96.         }
  97.     }
  98.  
  99.    
  100.     mLights[0].dir      = D3DXVECTOR3(0.57735f, -0.57735f, 0.57735f);
  101.     mLights[0].ambient  = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
  102.     mLights[0].diffuse  = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  103.     mLights[0].specular = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  104.  
  105.     buildFX();
  106.     buildVertexLayouts();
  107.  
  108.     D3D10_BUFFER_DESC ibd;
  109.     ibd.Usage = D3D10_USAGE_IMMUTABLE;
  110.     ibd.ByteWidth = sizeof(DWORD) * m_NumFaces * 3;
  111.     ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
  112.     ibd.CPUAccessFlags = 0;
  113.     ibd.MiscFlags = 0;
  114.     D3D10_SUBRESOURCE_DATA iInitData;
  115.     iInitData.pSysMem = &indices[0];
  116.  
  117.     HR(m_pLevel->GetDevice()->CreateBuffer(&ibd,&iInitData,&m_IB));
  118.  
  119.    
  120. }
  121. void Waves::update(float dt)
  122. {
  123.     static float t = 0;
  124.  
  125.     //tijd verhogen
  126.  
  127.     t += dt;
  128.  
  129.     // simulatie alleen updaten op bepaald tijdstip
  130.  
  131.     if( t >= m_TimeStep)
  132.     {
  133.         //Only update interior points; we use zero boundary conditions.
  134.         for(DWORD i = 1; i < m_NumRows-1; ++i)
  135.         {
  136.             for(DWORD j = 1; j < m_NumCols-1;++j)
  137.             {
  138.                 // After this update we will be discarding the old previous
  139.                 // buffer, so overwrite that buffer with the new update.
  140.                 // Note how we can do this inplace (read/write to same element)
  141.                 // because we won't need prev_ij again and the assignment happens last.
  142.  
  143.                 // Note j indexes x and i indexes z: h(x_j, z_i, t_k)
  144.                 // Moreover, out +z axis goes "down"; this is just to
  145.                 // keep consistent with our row indices going down.
  146.                 m_PrevSolution[i*m_NumCols+j].y =
  147.                 m_K1*m_PrevSolution[i*m_NumCols+j].y +
  148.                 m_K2*m_CurrSolution[i*m_NumCols+j].y +
  149.                 m_K3*(m_CurrSolution[(i+1)*m_NumCols+j].y +
  150.                          m_CurrSolution[(i-1)*m_NumCols+j].y +
  151.                          m_CurrSolution[i*m_NumCols+j+1].y +
  152.                          m_CurrSolution[i*m_NumCols+j-1].y);
  153.             }
  154.         }
  155.  
  156.         // We just overwrite the previous buffer with the new data, so
  157.         // this data needs to become the current solution and the old
  158.         // current solution becomes the new previous solution.
  159.         swap(m_PrevSolution, m_CurrSolution);
  160.  
  161.         t = 0.0f; // reset tijd
  162.  
  163.         // Compute normals using finite difference scheme.
  164.         for(DWORD i = 1; i < m_NumRows-1; ++i)
  165.         {
  166.             for(DWORD j = 1; j < m_NumCols-1; ++j)
  167.             {
  168.                 float l = m_CurrSolution[i*m_NumCols+j-1].y;
  169.                 float r = m_CurrSolution[i*m_NumCols+j+1].y;
  170.                 float t = m_CurrSolution[(i-1)*m_NumCols+j-1].y;
  171.                 float b = m_CurrSolution[(i+1)*m_NumCols+j+1].y;
  172.                 m_Normals[i*m_NumCols+j].x = -r+l;
  173.                 m_Normals[i*m_NumCols+j].y = 2*m_SpatialStep;
  174.                 m_Normals[i*m_NumCols+j].z = b-t;
  175.  
  176.                 D3DXVec3Normalize(&m_Normals[i*m_NumCols+j], &m_Normals[i*m_NumCols+j]);
  177.             }
  178.         }
  179.  
  180.         float xTrans = 20000;
  181.         float yTrans = 20000;
  182.         float zTrans = 20000;
  183.         D3DXMatrixTranslation(&mWavesWorld, xTrans, yTrans, zTrans);
  184.  
  185.  
  186.  
  187.         // Update the vertex buffer with the new solution.
  188.         Vertex* v = 0;
  189.         HR(m_VB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&v ));
  190.  
  191.         for(DWORD i = 0; i < m_NumVertices; ++i)
  192.         {
  193.             v[i].pos     = m_CurrSolution[i];
  194.             v[i].diffuse = D3DXCOLOR(0.0f, 0.0f, v[i].pos.z / 10, 1.0f);
  195.             v[i].spec    = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  196.             v[i].normal  = m_Normals[i];
  197.             v;
  198.         }
  199.  
  200.         m_VB->Unmap();
  201.     }
  202. }
  203. void Waves::disturb()
  204. {
  205.     // Don't disturb boundaries.
  206.     //assert(i > 1 && i < mNumRows-2);
  207.     //assert(j > 1 && j < mNumCols-2);
  208.  
  209.     DWORD i = 1 + rand() % 119;
  210.     DWORD j = 1 + rand() % 119;
  211.     float r = RandF(0.1f, 0.2f);
  212.     //float r = RandF(1.0f, 2.0f);
  213.     float halfMag = 0.5f* r;
  214.  
  215.     m_CurrSolution[i*m_NumCols+j].y     += r;
  216.     m_CurrSolution[i*m_NumCols+j+1].y   += halfMag;
  217.     m_CurrSolution[i*m_NumCols+j-1].y   += halfMag;
  218.     m_CurrSolution[(i+1)*m_NumCols+j].y += halfMag;
  219.     m_CurrSolution[(i-1)*m_NumCols+j].y += halfMag;
  220.  
  221. }
  222. void Waves::draw(GraphCamera *pGraphCamera)
  223. {
  224.     m_pGraphCamera = pGraphCamera;
  225.  
  226.     UINT stride = sizeof(Vertex);
  227.     UINT offset = 0;
  228.     m_pLevel->GetDevice()->IASetVertexBuffers(0,1,&m_VB, &stride, &offset);
  229.    
  230.     m_pLevel->GetDevice()->IASetIndexBuffer(m_IB, DXGI_FORMAT_R32_UINT, 0);
  231.     m_pLevel->GetDevice()->DrawIndexed(m_NumFaces*3, 0, 0);
  232.  
  233.     m_pLevel->GetDevice()->IASetInputLayout(m_VertexLayout);
  234.     m_pLevel->GetDevice()->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  235.  
  236.     D3D10_TECHNIQUE_DESC techDesc;
  237.     m_Tech->GetDesc( &techDesc );
  238.  
  239.     D3DXVECTOR3 temp = m_pGraphCamera->GetPos();
  240.  
  241.     mfxEyePosVar->SetRawValue(&temp, 0, sizeof(D3DXVECTOR3));
  242.     mfxLightVar->SetRawValue(&mLights[mLightType], 0, sizeof(Light));
  243.     mfxLightType->SetInt(mLightType);
  244.  
  245.  
  246.     for(UINT i = 0; i < techDesc.Passes; ++i)
  247.     {
  248.         ID3D10EffectPass* pass = m_Tech->GetPassByIndex(i);
  249.  
  250.         mWVP = mWavesWorld*mView*mProj;
  251.         mfxWVPVar->SetMatrix((float*)&mWVP);
  252.         mfxWorldVar->SetMatrix((float*)&mWavesWorld);
  253.         pass->Apply(0);
  254.     }
  255.  
  256. }
  257. void Waves::buildFX()
  258. {
  259.     DWORD shaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
  260. #if defined( DEBUG ) || defined( _DEBUG )
  261.     shaderFlags |= D3D10_SHADER_DEBUG;
  262.     shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION;
  263. #endif
  264.  
  265.     ID3D10Blob* compilationErrors = 0;
  266.     HRESULT hr = 0;
  267.     hr = D3DX10CreateEffectFromFile(_T("./Effect/Waves.fx"), 0, 0,
  268.         "fx_4_0", shaderFlags, 0, m_pLevel->GetDevice(), 0, 0, &m_FX, &compilationErrors, 0);
  269.     if(FAILED(hr))
  270.     {
  271.         if( compilationErrors )
  272.         {
  273.             MessageBoxA(0, (char*)compilationErrors->GetBufferPointer(), 0, 0);
  274.             SafeRelease(compilationErrors);
  275.         }
  276.         DXTrace(__FILE__, (DWORD)__LINE__, hr, L"D3DX10CreateEffectFromFile", true);
  277.     }
  278.  
  279.     m_Tech = m_FX->GetTechniqueByName("LightTech");
  280.    
  281.  
  282.     mfxWVPVar    = m_FX->GetVariableByName("gWVP")->AsMatrix();
  283.     mfxWorldVar  = m_FX->GetVariableByName("gWorld")->AsMatrix();
  284.     mfxEyePosVar = m_FX->GetVariableByName("gEyePosW");
  285.     mfxLightVar  = m_FX->GetVariableByName("gLight");
  286.     mfxLightType = m_FX->GetVariableByName("gLightType")->AsScalar();
  287. }
  288. void Waves::buildVertexLayouts()
  289. {
  290.     // Create the vertex input layout.
  291.     D3D10_INPUT_ELEMENT_DESC vertexDesc[] =
  292.     {
  293.         {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D10_INPUT_PER_VERTEX_DATA, 0},
  294.         {"NORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0},
  295.         {"DIFFUSE",  0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0},
  296.         {"SPECULAR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 40, D3D10_INPUT_PER_VERTEX_DATA, 0}
  297.     };
  298.  
  299.     // Create the input layout
  300.     D3D10_PASS_DESC PassDesc;
  301.     m_Tech->GetPassByIndex(0)->GetDesc(&PassDesc);
  302.     HR(m_pLevel->GetDevice()->CreateInputLayout(vertexDesc, 4, PassDesc.pIAInputSignature,
  303.         PassDesc.IAInputSignatureSize, &m_VertexLayout));
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement