Commit 809cff33 authored by chili's avatar chili
Browse files

create normal map shader

creepy normal map shitting the bed hard
parent 9f94557f
......@@ -102,6 +102,7 @@ void App::DoFrame()
ShowImguiDemoWindow();
nano.ShowWindow( "Model 1" );
nano2.ShowWindow( "Model 2" );
plane.SpawnControlWindow( wnd.Gfx() );
// present
wnd.Gfx().EndFrame();
......
cbuffer LightCBuf
{
float3 lightPos;
float3 ambient;
float3 diffuseColor;
float diffuseIntensity;
float attConst;
float attLin;
float attQuad;
};
cbuffer ObjectCBuf
{
float specularIntensity;
float specularPower;
bool normalMapEnabled;
float padding[1];
};
Texture2D tex;
Texture2D nmap;
SamplerState splr;
float4 main(float3 worldPos : Position, float3 n : Normal, float2 tc : Texcoord) : SV_Target
{
// sample normal from map if normal mapping enabled
if( normalMapEnabled )
{
n = nmap.Sample(splr, tc).xyz;
}
// fragment to light vector data
const float3 vToL = lightPos - worldPos;
const float distToL = length(vToL);
const float3 dirToL = vToL / distToL;
// attenuation
const float att = 1.0f / (attConst + attLin * distToL + attQuad * (distToL * distToL));
// diffuse intensity
const float3 diffuse = diffuseColor * diffuseIntensity * att * max(0.0f, dot(dirToL, n));
// reflected light vector
const float3 w = n * dot(vToL, n);
const float3 r = w * 2.0f - vToL;
// calculate specular intensity based on angle between viewing vector and reflection vector, narrow with power function
const float3 specular = att * (diffuseColor * diffuseIntensity) * specularIntensity * pow(max(0.0f, dot(normalize(-r), normalize(worldPos))), specularPower);
// final color
return float4(saturate((diffuse + ambient) * tex.Sample(splr, tc).rgb + specular), 1.0f);
}
\ No newline at end of file
#include "TestPlane.h"
#include "Plane.h"
#include "BindableCommon.h"
#include "imgui/imgui.h"
TestPlane::TestPlane( Graphics& gfx,float size )
{
......@@ -19,14 +20,8 @@ TestPlane::TestPlane( Graphics& gfx,float size )
auto pvsbc = pvs->GetBytecode();
AddBind( std::move( pvs ) );
AddBind( PixelShader::Resolve( gfx,"PhongPS.cso" ) );
AddBind( PixelShader::Resolve( gfx,"PhongPSNormalMap.cso" ) );
struct PSMaterialConstant
{
float specularIntensity = 0.1f;
float specularPower = 20.0f;
float padding[2];
} pmc;
AddBind( PixelConstantBuffer<PSMaterialConstant>::Resolve( gfx,pmc,1u ) );
AddBind( InputLayout::Resolve( gfx,model.vertices.GetLayout(),pvsbc ) );
......@@ -53,3 +48,29 @@ DirectX::XMMATRIX TestPlane::GetTransformXM() const noexcept
return DirectX::XMMatrixRotationRollPitchYaw( roll,pitch,yaw ) *
DirectX::XMMatrixTranslation( pos.x,pos.y,pos.z );
}
void TestPlane::SpawnControlWindow( Graphics& gfx ) noexcept
{
if( ImGui::Begin( "Plane" ) )
{
ImGui::Text( "Position" );
ImGui::SliderFloat( "X",&pos.x,-80.0f,80.0f,"%.1f" );
ImGui::SliderFloat( "Y",&pos.y,-80.0f,80.0f,"%.1f" );
ImGui::SliderFloat( "Z",&pos.z,-80.0f,80.0f,"%.1f" );
ImGui::Text( "Orientation" );
ImGui::SliderAngle( "Roll",&roll,-180.0f,180.0f );
ImGui::SliderAngle( "Pitch",&pitch,-180.0f,180.0f );
ImGui::SliderAngle( "Yaw",&yaw,-180.0f,180.0f );
ImGui::Text( "Shading" );
bool changed0 = ImGui::SliderFloat( "Spec. Int.",&pmc.specularIntensity,0.0f,1.0f );
bool changed1 = ImGui::SliderFloat( "Spec. Power",&pmc.specularPower,0.0f,100.0f );
bool checkState = pmc.normalMappingEnabled == TRUE;
bool changed2 = ImGui::Checkbox( "Enable Normal Map",&checkState );
pmc.normalMappingEnabled = checkState ? TRUE : FALSE;
if( changed0 || changed1 || changed2 )
{
QueryBindable<Bind::PixelConstantBuffer<PSMaterialConstant>>()->Update( gfx,pmc );
}
}
ImGui::End();
}
......@@ -8,7 +8,15 @@ public:
void SetPos( DirectX::XMFLOAT3 pos ) noexcept;
void SetRotation( float roll,float pitch,float yaw ) noexcept;
DirectX::XMMATRIX GetTransformXM() const noexcept override;
void SpawnControlWindow( Graphics& gfx ) noexcept;
private:
struct PSMaterialConstant
{
float specularIntensity = 0.1f;
float specularPower = 20.0f;
BOOL normalMappingEnabled = TRUE;
float padding[1];
} pmc;
DirectX::XMFLOAT3 pos = { 1.0f,1.0f,1.0f };
float roll = 0.0f;
float pitch = 0.0f;
......
......@@ -194,6 +194,14 @@
<None Include="DXTrace.inl" />
</ItemGroup>
<ItemGroup>
<FxCompile Include="PhongPSNormalMap.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">4.0</ShaderModel>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|x64'">4.0</ShaderModel>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="PhongPSSpecMap.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">4.0</ShaderModel>
......
......@@ -151,7 +151,7 @@
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="TestPlane.cpp">
<Filter>Source Files</Filter>
<Filter>Source Files\Drawable</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
......@@ -346,5 +346,8 @@
<FxCompile Include="PhongPSSpecMap.hlsl">
<Filter>Shader</Filter>
</FxCompile>
<FxCompile Include="PhongPSNormalMap.hlsl">
<Filter>Shader</Filter>
</FxCompile>
</ItemGroup>
</Project>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment