Ledger Nano X - The secure hardware wallet
SHARE
TWEET

netwars-govnoengine-01

a guest May 31st, 2015 276 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //--------------------------------------------------------------------------------------
  2. // File: Effect.cpp
  3. // Description: Class for effect.
  4. // Copyright (©) Dmitry N. Blinnikov (Jimnik). All rights reserved.
  5. //--------------------------------------------------------------------------------------
  6.  
  7.  
  8. #include "..\Engine\NetWars.h"  // NetWars Engine
  9. #include "D3Dcompiler.h"
  10. #include "D3DX10Math.h"
  11.  
  12. #include "Effect.h"     // Class of Effect
  13.  
  14.  
  15. CHAR sEffect[] = \
  16. "cbuffer cbPerObject : register(b1)"\
  17. "{      matrix mWVP;"\
  18. "       matrix mWVPLight;"\
  19. "       matrix worldMatrix;"\
  20. "};"\
  21. ""\
  22. "cbuffer cbPerFrame : register(b2)"\
  23. "{      float3  vLight : packoffset(c0);"\
  24. "};"\
  25. ""\
  26. "cbuffer cbPerObject : register(b3)"\
  27. "{      float   alpha;"\
  28. "       float3  padding;"\
  29. "};"\
  30. ""\
  31. "Texture2D              txDiffuse : register(t0);"\
  32. "SamplerState   samAnisot : register(s0);"\
  33. ""\
  34. "struct VS_INPUT"\
  35. "{      float4  vPos : POSITION;"\
  36. "       float3  vNor : NORMAL;"\
  37. "       float2  vTex : TEXCOORD0;"\
  38. "};"\
  39. ""\
  40. "struct VS_OUTPUT"\
  41. "{      float3  vNor : NORMAL;"\
  42. "       float2  vTex : TEXCOORD0;"\
  43. "       float4  vPos : SV_POSITION;"\
  44. "};"\
  45. ""\
  46. "struct PS_INPUT"\
  47. "{      float3  vNor : NORMAL;"\
  48. "       float2  vTex : TEXCOORD0;"\
  49. "};"\
  50. ""\
  51. "VS_OUTPUT VS(VS_INPUT Input)"\
  52. "{      VS_OUTPUT Output;"\
  53. "       Output.vPos = mul(Input.vPos, mWVP);"\
  54. "       Output.vNor = Input.vNor;"\
  55. "       Output.vTex = Input.vTex;"\
  56. "       return Output;"\
  57. "};"\
  58. ""\
  59. "float4 PS(PS_INPUT Input) : SV_TARGET"\
  60. "{      if (alpha<1.0f)"\
  61. "       {"\
  62. "               float4 vDiffuse = 0.5f*txDiffuse.Sample(samAnisot, Input.vTex)*(1.0f + saturate(dot(vLight, Input.vNor)));"\
  63. "               vDiffuse.a = alpha;"\
  64. "               return vDiffuse;"\
  65. "       }"\
  66. "       float4 vDiffuse = txDiffuse.Sample(samAnisot, Input.vTex);"\
  67. "       float fAlpha = vDiffuse.a;"\
  68. "       vDiffuse *= 0.5f*(1.0f + saturate(dot(vLight, Input.vNor)));"\
  69. "       vDiffuse.a = fAlpha;"\
  70. "       return vDiffuse;"\
  71. "}"\
  72. ""\
  73. "float4 AS(PS_INPUT Input) : SV_TARGET"\
  74. "{      float4 vDiffuse = txDiffuse.Sample(samAnisot, Input.vTex);"\
  75. "       float fAlpha = vDiffuse.a;"\
  76. "       vDiffuse *= 0.5f*(1.0f + saturate(dot(vLight, Input.vNor)));"\
  77. "       vDiffuse.a = fAlpha;"\
  78. "       return vDiffuse;"\
  79. "}"\
  80. ""\
  81. "float4 TS(PS_INPUT Input) : SV_TARGET"\
  82. "{      float4 vDiffuse = 0.5f*txDiffuse.Sample(samAnisot, Input.vTex)*(1.0f + saturate(dot(vLight, Input.vNor)));"\
  83. "       vDiffuse.a = alpha;"\
  84. "       return vDiffuse;"\
  85. "}";
  86. const UINT uEffect = sizeof(sEffect);
  87.  
  88. struct CB4VS_PER_OBJECT { D3DXMATRIX mWVP; };                                   // World-View-Project Matrix
  89. struct CB4PS_PER_FRAME  { D3DXVECTOR4 lightDirection; };                // Light direction
  90. struct CB4PS_PER_OBJECT { float alpha; D3DXVECTOR3 padding; };  // Transparency
  91.  
  92.  
  93. extern  CEngine g_pAppl;        // Base Engine class
  94. extern  CTex    g_pTex;         // Texture class
  95. extern  C3D             g_p3D;          // 3D class
  96. extern  CCam    g_pCam;         // Camera class
  97.  
  98.  
  99. //--------------------------------------------------------------------------------------
  100. // CONSTRUCTOR
  101. //--------------------------------------------------------------------------------------
  102. CEffect::CEffect()
  103. {
  104.         m_pVS = NULL;           // Vertex shader for rendering scene
  105.         m_pPS = NULL;           // Pixel shader for rendering scene
  106.         m_pAS = NULL;           // Pixel shader for texture with an Alpha channel (elements highlight)
  107.         m_pTS = NULL;           // Pixel shader for texture with partial Transparency (figure)
  108.  
  109.         m_pVL = NULL;           // Vertex layout for meshes
  110.  
  111.         m_pcbVO = NULL;         // Buffer with parameters of the matrices
  112.         m_pcbPF = NULL;         // Buffer with the parameters of the light
  113.         m_pcbPO = NULL;         // Buffer with the parameter of the object's color (transparency)
  114.  
  115.         m_pAnisot = NULL;       // Filter type - ANISOTROPIC
  116.  
  117.         m_iCBVSPerObjectBind = 1;       // In the shader, register for the matrices
  118.         m_iCBPSPerFrameBind = 2;        // In the shader, register for the light
  119.         m_iCBPSPerObjectBind = 3;       // In the shader, register for the object's color (transparency)
  120. }
  121.  
  122.  
  123. //--------------------------------------------------------------------------------------
  124. // INITIALIZATION
  125. //--------------------------------------------------------------------------------------
  126. void CEffect::init(ID3D11Device* pD3D)
  127. {
  128.         char    sVS[20] = "vs_5_0";     // Version of vertex shader
  129.         char    sPS[20] = "ps_5_0";     // Version of pixel shader
  130.  
  131.         ID3DBlob* pVSB = NULL;  // VertexShaderBuffer
  132.         ID3DBlob* pPSB = NULL;  // PixelShaderBuffer, texture with no Alpha channel
  133.         ID3DBlob* pVRB = NULL;  // VertexShaderBufferReflection
  134.         ID3DBlob* pVDB = NULL;  // VertexShaderBufferDepth
  135.         ID3DBlob* pPRB = NULL;  // PixelShaderBufferReflection
  136.         ID3DBlob* pASB = NULL;  // AlphaShaderBuffer, texture with no Alpha channel (elements highlight)
  137.         ID3DBlob* pTSB = NULL;  // TextureShaderBuffer, texture with partial Transparency (figure)
  138.  
  139.         DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
  140. #if defined( DEBUG ) || defined( _DEBUG )
  141.         // Set the D3D10_SHADER_DEBUG flag to embed debug information in the shaders.
  142.         // Setting this flag improves the shader debugging experience, but still allows the shaders to be optimized and to run exactly the way they will run in the release configuration of this program.
  143.         dwShaderFlags |= D3D10_SHADER_DEBUG;
  144. #endif
  145.  
  146.         HRESULT hr;
  147.         V(D3DCompile(sEffect, uEffect, "none", NULL, NULL, "VS", sVS, dwShaderFlags, 0, &pVSB, NULL));
  148.         V(D3DCompile(sEffect, uEffect, "none", NULL, NULL, "PS", sPS, dwShaderFlags, 0, &pPSB, NULL));
  149.         V(D3DCompile(sEffect, uEffect, "none", NULL, NULL, "AS", sPS, dwShaderFlags, 0, &pASB, NULL));
  150.         V(D3DCompile(sEffect, uEffect, "none", NULL, NULL, "TS", sPS, dwShaderFlags, 0, &pTSB, NULL));
  151.  
  152.         // Create shaders
  153.         V(pD3D->CreateVertexShader(pVSB->GetBufferPointer(), pVSB->GetBufferSize(), NULL, &m_pVS));
  154.         V(pD3D->CreatePixelShader(pPSB->GetBufferPointer(), pPSB->GetBufferSize(), NULL, &m_pPS));
  155.         V(pD3D->CreatePixelShader(pASB->GetBufferPointer(), pASB->GetBufferSize(), NULL, &m_pAS));
  156.         V(pD3D->CreatePixelShader(pTSB->GetBufferPointer(), pTSB->GetBufferSize(), NULL, &m_pTS));
  157.  
  158.         // Create vertex layout for meshes
  159.         const D3D11_INPUT_ELEMENT_DESC layout[] =
  160.         {       { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  161.                 { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  162.                 { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  163.         };
  164.         V(pD3D->CreateInputLayout(layout, ARRAYSIZE(layout), pVSB->GetBufferPointer(), pVSB->GetBufferSize(), &m_pVL));
  165.  
  166.         // Free shader buffers
  167.         SAFE_RELEASE(pTSB);    
  168.         SAFE_RELEASE(pASB);
  169.         SAFE_RELEASE(pVDB);
  170.         SAFE_RELEASE(pPRB);
  171.         SAFE_RELEASE(pVRB);
  172.         SAFE_RELEASE(pPSB);
  173.         SAFE_RELEASE(pVSB);
  174.  
  175.         // Configure the buffers constants passed to the shader
  176.         D3D11_BUFFER_DESC Desc;
  177.         Desc.Usage = D3D11_USAGE_DYNAMIC;
  178.         Desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  179.         Desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  180.         Desc.MiscFlags = 0;
  181.         Desc.ByteWidth = sizeof(CB4VS_PER_OBJECT);      V(pD3D->CreateBuffer(&Desc, NULL, &m_pcbVO));   // Each object for vertex shader buffer
  182.         Desc.ByteWidth = sizeof(CB4PS_PER_FRAME);       V(pD3D->CreateBuffer(&Desc, NULL, &m_pcbPF));   // Each frame for pixel shaderbuffer
  183.         Desc.ByteWidth = sizeof(CB4PS_PER_OBJECT);      V(pD3D->CreateBuffer(&Desc, NULL, &m_pcbPO));   // Each object for pixel shader buffer
  184.  
  185.         // Create an anisotropic filter
  186.         D3D11_SAMPLER_DESC SSDesc;
  187.         ZeroMemory(&SSDesc, sizeof(D3D11_SAMPLER_DESC));
  188.         SSDesc.Filter = D3D11_FILTER_ANISOTROPIC;
  189.         SSDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  190.         SSDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
  191.         SSDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  192.         SSDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; //D3D11_COMPARISON_ALWAYS;
  193.         SSDesc.MaxAnisotropy = 16;
  194.         SSDesc.MinLOD = 0;
  195.         SSDesc.MaxLOD = D3D11_FLOAT32_MAX;
  196.         if (pD3D->GetFeatureLevel() < D3D_FEATURE_LEVEL_9_3)
  197.         {
  198.                 SSDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  199.                 SSDesc.MaxAnisotropy = 0;
  200.         }
  201.         V(pD3D->CreateSamplerState(&SSDesc, &m_pAnisot));
  202. }
  203.  
  204.  
  205. //--------------------------------------------------------------------------------------
  206. // RESIZE THE OUTPUT SCENE
  207. //--------------------------------------------------------------------------------------
  208. /*HRESULT       CEffect::resize(ID3D11Device* pD3D, float fW, float fH)
  209. {
  210.         //m_fW = fW;    // Width
  211.         //m_fH = fH;    // Height
  212.  
  213.         return S_OK;
  214. }*/
  215.  
  216.  
  217. //--------------------------------------------------------------------------------------
  218. // RENDERING SCENE
  219. //--------------------------------------------------------------------------------------
  220. void CEffect::render(ID3D11DeviceContext* pContext, float fAlpha)       // D3DXMATRIX mVP,
  221. {
  222.         HRESULT hr;
  223.         D3DXMATRIX      mVP;    // Matrix View Projection
  224.         g_pCam.getMatrixViewProj(&mVP); // Matrix View Projection
  225.         D3DXMATRIX      mWVP;   // Matrix World View Projection for rendering object
  226.         D3DXMATRIX      mWorld; // Matrix World
  227.  
  228.         // Remember current render state
  229.         ID3D11SamplerState*     pSamplers = NULL;       pContext->PSGetSamplers(0, 1, &pSamplers);
  230.         ID3D11InputLayout* pInputLayout = NULL; pContext->IAGetInputLayout(&pInputLayout);
  231.         ID3D11VertexShader* pVS = NULL;                 pContext->VSGetShader(&pVS, NULL, 0);
  232.         ID3D11PixelShader* pPS = NULL;                  pContext->PSGetShader(&pPS, NULL, 0);
  233.         ID3D11GeometryShader* pGS = NULL;               pContext->GSGetShader(&pGS, NULL, 0);
  234.         ID3D11HullShader* pHS = NULL;                   pContext->HSGetShader(&pHS, NULL, 0);
  235.         ID3D11DomainShader* pDS = NULL;                 pContext->DSGetShader(&pDS, NULL, 0);
  236.  
  237.         //pContext->ClearDepthStencilView(DXUTGetD3D11DepthStencilView(), D3D11_CLEAR_DEPTH, 1.0f, 0);
  238.         g_pAppl.clearDepthStencilView();
  239.  
  240.         // Set shaders and rendering state
  241.         pContext->PSSetSamplers(0, 1, &m_pAnisot);      // Anisotropic filter
  242.         pContext->IASetInputLayout(m_pVL);              // Layout
  243.         pContext->VSSetShader(m_pVS, NULL, 0);  // Vertex shader
  244.         pContext->PSSetShader(m_pPS, NULL, 0);  // Pixel shader for rendering texture with no alpha channel
  245.  
  246.         // PS The light direction (set each frame on the whole scene)
  247.         D3D11_MAPPED_SUBRESOURCE MappedResource;
  248.         V(pContext->Map(m_pcbPF, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource));
  249.         CB4PS_PER_FRAME* pPSPerFrame = (CB4PS_PER_FRAME*)MappedResource.pData;
  250.         pPSPerFrame->lightDirection = D3DXVECTOR4(0.0f, 1.0f, 0.0f, 0.1f);      // float fAmbient = 0.1f;
  251.         pContext->Unmap(m_pcbPF, 0);
  252.         pContext->PSSetConstantBuffers(m_iCBPSPerFrameBind, 1, &m_pcbPF);
  253.  
  254.         // Set the opacity (default opaque)
  255.         V(pContext->Map(m_pcbPO, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource));
  256.         CB4PS_PER_OBJECT* pPSPerObject = (CB4PS_PER_OBJECT*)MappedResource.pData;
  257.         pPSPerObject->alpha = 1.0f;     // Transparency
  258.         pContext->Unmap(m_pcbPO, 0);
  259.         pContext->PSSetConstantBuffers(m_iCBPSPerObjectBind, 1, &m_pcbPO);
  260.  
  261.         // Board
  262.         D3DXMATRIX      mWorldBoard;    // World matrix board
  263.         D3DXMatrixTranslation(&mWorldBoard, 0.0f, 0.0f, 0.0f);  // World matrix board
  264.         D3DXMatrixMultiply(&mWVP, &mWorldBoard, &mVP);
  265.         V(pContext->Map(m_pcbVO, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource));
  266.         CB4VS_PER_OBJECT* pVSPerObject = (CB4VS_PER_OBJECT*)MappedResource.pData;
  267.         D3DXMatrixTranspose(&pVSPerObject->mWVP, &mWVP);
  268.         pContext->Unmap(m_pcbVO, 0);
  269.         pContext->VSSetConstantBuffers(m_iCBVSPerObjectBind, 1, &m_pcbVO);
  270.         //pContext->PSSetShaderResources(0, 1, &m_pTBoard);     // Board texture
  271.         //RenderMesh(pd3dContext, &m_MeshBoard);        // Board with markings / no markup
  272.  
  273.         // Render static mesh
  274.         g_pTex.render(0);       // ID_TEXTURE_BOARD
  275.         g_p3D.renderMesh(0, 0.0f, 0.0f, 0.0f);
  276.  
  277.         mWVP = mWorldBoard * mVP;
  278.         V(pContext->Map(m_pcbVO, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource));
  279.         pVSPerObject = (CB4VS_PER_OBJECT*)MappedResource.pData;
  280.         D3DXMatrixTranspose(&pVSPerObject->mWVP, &mWVP);
  281.         pContext->Unmap(m_pcbVO, 0);
  282.         pContext->VSSetConstantBuffers(m_iCBVSPerObjectBind, 1, &m_pcbVO);
  283.  
  284.         // Render motion mesh
  285.         g_pTex.render(1);       // ID_TEXTURE_KNIGHT
  286.         g_p3D.renderMesh(1, 5.0f * cos(fAlpha), 0.0f, 5.0f * sin(fAlpha));
  287.  
  288.         //pd3dContext->ClearDepthStencilView(DXUTGetD3D11DepthStencilView(), D3D11_CLEAR_DEPTH, 1.0f, 0);
  289.         g_pAppl.clearDepthStencilView();
  290.  
  291.         // Set remember render state
  292.         pContext->PSSetSamplers(0, 1, &pSamplers);      //
  293.         pContext->IASetInputLayout(pInputLayout);       //
  294.         pContext->VSSetShader(pVS, NULL, 0);            //
  295.         pContext->PSSetShader(pPS, NULL, 0);            //
  296.         pContext->GSSetShader(pGS, NULL, 0);            //
  297.         pContext->HSSetShader(pHS, NULL, 0);            //
  298.         pContext->DSSetShader(pDS, NULL, 0);            //
  299.  
  300.         SAFE_RELEASE(pSamplers);
  301.         SAFE_RELEASE(pInputLayout);
  302.         SAFE_RELEASE(pVS);
  303.         SAFE_RELEASE(pPS);
  304.         SAFE_RELEASE(pGS);
  305.         SAFE_RELEASE(pHS);
  306.         SAFE_RELEASE(pDS);
  307. }
  308.  
  309.  
  310. //--------------------------------------------------------------------------------------
  311. // DEINITIALIZATION
  312. //--------------------------------------------------------------------------------------
  313. void CEffect::free()
  314. {
  315.         SAFE_RELEASE(m_pVS);    // Vertex shader for rendering scene
  316.         SAFE_RELEASE(m_pPS);    // Pixel shader for rendering scene
  317.         SAFE_RELEASE(m_pAS);    // Pixel shader for texture with an Alpha channel (elements highlight)
  318.         SAFE_RELEASE(m_pTS);    // Pixel shader for texture with partial Transparency (figure)
  319.  
  320.         SAFE_RELEASE(m_pVL);    // Vertex layout for meshes
  321.  
  322.         SAFE_RELEASE(m_pcbVO);  // Buffer with parameters of the matrices
  323.         SAFE_RELEASE(m_pcbPF);  // Buffer with the parameters of the light
  324.         SAFE_RELEASE(m_pcbPO);  // Buffer with the parameter of the object's color (transparency)
  325.  
  326.         SAFE_RELEASE(m_pAnisot);// Filter Type - ANISOTROPIC
  327. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top