Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Initialize(HWND hWindow);
- void CreateRenderTexture();
- void RenderToTexture();
- void Render();
- void Destroy();
- int main()
- {
- HWND hWindow;
- WNDCLASS windowClass;
- ZeroMemory(&windowClass, sizeof(WNDCLASS));
- windowClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
- windowClass.hCursor = LoadCursor(nullptr, IDC_ARROW);
- windowClass.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
- windowClass.hInstance = GetModuleHandle(0);
- windowClass.lpfnWndProc = fnWindowProcessor;
- windowClass.lpszClassName = "WINDOW";
- windowClass.style = CS_HREDRAW | CS_VREDRAW;
- RegisterClass(&windowClass);
- hWindow = CreateWindow("WINDOW", "Application", WS_OVERLAPPEDWINDOW, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0, GetModuleHandle(0), 0);
- ShowWindow(hWindow, SW_SHOW);
- UpdateWindow(hWindow);
- Initialize(hWindow);
- MSG msg = { 0 };
- while (msg.message != WM_QUIT)
- {
- if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else {
- Render();
- }
- }
- Destroy();
- return 0;
- }
- ID3D11Device* pDevice;
- ID3D11DeviceContext* pDeviceContext;
- IDXGISwapChain* pSwapChain;
- ID3D11RenderTargetView* pRenderTargetView;
- ID3D11RenderTargetView* pRTRenderTargetView;
- ID3D11Buffer* pConstantBuffer;
- ID3D11Buffer* pShadowConstantBuffer;
- ID3DBlob* pRTVertexShaderBlob;
- ID3DBlob* pRTPixelShaderBlob;
- ID3DBlob* pShadowVertexShaderBlob;
- ID3DBlob* pShadowPixelShaderBlob;
- ID3D11VertexShader* pVertexShader;
- ID3D11PixelShader* pPixelShader;
- ID3D11VertexShader* pRTVertexShader;
- ID3D11PixelShader* pRTPixelShader;
- ID3D11VertexShader* pShadowVertexShader;
- ID3D11PixelShader* pShadowPixelShader;
- ID3DBlob* pVertexShaderBlob;
- ID3DBlob* pPixelShaderBlob;
- ID3D11InputLayout* pInputLayout;
- ID3D11InputLayout* pRTInputLayout;
- ID3D11InputLayout* pShadowInputLayout;
- ID3D11ShaderResourceView* pShaderResourceView;
- ID3D11ShaderResourceView* pRTShaderResourceView;
- ID3D11SamplerState* pSamplerState;
- ID3D11SamplerState* pShadowSamplerState;
- ID3D11DepthStencilView* pDepthStencilView;
- ID3D11DepthStencilView* pRTDepthStencilView;
- ID3D11DepthStencilState* pDepthStencilState;
- ID3D11DepthStencilState* pDisabledDepthStencilState;
- D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix;
- D3DXMATRIX lightViewMatrix, lightProjectionMatrix;
- Cube* cube;
- Sprite* sprite;
- Plane* plane;
- struct ConstantData
- {
- D3DXMATRIX worldMatrix;
- D3DXMATRIX viewMatrix;
- D3DXMATRIX projectionMatrix;
- } constantData;
- struct ShadowConstantData
- {
- D3DXMATRIX worldMatrix;
- D3DXMATRIX viewMatrix;
- D3DXMATRIX projectionMatrix;
- D3DXMATRIX lightViewMatrix;
- D3DXMATRIX lightProjectionMatrix;
- } shadowConstantData;
- void Initialize(HWND hWindow)
- {
- DXGI_SWAP_CHAIN_DESC swapChainDesc;
- ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
- swapChainDesc.BufferCount = 1;
- swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- swapChainDesc.BufferDesc.Width = 800;
- swapChainDesc.BufferDesc.Height = 600;
- swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- swapChainDesc.OutputWindow = hWindow;
- swapChainDesc.SampleDesc.Count = 1;
- swapChainDesc.SampleDesc.Quality = 0;
- swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
- swapChainDesc.Windowed = true;
- D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr, 0, D3D11_SDK_VERSION, &swapChainDesc, &pSwapChain, &pDevice,nullptr, &pDeviceContext);
- D3D11_TEXTURE2D_DESC depthStencilDesc;
- ZeroMemory(&depthStencilDesc, sizeof(D3D11_TEXTURE2D_DESC));
- depthStencilDesc.ArraySize = 1;
- depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
- depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
- depthStencilDesc.Width = 800;
- depthStencilDesc.Height = 600;
- depthStencilDesc.MipLevels = 1;
- depthStencilDesc.SampleDesc.Count = 1;
- depthStencilDesc.SampleDesc.Quality = 0;
- depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
- ID3D11Texture2D* pDepthStencilTexture;
- pDevice->CreateTexture2D(&depthStencilDesc, nullptr, &pDepthStencilTexture);
- pDevice->CreateDepthStencilView(pDepthStencilTexture, nullptr, &pDepthStencilView);
- ID3D11Texture2D* pBackBuffer;
- pSwapChain->GetBuffer(0, __uuidof(pBackBuffer), reinterpret_cast<void**>(&pBackBuffer));
- pDevice->CreateRenderTargetView(pBackBuffer, nullptr, &pRenderTargetView);
- D3D11_BUFFER_DESC constantBufferDesc;
- ZeroMemory(&constantBufferDesc, sizeof(D3D11_BUFFER_DESC));
- constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
- constantBufferDesc.Usage = D3D11_USAGE_DEFAULT;
- constantBufferDesc.ByteWidth = sizeof(ConstantData);
- pDevice->CreateBuffer(&constantBufferDesc, nullptr, &pConstantBuffer);
- D3D11_BUFFER_DESC shadowConstantBufferDesc;
- ZeroMemory(&shadowConstantBufferDesc, sizeof(D3D11_BUFFER_DESC));
- shadowConstantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
- shadowConstantBufferDesc.Usage = D3D11_USAGE_DEFAULT;
- shadowConstantBufferDesc.ByteWidth = sizeof(ShadowConstantData);
- pDevice->CreateBuffer(&shadowConstantBufferDesc, nullptr, &pShadowConstantBuffer);
- D3DX11CompileFromFile("shader.fx", nullptr, nullptr, "VS", "vs_5_0", 0, 0, nullptr, &pVertexShaderBlob, nullptr, nullptr);
- D3DX11CompileFromFile("shader.fx", nullptr, nullptr, "PS", "ps_5_0", 0, 0, nullptr, &pPixelShaderBlob, nullptr, nullptr);
- pDevice->CreateVertexShader(pVertexShaderBlob->GetBufferPointer(), pVertexShaderBlob->GetBufferSize(), nullptr, &pVertexShader);
- pDevice->CreatePixelShader(pPixelShaderBlob->GetBufferPointer(), pPixelShaderBlob->GetBufferSize(), nullptr, &pPixelShader);
- D3D11_INPUT_ELEMENT_DESC layout[] =
- {
- { "POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
- { "TEXCOORD",0,DXGI_FORMAT_R32G32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0 }
- };
- pDevice->CreateInputLayout(layout, 2, pVertexShaderBlob->GetBufferPointer(), pVertexShaderBlob->GetBufferSize(), &pInputLayout);
- D3DX11CreateShaderResourceViewFromFile(pDevice, "texture.png", nullptr, nullptr, &pShaderResourceView, nullptr);
- D3D11_SAMPLER_DESC samplerDesc;
- ZeroMemory(&samplerDesc, sizeof(D3D11_SAMPLER_DESC));
- samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
- samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
- samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
- samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
- pDevice->CreateSamplerState(&samplerDesc, &pSamplerState);
- samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
- samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
- samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
- samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
- pDevice->CreateSamplerState(&samplerDesc, &pShadowSamplerState);
- D3DX11CompileFromFile("depthShader.txt", nullptr, nullptr, "VS", "vs_5_0", 0, 0, nullptr, &pRTVertexShaderBlob, nullptr, nullptr);
- D3DX11CompileFromFile("depthShader.txt", nullptr, nullptr, "PS", "ps_5_0", 0, 0, nullptr, &pRTPixelShaderBlob, nullptr, nullptr);
- pDevice->CreateVertexShader(pRTVertexShaderBlob->GetBufferPointer(), pRTVertexShaderBlob->GetBufferSize(), nullptr, &pRTVertexShader);
- pDevice->CreatePixelShader(pRTPixelShaderBlob->GetBufferPointer(), pRTPixelShaderBlob->GetBufferSize(), nullptr, &pRTPixelShader);
- D3D11_INPUT_ELEMENT_DESC RTLayout[] =
- {
- { "POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
- { "TEXCOORD",0,DXGI_FORMAT_R32G32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0 }
- };
- pDevice->CreateInputLayout(RTLayout, 2, pRTVertexShaderBlob->GetBufferPointer(), pRTVertexShaderBlob->GetBufferSize(), &pRTInputLayout);
- D3DX11CompileFromFile("shadowShader.txt", nullptr, nullptr, "VS", "vs_5_0", 0, 0, nullptr, &pShadowVertexShaderBlob, nullptr, nullptr);
- D3DX11CompileFromFile("shadowShader.txt", nullptr, nullptr, "PS", "ps_5_0", 0, 0, nullptr, &pShadowPixelShaderBlob, nullptr, nullptr);
- pDevice->CreateVertexShader(pShadowVertexShaderBlob->GetBufferPointer(), pShadowVertexShaderBlob->GetBufferSize(), nullptr, &pShadowVertexShader);
- pDevice->CreatePixelShader(pShadowPixelShaderBlob->GetBufferPointer(), pShadowPixelShaderBlob->GetBufferSize(), nullptr, &pShadowPixelShader);
- D3D11_INPUT_ELEMENT_DESC shadowLayout[] =
- {
- { "POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
- { "TEXCOORD",0,DXGI_FORMAT_R32G32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0 }
- };
- pDevice->CreateInputLayout(shadowLayout, 2, pShadowVertexShaderBlob->GetBufferPointer(), pShadowVertexShaderBlob->GetBufferSize(), &pShadowInputLayout);
- D3D11_DEPTH_STENCIL_DESC depthStencilStateDesc;
- ZeroMemory(&depthStencilStateDesc, sizeof(D3D11_DEPTH_STENCIL_DESC));
- depthStencilStateDesc.DepthEnable = true;
- pDevice->CreateDepthStencilState(&depthStencilStateDesc, &pDepthStencilState);
- D3D11_DEPTH_STENCIL_DESC disabledDepthStencilStateDesc;
- ZeroMemory(&disabledDepthStencilStateDesc, sizeof(D3D11_DEPTH_STENCIL_DESC));
- disabledDepthStencilStateDesc.DepthEnable = false;
- pDevice->CreateDepthStencilState(&disabledDepthStencilStateDesc, &pDisabledDepthStencilState);
- cube = new Cube();
- cube->Initialize(pDevice, pDeviceContext);
- sprite = new Sprite();
- sprite->Initialize(pDevice, pDeviceContext);
- plane = new Plane();
- plane->Initialize(pDevice, pDeviceContext);
- CreateRenderTexture();
- }
- void Render()
- {
- float fColor[4] = { 0.0f,0.0f,0.0f,1.0f };
- D3D11_VIEWPORT viewport;
- ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
- viewport.TopLeftX = 0;
- viewport.TopLeftY = 0;
- viewport.Width = 800;
- viewport.Height = 600;
- viewport.MinDepth = 0.0f;
- viewport.MaxDepth = 1.0f;
- pDeviceContext->RSSetViewports(1, &viewport);
- pDeviceContext->IASetInputLayout(pInputLayout);
- pDeviceContext->OMSetDepthStencilState(pDepthStencilState, 1);
- pDeviceContext->OMSetRenderTargets(1, &pRenderTargetView, pDepthStencilView);
- pDeviceContext->ClearRenderTargetView(pRenderTargetView, fColor);
- pDeviceContext->ClearDepthStencilView(pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0.0f);
- pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
- D3DXMatrixIdentity(&worldMatrix);
- D3DXMatrixPerspectiveFovLH(&projectionMatrix, D3DXToRadian(45), static_cast<float>(800) / static_cast<float>(600), 1.0f, 100.0f);
- D3DXMatrixLookAtLH(&viewMatrix, &D3DXVECTOR3(0.0f, 10.0f, -4.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
- constantData.worldMatrix = worldMatrix;
- constantData.viewMatrix = viewMatrix;
- constantData.projectionMatrix = projectionMatrix;
- pDeviceContext->VSSetShader(pVertexShader, nullptr, 0);
- pDeviceContext->PSSetShader(pPixelShader, nullptr, 0);
- pDeviceContext->UpdateSubresource(pConstantBuffer, 0, 0, &constantData, 0, 0);
- pDeviceContext->VSSetConstantBuffers(0, 1, &pConstantBuffer);
- pDeviceContext->PSSetSamplers(0, 1, &pSamplerState);
- pDeviceContext->PSSetShaderResources(0, 1, &pShaderResourceView);
- cube->Render();
- D3DXMatrixPerspectiveFovLH(&lightProjectionMatrix, D3DXToRadian(90), 1.0f, 1.0f, 100.0f);
- D3DXMatrixLookAtLH(&lightViewMatrix, &D3DXVECTOR3(0.0f, 3.0f, -3.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
- shadowConstantData.worldMatrix = worldMatrix;
- shadowConstantData.viewMatrix = viewMatrix;
- shadowConstantData.projectionMatrix = projectionMatrix;
- shadowConstantData.lightViewMatrix = lightViewMatrix;
- shadowConstantData.lightProjectionMatrix = lightProjectionMatrix;
- pDeviceContext->IASetInputLayout(pShadowInputLayout);
- pDeviceContext->VSSetShader(pShadowVertexShader, nullptr, 0);
- pDeviceContext->PSSetShader(pShadowPixelShader, nullptr, 0);
- pDeviceContext->UpdateSubresource(pShadowConstantBuffer, 0, 0, &shadowConstantData, 0, 0);
- pDeviceContext->VSSetConstantBuffers(0, 1, &pShadowConstantBuffer);
- pDeviceContext->PSSetSamplers(0, 1, &pShadowSamplerState);
- pDeviceContext->PSSetShaderResources(0, 1, &pRTShaderResourceView);
- plane->Render();
- pDeviceContext->OMSetDepthStencilState(pDisabledDepthStencilState, 1);
- D3DXMatrixIdentity(&worldMatrix);
- D3DXMatrixLookAtLH(&viewMatrix, &D3DXVECTOR3(400.0f, 300.0f, -5.0f), &D3DXVECTOR3(400.0f, 300.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
- D3DXMatrixOrthoLH(&projectionMatrix, 800.0f, 600.0f, 1.0f, 10.0f);
- constantData.worldMatrix = worldMatrix;
- constantData.viewMatrix = viewMatrix;
- constantData.projectionMatrix = projectionMatrix;
- pDeviceContext->VSSetShader(pVertexShader, nullptr, 0);
- pDeviceContext->PSSetShader(pPixelShader, nullptr, 0);
- pDeviceContext->UpdateSubresource(pConstantBuffer, 0, 0, &constantData, 0, 0);
- pDeviceContext->VSSetConstantBuffers(0, 1, &pConstantBuffer);
- pDeviceContext->PSSetSamplers(0, 1, &pSamplerState);
- pDeviceContext->PSSetShaderResources(0, 1, &pRTShaderResourceView);
- sprite->Render();
- pDeviceContext->OMSetDepthStencilState(pDepthStencilState, 1);
- pDeviceContext->IASetInputLayout(pRTInputLayout);
- pDeviceContext->VSSetShader(pRTVertexShader, nullptr, 0);
- pDeviceContext->PSSetShader(pRTPixelShader, nullptr, 0);
- RenderToTexture();
- pSwapChain->Present(0, 0);
- }
- void CreateRenderTexture()
- {
- D3D11_TEXTURE2D_DESC RTTextureDesc;
- ZeroMemory(&RTTextureDesc, sizeof(D3D11_TEXTURE2D_DESC));
- RTTextureDesc.ArraySize = 1;
- RTTextureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
- RTTextureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
- RTTextureDesc.Width = 1024;
- RTTextureDesc.Height = 1024;
- RTTextureDesc.SampleDesc.Count = 1;
- RTTextureDesc.MipLevels = 1;
- RTTextureDesc.Usage = D3D11_USAGE_DEFAULT;
- ID3D11Texture2D* pRTTexture;
- pDevice->CreateTexture2D(&RTTextureDesc, nullptr, &pRTTexture);
- D3D11_RENDER_TARGET_VIEW_DESC RTRenderTargetViewDesc;
- ZeroMemory(&RTRenderTargetViewDesc, sizeof(D3D11_RENDER_TARGET_VIEW_DESC));
- RTRenderTargetViewDesc.Format = RTTextureDesc.Format;
- RTRenderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
- RTRenderTargetViewDesc.Texture2D.MipSlice = 0;
- pDevice->CreateRenderTargetView(pRTTexture, &RTRenderTargetViewDesc, &pRTRenderTargetView);
- D3D11_SHADER_RESOURCE_VIEW_DESC RTShaderResourceViewDesc;
- ZeroMemory(&RTShaderResourceViewDesc, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));
- RTShaderResourceViewDesc.Format = RTTextureDesc.Format;
- RTShaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
- RTShaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
- RTShaderResourceViewDesc.Texture2D.MipLevels = 1;
- pDevice->CreateShaderResourceView(pRTTexture, &RTShaderResourceViewDesc, &pRTShaderResourceView);
- D3D11_TEXTURE2D_DESC RTDepthStencilDesc;
- ZeroMemory(&RTDepthStencilDesc, sizeof(D3D11_TEXTURE2D_DESC));
- RTDepthStencilDesc.ArraySize = 1;
- RTDepthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
- RTDepthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
- RTDepthStencilDesc.Width = 1024;
- RTDepthStencilDesc.Height = 1024;
- RTDepthStencilDesc.MipLevels = 1;
- RTDepthStencilDesc.SampleDesc.Count = 1;
- RTDepthStencilDesc.SampleDesc.Quality = 0;
- RTDepthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
- ID3D11Texture2D* pRTDepthStencilTexture;
- pDevice->CreateTexture2D(&RTDepthStencilDesc, nullptr, &pRTDepthStencilTexture);
- D3D11_DEPTH_STENCIL_VIEW_DESC RTDepthStencilViewDesc;
- ZeroMemory(&RTDepthStencilViewDesc, sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC));
- RTDepthStencilViewDesc.Format = RTDepthStencilDesc.Format;
- RTDepthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
- RTDepthStencilViewDesc.Texture2D.MipSlice = 0;
- pDevice->CreateDepthStencilView(pRTDepthStencilTexture, &RTDepthStencilViewDesc, &pRTDepthStencilView);
- }
- void RenderToTexture()
- {
- float fColor[4] = { 0.0f,0.0f,0.0f,1.0f };
- D3D11_VIEWPORT viewport;
- ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
- viewport.TopLeftX = 0;
- viewport.TopLeftY = 0;
- viewport.Width = 1024;
- viewport.Height = 1024;
- viewport.MinDepth = 0.0f;
- viewport.MaxDepth = 1.0f;
- pDeviceContext->RSSetViewports(1, &viewport);
- pDeviceContext->OMSetRenderTargets(1, &pRTRenderTargetView, pRTDepthStencilView);
- pDeviceContext->ClearRenderTargetView(pRTRenderTargetView, fColor);
- pDeviceContext->ClearDepthStencilView(pRTDepthStencilView, D3D11_CLEAR_DEPTH , 1.0f, 0.0f);
- D3DXMatrixIdentity(&worldMatrix);
- constantData.worldMatrix = worldMatrix;
- constantData.viewMatrix = lightViewMatrix;
- constantData.projectionMatrix = lightProjectionMatrix;
- pDeviceContext->UpdateSubresource(pConstantBuffer, 0, 0, &constantData, 0, 0);
- pDeviceContext->VSSetConstantBuffers(0, 1, &pConstantBuffer);
- pDeviceContext->PSSetSamplers(0, 1, &pSamplerState);
- pDeviceContext->PSSetShaderResources(0, 1, &pShaderResourceView);
- cube->Render();
- plane->Render();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement