Cel Shading [WIP]
+ Cel shading shader + Shader Manager window
This commit is contained in:
parent
d9c9a8f0b5
commit
eea4518a0a
397
enginecustom/CelShadingShader.cpp
Normal file
397
enginecustom/CelShadingShader.cpp
Normal file
@ -0,0 +1,397 @@
|
|||||||
|
#include "CelShadingShader.h"
|
||||||
|
|
||||||
|
CelShadingShader::CelShadingShader()
|
||||||
|
{
|
||||||
|
m_vertexShader = 0;
|
||||||
|
m_pixelShader = 0;
|
||||||
|
m_layout = 0;
|
||||||
|
m_matrixBuffer = 0;
|
||||||
|
m_sampleState = 0;
|
||||||
|
m_lightBuffer = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
CelShadingShader::CelShadingShader(const CelShadingShader& other)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
CelShadingShader::~CelShadingShader()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CelShadingShader::Initialize(ID3D11Device* device, HWND hwnd)
|
||||||
|
{
|
||||||
|
Logger::Get().Log("Initializing CelShadingShader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||||
|
|
||||||
|
bool result;
|
||||||
|
wchar_t vsFilename[128];
|
||||||
|
wchar_t psFilename[128];
|
||||||
|
int error;
|
||||||
|
|
||||||
|
// Set the filename of the vertex shader.
|
||||||
|
error = wcscpy_s(vsFilename, 128, L"celshading.vs");
|
||||||
|
if (error != 0)
|
||||||
|
{
|
||||||
|
Logger::Get().Log("Failed to set the filename of the vertex shader", __FILE__, __LINE__);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the filename of the pixel shader.
|
||||||
|
error = wcscpy_s(psFilename, 128, L"celshading.ps");
|
||||||
|
if (error != 0)
|
||||||
|
{
|
||||||
|
Logger::Get().Log("Failed to set the filename of the pixel shader", __FILE__, __LINE__);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize the vertex and pixel shaders.
|
||||||
|
result = InitializeShader(device, hwnd, vsFilename, psFilename);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
Logger::Get().Log("Failed to initialize the vertex and pixel shaders", __FILE__, __LINE__);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::Get().Log("Successfully initialized CelShadingShader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CelShadingShader::Shutdown()
|
||||||
|
{
|
||||||
|
// Shutdown the vertex and pixel shaders as well as the related objects.
|
||||||
|
ShutdownShader();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CelShadingShader::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||||
|
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor[])
|
||||||
|
{
|
||||||
|
bool result;
|
||||||
|
|
||||||
|
// Set the shader parameters that it will use for rendering.
|
||||||
|
result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, diffuseColor);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now render the prepared buffers with the shader.
|
||||||
|
RenderShader(deviceContext, indexCount);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CelShadingShader::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
|
||||||
|
{
|
||||||
|
HRESULT result;
|
||||||
|
ID3D10Blob* errorMessage = nullptr;
|
||||||
|
ID3D10Blob* vertexShaderBuffer = nullptr;
|
||||||
|
ID3D10Blob* pixelShaderBuffer = nullptr;
|
||||||
|
D3D11_INPUT_ELEMENT_DESC polygonLayout[2];
|
||||||
|
unsigned int numElements;
|
||||||
|
D3D11_BUFFER_DESC matrixBufferDesc;
|
||||||
|
D3D11_SAMPLER_DESC samplerDesc;
|
||||||
|
D3D11_BUFFER_DESC lightBufferDesc;
|
||||||
|
|
||||||
|
// Compile the vertex shader code.
|
||||||
|
result = D3DCompileFromFile(vsFilename, nullptr, nullptr, "CelShadingVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &vertexShaderBuffer, &errorMessage);
|
||||||
|
if (FAILED(result))
|
||||||
|
{
|
||||||
|
// If the shader failed to compile it should have written something to the error message.
|
||||||
|
if (errorMessage)
|
||||||
|
{
|
||||||
|
OutputShaderErrorMessage(errorMessage, hwnd, vsFilename);
|
||||||
|
}
|
||||||
|
// If there was nothing in the error message then it simply could not find the shader file itself.
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox(hwnd, vsFilename, L"Missing Shader File", MB_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compile the pixel shader code.
|
||||||
|
result = D3DCompileFromFile(psFilename, nullptr, nullptr, "CelShadingPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &pixelShaderBuffer, &errorMessage);
|
||||||
|
if (FAILED(result))
|
||||||
|
{
|
||||||
|
// If the shader failed to compile it should have written something to the error message.
|
||||||
|
if (errorMessage)
|
||||||
|
{
|
||||||
|
OutputShaderErrorMessage(errorMessage, hwnd, psFilename);
|
||||||
|
}
|
||||||
|
// If there was nothing in the error message then it simply could not find the file itself.
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox(hwnd, psFilename, L"Missing Shader File", MB_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the vertex shader from the buffer.
|
||||||
|
result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), nullptr, &m_vertexShader);
|
||||||
|
if (FAILED(result))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the pixel shader from the buffer.
|
||||||
|
result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), nullptr, &m_pixelShader);
|
||||||
|
if (FAILED(result))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the vertex input layout description.
|
||||||
|
// This setup needs to match the VertexType structure in the ModelClass and in the shader.
|
||||||
|
polygonLayout[0].SemanticName = "POSITION";
|
||||||
|
polygonLayout[0].SemanticIndex = 0;
|
||||||
|
polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||||
|
polygonLayout[0].InputSlot = 0;
|
||||||
|
polygonLayout[0].AlignedByteOffset = 0;
|
||||||
|
polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||||
|
polygonLayout[0].InstanceDataStepRate = 0;
|
||||||
|
|
||||||
|
polygonLayout[1].SemanticName = "TEXCOORD";
|
||||||
|
polygonLayout[1].SemanticIndex = 0;
|
||||||
|
polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||||
|
polygonLayout[1].InputSlot = 0;
|
||||||
|
polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||||
|
polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
||||||
|
polygonLayout[1].InstanceDataStepRate = 0;
|
||||||
|
|
||||||
|
// Get a count of the elements in the layout.
|
||||||
|
numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
|
||||||
|
|
||||||
|
// Create the vertex input layout.
|
||||||
|
result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_layout);
|
||||||
|
if (FAILED(result))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release the vertex shader buffer and pixel shader buffer since they are no longer needed.
|
||||||
|
vertexShaderBuffer->Release();
|
||||||
|
vertexShaderBuffer = nullptr;
|
||||||
|
|
||||||
|
pixelShaderBuffer->Release();
|
||||||
|
pixelShaderBuffer = nullptr;
|
||||||
|
|
||||||
|
// Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
|
||||||
|
matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||||
|
matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
|
||||||
|
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||||
|
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||||
|
matrixBufferDesc.MiscFlags = 0;
|
||||||
|
matrixBufferDesc.StructureByteStride = 0;
|
||||||
|
|
||||||
|
// Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class.
|
||||||
|
result = device->CreateBuffer(&matrixBufferDesc, nullptr, &m_matrixBuffer);
|
||||||
|
if (FAILED(result))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a texture sampler state description.
|
||||||
|
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
|
||||||
|
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||||
|
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||||
|
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
||||||
|
samplerDesc.MipLODBias = 0.0f;
|
||||||
|
samplerDesc.MaxAnisotropy = 1;
|
||||||
|
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
|
||||||
|
samplerDesc.BorderColor[0] = 0;
|
||||||
|
samplerDesc.BorderColor[1] = 0;
|
||||||
|
samplerDesc.BorderColor[2] = 0;
|
||||||
|
samplerDesc.BorderColor[3] = 0;
|
||||||
|
samplerDesc.MinLOD = 0;
|
||||||
|
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
|
||||||
|
|
||||||
|
// Create the texture sampler state.
|
||||||
|
result = device->CreateSamplerState(&samplerDesc, &m_sampleState);
|
||||||
|
if (FAILED(result))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup the description of the light dynamic constant buffer that is in the pixel shader.
|
||||||
|
lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
|
||||||
|
lightBufferDesc.ByteWidth = sizeof(LightBufferType);
|
||||||
|
lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||||
|
lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
||||||
|
lightBufferDesc.MiscFlags = 0;
|
||||||
|
lightBufferDesc.StructureByteStride = 0;
|
||||||
|
|
||||||
|
// Create the constant buffer pointer so we can access the pixel shader constant buffer from within this class.
|
||||||
|
result = device->CreateBuffer(&lightBufferDesc, nullptr, &m_lightBuffer);
|
||||||
|
if (FAILED(result))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void CelShadingShader::ShutdownShader()
|
||||||
|
{
|
||||||
|
// Release the light constant buffer.
|
||||||
|
if (m_lightBuffer)
|
||||||
|
{
|
||||||
|
m_lightBuffer->Release();
|
||||||
|
m_lightBuffer = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release the sampler state.
|
||||||
|
if (m_sampleState)
|
||||||
|
{
|
||||||
|
m_sampleState->Release();
|
||||||
|
m_sampleState = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release the matrix constant buffer.
|
||||||
|
if (m_matrixBuffer)
|
||||||
|
{
|
||||||
|
m_matrixBuffer->Release();
|
||||||
|
m_matrixBuffer = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release the layout.
|
||||||
|
if (m_layout)
|
||||||
|
{
|
||||||
|
m_layout->Release();
|
||||||
|
m_layout = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release the pixel shader.
|
||||||
|
if (m_pixelShader)
|
||||||
|
{
|
||||||
|
m_pixelShader->Release();
|
||||||
|
m_pixelShader = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release the vertex shader.
|
||||||
|
if (m_vertexShader)
|
||||||
|
{
|
||||||
|
m_vertexShader->Release();
|
||||||
|
m_vertexShader = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void CelShadingShader::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
|
||||||
|
{
|
||||||
|
char* compileErrors;
|
||||||
|
unsigned long bufferSize, i;
|
||||||
|
std::ofstream fout;
|
||||||
|
|
||||||
|
// Get a pointer to the error message text buffer.
|
||||||
|
compileErrors = (char*)(errorMessage->GetBufferPointer());
|
||||||
|
|
||||||
|
// Get the length of the message.
|
||||||
|
bufferSize = errorMessage->GetBufferSize();
|
||||||
|
|
||||||
|
// Open a file to write the error message to.
|
||||||
|
fout.open("shader-error.txt");
|
||||||
|
|
||||||
|
// Write out the error message.
|
||||||
|
for (i = 0; i < bufferSize; i++)
|
||||||
|
{
|
||||||
|
fout << compileErrors[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the file.
|
||||||
|
fout.close();
|
||||||
|
|
||||||
|
// Release the error message.
|
||||||
|
errorMessage->Release();
|
||||||
|
errorMessage = nullptr;
|
||||||
|
|
||||||
|
// Pop a message up on the screen to notify the user to check the text file for compile errors.
|
||||||
|
MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool CelShadingShader::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||||
|
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor[])
|
||||||
|
{
|
||||||
|
HRESULT result;
|
||||||
|
D3D11_MAPPED_SUBRESOURCE mappedResource;
|
||||||
|
MatrixBufferType* dataPtr;
|
||||||
|
LightBufferType* dataPtr2;
|
||||||
|
unsigned int bufferNumber;
|
||||||
|
|
||||||
|
// Transpose the matrices to prepare them for the shader.
|
||||||
|
worldMatrix = XMMatrixTranspose(worldMatrix);
|
||||||
|
viewMatrix = XMMatrixTranspose(viewMatrix);
|
||||||
|
projectionMatrix = XMMatrixTranspose(projectionMatrix);
|
||||||
|
|
||||||
|
// Lock the constant buffer so it can be written to.
|
||||||
|
result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||||
|
if (FAILED(result))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a pointer to the data in the constant buffer.
|
||||||
|
dataPtr = (MatrixBufferType*)mappedResource.pData;
|
||||||
|
|
||||||
|
// Copy the matrices into the constant buffer.
|
||||||
|
dataPtr->world = worldMatrix;
|
||||||
|
dataPtr->view = viewMatrix;
|
||||||
|
dataPtr->projection = projectionMatrix;
|
||||||
|
|
||||||
|
// Unlock the constant buffer.
|
||||||
|
deviceContext->Unmap(m_matrixBuffer, 0);
|
||||||
|
|
||||||
|
// Set the position of the constant buffer in the vertex shader.
|
||||||
|
bufferNumber = 0;
|
||||||
|
|
||||||
|
// Finally set the constant buffer in the vertex shader with the updated values.
|
||||||
|
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);
|
||||||
|
|
||||||
|
// Set shader texture resource in the pixel shader.
|
||||||
|
deviceContext->PSSetShaderResources(0, 1, &texture);
|
||||||
|
|
||||||
|
// Lock the light constant buffer so it can be written to.
|
||||||
|
result = deviceContext->Map(m_lightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
|
||||||
|
if (FAILED(result))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a pointer to the data in the constant buffer.
|
||||||
|
dataPtr2 = (LightBufferType*)mappedResource.pData;
|
||||||
|
|
||||||
|
// Copy the lighting variables into the constant buffer.
|
||||||
|
dataPtr2->diffuseColor = diffuseColor[0];
|
||||||
|
dataPtr2->lightDirection = lightDirection;
|
||||||
|
dataPtr2->padding = 0.0f;
|
||||||
|
|
||||||
|
// Unlock the constant buffer.
|
||||||
|
deviceContext->Unmap(m_lightBuffer, 0);
|
||||||
|
|
||||||
|
// Set the position of the light constant buffer in the pixel shader.
|
||||||
|
bufferNumber = 0;
|
||||||
|
|
||||||
|
// Finally set the light constant buffer in the pixel shader with the updated values.
|
||||||
|
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void CelShadingShader::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount)
|
||||||
|
{
|
||||||
|
// Set the vertex input layout.
|
||||||
|
deviceContext->IASetInputLayout(m_layout);
|
||||||
|
|
||||||
|
// Set the vertex and pixel shaders that will be used to render this triangle.
|
||||||
|
deviceContext->VSSetShader(m_vertexShader, nullptr, 0);
|
||||||
|
deviceContext->PSSetShader(m_pixelShader, nullptr, 0);
|
||||||
|
|
||||||
|
// Set the sampler state in the pixel shader.
|
||||||
|
deviceContext->PSSetSamplers(0, 1, &m_sampleState);
|
||||||
|
|
||||||
|
// Render the triangle.
|
||||||
|
deviceContext->DrawIndexed(indexCount, 0, 0);
|
||||||
|
}
|
61
enginecustom/CelShadingShader.h
Normal file
61
enginecustom/CelShadingShader.h
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#ifndef _CELSHADINGSHADER_H_
|
||||||
|
#define _CELSHADINGSHADER_H_
|
||||||
|
|
||||||
|
//////////////
|
||||||
|
// INCLUDES //
|
||||||
|
//////////////
|
||||||
|
#include "Logger.h"
|
||||||
|
#include <d3d11.h>
|
||||||
|
#include <d3dcompiler.h>
|
||||||
|
#include <directxmath.h>
|
||||||
|
#include <fstream>
|
||||||
|
using namespace DirectX;
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Class name: CelShadingShader
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
class CelShadingShader
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
struct MatrixBufferType
|
||||||
|
{
|
||||||
|
XMMATRIX world;
|
||||||
|
XMMATRIX view;
|
||||||
|
XMMATRIX projection;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct LightBufferType
|
||||||
|
{
|
||||||
|
XMFLOAT4 diffuseColor;
|
||||||
|
XMFLOAT3 lightDirection;
|
||||||
|
float padding;
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
CelShadingShader();
|
||||||
|
CelShadingShader(const CelShadingShader&);
|
||||||
|
~CelShadingShader();
|
||||||
|
|
||||||
|
bool Initialize(ID3D11Device*, HWND);
|
||||||
|
void Shutdown();
|
||||||
|
bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4[]);
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
|
||||||
|
void ShutdownShader();
|
||||||
|
void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);
|
||||||
|
|
||||||
|
bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4[]);
|
||||||
|
void RenderShader(ID3D11DeviceContext*, int);
|
||||||
|
|
||||||
|
private:
|
||||||
|
ID3D11VertexShader* m_vertexShader;
|
||||||
|
ID3D11PixelShader* m_pixelShader;
|
||||||
|
ID3D11InputLayout* m_layout;
|
||||||
|
ID3D11Buffer* m_matrixBuffer;
|
||||||
|
ID3D11SamplerState* m_sampleState;
|
||||||
|
ID3D11Buffer* m_lightBuffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -1021,6 +1021,17 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t
|
|||||||
result = m_ShaderManager->RenderlightShader(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0),
|
result = m_ShaderManager->RenderlightShader(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0),
|
||||||
diffuseColor, lightPosition, ambientColor);
|
diffuseColor, lightPosition, ambientColor);
|
||||||
|
|
||||||
|
// Render cel shading globally to the scene using the cel shader if the checkbox is checked.
|
||||||
|
if (m_enableCelShading) {
|
||||||
|
result = m_ShaderManager->RenderCelShadingShader(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0),
|
||||||
|
m_Lights[0]->GetDirection(), ambientColor);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
Logger::Get().Log("Could not render the model using the cel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (auto cube : m_cubes)
|
for (auto cube : m_cubes)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -1055,6 +1066,7 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t
|
|||||||
Logger::Get().Log("Could not render the cube model using the light shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
Logger::Get().Log("Could not render the cube model using the light shader", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& object : m_object)
|
for (auto& object : m_object)
|
||||||
|
@ -83,6 +83,8 @@ public:
|
|||||||
bool GetShouldQuit() const { return m_ShouldQuit; };
|
bool GetShouldQuit() const { return m_ShouldQuit; };
|
||||||
void SetShouldQuit(bool shouldQuit) { m_ShouldQuit = shouldQuit; };
|
void SetShouldQuit(bool shouldQuit) { m_ShouldQuit = shouldQuit; };
|
||||||
|
|
||||||
|
void SetCelShading(bool enable) { m_enableCelShading = enable; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool Render(float, float, float, float, float);
|
bool Render(float, float, float, float, float);
|
||||||
bool UpdateMouseStrings(int, int, bool);
|
bool UpdateMouseStrings(int, int, bool);
|
||||||
@ -142,6 +144,8 @@ private :
|
|||||||
FontShaderClass* m_FontShader;
|
FontShaderClass* m_FontShader;
|
||||||
BitmapClass* m_Bitmap;
|
BitmapClass* m_Bitmap;
|
||||||
SpriteClass* m_Sprite;
|
SpriteClass* m_Sprite;
|
||||||
|
|
||||||
|
bool m_enableCelShading;
|
||||||
|
|
||||||
// ----------------------------------- //
|
// ----------------------------------- //
|
||||||
// ------------ VARIABLES ------------ //
|
// ------------ VARIABLES ------------ //
|
||||||
|
44
enginecustom/celshading.ps
Normal file
44
enginecustom/celshading.ps
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
// celshading.ps
|
||||||
|
cbuffer LightBuffer
|
||||||
|
{
|
||||||
|
float4 diffuseColor;
|
||||||
|
float3 lightDirection;
|
||||||
|
float padding; // Padding to ensure the structure is a multiple of 16 bytes.
|
||||||
|
};
|
||||||
|
|
||||||
|
Texture2D shaderTexture;
|
||||||
|
SamplerState SampleType;
|
||||||
|
|
||||||
|
struct PixelInputType
|
||||||
|
{
|
||||||
|
float4 position : SV_POSITION;
|
||||||
|
float2 tex : TEXCOORD0;
|
||||||
|
};
|
||||||
|
|
||||||
|
float4 CelShadingPixelShader(PixelInputType input) : SV_TARGET
|
||||||
|
{
|
||||||
|
float4 textureColor;
|
||||||
|
float lightIntensity;
|
||||||
|
float4 finalColor;
|
||||||
|
|
||||||
|
// Sample the pixel color from the texture.
|
||||||
|
textureColor = shaderTexture.Sample(SampleType, input.tex);
|
||||||
|
|
||||||
|
// Calculate the light intensity based on the light direction.
|
||||||
|
lightIntensity = saturate(dot(normalize(lightDirection), float3(0.0f, 0.0f, -1.0f)));
|
||||||
|
|
||||||
|
// Apply a step function to create the cel shading effect.
|
||||||
|
if (lightIntensity > 0.5f)
|
||||||
|
{
|
||||||
|
lightIntensity = 1.0f;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lightIntensity = 0.3f;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate the final color by combining the texture color with the light intensity and diffuse color.
|
||||||
|
finalColor = textureColor * diffuseColor * lightIntensity;
|
||||||
|
|
||||||
|
return finalColor;
|
||||||
|
}
|
37
enginecustom/celshading.vs
Normal file
37
enginecustom/celshading.vs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// celshading.vs
|
||||||
|
cbuffer MatrixBuffer
|
||||||
|
{
|
||||||
|
matrix worldMatrix;
|
||||||
|
matrix viewMatrix;
|
||||||
|
matrix projectionMatrix;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VertexInputType
|
||||||
|
{
|
||||||
|
float4 position : POSITION;
|
||||||
|
float2 tex : TEXCOORD0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct PixelInputType
|
||||||
|
{
|
||||||
|
float4 position : SV_POSITION;
|
||||||
|
float2 tex : TEXCOORD0;
|
||||||
|
};
|
||||||
|
|
||||||
|
PixelInputType CelShadingVertexShader(VertexInputType input)
|
||||||
|
{
|
||||||
|
PixelInputType output;
|
||||||
|
|
||||||
|
// Change the position vector to be 4 units for proper matrix calculations.
|
||||||
|
input.position.w = 1.0f;
|
||||||
|
|
||||||
|
// Calculate the position of the vertex against the world, view, and projection matrices.
|
||||||
|
output.position = mul(input.position, worldMatrix);
|
||||||
|
output.position = mul(output.position, viewMatrix);
|
||||||
|
output.position = mul(output.position, projectionMatrix);
|
||||||
|
|
||||||
|
// Store the texture coordinates for the pixel shader.
|
||||||
|
output.tex = input.tex;
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
@ -23,6 +23,7 @@
|
|||||||
<ClCompile Include="applicationclass.cpp" />
|
<ClCompile Include="applicationclass.cpp" />
|
||||||
<ClCompile Include="bitmapclass.cpp" />
|
<ClCompile Include="bitmapclass.cpp" />
|
||||||
<ClCompile Include="Cameraclass.cpp" />
|
<ClCompile Include="Cameraclass.cpp" />
|
||||||
|
<ClCompile Include="CelShadingShader.cpp" />
|
||||||
<ClCompile Include="Colorshaderclass.cpp" />
|
<ClCompile Include="Colorshaderclass.cpp" />
|
||||||
<ClCompile Include="d3dclass.cpp" />
|
<ClCompile Include="d3dclass.cpp" />
|
||||||
<ClCompile Include="imguiManager.cpp" />
|
<ClCompile Include="imguiManager.cpp" />
|
||||||
@ -70,6 +71,7 @@
|
|||||||
<ClInclude Include="applicationclass.h" />
|
<ClInclude Include="applicationclass.h" />
|
||||||
<ClInclude Include="bitmapclass.h" />
|
<ClInclude Include="bitmapclass.h" />
|
||||||
<ClInclude Include="Cameraclass.h" />
|
<ClInclude Include="Cameraclass.h" />
|
||||||
|
<ClInclude Include="CelShadingShader.h" />
|
||||||
<ClInclude Include="Colorshaderclass.h" />
|
<ClInclude Include="Colorshaderclass.h" />
|
||||||
<ClInclude Include="d3dclass.h" />
|
<ClInclude Include="d3dclass.h" />
|
||||||
<ClInclude Include="imguiManager.h" />
|
<ClInclude Include="imguiManager.h" />
|
||||||
@ -154,6 +156,12 @@
|
|||||||
<CopyFileToFolders Include="..\KhaoticDemo\water.ps">
|
<CopyFileToFolders Include="..\KhaoticDemo\water.ps">
|
||||||
<FileType>Document</FileType>
|
<FileType>Document</FileType>
|
||||||
</CopyFileToFolders>
|
</CopyFileToFolders>
|
||||||
|
<CopyFileToFolders Include="celshading.ps">
|
||||||
|
<FileType>Document</FileType>
|
||||||
|
</CopyFileToFolders>
|
||||||
|
<CopyFileToFolders Include="celshading.vs">
|
||||||
|
<FileType>Document</FileType>
|
||||||
|
</CopyFileToFolders>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
<CopyFileToFolders Include="reflection.ps">
|
<CopyFileToFolders Include="reflection.ps">
|
||||||
<FileType>Document</FileType>
|
<FileType>Document</FileType>
|
||||||
|
@ -171,6 +171,9 @@
|
|||||||
<ClCompile Include="normalmapshaderclass.cpp">
|
<ClCompile Include="normalmapshaderclass.cpp">
|
||||||
<Filter>Fichiers sources</Filter>
|
<Filter>Fichiers sources</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="CelShadingShader.cpp">
|
||||||
|
<Filter>Fichiers sources</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="systemclass.h">
|
<ClInclude Include="systemclass.h">
|
||||||
@ -317,6 +320,9 @@
|
|||||||
<ClInclude Include="physics.h">
|
<ClInclude Include="physics.h">
|
||||||
<Filter>Fichiers d%27en-tête</Filter>
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="CelShadingShader.h">
|
||||||
|
<Filter>Fichiers d%27en-tête</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Image Include="papier.tga">
|
<Image Include="papier.tga">
|
||||||
@ -508,5 +514,11 @@
|
|||||||
<CopyFileToFolders Include="86.obj">
|
<CopyFileToFolders Include="86.obj">
|
||||||
<Filter>Assets</Filter>
|
<Filter>Assets</Filter>
|
||||||
</CopyFileToFolders>
|
</CopyFileToFolders>
|
||||||
|
<CopyFileToFolders Include="celshading.vs">
|
||||||
|
<Filter>shader</Filter>
|
||||||
|
</CopyFileToFolders>
|
||||||
|
<CopyFileToFolders Include="celshading.ps">
|
||||||
|
<Filter>shader</Filter>
|
||||||
|
</CopyFileToFolders>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -18,3 +18,7 @@ Size=342,82
|
|||||||
Pos=95,296
|
Pos=95,296
|
||||||
Size=345,230
|
Size=345,230
|
||||||
|
|
||||||
|
[Window][Shader Manager]
|
||||||
|
Pos=60,60
|
||||||
|
Size=172,284
|
||||||
|
|
||||||
|
@ -111,6 +111,17 @@ void imguiManager::WidgetAddObject(ApplicationClass* app)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void imguiManager::WidgetShaderWindow(ApplicationClass* app)
|
||||||
|
{
|
||||||
|
ImGui::Begin("Shader Manager");
|
||||||
|
|
||||||
|
// Checkbox for toggling cel shading globally in the application class by calling the SetCelShading function in the application class when the checkbox state changes
|
||||||
|
ImGui::Checkbox("Enable Cel Shading", &m_EnableCelShading);
|
||||||
|
app->SetCelShading(m_EnableCelShading);
|
||||||
|
|
||||||
|
ImGui::End();
|
||||||
|
}
|
||||||
|
|
||||||
void imguiManager::WidgetObjectWindow(ApplicationClass* app)
|
void imguiManager::WidgetObjectWindow(ApplicationClass* app)
|
||||||
{
|
{
|
||||||
ImGui::Begin("Objects", &showObjectWindow);
|
ImGui::Begin("Objects", &showObjectWindow);
|
||||||
@ -300,6 +311,11 @@ bool imguiManager::ImGuiWidgetRenderer(ApplicationClass* app)
|
|||||||
showLightWindow = true;
|
showLightWindow = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ImGui::Button("Open Shader Window"))
|
||||||
|
{
|
||||||
|
showShaderWindow = true;
|
||||||
|
}
|
||||||
|
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
// Show windows if their corresponding variables are true
|
// Show windows if their corresponding variables are true
|
||||||
@ -318,6 +334,11 @@ bool imguiManager::ImGuiWidgetRenderer(ApplicationClass* app)
|
|||||||
WidgetLightWindow(app);
|
WidgetLightWindow(app);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (showShaderWindow)
|
||||||
|
{
|
||||||
|
WidgetShaderWindow(app);
|
||||||
|
}
|
||||||
|
|
||||||
//render imgui
|
//render imgui
|
||||||
Render();
|
Render();
|
||||||
|
|
||||||
|
@ -31,13 +31,19 @@ public:
|
|||||||
void WidgetObjectWindow(ApplicationClass* app);
|
void WidgetObjectWindow(ApplicationClass* app);
|
||||||
void WidgetTerrainWindow(ApplicationClass* app);
|
void WidgetTerrainWindow(ApplicationClass* app);
|
||||||
void WidgetLightWindow(ApplicationClass* app);
|
void WidgetLightWindow(ApplicationClass* app);
|
||||||
|
void WidgetShaderWindow(ApplicationClass* app);
|
||||||
|
|
||||||
bool ImGuiWidgetRenderer(ApplicationClass* app);
|
bool ImGuiWidgetRenderer(ApplicationClass* app);
|
||||||
|
|
||||||
|
// Shader toggles
|
||||||
|
|
||||||
|
bool m_EnableCelShading;
|
||||||
|
|
||||||
private :
|
private :
|
||||||
bool showObjectWindow = false;
|
bool showObjectWindow = false;
|
||||||
bool showTerrainWindow = false;
|
bool showTerrainWindow = false;
|
||||||
bool showLightWindow = false;
|
bool showLightWindow = false;
|
||||||
|
bool showShaderWindow = false;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ImGuiIO* io;
|
ImGuiIO* io;
|
||||||
|
@ -13,6 +13,7 @@ ShaderManagerClass::ShaderManagerClass()
|
|||||||
m_LightMapShader = 0;
|
m_LightMapShader = 0;
|
||||||
m_RefractionShader = 0;
|
m_RefractionShader = 0;
|
||||||
m_WaterShader = 0;
|
m_WaterShader = 0;
|
||||||
|
m_CelShadingShader = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -140,6 +141,14 @@ bool ShaderManagerClass::Initialize(ID3D11Device* device, HWND hwnd)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_CelShadingShader = new CelShadingShader;
|
||||||
|
|
||||||
|
result = m_CelShadingShader->Initialize(device, hwnd);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
Logger::Get().Log("ShaderManagerClass initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
Logger::Get().Log("ShaderManagerClass initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -417,4 +426,18 @@ bool ShaderManagerClass::RenderWaterShader(ID3D11DeviceContext* deviceContext, i
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ShaderManagerClass::RenderCelShadingShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
|
||||||
|
ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor[])
|
||||||
|
{
|
||||||
|
bool result;
|
||||||
|
|
||||||
|
result = m_CelShadingShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, diffuseColor);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
@ -15,6 +15,7 @@
|
|||||||
#include "lightmapshaderclass.h"
|
#include "lightmapshaderclass.h"
|
||||||
#include "refractionshaderclass.h"
|
#include "refractionshaderclass.h"
|
||||||
#include "watershaderclass.h"
|
#include "watershaderclass.h"
|
||||||
|
#include "CelShadingShader.h"
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
@ -43,6 +44,7 @@ public:
|
|||||||
XMFLOAT3, XMFLOAT4[], XMFLOAT4[], XMFLOAT4[], XMFLOAT4);
|
XMFLOAT3, XMFLOAT4[], XMFLOAT4[], XMFLOAT4[], XMFLOAT4);
|
||||||
bool RenderWaterShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*,
|
bool RenderWaterShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*,
|
||||||
ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, float, float);
|
ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, float, float);
|
||||||
|
bool RenderCelShadingShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4[]);
|
||||||
private:
|
private:
|
||||||
TextureShaderClass* m_TextureShader;
|
TextureShaderClass* m_TextureShader;
|
||||||
NormalMapShaderClass* m_NormalMapShader;
|
NormalMapShaderClass* m_NormalMapShader;
|
||||||
@ -56,6 +58,7 @@ private:
|
|||||||
LightMapShaderClass* m_LightMapShader;
|
LightMapShaderClass* m_LightMapShader;
|
||||||
RefractionShaderClass* m_RefractionShader;
|
RefractionShaderClass* m_RefractionShader;
|
||||||
WaterShaderClass* m_WaterShader;
|
WaterShaderClass* m_WaterShader;
|
||||||
|
CelShadingShader* m_CelShadingShader;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
x
Reference in New Issue
Block a user