1#include "refraction_shader_class.h"
4refraction_shader_class::refraction_shader_class()
12 clip_plane_buffer_ = 0;
21refraction_shader_class::~refraction_shader_class()
26bool refraction_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/refraction.vs");
41 error = wcscpy_s(psFilename, 128, L
"src/hlsl/refraction.ps");
48 result = initialize_shader(device, hwnd, vsFilename, psFilename);
58void refraction_shader_class::shutdown()
66bool refraction_shader_class::render(ID3D11DeviceContext* deviceContext,
int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
67 ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 ambientColor[], XMFLOAT4 diffuseColor[], XMFLOAT4 lightPosition[], XMFLOAT4 clipPlane)
73 result = set_shader_parameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, ambientColor, diffuseColor, lightPosition, clipPlane);
80 render_shader(deviceContext, indexCount);
86bool refraction_shader_class::initialize_shader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
89 ID3D10Blob* errorMessage;
90 ID3D10Blob* vertexShaderBuffer;
91 ID3D10Blob* pixelShaderBuffer;
92 D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
93 unsigned int numElements;
94 D3D11_BUFFER_DESC matrixBufferDesc;
95 D3D11_SAMPLER_DESC samplerDesc;
96 D3D11_BUFFER_DESC lightBufferDesc;
97 D3D11_BUFFER_DESC clipPlaneBufferDesc;
102 vertexShaderBuffer = 0;
103 pixelShaderBuffer = 0;
106 result = D3DCompileFromFile(vsFilename, NULL, NULL,
"RefractionVertexShader",
"vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
107 &vertexShaderBuffer, &errorMessage);
113 output_shader_error_message(errorMessage, hwnd, vsFilename);
118 MessageBox(hwnd, vsFilename, L
"Missing Shader File", MB_OK);
125 result = D3DCompileFromFile(psFilename, NULL, NULL,
"RefractionPixelShader",
"ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
126 &pixelShaderBuffer, &errorMessage);
132 output_shader_error_message(errorMessage, hwnd, psFilename);
137 MessageBox(hwnd, psFilename, L
"Missing Shader File", MB_OK);
144 result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &vertex_shader_);
151 result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &pixel_shader_);
158 polygonLayout[0].SemanticName =
"POSITION";
159 polygonLayout[0].SemanticIndex = 0;
160 polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
161 polygonLayout[0].InputSlot = 0;
162 polygonLayout[0].AlignedByteOffset = 0;
163 polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
164 polygonLayout[0].InstanceDataStepRate = 0;
166 polygonLayout[1].SemanticName =
"TEXCOORD";
167 polygonLayout[1].SemanticIndex = 0;
168 polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
169 polygonLayout[1].InputSlot = 0;
170 polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
171 polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
172 polygonLayout[1].InstanceDataStepRate = 0;
174 polygonLayout[2].SemanticName =
"NORMAL";
175 polygonLayout[2].SemanticIndex = 0;
176 polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
177 polygonLayout[2].InputSlot = 0;
178 polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
179 polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
180 polygonLayout[2].InstanceDataStepRate = 0;
183 numElements =
sizeof(polygonLayout) /
sizeof(polygonLayout[0]);
186 result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(),
187 vertexShaderBuffer->GetBufferSize(), &layout_);
194 vertexShaderBuffer->Release();
195 vertexShaderBuffer = 0;
197 pixelShaderBuffer->Release();
198 pixelShaderBuffer = 0;
201 matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
202 matrixBufferDesc.ByteWidth =
sizeof(matrix_buffer_type);
203 matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
204 matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
205 matrixBufferDesc.MiscFlags = 0;
206 matrixBufferDesc.StructureByteStride = 0;
209 result = device->CreateBuffer(&matrixBufferDesc, NULL, &matrix_buffer_);
216 samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
217 samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
218 samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
219 samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
220 samplerDesc.MipLODBias = 0.0f;
221 samplerDesc.MaxAnisotropy = 1;
222 samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
223 samplerDesc.BorderColor[0] = 0;
224 samplerDesc.BorderColor[1] = 0;
225 samplerDesc.BorderColor[2] = 0;
226 samplerDesc.BorderColor[3] = 0;
227 samplerDesc.MinLOD = 0;
228 samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
231 result = device->CreateSamplerState(&samplerDesc, &sample_state_);
239 lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
240 lightBufferDesc.ByteWidth =
sizeof(light_buffer_type);
241 lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
242 lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
243 lightBufferDesc.MiscFlags = 0;
244 lightBufferDesc.StructureByteStride = 0;
247 result = device->CreateBuffer(&lightBufferDesc, NULL, &light_buffer_);
254 clipPlaneBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
255 clipPlaneBufferDesc.ByteWidth =
sizeof(clip_plane_buffer_type);
256 clipPlaneBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
257 clipPlaneBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
258 clipPlaneBufferDesc.MiscFlags = 0;
259 clipPlaneBufferDesc.StructureByteStride = 0;
262 result = device->CreateBuffer(&clipPlaneBufferDesc, NULL, &clip_plane_buffer_);
272void refraction_shader_class::shutdown_shader()
275 if (clip_plane_buffer_)
277 clip_plane_buffer_->Release();
278 clip_plane_buffer_ = 0;
284 light_buffer_->Release();
291 sample_state_->Release();
298 matrix_buffer_->Release();
312 pixel_shader_->Release();
319 vertex_shader_->Release();
327void refraction_shader_class::output_shader_error_message(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
330 unsigned long long bufferSize, i;
335 compileErrors = (
char*)(errorMessage->GetBufferPointer());
338 bufferSize = errorMessage->GetBufferSize();
341 fout.open(
"shader-error.txt");
344 for (i = 0; i < bufferSize; i++)
346 fout << compileErrors[i];
353 errorMessage->Release();
357 MessageBox(hwnd, L
"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
363bool refraction_shader_class::set_shader_parameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
364 ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 ambientColor[], XMFLOAT4 diffuseColor[], XMFLOAT4 lightPosition[], XMFLOAT4 clipPlane)
367 D3D11_MAPPED_SUBRESOURCE mappedResource;
368 matrix_buffer_type* dataPtr;
369 unsigned int bufferNumber;
370 clip_plane_buffer_type* dataPtr2;
371 light_buffer_type* dataPtr3;
375 worldMatrix = XMMatrixTranspose(worldMatrix);
376 viewMatrix = XMMatrixTranspose(viewMatrix);
377 projectionMatrix = XMMatrixTranspose(projectionMatrix);
380 result = deviceContext->Map(matrix_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
387 dataPtr = (matrix_buffer_type*)mappedResource.pData;
390 dataPtr->world = worldMatrix;
391 dataPtr->view = viewMatrix;
392 dataPtr->projection = projectionMatrix;
395 deviceContext->Unmap(matrix_buffer_, 0);
401 deviceContext->VSSetConstantBuffers(bufferNumber, 1, &matrix_buffer_);
404 result = deviceContext->Map(clip_plane_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
411 dataPtr2 = (clip_plane_buffer_type*)mappedResource.pData;
414 dataPtr2->clip_plane = clipPlane;
417 deviceContext->Unmap(clip_plane_buffer_, 0);
423 deviceContext->VSSetConstantBuffers(bufferNumber, 1, &clip_plane_buffer_);
426 deviceContext->PSSetShaderResources(0, 1, &texture);
429 result = deviceContext->Map(light_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
436 dataPtr3 = (light_buffer_type*)mappedResource.pData;
439 dataPtr3->ambient_color = ambientColor[0];
440 dataPtr3->diffuse_color = diffuseColor[0];
441 dataPtr3->light_position = lightPosition[0];
442 dataPtr3->light_direction = lightDirection;
445 deviceContext->Unmap(light_buffer_, 0);
451 deviceContext->PSSetConstantBuffers(bufferNumber, 1, &light_buffer_);
457void refraction_shader_class::render_shader(ID3D11DeviceContext* deviceContext,
int indexCount)
460 deviceContext->IASetInputLayout(layout_);
463 deviceContext->VSSetShader(vertex_shader_, NULL, 0);
464 deviceContext->PSSetShader(pixel_shader_, NULL, 0);
467 deviceContext->PSSetSamplers(0, 1, &sample_state_);
470 deviceContext->DrawIndexed(indexCount, 0, 0);