Added: lighting
This commit is contained in:
@@ -221,6 +221,24 @@
|
|||||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
|
||||||
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
|
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
|
||||||
</FxCompile>
|
</FxCompile>
|
||||||
|
<FxCompile Include="LightingPixelShader.hlsl">
|
||||||
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
|
||||||
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">Pixel</ShaderType>
|
||||||
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
|
||||||
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Pixel</ShaderType>
|
||||||
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">Pixel</ShaderType>
|
||||||
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
|
||||||
|
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
|
||||||
|
</FxCompile>
|
||||||
|
<FxCompile Include="LightingVertexShader.hlsl">
|
||||||
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Vertex</ShaderType>
|
||||||
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">Vertex</ShaderType>
|
||||||
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Vertex</ShaderType>
|
||||||
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Vertex</ShaderType>
|
||||||
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">Vertex</ShaderType>
|
||||||
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
|
||||||
|
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
|
||||||
|
</FxCompile>
|
||||||
<FxCompile Include="TexturePixelShader.hlsl">
|
<FxCompile Include="TexturePixelShader.hlsl">
|
||||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
|
||||||
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">Pixel</ShaderType>
|
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">Pixel</ShaderType>
|
||||||
|
|||||||
@@ -91,6 +91,12 @@
|
|||||||
<FxCompile Include="TexturePixelShader.hlsl">
|
<FxCompile Include="TexturePixelShader.hlsl">
|
||||||
<Filter>Resource Files</Filter>
|
<Filter>Resource Files</Filter>
|
||||||
</FxCompile>
|
</FxCompile>
|
||||||
|
<FxCompile Include="LightingPixelShader.hlsl">
|
||||||
|
<Filter>Resource Files</Filter>
|
||||||
|
</FxCompile>
|
||||||
|
<FxCompile Include="LightingVertexShader.hlsl">
|
||||||
|
<Filter>Resource Files</Filter>
|
||||||
|
</FxCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
|
|||||||
@@ -1,7 +1,19 @@
|
|||||||
#include "Light.h"
|
#include "Light.h"
|
||||||
|
#include "Utilities.h"
|
||||||
|
|
||||||
UINT Light::init(ID3D11Device* pD3DDevice, LightBuffer& lightData)
|
UINT Light::init(ID3D11Device* pD3DDevice, LightBuffer& lightData)
|
||||||
{
|
{
|
||||||
|
D3D11_BUFFER_DESC desc = {};
|
||||||
|
desc.ByteWidth = sizeof(LightBuffer);
|
||||||
|
desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||||
|
desc.Usage = D3D11_USAGE_IMMUTABLE;
|
||||||
|
|
||||||
|
D3D11_SUBRESOURCE_DATA data = {};
|
||||||
|
data.pSysMem = &lightData;
|
||||||
|
|
||||||
|
HRESULT hr = pD3DDevice->CreateBuffer(&desc, &data, &_pLightBuffer);
|
||||||
|
CheckFailed(hr, 700);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -11,6 +23,7 @@ void Light::update(FLOAT dt)
|
|||||||
|
|
||||||
void Light::render(ID3D11DeviceContext* pD3DDeviceContext)
|
void Light::render(ID3D11DeviceContext* pD3DDeviceContext)
|
||||||
{
|
{
|
||||||
|
pD3DDeviceContext->PSSetConstantBuffers(0, 1, &_pLightBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Light::deInit()
|
void Light::deInit()
|
||||||
|
|||||||
@@ -9,8 +9,10 @@ class Light
|
|||||||
public:
|
public:
|
||||||
struct LightBuffer
|
struct LightBuffer
|
||||||
{
|
{
|
||||||
XMFLOAT3 LightVector;
|
XMFLOAT3A LightVector;
|
||||||
|
XMFLOAT3 LightPosition;
|
||||||
FLOAT LightIntensity;
|
FLOAT LightIntensity;
|
||||||
|
XMFLOAT3A LightAttenuation;
|
||||||
XMFLOAT4 DiffuseColor;
|
XMFLOAT4 DiffuseColor;
|
||||||
XMFLOAT4 SpecularColor;
|
XMFLOAT4 SpecularColor;
|
||||||
XMFLOAT3 CameraPosition;
|
XMFLOAT3 CameraPosition;
|
||||||
|
|||||||
Binary file not shown.
@@ -0,0 +1,59 @@
|
|||||||
|
Texture2D MainTexture;
|
||||||
|
SamplerState MainSampler;
|
||||||
|
|
||||||
|
cbuffer LightBuffer
|
||||||
|
{
|
||||||
|
float3 LightVector;
|
||||||
|
float3 LightPosition;
|
||||||
|
float LightIntensity;
|
||||||
|
float3 LightAttenuation;
|
||||||
|
float4 DiffuseColor;
|
||||||
|
float4 SpecularColor;
|
||||||
|
float3 CameraPosition;
|
||||||
|
float Phong;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct PixelInput
|
||||||
|
{
|
||||||
|
float4 position : SV_Position;
|
||||||
|
float3 positionWorld : POSITION1;
|
||||||
|
float2 uv : TEXCOORD;
|
||||||
|
float4 color : COLOR;
|
||||||
|
float3 normal : NORMAL;
|
||||||
|
};
|
||||||
|
|
||||||
|
float4 main(PixelInput INPUT) : SV_TARGET
|
||||||
|
{
|
||||||
|
float4 texColor = MainTexture.Sample(MainSampler, INPUT.uv) * INPUT.color;
|
||||||
|
float4 ambientColor = float4(0.2f, 0.2f, 0.2f, 1.0f);
|
||||||
|
float4 diffuseColor = 0;
|
||||||
|
float4 specularColor = 0;
|
||||||
|
float4 emissionColor = 0;
|
||||||
|
//float4 emissionColor = float4(0.7f, 0.0f, 0.0f, 1.0f);
|
||||||
|
|
||||||
|
// directional
|
||||||
|
//float lightIntensity = LightIntensity;
|
||||||
|
|
||||||
|
// point light
|
||||||
|
float3 light = LightPosition - INPUT.positionWorld;
|
||||||
|
float lightLength = length(light);
|
||||||
|
float attenuation = 1 / (LightAttenuation.x + LightAttenuation.y * lightLength + LightAttenuation.z * lightLength * lightLength); // 1 / (constant + linear + quadratic)
|
||||||
|
float lightIntensity = LightIntensity * attenuation;
|
||||||
|
|
||||||
|
// normalize vectors
|
||||||
|
float3 normal = normalize(INPUT.normal);
|
||||||
|
//float3 light = normalize(-LightVector); // directional light
|
||||||
|
light = light / lightLength; // point or spot light
|
||||||
|
|
||||||
|
// diffuse color
|
||||||
|
float diffuse = max(0, dot(normal, light));
|
||||||
|
diffuseColor = DiffuseColor * (diffuse * lightIntensity);
|
||||||
|
|
||||||
|
// specular color
|
||||||
|
float3 reflect = 2 * diffuse * normal - light;
|
||||||
|
float3 view = normalize(CameraPosition - INPUT.positionWorld);
|
||||||
|
float specular = pow(max(0, dot(reflect, view)), Phong);
|
||||||
|
specularColor = SpecularColor * (specular * lightIntensity);
|
||||||
|
|
||||||
|
return saturate(saturate(texColor * saturate(ambientColor + diffuseColor) + specularColor) + emissionColor);
|
||||||
|
}
|
||||||
Binary file not shown.
@@ -0,0 +1,35 @@
|
|||||||
|
cbuffer MatrixBuffer
|
||||||
|
{
|
||||||
|
float4x4 WorldViewProjectionMatrix;
|
||||||
|
float4x4 WorldMatrix;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VertexInput
|
||||||
|
{
|
||||||
|
float4 position : POSITION;
|
||||||
|
float2 uv : TEXCOORD;
|
||||||
|
float4 color : COLOR;
|
||||||
|
float3 normal : NORMAL;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VertexOutput
|
||||||
|
{
|
||||||
|
float4 position : SV_Position;
|
||||||
|
float3 positionWorld : POSITION1;
|
||||||
|
float2 uv : TEXCOORD;
|
||||||
|
float4 color : COLOR;
|
||||||
|
float3 normal : NORMAL;
|
||||||
|
};
|
||||||
|
|
||||||
|
VertexOutput main(VertexInput INPUT)
|
||||||
|
{
|
||||||
|
VertexOutput OUTPUT;
|
||||||
|
|
||||||
|
OUTPUT.position = mul(INPUT.position, WorldViewProjectionMatrix);
|
||||||
|
OUTPUT.positionWorld = mul(INPUT.position, WorldMatrix).xyz;
|
||||||
|
OUTPUT.normal = mul(INPUT.normal, (float3x3)WorldMatrix);
|
||||||
|
OUTPUT.uv = INPUT.uv;
|
||||||
|
OUTPUT.color = INPUT.color;
|
||||||
|
|
||||||
|
return OUTPUT;
|
||||||
|
}
|
||||||
@@ -99,7 +99,8 @@ UINT Material::createVertexShader(ID3D11Device* pD3DDevice)
|
|||||||
|
|
||||||
// option 2: load compiled shader from disk
|
// option 2: load compiled shader from disk
|
||||||
//hr = D3DReadFileToBlob(L"ColorVertexShader.cso", &pCompiledShaderCode);
|
//hr = D3DReadFileToBlob(L"ColorVertexShader.cso", &pCompiledShaderCode);
|
||||||
hr = D3DReadFileToBlob(L"TextureVertexShader.cso", &pCompiledShaderCode);
|
//hr = D3DReadFileToBlob(L"TextureVertexShader.cso", &pCompiledShaderCode);
|
||||||
|
hr = D3DReadFileToBlob(L"LightingVertexShader.cso", &pCompiledShaderCode);
|
||||||
|
|
||||||
CheckFailed(hr, 600);
|
CheckFailed(hr, 600);
|
||||||
|
|
||||||
@@ -127,7 +128,8 @@ UINT Material::createPixelShader(ID3D11Device* pD3DDevice)
|
|||||||
ID3DBlob* pCompiledShaderCode = nullptr;
|
ID3DBlob* pCompiledShaderCode = nullptr;
|
||||||
|
|
||||||
//HRESULT hr = D3DReadFileToBlob(L"ColorPixelShader.cso", &pCompiledShaderCode);
|
//HRESULT hr = D3DReadFileToBlob(L"ColorPixelShader.cso", &pCompiledShaderCode);
|
||||||
HRESULT hr = D3DReadFileToBlob(L"TexturePixelShader.cso", &pCompiledShaderCode);
|
//HRESULT hr = D3DReadFileToBlob(L"TexturePixelShader.cso", &pCompiledShaderCode);
|
||||||
|
HRESULT hr = D3DReadFileToBlob(L"LightingPixelShader.cso", &pCompiledShaderCode);
|
||||||
CheckFailed(hr, 620);
|
CheckFailed(hr, 620);
|
||||||
|
|
||||||
hr = pD3DDevice->CreatePixelShader(pCompiledShaderCode->GetBufferPointer(), pCompiledShaderCode->GetBufferSize(), nullptr, &_pPixelShader);
|
hr = pD3DDevice->CreatePixelShader(pCompiledShaderCode->GetBufferPointer(), pCompiledShaderCode->GetBufferSize(), nullptr, &_pPixelShader);
|
||||||
@@ -140,22 +142,27 @@ UINT Material::createPixelShader(ID3D11Device* pD3DDevice)
|
|||||||
|
|
||||||
UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pCompiledVertexShaderCode)
|
UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pCompiledVertexShaderCode)
|
||||||
{
|
{
|
||||||
D3D11_INPUT_ELEMENT_DESC elements[3] = {};
|
D3D11_INPUT_ELEMENT_DESC elements[4] = {};
|
||||||
|
|
||||||
// position
|
// position
|
||||||
elements[0].SemanticName = "POSITION";
|
elements[0].SemanticName = "POSITION";
|
||||||
elements[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
elements[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||||
|
|
||||||
// color
|
// normal
|
||||||
elements[1].SemanticName = "COLOR";
|
elements[1].SemanticName = "NORMAL";
|
||||||
elements[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
|
elements[1].Format = DXGI_FORMAT_R32G32B32_FLOAT;
|
||||||
elements[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
elements[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||||
|
|
||||||
// uv
|
// color
|
||||||
elements[2].SemanticName = "TEXCOORD";
|
elements[2].SemanticName = "COLOR";
|
||||||
elements[2].Format = DXGI_FORMAT_R32G32_FLOAT;
|
elements[2].Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
|
||||||
elements[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
elements[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||||
|
|
||||||
|
// uv
|
||||||
|
elements[3].SemanticName = "TEXCOORD";
|
||||||
|
elements[3].Format = DXGI_FORMAT_R32G32_FLOAT;
|
||||||
|
elements[3].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
|
||||||
|
|
||||||
HRESULT hr = pD3DDevice->CreateInputLayout(
|
HRESULT hr = pD3DDevice->CreateInputLayout(
|
||||||
elements, // array of input layout elements
|
elements, // array of input layout elements
|
||||||
std::size(elements), // count of elements
|
std::size(elements), // count of elements
|
||||||
|
|||||||
@@ -106,6 +106,13 @@ UINT Mesh::loadMeshFromFile(ID3D11Device* pD3DDevice, string filename)
|
|||||||
// position
|
// position
|
||||||
const aiVector3D& position = pMesh->mVertices[v];
|
const aiVector3D& position = pMesh->mVertices[v];
|
||||||
|
|
||||||
|
// normal
|
||||||
|
aiVector3D normal(0.0f, 0.0f, 0.0f);
|
||||||
|
if (pMesh->HasNormals())
|
||||||
|
{
|
||||||
|
normal = pMesh->mNormals[v];
|
||||||
|
}
|
||||||
|
|
||||||
// uv
|
// uv
|
||||||
if (pMesh->HasTextureCoords(0))
|
if (pMesh->HasTextureCoords(0))
|
||||||
{
|
{
|
||||||
@@ -113,6 +120,7 @@ UINT Mesh::loadMeshFromFile(ID3D11Device* pD3DDevice, string filename)
|
|||||||
|
|
||||||
vertices.emplace_back(
|
vertices.emplace_back(
|
||||||
position.x, position.y, position.z,
|
position.x, position.y, position.z,
|
||||||
|
normal.x, normal.y, normal.z,
|
||||||
uv.x, uv.y
|
uv.x, uv.y
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,15 @@ using namespace DirectX;
|
|||||||
struct Vertex
|
struct Vertex
|
||||||
{
|
{
|
||||||
XMFLOAT3 position;
|
XMFLOAT3 position;
|
||||||
|
XMFLOAT3 normal;
|
||||||
XMFLOAT4 color;
|
XMFLOAT4 color;
|
||||||
XMFLOAT2 uv;
|
XMFLOAT2 uv;
|
||||||
|
|
||||||
Vertex() : position(0.0f, 0.0f, 0.0f), color(1.0f, 1.0f, 1.0f, 1.0f), uv(0.0f, 0.0f) {}
|
Vertex() : position(0.0f, 0.0f, 0.0f), color(1.0f, 1.0f, 1.0f, 1.0f), uv(0.0f, 0.0f), normal(0.0f, 0.0f, 0.0f) {}
|
||||||
Vertex(float x, float y, float z) : position(x, y, z), color(1.0f, 1.0f, 1.0f, 1.0f), uv(0.0f, 0.0f) {}
|
Vertex(float x, float y, float z) : position(x, y, z), color(1.0f, 1.0f, 1.0f, 1.0f), uv(0.0f, 0.0f), normal(0.0f, 0.0f, 0.0f) {}
|
||||||
Vertex(XMFLOAT3 p) : position(p), color(1.0f, 1.0f, 1.0f, 1.0f), uv(0.0f, 0.0f) {}
|
Vertex(XMFLOAT3 p) : position(p), color(1.0f, 1.0f, 1.0f, 1.0f), uv(0.0f, 0.0f), normal(0.0f, 0.0f, 0.0f) {}
|
||||||
Vertex(float x, float y, float z, float r, float g, float b) : position(x, y, z), color(r, g, b, 1.0f), uv(0.0f, 0.0f) {}
|
Vertex(float x, float y, float z, float r, float g, float b) : position(x, y, z), color(r, g, b, 1.0f), uv(0.0f, 0.0f), normal(0.0f, 0.0f, 0.0f) {}
|
||||||
Vertex(float x, float y, float z, float u, float v) : position(x, y, z), color(1.0f, 1.0f, 1.0f, 1.0f), uv(u, v) {}
|
Vertex(float x, float y, float z, float u, float v) : position(x, y, z), color(1.0f, 1.0f, 1.0f, 1.0f), uv(u, v), normal(0.0f, 0.0f, 0.0f) {}
|
||||||
|
Vertex(float x, float y, float z, float nx, float ny, float nz, float u, float v) :
|
||||||
|
position(x, y, z), normal(nx, ny, nz), uv(u, v), color(1.0f, 1.0f, 1.0f, 1.0f) {}
|
||||||
};
|
};
|
||||||
@@ -58,6 +58,19 @@ int WINAPI WinMain(
|
|||||||
// 7. create light source
|
// 7. create light source
|
||||||
Light light = {};
|
Light light = {};
|
||||||
Light::LightBuffer lightData = {};
|
Light::LightBuffer lightData = {};
|
||||||
|
|
||||||
|
// directional light
|
||||||
|
lightData.LightVector = { -1.0f, -1.0f, 1.0f };
|
||||||
|
|
||||||
|
// point light
|
||||||
|
lightData.LightPosition = { -50.0f, 400.0f, 0.0f };
|
||||||
|
lightData.LightAttenuation = { 1.0f, 0.3f, 0.1f };
|
||||||
|
|
||||||
|
lightData.LightIntensity = 1000.0f;
|
||||||
|
lightData.DiffuseColor = { 1.0f, 1.0f, 1.0f, 1.0f };
|
||||||
|
lightData.SpecularColor = { 0.2f, 0.2f, 0.2f, 1.0f };
|
||||||
|
lightData.Phong = 8.0f;
|
||||||
|
lightData.CameraPosition = camera.getCameraPosition();
|
||||||
error = light.init(d3d.getDevice(), lightData);
|
error = light.init(d3d.getDevice(), lightData);
|
||||||
CheckError(error);
|
CheckError(error);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user