4#include "skybox_shader_class.h"
7skybox_shader_class::skybox_shader_class()
16 m_sunlightColorBuffer = 0;
17 m_sunlightPositionBuffer = 0;
26skybox_shader_class::~skybox_shader_class()
31bool skybox_shader_class::Initialize(ID3D11Device* device, HWND hwnd)
33 Logger::Get().
Log(
"Initializing LightShaderClass", __FILE__, __LINE__, Logger::LogLevel::Initialize);
35 wchar_t vsFilename[128];
36 wchar_t psFilename[128];
41 error = wcscpy_s(vsFilename, 128, L
"src/hlsl/skybox.vs");
44 Logger::Get().
Log(
"Failed to copy string", __FILE__, __LINE__, Logger::LogLevel::Error);
49 error = wcscpy_s(psFilename, 128, L
"src/hlsl/skybox.ps");
52 Logger::Get().
Log(
"Failed to copy string", __FILE__, __LINE__, Logger::LogLevel::Error);
56 result = InitializeShader(device, hwnd, vsFilename, psFilename);
59 Logger::Get().
Log(
"Failed to initialize shader", __FILE__, __LINE__, Logger::LogLevel::Error);
63 Logger::Get().
Log(
"skybox_shader_class initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
69void skybox_shader_class::Shutdown()
77bool skybox_shader_class::Render(ID3D11DeviceContext* deviceContext,
int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
78 ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor, XMFLOAT4 ambientColor, XMFLOAT3 sunDirection,
float sunIntensity)
84 result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, diffuseColor, ambientColor, sunDirection, sunIntensity);
87 Logger::Get().
Log(
"Failed to set shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error);
92 RenderShader(deviceContext, indexCount);
98bool skybox_shader_class::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
100 Logger::Get().
Log(
"Initializing shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
103 ID3D10Blob* errorMessage;
104 ID3D10Blob* vertexShaderBuffer;
105 ID3D10Blob* pixelShaderBuffer;
106 D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
107 unsigned int numElements;
108 D3D11_SAMPLER_DESC samplerDesc;
109 D3D11_BUFFER_DESC matrixBufferDesc;
110 D3D11_BUFFER_DESC sunlightBufferDesc;
114 vertexShaderBuffer = 0;
115 pixelShaderBuffer = 0;
118 result = D3DCompileFromFile(vsFilename, NULL, NULL,
"SkyboxVertexShader",
"vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &vertexShaderBuffer, &errorMessage);
123 OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
127 Logger::Get().
Log(
"Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error);
133 result = D3DCompileFromFile(psFilename, NULL, NULL,
"SkyboxPixelShader",
"ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &pixelShaderBuffer, &errorMessage);
138 OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
142 Logger::Get().
Log(
"Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error);
148 result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);
151 Logger::Get().
Log(
"Failed to create vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
156 result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader);
159 Logger::Get().
Log(
"Failed to create pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
164 polygonLayout[0].SemanticName =
"POSITION";
165 polygonLayout[0].SemanticIndex = 0;
166 polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
167 polygonLayout[0].InputSlot = 0;
168 polygonLayout[0].AlignedByteOffset = 0;
169 polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
170 polygonLayout[0].InstanceDataStepRate = 0;
172 polygonLayout[1].SemanticName =
"TEXCOORD";
173 polygonLayout[1].SemanticIndex = 0;
174 polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
175 polygonLayout[1].InputSlot = 0;
176 polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
177 polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
178 polygonLayout[1].InstanceDataStepRate = 0;
180 polygonLayout[2].SemanticName =
"NORMAL";
181 polygonLayout[2].SemanticIndex = 0;
182 polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
183 polygonLayout[2].InputSlot = 0;
184 polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
185 polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
186 polygonLayout[2].InstanceDataStepRate = 0;
189 numElements =
sizeof(polygonLayout) /
sizeof(polygonLayout[0]);
192 result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_layout);
195 Logger::Get().
Log(
"Failed to create input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
200 vertexShaderBuffer->Release();
201 vertexShaderBuffer = 0;
203 pixelShaderBuffer->Release();
204 pixelShaderBuffer = 0;
207 samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
208 samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
209 samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
210 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
211 samplerDesc.MipLODBias = 0.0f;
212 samplerDesc.MaxAnisotropy = 1;
213 samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
214 samplerDesc.BorderColor[0] = 0;
215 samplerDesc.BorderColor[1] = 0;
216 samplerDesc.BorderColor[2] = 0;
217 samplerDesc.BorderColor[3] = 0;
218 samplerDesc.MinLOD = 0;
219 samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
222 result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
225 Logger::Get().
Log(
"Failed to create sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
230 matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
231 matrixBufferDesc.ByteWidth =
sizeof(MatrixBufferType);
232 matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
233 matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
234 matrixBufferDesc.MiscFlags = 0;
235 matrixBufferDesc.StructureByteStride = 0;
238 result = device->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer);
241 Logger::Get().
Log(
"Failed to create matrix buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
246 sunlightBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
247 sunlightBufferDesc.ByteWidth =
sizeof(SkyboxBufferType);
248 sunlightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
249 sunlightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
250 sunlightBufferDesc.MiscFlags = 0;
251 sunlightBufferDesc.StructureByteStride = 0;
254 result = device->CreateBuffer(&sunlightBufferDesc, NULL, &m_sunlightBuffer);
257 Logger::Get().
Log(
"Failed to create sunlight buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
261 Logger::Get().
Log(
"Shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
269void skybox_shader_class::ShutdownShader()
271 Logger::Get().
Log(
"Shutting down SunLightShaderClass", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
274 if (m_sunlightColorBuffer)
276 m_sunlightColorBuffer->Release();
277 m_sunlightColorBuffer = 0;
280 if (m_sunlightPositionBuffer)
282 m_sunlightPositionBuffer->Release();
283 m_sunlightPositionBuffer = 0;
287 if (m_sunlightBuffer)
289 m_sunlightBuffer->Release();
290 m_sunlightBuffer = 0;
296 m_cameraBuffer->Release();
303 m_matrixBuffer->Release();
310 m_sampleState->Release();
324 m_pixelShader->Release();
331 m_vertexShader->Release();
335 Logger::Get().
Log(
"SunLightShaderClass shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
341void skybox_shader_class::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
344 unsigned __int64 bufferSize, i;
349 compileErrors = (
char*)(errorMessage->GetBufferPointer());
352 bufferSize = errorMessage->GetBufferSize();
355 fout.open(
"shader-error.txt");
358 for (i = 0; i < bufferSize; i++)
360 fout << compileErrors[i];
367 errorMessage->Release();
371 MessageBox(hwnd, L
"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
377bool skybox_shader_class::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture, XMFLOAT4 ambientColor, XMFLOAT4 diffuseColor, XMFLOAT3 lightDirection,
float sunIntensity)
380 D3D11_MAPPED_SUBRESOURCE mappedResource;
381 MatrixBufferType* dataPtr;
382 CameraBufferType* dataPtr2;
383 SkyboxBufferType* dataPtr3;
384 unsigned int bufferNumber;
387 worldMatrix = XMMatrixTranspose(worldMatrix);
388 viewMatrix = XMMatrixTranspose(viewMatrix);
389 projectionMatrix = XMMatrixTranspose(projectionMatrix);
392 result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
399 dataPtr = (MatrixBufferType*)mappedResource.pData;
402 dataPtr->world = worldMatrix;
403 dataPtr->view = viewMatrix;
404 dataPtr->projection = projectionMatrix;
407 deviceContext->Unmap(m_matrixBuffer, 0);
413 deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
416 result = deviceContext->Map(m_sunlightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
423 dataPtr3 = (SkyboxBufferType*)mappedResource.pData;
426 dataPtr3->ambientColor = ambientColor;
427 dataPtr3->diffuseColor = diffuseColor;
428 dataPtr3->sunDirection = lightDirection;
429 dataPtr3->intensity = sunIntensity;
432 deviceContext->Unmap(m_sunlightBuffer, 0);
438 deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_sunlightBuffer);
441 deviceContext->PSSetShaderResources(0, 1, &texture);
446void skybox_shader_class::RenderShader(ID3D11DeviceContext* deviceContext,
int indexCount)
449 deviceContext->IASetInputLayout(m_layout);
452 deviceContext->VSSetShader(m_vertexShader, NULL, 0);
453 deviceContext->PSSetShader(m_pixelShader, NULL, 0);
456 deviceContext->PSSetSamplers(0, 1, &m_sampleState);
459 deviceContext->DrawIndexed(indexCount, 0, 0);
void Log(const std::string &message, const std::string &fileName, int lineNumber, LogLevel level=LogLevel::Info)