1
0
mirror of https://github.com/ncblakely/GiantsTools synced 2024-11-23 22:55:37 +01:00

Code cleanup

This commit is contained in:
Nick Blakely 2020-11-03 01:46:00 -08:00
parent 58fc85c206
commit 7aee9acc9c
9 changed files with 248 additions and 797 deletions

2
Shaders/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
fxo/
reference/

View File

@ -95,7 +95,7 @@
</CustomBuild>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="fx\FFP.fx">
<CustomBuild Include="fx\Object.fx">
<FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">"$(JUN10SDKUTILPATH)\fxc.exe" /LD /Zi /Tfx_2_0 %(FullPath) /Fo fxo\%(Filename).fxo</Command>
@ -114,6 +114,13 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
</FxCompile>
</ItemGroup>
<ItemGroup>
<None Include="FFP_orig.fx" />
<None Include="fx\Lighting.fxh" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="fx\Constants.fxh" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<CustomBuild Include="fx\LandBump.fx" />
<CustomBuild Include="fx\Ocean.fx" />
<CustomBuild Include="fx\Sky.fx" />
<CustomBuild Include="fx\Water.fx" />
<CustomBuild Include="fx\Object.fx" />
</ItemGroup>
<ItemGroup>
<Filter Include="Include">
<UniqueIdentifier>{026956f1-0f27-4b3d-80ab-f81eafce58f5}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="fx\Constants.fxh">
<Filter>Include</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="fx\Lighting.fxh">
<Filter>Include</Filter>
</None>
</ItemGroup>
</Project>

View File

@ -1,325 +0,0 @@
//======================================================================================//
// filename: FixedFuncShader.fx //
// //
// author: Pedro V. Sander //
// ATI Research, Inc. //
// 3D Application Research Group //
// email: psander@ati.com //
// //
// Description: A programmable shader that emulates the fixed function pipeline //
// //
//======================================================================================//
// (C) 2003 ATI Research, Inc. All rights reserved. //
//======================================================================================//
#define PI 3.14f
//this file contains light, fog, and texture types
// (originally a include, inserted here)
#define FOG_TYPE_NONE 0
#define FOG_TYPE_EXP 1
#define FOG_TYPE_EXP2 2
#define FOG_TYPE_LINEAR 3
#define FOG_NUM_TYPES 4
#define TEX_TYPE_NONE 0
#define TEX_TYPE_CUBEMAP 1
#define TEX_NUM_TYPES 2
#define TEXGEN_TYPE_NONE 0
#define TEXGEN_TYPE_CAMERASPACENORMAL 1
#define TEXGEN_TYPE_CAMERASPACEPOSITION 2
#define TEXGEN_TYPE_CAMERASPACEREFLECTIONVECTOR 3
#define TEXGEN_NUM_TYPES 4
#define MAX_DIRECTIONAL_LIGHTS 3
// Structs and variables with default values
//fog settings
int iFogType = FOG_TYPE_LINEAR;
float4 vFogColor = float4(0.0f, 0.0f, 0.0f, 0.0f);
float fFogStart = 0;
float fFogEnd = 8845.00000;
float fFogDensity = .02f;
bool bFogRange : register(b4) = true;
int iTexType = TEX_TYPE_NONE;
int iTexGenType = TEXGEN_TYPE_NONE;
float4 DirectionalLightAmbient[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightAmbient;
float4 DirectionalLightDiffuse[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightDiffuse;
float3 DirectionalLightDirection[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightDirection;
bool DirectionalLightEnabled[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightEnabled;
float4 DirectionalLightSpecular[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightSpecular;
float3 g_CameraPosition : CameraPosition;
struct Material
{
float4 Diffuse;
float4 Ambient;
float4 Specular;
float4 Emissive;
float Power;
};
Material g_Material : Material;
//transformation matrices
float4x4 matWorldViewProjection : WorldViewProjection;
float4x4 matWorldView : WorldView;
float4x4 matWorld : World;
float4x4 matView : View;
//function output structures
struct VS_OUTPUT
{
float4 Pos : POSITION;
float4 Color : COLOR0;
float4 ColorSpec : COLOR1;
float4 Tex0 : TEXCOORD0;
float Fog : FOG;
};
struct COLOR_PAIR
{
float4 Color : COLOR0;
float4 ColorSpec : COLOR1;
};
float4 CalculateAmbientLight()
{
float4 ambient = 0;
for (int i = 0; i < MAX_DIRECTIONAL_LIGHTS; i++)
{
if (DirectionalLightEnabled[i])
{
ambient += DirectionalLightAmbient[i];
}
}
return ambient;
}
COLOR_PAIR DoDirectionalLight(float3 worldPos, float3 N, int i)
{
COLOR_PAIR Out = (COLOR_PAIR)0;
float NdotL = dot(N, -DirectionalLightDirection[i]);
if (NdotL > 0.f)
{
//compute diffuse color
Out.Color += max(0, NdotL * DirectionalLightDiffuse[i]);
if (g_Material.Power > 0)
{
float3 toEye = g_CameraPosition.xyz - worldPos;
toEye = normalize(toEye);
float3 halfway = normalize(toEye + -DirectionalLightDirection[i]);
float NDotH = saturate(dot(halfway, N));
float4 spec = DirectionalLightSpecular[i];
Out.ColorSpec += spec * pow(NDotH, g_Material.Power);
}
}
return Out;
}
COLOR_PAIR ComputeLighting(float3 worldPos, float3 N)
{
COLOR_PAIR finalResult = (COLOR_PAIR)0;
for (int i = 0; i < MAX_DIRECTIONAL_LIGHTS; i++)
{
COLOR_PAIR lightResult = (COLOR_PAIR)0;
if (DirectionalLightEnabled[i])
{
lightResult = DoDirectionalLight(worldPos, N, i);
}
finalResult.Color += lightResult.Color;
finalResult.ColorSpec += lightResult.ColorSpec;
}
float4 ambient = g_Material.Ambient * CalculateAmbientLight();
float4 diffuse = g_Material.Diffuse * finalResult.Color;
float4 specular = g_Material.Specular * finalResult.ColorSpec;
finalResult.Color = saturate(ambient + diffuse);
finalResult.ColorSpec = saturate(specular);
return finalResult;
}
//-----------------------------------------------------------------------------
// Name: DoPointLight()
// Desc: Point light computation
//-----------------------------------------------------------------------------
//COLOR_PAIR DoPointLight(float4 vPosition, float3 N, float3 V, int i)
//{
// float3 L = mul((float3x3)matViewIT, normalize((lights[i].vPos-(float3)mul(matWorld,vPosition))));
// COLOR_PAIR Out;
// float NdotL = dot(N, L);
// Out.Color = lights[i].vAmbient;
// Out.ColorSpec = 0;
// float fAtten = 1.f;
// if(NdotL >= 0.f)
// {
// //compute diffuse color
// Out.Color += NdotL * lights[i].vDiffuse;
//
// //add specular component
// if(bSpecular)
// {
// float3 H = normalize(L + V); //half vector
// Out.ColorSpec = pow(max(0, dot(H, N)), fMaterialPower) * lights[i].vSpecular;
// }
//
// float LD = length(lights[i].vPos-(float3)mul(matWorld,vPosition));
// if(LD > lights[i].fRange)
// {
// fAtten = 0.f;
// }
// else
// {
// fAtten *= 1.f/(lights[i].vAttenuation.x + lights[i].vAttenuation.y*LD + lights[i].vAttenuation.z*LD*LD);
// }
// Out.Color *= fAtten;
// Out.ColorSpec *= fAtten;
// }
// return Out;
//}
//-----------------------------------------------------------------------------
// Name: vs_main()
// Desc: The vertex shader
//-----------------------------------------------------------------------------
VS_OUTPUT vs_main (float4 vPosition : POSITION0,
float3 vNormal : NORMAL0,
float2 tc : TEXCOORD0)
{
VS_OUTPUT Out = (VS_OUTPUT) 0;
vNormal = normalize(vNormal);
Out.Pos = mul(vPosition, matWorldViewProjection);
//automatic texture coordinate generation
Out.Tex0.xy = tc;
/*Out.Tex0 = float4((2.f * dot(V,N) * N - V) * (iTexGenType == TEXGEN_TYPE_CAMERASPACEREFLECTIONVECTOR)
+ N * (iTexGenType == TEXGEN_TYPE_CAMERASPACENORMAL)
+ P * (iTexGenType == TEXGEN_TYPE_CAMERASPACEPOSITION), 0);
Out.Tex0.xy += tc * (iTexGenType == TEXGEN_TYPE_NONE);*/
//light computation
//directional lights
float4 worldPos = mul(vPosition, matWorld); //position in view space
float3 N = mul(vNormal, (float3x3)matWorld);
COLOR_PAIR lighting = ComputeLighting(worldPos, N);
////point lights
//for(int i = 0; i < iLightPointNum; i++)
//{
// COLOR_PAIR ColOut = DoPointLight(vPosition, N, V, i+iLightPointIni);
// Out.Color += ColOut.Color;
// Out.ColorSpec += ColOut.ColorSpec;
//}
//apply material color
Out.Color = lighting.Color;
Out.ColorSpec = lighting.ColorSpec;
//apply fog
float4 P = mul(matWorldView, vPosition); //position in view space
float d;
if(bFogRange)
d = length(P);
else
d = P.z;
Out.Fog = 1.f * (iFogType == FOG_TYPE_NONE)
+ 1.f/exp(d * fFogDensity) * (iFogType == FOG_TYPE_EXP)
+ 1.f/exp(pow(d * fFogDensity, 2)) * (iFogType == FOG_TYPE_EXP2)
+ saturate((fFogEnd - d)/(fFogEnd - fFogStart)) * (iFogType == FOG_TYPE_LINEAR);
return Out;
}
// Techniques
//the technique for the programmable shader (simply sets the vertex shader)
technique basic_with_shader
{
pass P0
{
VertexShader = compile vs_2_0 vs_main();
}
}
TEXTURE tex1;
TEXTURE tex2;
//Sampler for the diff mode
sampler DiffSampler1 = sampler_state
{
Texture = (tex1);
MinFilter = Point;
MagFilter = Point;
MipFilter = Point;
AddressU = Wrap;
AddressV = Wrap;
AddressW = Wrap;
MaxAnisotropy = 8;
};
sampler DiffSampler2 = sampler_state
{
Texture = (tex2);
MinFilter = Point;
MagFilter = Point;
MipFilter = Point;
AddressU = Wrap;
AddressV = Wrap;
AddressW = Wrap;
MaxAnisotropy = 8;
};
bool bDiffSensitivity = false;
//-----------------------------------------------------------------------------
// Name: ps_diff()
// Desc: Pixel shader for the diff mode
// Tiny errors: green. Larger errors: yellow to red.
//-----------------------------------------------------------------------------
float4 ps_diff (float2 tcBase : TEXCOORD0) : COLOR
{
float E = length(tex2D(DiffSampler1, tcBase) - tex2D(DiffSampler2, tcBase))/sqrt(3);
float4 C = float4(0.f,0.f,0.f,E);
if(E > 0.f)
{
if(E <= 1.f/255.f)
{
if(bDiffSensitivity)
{
C = float4(0.f,1.f,0.f,E);
}
}
else
{
C = lerp(float4(1.f,1.f,0.f,E), float4(1.f,0.f,0.f,E),E);
}
}
return C;
}
//technique for the diff mode
technique technique_diff
{
pass P0
{
PixelShader = compile ps_2_0 ps_diff();
}
}

View File

@ -1,470 +0,0 @@
// FixedFuncEMU.fx
// Copyright (c) 2005 Microsoft Corporation. All rights reserved.
//
struct VSSceneIn
{
float3 pos : POSITION; //position of the particle
float3 norm : NORMAL; //velocity of the particle
float2 tex : TEXTURE0; //tex coords
};
struct VSSceneOut
{
float4 pos : SV_Position; //position
float2 tex : TEXTURE0; //texture coordinate
float3 wPos : TEXTURE1; //world space pos
float3 wNorm : TEXTURE2; //world space normal
float4 colorD : COLOR0; //color for gouraud and flat shading
float4 colorS : COLOR1; //color for specular
float fogDist : FOGDISTANCE; //distance used for fog calculations
float3 planeDist : SV_ClipDistance0; //clip distance for 3 planes
};
struct PSSceneIn
{
float4 pos : SV_Position; //position
float2 tex : TEXTURE0; //texture coordinate
float3 wPos : TEXTURE1; //world space pos
float3 wNorm : TEXTURE2; //world space normal
float4 colorD : COLOR0; //color for gouraud and flat shading
float4 colorS : COLOR1; //color for specular
float fogDist : FOGDISTANCE; //distance used for fog calculations
};
struct Light
{
float4 Position;
float4 Diffuse;
float4 Specular;
float4 Ambient;
float4 Atten;
};
#define FOGMODE_NONE 0
#define FOGMODE_LINEAR 1
#define FOGMODE_EXP 2
#define FOGMODE_EXP2 3
#define E 2.71828
cbuffer cbLights
{
float4 g_clipplanes[3];
Light g_lights[8];
};
cbuffer cbPerFrame
{
float4x4 g_mWorld;
float4x4 g_mView;
float4x4 g_mProj;
float4x4 g_mInvProj;
float4x4 g_mLightViewProj;
};
cbuffer cbPerTechnique
{
bool g_bEnableLighting = true;
bool g_bEnableClipping = true;
bool g_bPointScaleEnable = false;
float g_pointScaleA;
float g_pointScaleB;
float g_pointScaleC;
float g_pointSize;
//fog params
int g_fogMode = FOGMODE_NONE;
float g_fogStart;
float g_fogEnd;
float g_fogDensity;
float4 g_fogColor;
};
cbuffer cbPerViewChange
{
//viewport params
float g_viewportHeight;
float g_viewportWidth;
float g_nearPlane;
};
cbuffer cbImmutable
{
float3 g_positions[4] =
{
float3( -0.5, 0.5, 0 ),
float3( 0.5, 0.5, 0 ),
float3( -0.5, -0.5, 0 ),
float3( 0.5, -0.5, 0 ),
};
};
Texture2D g_txDiffuse;
Texture2D g_txProjected;
SamplerState g_samLinear
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Clamp;
AddressV = Clamp;
};
DepthStencilState DisableDepth
{
DepthEnable = FALSE;
DepthWriteMask = ZERO;
};
DepthStencilState EnableDepth
{
DepthEnable = TRUE;
DepthWriteMask = ALL;
};
struct ColorsOutput
{
float4 Diffuse;
float4 Specular;
};
// cameraPos = WorldView
// worldNormal = inputNormal * World
// worldPos = inputPos * World
ColorsOutput CalcLighting( float3 worldNormal, float3 worldPos, float3 cameraPos )
{
ColorsOutput output = (ColorsOutput)0.0;
for(int i=0; i<8; i++)
{
float3 toLight = g_lights[i].Position.xyz - worldPos;
float lightDist = length( toLight );
float fAtten = 1.0/dot( g_lights[i].Atten, float4(1,lightDist,lightDist*lightDist,0) );
float3 lightDir = normalize( toLight );
float3 halfAngle = normalize( normalize(-cameraPos) + lightDir );
output.Diffuse += max(0,dot( lightDir, worldNormal ) * g_lights[i].Diffuse * fAtten) + g_lights[i].Ambient;
output.Specular += max(0,pow( dot( halfAngle, worldNormal ), 64 ) * g_lights[i].Specular * fAtten );
}
return output;
}
//
// VS for emulating fixed function pipeline
//
VSSceneOut VSScenemain(VSSceneIn input)
{
VSSceneOut output = (VSSceneOut)0.0;
//output our final position in clipspace
float4 worldPos = mul( float4( input.pos, 1 ), g_mWorld );
float4 cameraPos = mul( worldPos, g_mView ); //Save cameraPos for fog calculations
output.pos = mul( cameraPos, g_mProj );
//save world pos for later
output.wPos = worldPos;
//save the fog distance for later
output.fogDist = cameraPos.z;
//find our clipping planes (fixed function clipping is done in world space)
if( g_bEnableClipping )
{
worldPos.w = 1;
//calc the distance from the 3 clipping planes
output.planeDist.x = dot( worldPos, g_clipplanes[0] );
output.planeDist.y = dot( worldPos, g_clipplanes[1] );
output.planeDist.z = dot( worldPos, g_clipplanes[2] );
}
else
{
output.planeDist.x = 1;
output.planeDist.y = 1;
output.planeDist.z = 1;
}
//do gouraud lighting
if( g_bEnableLighting )
{
float3 worldNormal = normalize( mul( input.norm, (float3x3)g_mWorld ) );
output.wNorm = worldNormal;
ColorsOutput cOut = CalcLighting( worldNormal, worldPos, cameraPos );
output.colorD = cOut.Diffuse;
output.colorS = cOut.Specular;
}
else
{
output.colorD = float4(1,1,1,1);
}
//propogate texture coordinate
output.tex = input.tex;
return output;
}
//
// VS for rendering in screen space
//
PSSceneIn VSScreenSpacemain(VSSceneIn input)
{
PSSceneIn output = (PSSceneIn)0.0;
//output our final position
output.pos.x = (input.pos.x / (g_viewportWidth/2.0)) -1;
output.pos.y = -(input.pos.y / (g_viewportHeight/2.0)) +1;
output.pos.z = input.pos.z;
output.pos.w = 1;
//propogate texture coordinate
output.tex = input.tex;
output.colorD = float4(1,1,1,1);
return output;
}
//
// GS for flat shaded rendering
//
[maxvertexcount(3)]
void GSFlatmain( triangle VSSceneOut input[3], inout TriangleStream<VSSceneOut> FlatTriStream )
{
VSSceneOut output;
//
// Calculate the face normal
//
float3 faceEdgeA = input[1].wPos - input[0].wPos;
float3 faceEdgeB = input[2].wPos - input[0].wPos;
//
// Cross product
//
float3 faceNormal = cross(faceEdgeA, faceEdgeB);
//
//calculate the face center
//
float3 faceCenter = (input[0].wPos + input[1].wPos + input[2].wPos)/3.0;
//find world pos and camera pos
float4 worldPos = float4( faceCenter, 1 );
float4 cameraPos = mul( worldPos, g_mView );
//do shading
float3 worldNormal = normalize( faceNormal );
ColorsOutput cOut = CalcLighting( worldNormal, worldPos, cameraPos );
for(int i=0; i<3; i++)
{
output = input[i];
output.colorD = cOut.Diffuse;
output.colorS = cOut.Specular;
FlatTriStream.Append( output );
}
FlatTriStream.RestartStrip();
}
//
// GS for point rendering
//
[maxvertexcount(12)]
void GSPointmain( triangle VSSceneOut input[3], inout TriangleStream<VSSceneOut> PointTriStream )
{
VSSceneOut output;
//
// Calculate the point size
//
//float fSizeX = (g_pointSize/g_viewportWidth)/4.0;
float fSizeY = (g_pointSize/g_viewportHeight)/4.0;
float fSizeX = fSizeY;
for(int i=0; i<3; i++)
{
output = input[i];
//find world pos and camera pos
float4 worldPos = float4(input[i].wPos,1);
float4 cameraPos = mul( worldPos, g_mView );
//find our size
if( g_bPointScaleEnable )
{
float dEye = length( cameraPos.xyz );
fSizeX = fSizeY = g_viewportHeight * g_pointSize *
sqrt( 1.0f/( g_pointScaleA + g_pointScaleB*dEye + g_pointScaleC*(dEye*dEye) ) );
}
//do shading
if(g_bEnableLighting)
{
float3 worldNormal = input[i].wNorm;
ColorsOutput cOut = CalcLighting( worldNormal, worldPos, cameraPos );
output.colorD = cOut.Diffuse;
output.colorS = cOut.Specular;
}
else
{
output.colorD = float4(1,1,1,1);
}
output.tex = input[i].tex;
//
// Emit two new triangles
//
for(int i=0; i<4; i++)
{
float4 outPos = mul( worldPos, g_mView );
output.pos = mul( outPos, g_mProj );
float zoverNear = (outPos.z)/g_nearPlane;
float4 posSize = float4( g_positions[i].x*fSizeX*zoverNear,
g_positions[i].y*fSizeY*zoverNear,
0,
0 );
output.pos += posSize;
PointTriStream.Append(output);
}
PointTriStream.RestartStrip();
}
}
//
// Calculates fog factor based upon distance
//
float CalcFogFactor( float d )
{
float fogCoeff = 1.0;
if( FOGMODE_LINEAR == g_fogMode )
{
fogCoeff = (g_fogEnd - d)/(g_fogEnd - g_fogStart);
}
else if( FOGMODE_EXP == g_fogMode )
{
fogCoeff = 1.0 / pow( E, d*g_fogDensity );
}
else if( FOGMODE_EXP2 == g_fogMode )
{
fogCoeff = 1.0 / pow( E, d*d*g_fogDensity*g_fogDensity );
}
return clamp( fogCoeff, 0, 1 );
}
//
// PS for rendering with clip planes
//
float4 PSScenemain(PSSceneIn input) : SV_Target
{
//calculate the fog factor
float fog = CalcFogFactor( input.fogDist );
//calculate the color based off of the normal, textures, etc
float4 normalColor = g_txDiffuse.Sample( g_samLinear, input.tex ) * input.colorD + input.colorS;
//calculate the color from the projected texture
float4 cookieCoord = mul( float4(input.wPos,1), g_mLightViewProj );
//since we don't have texldp, we must perform the w divide ourselves befor the texture lookup
cookieCoord.xy = 0.5 * cookieCoord.xy / cookieCoord.w + float2( 0.5, 0.5 );
float4 cookieColor = float4(0,0,0,0);
if( cookieCoord.z > 0 )
cookieColor = g_txProjected.Sample( g_samLinear, cookieCoord.xy );
//for standard light-modulating effects just multiply normalcolor and coookiecolor
normalColor += cookieColor;
return fog * normalColor + (1.0 - fog)*g_fogColor;
}
//
// PS for rendering with alpha test
//
float4 PSAlphaTestmain(PSSceneIn input) : SV_Target
{
float4 color = g_txDiffuse.Sample( g_samLinear, input.tex ) * input.colorD;
if( color.a < 0.5 )
discard;
return color;
}
//
// RenderSceneGouraud - renders gouraud-shaded primitives
//
technique10 RenderSceneGouraud
{
pass p0
{
SetVertexShader( CompileShader( vs_4_0, VSScenemain() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );
SetDepthStencilState( EnableDepth, 0 );
}
}
//
// RenderSceneFlat - renders flat-shaded primitives
//
technique10 RenderSceneFlat
{
pass p0
{
SetVertexShader( CompileShader( vs_4_0, VSScenemain() ) );
SetGeometryShader( CompileShader( gs_4_0, GSFlatmain() ) );
SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );
SetDepthStencilState( EnableDepth, 0 );
}
}
//
// RenderScenePoint - replaces d3dfill_point
//
technique10 RenderScenePoint
{
pass p0
{
SetVertexShader( CompileShader( vs_4_0, VSScenemain() ) );
SetGeometryShader( CompileShader( gs_4_0, GSPointmain() ) );
SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );
SetDepthStencilState( EnableDepth, 0 );
}
}
//
// RenderScreneSpace - shows how to render something in screenspace
//
technique10 RenderScreenSpaceAlphaTest
{
pass p0
{
SetVertexShader( CompileShader( vs_4_0, VSScreenSpacemain() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, PSAlphaTestmain() ) );
SetDepthStencilState( DisableDepth, 0 );
}
}
//
// RenderScreneSpace - shows how to render something in screenspace
//
technique10 RenderTextureOnly
{
pass p0
{
SetVertexShader( CompileShader( vs_4_0, VSScenemain() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );
SetDepthStencilState( EnableDepth, 0 );
}
}

View File

@ -4,7 +4,7 @@
// Land bumpmapping and lighting shader.
//--------------------------------------------------------------------------------------
#define MAX_LIGHTS 4
#include "Constants.fxh"
float4x4 g_mWorldViewProjection : WorldViewProjection;
float4x4 g_World : World;

192
Shaders/fx/Object.fx Normal file
View File

@ -0,0 +1,192 @@
#include "../fxh/Constants.fxh"
#include "../fxh/Lighting.fxh"
// Lighting state
float4 g_DirectionalLightAmbient[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightAmbient;
float4 g_DirectionalLightDiffuse[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightDiffuse;
float3 g_DirectionalLightDirection[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightDirection;
bool g_DirectionalLightEnabled[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightEnabled;
float4 g_DirectionalLightSpecular[MAX_DIRECTIONAL_LIGHTS] : DirectionalLightSpecular;
// Camera
float3 g_CameraPosition : CameraPosition;
// Current material
Material g_Material : Material;
// Transforms
float4x4 g_WorldViewProjection : WorldViewProjection;
float4x4 g_World : World;
struct VS_OUTPUT
{
float4 Pos : POSITION;
float4 Diffuse : COLOR0;
float4 Specular : COLOR1;
float4 Tex0 : TEXCOORD0;
};
float4 CalculateAmbientLight()
{
float4 ambient = 0;
for (int i = 0; i < MAX_DIRECTIONAL_LIGHTS; i++)
{
if (g_DirectionalLightEnabled[i])
{
ambient += g_DirectionalLightAmbient[i];
}
}
return ambient;
}
float4 CalculateDiffuse(float3 N, float3 L, float4 diffuseColor)
{
float NDotL = dot(N, L);
float4 finalColor = 0;
if (NDotL > 0.0f)
{
finalColor = max(0, NDotL * diffuseColor);
}
return finalColor;
}
float4 CalculateSpecular(float3 worldPos, float3 N, float3 L, float4 specularColor)
{
float4 finalColor = 0;
if (g_Material.Power > 0)
{
float3 toEye = normalize(g_CameraPosition.xyz - worldPos);
float3 halfway = normalize(toEye + L);
float NDotH = saturate(dot(halfway, N));
finalColor = max(0, pow(NDotH, g_Material.Power) * specularColor);
}
return finalColor;
}
Lighting DoDirectionalLight(float3 worldPos, float3 N, int i)
{
Lighting Out;
Out.Diffuse = CalculateDiffuse(
N,
-g_DirectionalLightDirection[i],
g_DirectionalLightDiffuse[i]);
Out.Specular = CalculateSpecular(
worldPos,
N,
-g_DirectionalLightDirection[i],
g_DirectionalLightSpecular[i]);
return Out;
}
Lighting ComputeLighting(float3 worldPos, float3 N)
{
Lighting finalLighting = (Lighting)0;
for (int i = 0; i < MAX_DIRECTIONAL_LIGHTS; i++)
{
if (g_DirectionalLightEnabled[i])
{
Lighting lighting = DoDirectionalLight(worldPos, N, i);
finalLighting.Diffuse += lighting.Diffuse;
finalLighting.Specular += lighting.Specular;
}
}
float4 ambient = g_Material.Ambient * CalculateAmbientLight();
float4 diffuse = g_Material.Diffuse * finalLighting.Diffuse;
float4 specular = g_Material.Specular * finalLighting.Specular;
finalLighting.Diffuse = saturate(ambient + diffuse);
finalLighting.Specular = saturate(specular);
return finalLighting;
}
//-----------------------------------------------------------------------------
// Name: DoPointLight()
// Desc: Point light computation
//-----------------------------------------------------------------------------
//COLOR_PAIR DoPointLight(float4 vPosition, float3 N, float3 V, int i)
//{
// float3 L = mul((float3x3)matViewIT, normalize((lights[i].vPos-(float3)mul(matWorld,vPosition))));
// COLOR_PAIR Out;
// float NdotL = dot(N, L);
// Out.Color = lights[i].vAmbient;
// Out.Specular = 0;
// float fAtten = 1.f;
// if(NdotL >= 0.f)
// {
// //compute diffuse color
// Out.Color += NdotL * lights[i].vDiffuse;
//
// //add specular component
// if(bSpecular)
// {
// float3 H = normalize(L + V); //half vector
// Out.Specular = pow(max(0, dot(H, N)), fMaterialPower) * lights[i].vSpecular;
// }
//
// float LD = length(lights[i].vPos-(float3)mul(matWorld,vPosition));
// if(LD > lights[i].fRange)
// {
// fAtten = 0.f;
// }
// else
// {
// fAtten *= 1.f/(lights[i].vAttenuation.x + lights[i].vAttenuation.y*LD + lights[i].vAttenuation.z*LD*LD);
// }
// Out.Color *= fAtten;
// Out.Specular *= fAtten;
// }
// return Out;
//}
VS_OUTPUT VSMain (
float4 vPosition : POSITION0,
float3 vNormal : NORMAL0,
float2 tc : TEXCOORD0)
{
VS_OUTPUT Out = (VS_OUTPUT)0;
vNormal = normalize(vNormal);
Out.Pos = mul(vPosition, g_WorldViewProjection);
//automatic texture coordinate generation
Out.Tex0.xy = tc;
//directional lights
float4 worldPos = mul(vPosition, g_World); //position in view space
float3 normal = mul(vNormal, (float3x3)g_World);
Lighting lighting = ComputeLighting(worldPos, normal);
////point lights
//for(int i = 0; i < iLightPointNum; i++)
//{
// COLOR_PAIR ColOut = DoPointLight(vPosition, N, V, i+iLightPointIni);
// Out.Color += ColOut.Color;
// Out.Specular += ColOut.Specular;
//}
Out.Diffuse = lighting.Diffuse;
Out.Specular = lighting.Specular;
return Out;
}
float4 ps_main(VS_OUTPUT input) : COLOR0
{
return input.Diffuse;
}
technique TexturedVertexLighting
{
pass P0
{
//PixelShader = compile ps_2_0 ps_main();
VertexShader = compile vs_2_0 VSMain();
}
}

View File

@ -0,0 +1,4 @@
#pragma once
#define MAX_DIRECTIONAL_LIGHTS 3
#define MAX_LIGHTS 4

16
Shaders/fxh/Lighting.fxh Normal file
View File

@ -0,0 +1,16 @@
#pragma once
struct Material
{
float4 Diffuse;
float4 Ambient;
float4 Specular;
float4 Emissive;
float Power;
};
struct Lighting
{
float4 Diffuse : COLOR0;
float4 Specular : COLOR1;
};