1#include "water_shader_class.h"
4water_shader_class::water_shader_class()
11 reflection_buffer_ = 0;
21water_shader_class::~water_shader_class()
26bool water_shader_class::initialize(ID3D11Device* device, HWND hwnd)
29 wchar_t vsFilename[128];
30 wchar_t psFilename[128];
34 error = wcscpy_s(vsFilename, 128, L
"src/hlsl/water.vs");
41 error = wcscpy_s(psFilename, 128, L
"src/hlsl/water.ps");
48 result = initialize_shader(device, hwnd, vsFilename, psFilename);
58void water_shader_class::shutdown()
66bool water_shader_class::render(ID3D11DeviceContext* deviceContext,
int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
67 XMMATRIX reflectionMatrix, ID3D11ShaderResourceView* reflectionTexture, ID3D11ShaderResourceView* refractionTexture,
68 ID3D11ShaderResourceView* normalTexture,
float waterTranslation,
float reflectRefractScale)
74 result = set_shader_parameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, reflectionMatrix, reflectionTexture,
75 refractionTexture, normalTexture, waterTranslation, reflectRefractScale);
82 render_shader(deviceContext, indexCount);
88bool water_shader_class::initialize_shader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
91 ID3D10Blob* errorMessage;
92 ID3D10Blob* vertexShaderBuffer;
93 ID3D10Blob* pixelShaderBuffer;
94 D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
95 unsigned int numElements;
96 D3D11_BUFFER_DESC matrixBufferDesc;
97 D3D11_SAMPLER_DESC samplerDesc;
98 D3D11_BUFFER_DESC reflectionBufferDesc;
99 D3D11_BUFFER_DESC waterBufferDesc;
104 vertexShaderBuffer = 0;
105 pixelShaderBuffer = 0;
108 result = D3DCompileFromFile(vsFilename, NULL, NULL,
"WaterVertexShader",
"vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
109 &vertexShaderBuffer, &errorMessage);
115 output_shader_error_message(errorMessage, hwnd, vsFilename);
120 MessageBox(hwnd, vsFilename, L
"Missing Shader File", MB_OK);
127 result = D3DCompileFromFile(psFilename, NULL, NULL,
"WaterPixelShader",
"ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
128 &pixelShaderBuffer, &errorMessage);
134 output_shader_error_message(errorMessage, hwnd, psFilename);
139 MessageBox(hwnd, psFilename, L
"Missing Shader File", MB_OK);
146 result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &vertex_shader_);
153 result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &pixel_shader_);
160 polygonLayout[0].SemanticName =
"POSITION";
161 polygonLayout[0].SemanticIndex = 0;
162 polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
163 polygonLayout[0].InputSlot = 0;
164 polygonLayout[0].AlignedByteOffset = 0;
165 polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
166 polygonLayout[0].InstanceDataStepRate = 0;
168 polygonLayout[1].SemanticName =
"TEXCOORD";
169 polygonLayout[1].SemanticIndex = 0;
170 polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
171 polygonLayout[1].InputSlot = 0;
172 polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
173 polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
174 polygonLayout[1].InstanceDataStepRate = 0;
177 numElements =
sizeof(polygonLayout) /
sizeof(polygonLayout[0]);
180 result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
181 vertexShaderBuffer->GetBufferSize(), &layout_);
188 vertexShaderBuffer->Release();
189 vertexShaderBuffer = 0;
191 pixelShaderBuffer->Release();
192 pixelShaderBuffer = 0;
195 matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
196 matrixBufferDesc.ByteWidth =
sizeof(matrix_buffer_type);
197 matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
198 matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
199 matrixBufferDesc.MiscFlags = 0;
200 matrixBufferDesc.StructureByteStride = 0;
203 result = device->CreateBuffer(&matrixBufferDesc, NULL, &matrix_buffer_);
210 samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
211 samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
212 samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
213 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
214 samplerDesc.MipLODBias = 0.0f;
215 samplerDesc.MaxAnisotropy = 1;
216 samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
217 samplerDesc.BorderColor[0] = 0;
218 samplerDesc.BorderColor[1] = 0;
219 samplerDesc.BorderColor[2] = 0;
220 samplerDesc.BorderColor[3] = 0;
221 samplerDesc.MinLOD = 0;
222 samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
225 result = device->CreateSamplerState(&samplerDesc, &sample_state_);
232 reflectionBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
233 reflectionBufferDesc.ByteWidth =
sizeof(reflection_buffer_type);
234 reflectionBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
235 reflectionBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
236 reflectionBufferDesc.MiscFlags = 0;
237 reflectionBufferDesc.StructureByteStride = 0;
240 result = device->CreateBuffer(&reflectionBufferDesc, NULL, &reflection_buffer_);
247 waterBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
248 waterBufferDesc.ByteWidth =
sizeof(water_buffer_type);
249 waterBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
250 waterBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
251 waterBufferDesc.MiscFlags = 0;
252 waterBufferDesc.StructureByteStride = 0;
255 result = device->CreateBuffer(&waterBufferDesc, NULL, &water_buffer_);
265void water_shader_class::shutdown_shader()
270 water_buffer_->Release();
275 if (reflection_buffer_)
277 reflection_buffer_->Release();
278 reflection_buffer_ = 0;
284 sample_state_->Release();
291 matrix_buffer_->Release();
305 pixel_shader_->Release();
312 vertex_shader_->Release();
320void water_shader_class::output_shader_error_message(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
323 unsigned long long bufferSize, i;
328 compileErrors = (
char*)(errorMessage->GetBufferPointer());
331 bufferSize = errorMessage->GetBufferSize();
334 fout.open(
"shader-error.txt");
337 for (i = 0; i < bufferSize; i++)
339 fout << compileErrors[i];
346 errorMessage->Release();
350 MessageBox(hwnd, L
"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
355bool water_shader_class::set_shader_parameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, XMMATRIX reflectionMatrix,
356 ID3D11ShaderResourceView* reflectionTexture, ID3D11ShaderResourceView* refractionTexture, ID3D11ShaderResourceView* normalTexture,
357 float waterTranslation,
float reflectRefractScale)
360 D3D11_MAPPED_SUBRESOURCE mappedResource;
361 matrix_buffer_type* dataPtr;
362 unsigned int bufferNumber;
363 reflection_buffer_type* dataPtr2;
364 water_buffer_type* dataPtr3;
367 worldMatrix = XMMatrixTranspose(worldMatrix);
368 viewMatrix = XMMatrixTranspose(viewMatrix);
369 projectionMatrix = XMMatrixTranspose(projectionMatrix);
370 reflectionMatrix = XMMatrixTranspose(reflectionMatrix);
373 result = deviceContext->Map(matrix_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
380 dataPtr = (matrix_buffer_type*)mappedResource.pData;
383 dataPtr->world = worldMatrix;
384 dataPtr->view = viewMatrix;
385 dataPtr->projection = projectionMatrix;
388 deviceContext->Unmap(matrix_buffer_, 0);
394 deviceContext->VSSetConstantBuffers(bufferNumber, 1, &matrix_buffer_);
397 result = deviceContext->Map(reflection_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
404 dataPtr2 = (reflection_buffer_type*)mappedResource.pData;
407 dataPtr2->reflection = reflectionMatrix;
410 deviceContext->Unmap(reflection_buffer_, 0);
416 deviceContext->VSSetConstantBuffers(bufferNumber, 1, &reflection_buffer_);
419 deviceContext->PSSetShaderResources(0, 1, &reflectionTexture);
422 deviceContext->PSSetShaderResources(1, 1, &refractionTexture);
425 deviceContext->PSSetShaderResources(2, 1, &normalTexture);
428 result = deviceContext->Map(water_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
435 dataPtr3 = (water_buffer_type*)mappedResource.pData;
438 dataPtr3->water_translation = waterTranslation;
439 dataPtr3->reflect_refract_scale = reflectRefractScale;
440 dataPtr3->padding = XMFLOAT2(0.0f, 0.0f);
443 deviceContext->Unmap(water_buffer_, 0);
449 deviceContext->PSSetConstantBuffers(bufferNumber, 1, &water_buffer_);
454void water_shader_class::render_shader(ID3D11DeviceContext* deviceContext,
int indexCount)
457 deviceContext->IASetInputLayout(layout_);
460 deviceContext->VSSetShader(vertex_shader_, NULL, 0);
461 deviceContext->PSSetShader(pixel_shader_, NULL, 0);
464 deviceContext->PSSetSamplers(0, 1, &sample_state_);
467 deviceContext->DrawIndexed(indexCount, 0, 0);