Advertisement
Guest User

Shadow Mapping with Directx 11

a guest
Nov 24th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.50 KB | None | 0 0
  1.  
  2. void Initialize(HWND hWindow);
  3. void CreateRenderTexture();
  4. void RenderToTexture();
  5. void Render();
  6. void Destroy();
  7.  
  8. int main()
  9. {
  10.     HWND hWindow;
  11.  
  12.     WNDCLASS windowClass;
  13.     ZeroMemory(&windowClass, sizeof(WNDCLASS));
  14.  
  15.     windowClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
  16.     windowClass.hCursor = LoadCursor(nullptr, IDC_ARROW);
  17.     windowClass.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
  18.     windowClass.hInstance = GetModuleHandle(0);
  19.     windowClass.lpfnWndProc = fnWindowProcessor;
  20.     windowClass.lpszClassName = "WINDOW";
  21.     windowClass.style = CS_HREDRAW | CS_VREDRAW;
  22.  
  23.     RegisterClass(&windowClass);
  24.  
  25.     hWindow = CreateWindow("WINDOW", "Application", WS_OVERLAPPEDWINDOW, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0, GetModuleHandle(0), 0);
  26.  
  27.     ShowWindow(hWindow, SW_SHOW);
  28.     UpdateWindow(hWindow);
  29.  
  30.     Initialize(hWindow);
  31.  
  32.     MSG msg = { 0 };
  33.  
  34.     while (msg.message != WM_QUIT)
  35.     {
  36.         if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  37.         {
  38.             TranslateMessage(&msg);
  39.             DispatchMessage(&msg);
  40.         }
  41.         else {
  42.             Render();
  43.         }
  44.     }
  45.  
  46.     Destroy();
  47.    
  48.     return 0;
  49. }
  50.  
  51. ID3D11Device* pDevice;
  52. ID3D11DeviceContext* pDeviceContext;
  53.  
  54. IDXGISwapChain* pSwapChain;
  55.  
  56. ID3D11RenderTargetView* pRenderTargetView;
  57. ID3D11RenderTargetView* pRTRenderTargetView;
  58.  
  59. ID3D11Buffer* pConstantBuffer;
  60. ID3D11Buffer* pShadowConstantBuffer;
  61.  
  62. ID3DBlob* pRTVertexShaderBlob;
  63. ID3DBlob* pRTPixelShaderBlob;
  64.  
  65. ID3DBlob* pShadowVertexShaderBlob;
  66. ID3DBlob* pShadowPixelShaderBlob;
  67.  
  68. ID3D11VertexShader* pVertexShader;
  69. ID3D11PixelShader* pPixelShader;
  70.  
  71. ID3D11VertexShader* pRTVertexShader;
  72. ID3D11PixelShader* pRTPixelShader;
  73.  
  74. ID3D11VertexShader* pShadowVertexShader;
  75. ID3D11PixelShader* pShadowPixelShader;
  76.  
  77. ID3DBlob* pVertexShaderBlob;
  78. ID3DBlob* pPixelShaderBlob;
  79.  
  80. ID3D11InputLayout* pInputLayout;
  81. ID3D11InputLayout* pRTInputLayout;
  82. ID3D11InputLayout* pShadowInputLayout;
  83.  
  84. ID3D11ShaderResourceView* pShaderResourceView;
  85. ID3D11ShaderResourceView* pRTShaderResourceView;
  86.  
  87. ID3D11SamplerState* pSamplerState;
  88. ID3D11SamplerState* pShadowSamplerState;
  89.  
  90. ID3D11DepthStencilView* pDepthStencilView;
  91. ID3D11DepthStencilView* pRTDepthStencilView;
  92.  
  93. ID3D11DepthStencilState* pDepthStencilState;
  94. ID3D11DepthStencilState* pDisabledDepthStencilState;
  95.  
  96. D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix;
  97. D3DXMATRIX lightViewMatrix, lightProjectionMatrix;
  98.  
  99. Cube* cube;
  100. Sprite* sprite;
  101. Plane* plane;
  102.  
  103. struct ConstantData
  104. {
  105.     D3DXMATRIX worldMatrix;
  106.     D3DXMATRIX viewMatrix;
  107.     D3DXMATRIX projectionMatrix;
  108. } constantData;
  109.  
  110. struct ShadowConstantData
  111. {
  112.     D3DXMATRIX worldMatrix;
  113.     D3DXMATRIX viewMatrix;
  114.     D3DXMATRIX projectionMatrix;
  115.     D3DXMATRIX lightViewMatrix;
  116.     D3DXMATRIX lightProjectionMatrix;
  117. } shadowConstantData;
  118.  
  119. void Initialize(HWND hWindow)
  120. {
  121.     DXGI_SWAP_CHAIN_DESC swapChainDesc;
  122.     ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  123.  
  124.     swapChainDesc.BufferCount = 1;
  125.     swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  126.     swapChainDesc.BufferDesc.Width = 800;
  127.     swapChainDesc.BufferDesc.Height = 600;
  128.     swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  129.     swapChainDesc.OutputWindow = hWindow;
  130.     swapChainDesc.SampleDesc.Count = 1;
  131.     swapChainDesc.SampleDesc.Quality = 0;
  132.     swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  133.     swapChainDesc.Windowed = true;
  134.  
  135.     D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr, 0, D3D11_SDK_VERSION, &swapChainDesc, &pSwapChain, &pDevice,nullptr, &pDeviceContext);
  136.  
  137.     D3D11_TEXTURE2D_DESC depthStencilDesc;
  138.     ZeroMemory(&depthStencilDesc, sizeof(D3D11_TEXTURE2D_DESC));
  139.  
  140.     depthStencilDesc.ArraySize = 1;
  141.     depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  142.     depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  143.     depthStencilDesc.Width = 800;
  144.     depthStencilDesc.Height = 600;
  145.     depthStencilDesc.MipLevels = 1;
  146.     depthStencilDesc.SampleDesc.Count = 1;
  147.     depthStencilDesc.SampleDesc.Quality = 0;
  148.     depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
  149.  
  150.     ID3D11Texture2D* pDepthStencilTexture;
  151.  
  152.     pDevice->CreateTexture2D(&depthStencilDesc, nullptr, &pDepthStencilTexture);
  153.  
  154.     pDevice->CreateDepthStencilView(pDepthStencilTexture, nullptr, &pDepthStencilView);
  155.  
  156.     ID3D11Texture2D* pBackBuffer;
  157.  
  158.     pSwapChain->GetBuffer(0, __uuidof(pBackBuffer), reinterpret_cast<void**>(&pBackBuffer));
  159.  
  160.     pDevice->CreateRenderTargetView(pBackBuffer, nullptr, &pRenderTargetView);
  161.  
  162.     D3D11_BUFFER_DESC constantBufferDesc;
  163.     ZeroMemory(&constantBufferDesc, sizeof(D3D11_BUFFER_DESC));
  164.  
  165.     constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  166.     constantBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  167.     constantBufferDesc.ByteWidth = sizeof(ConstantData);
  168.  
  169.     pDevice->CreateBuffer(&constantBufferDesc, nullptr, &pConstantBuffer);
  170.  
  171.     D3D11_BUFFER_DESC shadowConstantBufferDesc;
  172.     ZeroMemory(&shadowConstantBufferDesc, sizeof(D3D11_BUFFER_DESC));
  173.  
  174.     shadowConstantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  175.     shadowConstantBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  176.     shadowConstantBufferDesc.ByteWidth = sizeof(ShadowConstantData);
  177.  
  178.     pDevice->CreateBuffer(&shadowConstantBufferDesc, nullptr, &pShadowConstantBuffer);
  179.  
  180.     D3DX11CompileFromFile("shader.fx", nullptr, nullptr, "VS", "vs_5_0", 0, 0, nullptr, &pVertexShaderBlob, nullptr, nullptr);
  181.     D3DX11CompileFromFile("shader.fx", nullptr, nullptr, "PS", "ps_5_0", 0, 0, nullptr, &pPixelShaderBlob, nullptr, nullptr);
  182.  
  183.     pDevice->CreateVertexShader(pVertexShaderBlob->GetBufferPointer(), pVertexShaderBlob->GetBufferSize(), nullptr, &pVertexShader);
  184.     pDevice->CreatePixelShader(pPixelShaderBlob->GetBufferPointer(), pPixelShaderBlob->GetBufferSize(), nullptr, &pPixelShader);
  185.  
  186.     D3D11_INPUT_ELEMENT_DESC layout[] =
  187.     {
  188.         { "POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
  189.         { "TEXCOORD",0,DXGI_FORMAT_R32G32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0 }
  190.     };
  191.  
  192.     pDevice->CreateInputLayout(layout, 2, pVertexShaderBlob->GetBufferPointer(), pVertexShaderBlob->GetBufferSize(), &pInputLayout);
  193.  
  194.     D3DX11CreateShaderResourceViewFromFile(pDevice, "texture.png", nullptr, nullptr, &pShaderResourceView, nullptr);
  195.  
  196.     D3D11_SAMPLER_DESC samplerDesc;
  197.     ZeroMemory(&samplerDesc, sizeof(D3D11_SAMPLER_DESC));
  198.  
  199.     samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  200.     samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
  201.     samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  202.     samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  203.  
  204.     pDevice->CreateSamplerState(&samplerDesc, &pSamplerState);
  205.  
  206.     samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
  207.     samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
  208.     samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
  209.     samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  210.  
  211.     pDevice->CreateSamplerState(&samplerDesc, &pShadowSamplerState);
  212.  
  213.     D3DX11CompileFromFile("depthShader.txt", nullptr, nullptr, "VS", "vs_5_0", 0, 0, nullptr, &pRTVertexShaderBlob, nullptr, nullptr);
  214.     D3DX11CompileFromFile("depthShader.txt", nullptr, nullptr, "PS", "ps_5_0", 0, 0, nullptr, &pRTPixelShaderBlob, nullptr, nullptr);
  215.  
  216.     pDevice->CreateVertexShader(pRTVertexShaderBlob->GetBufferPointer(), pRTVertexShaderBlob->GetBufferSize(), nullptr, &pRTVertexShader);
  217.     pDevice->CreatePixelShader(pRTPixelShaderBlob->GetBufferPointer(), pRTPixelShaderBlob->GetBufferSize(), nullptr, &pRTPixelShader);
  218.  
  219.     D3D11_INPUT_ELEMENT_DESC RTLayout[] =
  220.     {
  221.         { "POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
  222.         { "TEXCOORD",0,DXGI_FORMAT_R32G32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0 }
  223.     };
  224.  
  225.     pDevice->CreateInputLayout(RTLayout, 2, pRTVertexShaderBlob->GetBufferPointer(), pRTVertexShaderBlob->GetBufferSize(), &pRTInputLayout);
  226.  
  227.     D3DX11CompileFromFile("shadowShader.txt", nullptr, nullptr, "VS", "vs_5_0", 0, 0, nullptr, &pShadowVertexShaderBlob, nullptr, nullptr);
  228.     D3DX11CompileFromFile("shadowShader.txt", nullptr, nullptr, "PS", "ps_5_0", 0, 0, nullptr, &pShadowPixelShaderBlob, nullptr, nullptr);
  229.  
  230.  
  231.     pDevice->CreateVertexShader(pShadowVertexShaderBlob->GetBufferPointer(), pShadowVertexShaderBlob->GetBufferSize(), nullptr, &pShadowVertexShader);
  232.     pDevice->CreatePixelShader(pShadowPixelShaderBlob->GetBufferPointer(), pShadowPixelShaderBlob->GetBufferSize(), nullptr, &pShadowPixelShader);
  233.  
  234.     D3D11_INPUT_ELEMENT_DESC shadowLayout[] =
  235.     {
  236.         { "POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
  237.         { "TEXCOORD",0,DXGI_FORMAT_R32G32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0 }
  238.     };
  239.  
  240.     pDevice->CreateInputLayout(shadowLayout, 2, pShadowVertexShaderBlob->GetBufferPointer(), pShadowVertexShaderBlob->GetBufferSize(), &pShadowInputLayout);
  241.  
  242.     D3D11_DEPTH_STENCIL_DESC depthStencilStateDesc;
  243.     ZeroMemory(&depthStencilStateDesc, sizeof(D3D11_DEPTH_STENCIL_DESC));
  244.  
  245.     depthStencilStateDesc.DepthEnable = true;
  246.  
  247.     pDevice->CreateDepthStencilState(&depthStencilStateDesc, &pDepthStencilState);
  248.  
  249.     D3D11_DEPTH_STENCIL_DESC disabledDepthStencilStateDesc;
  250.     ZeroMemory(&disabledDepthStencilStateDesc, sizeof(D3D11_DEPTH_STENCIL_DESC));
  251.  
  252.     disabledDepthStencilStateDesc.DepthEnable = false;
  253.  
  254.     pDevice->CreateDepthStencilState(&disabledDepthStencilStateDesc, &pDisabledDepthStencilState);
  255.  
  256.     cube = new Cube();
  257.     cube->Initialize(pDevice, pDeviceContext);
  258.  
  259.     sprite = new Sprite();
  260.     sprite->Initialize(pDevice, pDeviceContext);
  261.  
  262.     plane = new Plane();
  263.     plane->Initialize(pDevice, pDeviceContext);
  264.  
  265.     CreateRenderTexture();
  266. }
  267.  
  268. void Render()
  269. {
  270.     float fColor[4] = { 0.0f,0.0f,0.0f,1.0f };
  271.  
  272.     D3D11_VIEWPORT viewport;
  273.     ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
  274.  
  275.     viewport.TopLeftX = 0;
  276.     viewport.TopLeftY = 0;
  277.     viewport.Width = 800;
  278.     viewport.Height = 600;
  279.     viewport.MinDepth = 0.0f;
  280.     viewport.MaxDepth = 1.0f;
  281.  
  282.     pDeviceContext->RSSetViewports(1, &viewport);
  283.     pDeviceContext->IASetInputLayout(pInputLayout);
  284.  
  285.     pDeviceContext->OMSetDepthStencilState(pDepthStencilState, 1);
  286.  
  287.     pDeviceContext->OMSetRenderTargets(1, &pRenderTargetView, pDepthStencilView);
  288.     pDeviceContext->ClearRenderTargetView(pRenderTargetView, fColor);
  289.     pDeviceContext->ClearDepthStencilView(pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0.0f);
  290.  
  291.     pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  292.  
  293.     D3DXMatrixIdentity(&worldMatrix);
  294.     D3DXMatrixPerspectiveFovLH(&projectionMatrix, D3DXToRadian(45), static_cast<float>(800) / static_cast<float>(600), 1.0f, 100.0f);
  295.     D3DXMatrixLookAtLH(&viewMatrix, &D3DXVECTOR3(0.0f, 10.0f, -4.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  296.  
  297.     constantData.worldMatrix = worldMatrix;
  298.     constantData.viewMatrix = viewMatrix;
  299.     constantData.projectionMatrix = projectionMatrix;
  300.  
  301.     pDeviceContext->VSSetShader(pVertexShader, nullptr, 0);
  302.     pDeviceContext->PSSetShader(pPixelShader, nullptr, 0);
  303.  
  304.     pDeviceContext->UpdateSubresource(pConstantBuffer, 0, 0, &constantData, 0, 0);
  305.     pDeviceContext->VSSetConstantBuffers(0, 1, &pConstantBuffer);
  306.  
  307.     pDeviceContext->PSSetSamplers(0, 1, &pSamplerState);
  308.     pDeviceContext->PSSetShaderResources(0, 1, &pShaderResourceView);
  309.  
  310.     cube->Render();
  311.  
  312.     D3DXMatrixPerspectiveFovLH(&lightProjectionMatrix, D3DXToRadian(90), 1.0f, 1.0f, 100.0f);
  313.     D3DXMatrixLookAtLH(&lightViewMatrix, &D3DXVECTOR3(0.0f, 3.0f, -3.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  314.  
  315.     shadowConstantData.worldMatrix = worldMatrix;
  316.     shadowConstantData.viewMatrix = viewMatrix;
  317.     shadowConstantData.projectionMatrix = projectionMatrix;
  318.     shadowConstantData.lightViewMatrix = lightViewMatrix;
  319.     shadowConstantData.lightProjectionMatrix = lightProjectionMatrix;
  320.  
  321.     pDeviceContext->IASetInputLayout(pShadowInputLayout);
  322.  
  323.     pDeviceContext->VSSetShader(pShadowVertexShader, nullptr, 0);
  324.     pDeviceContext->PSSetShader(pShadowPixelShader, nullptr, 0);
  325.  
  326.     pDeviceContext->UpdateSubresource(pShadowConstantBuffer, 0, 0, &shadowConstantData, 0, 0);
  327.     pDeviceContext->VSSetConstantBuffers(0, 1, &pShadowConstantBuffer);
  328.  
  329.     pDeviceContext->PSSetSamplers(0, 1, &pShadowSamplerState);
  330.     pDeviceContext->PSSetShaderResources(0, 1, &pRTShaderResourceView);
  331.  
  332.     plane->Render();
  333.  
  334.     pDeviceContext->OMSetDepthStencilState(pDisabledDepthStencilState, 1);
  335.  
  336.     D3DXMatrixIdentity(&worldMatrix);
  337.  
  338.     D3DXMatrixLookAtLH(&viewMatrix, &D3DXVECTOR3(400.0f, 300.0f, -5.0f), &D3DXVECTOR3(400.0f, 300.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  339.     D3DXMatrixOrthoLH(&projectionMatrix, 800.0f, 600.0f, 1.0f, 10.0f);
  340.  
  341.     constantData.worldMatrix = worldMatrix;
  342.     constantData.viewMatrix = viewMatrix;
  343.     constantData.projectionMatrix = projectionMatrix;
  344.  
  345.     pDeviceContext->VSSetShader(pVertexShader, nullptr, 0);
  346.     pDeviceContext->PSSetShader(pPixelShader, nullptr, 0);
  347.  
  348.     pDeviceContext->UpdateSubresource(pConstantBuffer, 0, 0, &constantData, 0, 0);
  349.     pDeviceContext->VSSetConstantBuffers(0, 1, &pConstantBuffer);
  350.  
  351.     pDeviceContext->PSSetSamplers(0, 1, &pSamplerState);
  352.     pDeviceContext->PSSetShaderResources(0, 1, &pRTShaderResourceView);
  353.  
  354.     sprite->Render();
  355.  
  356.     pDeviceContext->OMSetDepthStencilState(pDepthStencilState, 1);
  357.     pDeviceContext->IASetInputLayout(pRTInputLayout);
  358.  
  359.     pDeviceContext->VSSetShader(pRTVertexShader, nullptr, 0);
  360.     pDeviceContext->PSSetShader(pRTPixelShader, nullptr, 0);
  361.  
  362.     RenderToTexture();
  363.  
  364.     pSwapChain->Present(0, 0);
  365. }
  366.  
  367. void CreateRenderTexture()
  368. {
  369.     D3D11_TEXTURE2D_DESC RTTextureDesc;
  370.     ZeroMemory(&RTTextureDesc, sizeof(D3D11_TEXTURE2D_DESC));
  371.  
  372.     RTTextureDesc.ArraySize = 1;
  373.     RTTextureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
  374.     RTTextureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
  375.     RTTextureDesc.Width = 1024;
  376.     RTTextureDesc.Height = 1024;
  377.     RTTextureDesc.SampleDesc.Count = 1;
  378.     RTTextureDesc.MipLevels = 1;
  379.     RTTextureDesc.Usage = D3D11_USAGE_DEFAULT;
  380.  
  381.     ID3D11Texture2D* pRTTexture;
  382.  
  383.     pDevice->CreateTexture2D(&RTTextureDesc, nullptr, &pRTTexture);
  384.  
  385.     D3D11_RENDER_TARGET_VIEW_DESC RTRenderTargetViewDesc;
  386.     ZeroMemory(&RTRenderTargetViewDesc, sizeof(D3D11_RENDER_TARGET_VIEW_DESC));
  387.  
  388.     RTRenderTargetViewDesc.Format = RTTextureDesc.Format;
  389.     RTRenderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
  390.     RTRenderTargetViewDesc.Texture2D.MipSlice = 0;
  391.  
  392.     pDevice->CreateRenderTargetView(pRTTexture, &RTRenderTargetViewDesc, &pRTRenderTargetView);
  393.  
  394.     D3D11_SHADER_RESOURCE_VIEW_DESC RTShaderResourceViewDesc;
  395.     ZeroMemory(&RTShaderResourceViewDesc, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
  396.  
  397.     RTShaderResourceViewDesc.Format = RTTextureDesc.Format;
  398.     RTShaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  399.     RTShaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
  400.     RTShaderResourceViewDesc.Texture2D.MipLevels = 1;
  401.  
  402.     pDevice->CreateShaderResourceView(pRTTexture, &RTShaderResourceViewDesc, &pRTShaderResourceView);
  403.  
  404.     D3D11_TEXTURE2D_DESC RTDepthStencilDesc;
  405.     ZeroMemory(&RTDepthStencilDesc, sizeof(D3D11_TEXTURE2D_DESC));
  406.  
  407.     RTDepthStencilDesc.ArraySize = 1;
  408.     RTDepthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  409.     RTDepthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  410.     RTDepthStencilDesc.Width = 1024;
  411.     RTDepthStencilDesc.Height = 1024;
  412.     RTDepthStencilDesc.MipLevels = 1;
  413.     RTDepthStencilDesc.SampleDesc.Count = 1;
  414.     RTDepthStencilDesc.SampleDesc.Quality = 0;
  415.     RTDepthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
  416.  
  417.     ID3D11Texture2D* pRTDepthStencilTexture;
  418.  
  419.     pDevice->CreateTexture2D(&RTDepthStencilDesc, nullptr, &pRTDepthStencilTexture);
  420.  
  421.     D3D11_DEPTH_STENCIL_VIEW_DESC RTDepthStencilViewDesc;
  422.     ZeroMemory(&RTDepthStencilViewDesc, sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC));
  423.  
  424.     RTDepthStencilViewDesc.Format = RTDepthStencilDesc.Format;
  425.     RTDepthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  426.     RTDepthStencilViewDesc.Texture2D.MipSlice = 0;
  427.  
  428.     pDevice->CreateDepthStencilView(pRTDepthStencilTexture, &RTDepthStencilViewDesc, &pRTDepthStencilView);
  429. }
  430.  
  431. void RenderToTexture()
  432. {
  433.     float fColor[4] = { 0.0f,0.0f,0.0f,1.0f };
  434.  
  435.     D3D11_VIEWPORT viewport;
  436.     ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
  437.  
  438.     viewport.TopLeftX = 0;
  439.     viewport.TopLeftY = 0;
  440.     viewport.Width = 1024;
  441.     viewport.Height = 1024;
  442.     viewport.MinDepth = 0.0f;
  443.     viewport.MaxDepth = 1.0f;
  444.  
  445.     pDeviceContext->RSSetViewports(1, &viewport);
  446.  
  447.     pDeviceContext->OMSetRenderTargets(1, &pRTRenderTargetView, pRTDepthStencilView);
  448.     pDeviceContext->ClearRenderTargetView(pRTRenderTargetView, fColor);
  449.     pDeviceContext->ClearDepthStencilView(pRTDepthStencilView, D3D11_CLEAR_DEPTH , 1.0f, 0.0f);
  450.  
  451.     D3DXMatrixIdentity(&worldMatrix);
  452.    
  453.     constantData.worldMatrix = worldMatrix;
  454.     constantData.viewMatrix = lightViewMatrix;
  455.     constantData.projectionMatrix = lightProjectionMatrix;
  456.  
  457.     pDeviceContext->UpdateSubresource(pConstantBuffer, 0, 0, &constantData, 0, 0);
  458.     pDeviceContext->VSSetConstantBuffers(0, 1, &pConstantBuffer);
  459.  
  460.     pDeviceContext->PSSetSamplers(0, 1, &pSamplerState);
  461.     pDeviceContext->PSSetShaderResources(0, 1, &pShaderResourceView);
  462.  
  463.     cube->Render();
  464.     plane->Render();
  465. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement