I have an old game source from 2004, the game runs DirectX9, and I want to improve the graphics.
I downloaded NVIDIA FX Composer 2.5, and I created a “Phong_bump_reflect.fx
”.
I want to add the effect to my terrain, is there any way ?
I tried like this, on the terrain render function:
void CMapOutdoor::__RenderTerrain_RenderHardwareTransformPatch()
{
m_pShader = NULL;
ms_lpd3dDevice = NULL;
if (D3DXCreateEffectFromFileA(ms_lpd3dDevice, "Phong_bump_relect.fx", 0, 0, 0, 0, &m_pShader, 0) != S_OK)
assert(!"Failed to create shader !");
DWORD dwFogColor;
float fFogFarDistance;
float fFogNearDistance;
if (mc_pEnvironmentData)
{
dwFogColor = mc_pEnvironmentData->FogColor;
#ifdef ENABLE_MODE_HD
if (ActiveHD)
{
fFogNearDistance = mc_pEnvironmentData->GetFogNearDistance() * 5;
fFogFarDistance = mc_pEnvironmentData->GetFogFarDistance() * 5;
}
else
{
fFogNearDistance = mc_pEnvironmentData->GetFogNearDistance();
fFogFarDistance = mc_pEnvironmentData->GetFogFarDistance();
}
#else
fFogNearDistance = mc_pEnvironmentData->GetFogNearDistance();
fFogFarDistance = mc_pEnvironmentData->GetFogFarDistance();
#endif
}
else
{
dwFogColor = 0xffffffff;
fFogNearDistance = 5000.0f;
fFogFarDistance = 10000.0f;
}
//////////////////////////////////////////////////////////////////////////
// Render State & TextureStageState
STATEMANAGER.SaveTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);
STATEMANAGER.SaveTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
STATEMANAGER.SaveTextureStageState(1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);
STATEMANAGER.SaveTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
STATEMANAGER.SaveRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
STATEMANAGER.SaveRenderState(D3DRS_ALPHATESTENABLE, TRUE);
STATEMANAGER.SaveRenderState(D3DRS_ALPHAREF, 0x00000000);
STATEMANAGER.SaveRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
STATEMANAGER.SaveRenderState(D3DRS_TEXTUREFACTOR, dwFogColor);
STATEMANAGER.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
STATEMANAGER.SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT);
STATEMANAGER.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
STATEMANAGER.SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
STATEMANAGER.SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
#ifdef DIRECTX9
STATEMANAGER.SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
STATEMANAGER.SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
#else
STATEMANAGER.SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP);
STATEMANAGER.SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP);
#endif
STATEMANAGER.SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_CURRENT);
STATEMANAGER.SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
STATEMANAGER.SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
STATEMANAGER.SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
#ifdef DIRECTX9
STATEMANAGER.SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
STATEMANAGER.SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
#else
STATEMANAGER.SetTextureStageState(1, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
STATEMANAGER.SetTextureStageState(1, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);
#endif
#ifdef WORLD_EDITOR
if (GetAsyncKeyState(VK_CAPITAL))
{
CSpeedTreeWrapper::ms_bSelfShadowOn = false;
STATEMANAGER.SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_GAUSSIANCUBIC);
STATEMANAGER.SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_GAUSSIANCUBIC);
STATEMANAGER.SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_GAUSSIANCUBIC);
STATEMANAGER.SetTextureStageState(1, D3DTSS_MINFILTER, D3DTEXF_GAUSSIANCUBIC);
STATEMANAGER.SetTextureStageState(1, D3DTSS_MAGFILTER, D3DTEXF_GAUSSIANCUBIC);
STATEMANAGER.SetTextureStageState(1, D3DTSS_MIPFILTER, D3DTEXF_GAUSSIANCUBIC);
}
else
{
CSpeedTreeWrapper::ms_bSelfShadowOn = true;
STATEMANAGER.SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
STATEMANAGER.SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
STATEMANAGER.SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
STATEMANAGER.SetTextureStageState(1, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
STATEMANAGER.SetTextureStageState(1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
STATEMANAGER.SetTextureStageState(1, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
}
#else
CSpeedTreeWrapper::ms_bSelfShadowOn = true;
STATEMANAGER.SetBestFiltering(0);
STATEMANAGER.SetBestFiltering(1);
#endif
m_matWorldForCommonUse._41 = 0.0f;
m_matWorldForCommonUse._42 = 0.0f;
STATEMANAGER.SetTransform(D3DTS_WORLD, &m_matWorldForCommonUse);
STATEMANAGER.SaveTransform(D3DTS_TEXTURE0, &m_matWorldForCommonUse);
STATEMANAGER.SaveTransform(D3DTS_TEXTURE1, &m_matWorldForCommonUse);
// Render State & TextureStageState
//////////////////////////////////////////////////////////////////////////
STATEMANAGER.SetVertexShader(D3DFVF_XYZ | D3DFVF_NORMAL);
m_iRenderedSplatNumSqSum = 0;
m_iRenderedPatchNum = 0;
m_iRenderedSplatNum = 0;
m_RenderedTextureNumVector.clear();
std::pair<float, long> fog_far(fFogFarDistance + 1600.0f, 0);
std::pair<float, long> fog_near(fFogNearDistance - 3200.0f, 0);
std::vector<std::pair<float, long> >::iterator far_it = std::upper_bound(m_PatchVector.begin(), m_PatchVector.end(), fog_far);
std::vector<std::pair<float, long> >::iterator near_it = std::upper_bound(m_PatchVector.begin(), m_PatchVector.end(), fog_near);
// NOTE: Word Editor 툴에서는 fog far보다 멀리있는 물체를 텍스쳐 없이 그리는 작업을 하지 않음
#ifdef WORLD_EDITOR
near_it = m_PatchVector.begin();
far_it = m_PatchVector.end();
#endif
WORD wPrimitiveCount;
D3DPRIMITIVETYPE ePrimitiveType;
BYTE byCUrrentLODLevel = 0;
float fLODLevel1Distance = __GetNoFogDistance();
float fLODLevel2Distance = __GetFogDistance();
#ifdef ENABLE_MODE_HD
if (ActiveHD)
{
float fLODLevel1Distance = __GetNoFogDistance() * 5;
float fLODLevel2Distance = __GetFogDistance() * 5;
}
else
{
float fLODLevel1Distance = __GetNoFogDistance();
float fLODLevel2Distance = __GetFogDistance();
}
#endif
SelectIndexBuffer(0, &wPrimitiveCount, &ePrimitiveType);
DWORD dwFogEnable = STATEMANAGER.GetRenderState(D3DRS_FOGENABLE);
std::vector<std::pair<float, long> >::iterator it = m_PatchVector.begin();
// NOTE: 맵툴에서는 view ~ fog near 사이의 지형을 fog disabled 상태로 그리는 작업을 하지 않음.
#ifndef WORLD_EDITOR
STATEMANAGER.SetRenderState(D3DRS_FOGENABLE, FALSE);
for (; it != near_it; ++it)
{
if (byCUrrentLODLevel == 0 && fLODLevel1Distance <= it->first)
{
byCUrrentLODLevel = 1;
SelectIndexBuffer(1, &wPrimitiveCount, &ePrimitiveType);
}
else if (byCUrrentLODLevel == 1 && fLODLevel2Distance <= it->first)
{
byCUrrentLODLevel = 2;
SelectIndexBuffer(2, &wPrimitiveCount, &ePrimitiveType);
}
__HardwareTransformPatch_RenderPatchSplat(it->second, wPrimitiveCount, ePrimitiveType);
if (m_iRenderedSplatNum >= m_iSplatLimit)
break;
if (m_bDrawWireFrame)
DrawWireFrame(it->second, wPrimitiveCount, ePrimitiveType);
}
#endif
STATEMANAGER.SetRenderState(D3DRS_FOGENABLE, dwFogEnable);
if (m_iRenderedSplatNum < m_iSplatLimit)
{
for (it = near_it; it != far_it; ++it)
{
if (byCUrrentLODLevel == 0 && fLODLevel1Distance <= it->first)
{
byCUrrentLODLevel = 1;
SelectIndexBuffer(1, &wPrimitiveCount, &ePrimitiveType);
}
else if (byCUrrentLODLevel == 1 && fLODLevel2Distance <= it->first)
{
byCUrrentLODLevel = 2;
SelectIndexBuffer(2, &wPrimitiveCount, &ePrimitiveType);
}
__HardwareTransformPatch_RenderPatchSplat(it->second, wPrimitiveCount, ePrimitiveType);
if (m_iRenderedSplatNum >= m_iSplatLimit)
break;
if (m_bDrawWireFrame)
DrawWireFrame(it->second, wPrimitiveCount, ePrimitiveType);
}
}
STATEMANAGER.SetRenderState(D3DRS_FOGENABLE, FALSE);
#ifdef ENABLE_MODE_HD
if (ActiveHD)
{
STATEMANAGER.SetRenderState(D3DRS_LIGHTING, TRUE);
}
else
{
STATEMANAGER.SetRenderState(D3DRS_LIGHTING, FALSE);
}
#else
STATEMANAGER.SetRenderState(D3DRS_LIGHTING, FALSE);
#endif
STATEMANAGER.SetTexture(0, NULL);
STATEMANAGER.SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, FALSE);
STATEMANAGER.SetTexture(1, NULL);
STATEMANAGER.SetTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, FALSE);
STATEMANAGER.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
STATEMANAGER.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
STATEMANAGER.SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
STATEMANAGER.SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
STATEMANAGER.SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
if (m_iRenderedSplatNum < m_iSplatLimit)
{
for (it = far_it; it != m_PatchVector.end(); ++it)
{
if (byCUrrentLODLevel == 0 && fLODLevel1Distance <= it->first)
{
byCUrrentLODLevel = 1;
SelectIndexBuffer(1, &wPrimitiveCount, &ePrimitiveType);
}
else if (byCUrrentLODLevel == 1 && fLODLevel2Distance <= it->first)
{
byCUrrentLODLevel = 2;
SelectIndexBuffer(2, &wPrimitiveCount, &ePrimitiveType);
}
__HardwareTransformPatch_RenderPatchNone(it->second, wPrimitiveCount, ePrimitiveType);
if (m_iRenderedSplatNum >= m_iSplatLimit)
break;
if (m_bDrawWireFrame)
DrawWireFrame(it->second, wPrimitiveCount, ePrimitiveType);
}
}
STATEMANAGER.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
STATEMANAGER.SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT);
STATEMANAGER.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
STATEMANAGER.SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
STATEMANAGER.SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
STATEMANAGER.SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_CURRENT);
STATEMANAGER.SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
STATEMANAGER.SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
STATEMANAGER.SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
STATEMANAGER.SetRenderState(D3DRS_FOGENABLE, dwFogEnable);
STATEMANAGER.SetRenderState(D3DRS_LIGHTING, TRUE);
std::sort(m_RenderedTextureNumVector.begin(), m_RenderedTextureNumVector.end());
//////////////////////////////////////////////////////////////////////////
// Render State & TextureStageState
STATEMANAGER.RestoreRenderState(D3DRS_TEXTUREFACTOR);
STATEMANAGER.RestoreTransform(D3DTS_TEXTURE0);
STATEMANAGER.RestoreTransform(D3DTS_TEXTURE1);
STATEMANAGER.RestoreTextureStageState(0, D3DTSS_TEXCOORDINDEX);
STATEMANAGER.RestoreTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS);
STATEMANAGER.RestoreTextureStageState(1, D3DTSS_TEXCOORDINDEX);
STATEMANAGER.RestoreTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS);
STATEMANAGER.RestoreRenderState(D3DRS_ALPHABLENDENABLE);
STATEMANAGER.RestoreRenderState(D3DRS_ALPHATESTENABLE);
STATEMANAGER.RestoreRenderState(D3DRS_ALPHAREF);
STATEMANAGER.RestoreRenderState(D3DRS_ALPHAFUNC);
// Render State & TextureStageState
//////////////////////////////////////////////////////////////////////////
}
FX Shader:
#define FLIP_TEXTURE_Y
float Script : STANDARDSGLOBAL <
string UIWidget = "none";
string ScriptClass = "object";
string ScriptOrder = "standard";
string ScriptOutput = "color";
string Script = "Technique=Phong?Main:Main10;";
> = 0.8;
//// UN-TWEAKABLES - AUTOMATICALLY-TRACKED TRANSFORMS ////////////////
float4x4 WorldITXf : WorldInverseTranspose < string UIWidget="None"; >;
float4x4 WvpXf : WorldViewProjection < string UIWidget="None"; >;
float4x4 WorldXf : World < string UIWidget="None"; >;
float4x4 ViewIXf : ViewInverse < string UIWidget="None"; >;
//// TWEAKABLE PARAMETERS ////////////////////
/// Point Lamp 0 ////////////
float3 Lamp0Pos : Position <
string Object = "PointLight0";
string UIName = "Lamp 0 Position";
string Space = "World";
> = {-0.5f,2.0f,1.25f};
float3 Lamp0Color : Specular <
string UIName = "Lamp 0";
string Object = "Pointlight0";
string UIWidget = "Color";
> = {1.0f,1.0f,1.0f};
// Ambient Light
float3 AmbiColor : Ambient <
string UIName = "Ambient Light";
string UIWidget = "Color";
> = {0.07f,0.07f,0.07f};
float Ks <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.05;
string UIName = "Specular";
> = 0.4;
float SpecExpon : SpecularPower <
string UIWidget = "slider";
float UIMin = 1.0;
float UIMax = 128.0;
float UIStep = 1.0;
string UIName = "Specular Power";
> = 55.0;
float Bump <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 3.0;
float UIStep = 0.01;
string UIName = "Bumpiness";
> = 1.0;
float Kr <
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.01;
string UIName = "Reflection Strength";
> = 10.5;
//////// COLOR & TEXTURE /////////////////////
texture ColorTexture : DIFFUSE <
string ResourceName = "default_color.dds";
string UIName = "Diffuse Texture";
string ResourceType = "2D";
>;
sampler2D ColorSampler = sampler_state {
Texture = <ColorTexture>;
FILTER = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
texture NormalTexture <
string ResourceName = "default_bump_normal.dds";
string UIName = "Normal-Map Texture";
string ResourceType = "2D";
>;
sampler2D NormalSampler = sampler_state {
Texture = <NormalTexture>;
FILTER = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
texture EnvTexture : ENVIRONMENT <
string ResourceName = "default_reflection.dds";
string UIName = "Environment";
string ResourceType = "Cube";
>;
samplerCUBE EnvSampler = sampler_state {
Texture = <EnvTexture>;
FILTER = MIN_MAG_MIP_LINEAR;
AddressU = Clamp;
AddressV = Clamp;
AddressW = Clamp;
};
// shared shadow mapping supported in Cg version
//////// CONNECTOR DATA STRUCTURES ///////////
/* data from application vertex buffer */
struct appdata {
float3 Position : POSITION;
float4 UV : TEXCOORD0;
float4 Normal : NORMAL;
float4 Tangent : TANGENT0;
float4 Binormal : BINORMAL0;
};
/* data passed from vertex shader to pixel shader */
struct vertexOutput {
float4 HPosition : POSITION;
float2 UV : TEXCOORD0;
// The following values are passed in "World" coordinates since
// it tends to be the most flexible and easy for handling
// reflections, sky lighting, and other "global" effects.
float3 LightVec : TEXCOORD1;
float3 WorldNormal : TEXCOORD2;
float3 WorldTangent : TEXCOORD3;
float3 WorldBinormal : TEXCOORD4;
float3 WorldView : TEXCOORD5;
};
///////// VERTEX SHADING /////////////////////
/*********** Generic Vertex Shader ******/
vertexOutput std_VS(appdata IN) {
vertexOutput OUT = (vertexOutput)0;
OUT.WorldNormal = mul(IN.Normal,WorldITXf).xyz;
OUT.WorldTangent = mul(IN.Tangent,WorldITXf).xyz;
OUT.WorldBinormal = mul(IN.Binormal,WorldITXf).xyz;
float4 Po = float4(IN.Position.xyz,1);
float3 Pw = mul(Po,WorldXf).xyz;
OUT.LightVec = (Lamp0Pos - Pw);
#ifdef FLIP_TEXTURE_Y
OUT.UV = float2(IN.UV.x,(1.0-IN.UV.y));
#else /* !FLIP_TEXTURE_Y */
OUT.UV = IN.UV.xy;
#endif /* !FLIP_TEXTURE_Y */
OUT.WorldView = normalize(ViewIXf[3].xyz - Pw);
OUT.HPosition = mul(Po,WvpXf);
return OUT;
}
///////// PIXEL SHADING //////////////////////
// Utility function for phong shading
void phong_shading(vertexOutput IN,
float3 LightColor,
float3 Nn,
float3 Ln,
float3 Vn,
out float3 DiffuseContrib,
out float3 SpecularContrib)
{
float3 Hn = normalize(Vn + Ln);
float4 litV = lit(dot(Ln,Nn),dot(Hn,Nn),SpecExpon);
DiffuseContrib = litV.y * LightColor;
SpecularContrib = litV.y * litV.z * Ks * LightColor;
}
float4 std_PS(vertexOutput IN) : COLOR {
float3 diffContrib;
float3 specContrib;
float3 Ln = normalize(IN.LightVec);
float3 Vn = normalize(IN.WorldView);
float3 Nn = normalize(IN.WorldNormal);
float3 Tn = normalize(IN.WorldTangent);
float3 Bn = normalize(IN.WorldBinormal);
float3 bump = Bump * (tex2D(NormalSampler,IN.UV).rgb - float3(0.5,0.5,0.5));
Nn = Nn + bump.x*Tn + bump.y*Bn;
Nn = normalize(Nn);
phong_shading(IN,Lamp0Color,Nn,Ln,Vn,diffContrib,specContrib);
float3 diffuseColor = tex2D(ColorSampler,IN.UV).rgb;
float3 result = specContrib+(diffuseColor*(diffContrib+AmbiColor));
float3 R = -reflect(Vn,Nn);
float3 reflColor = Kr * texCUBE(EnvSampler,R.xyz).rgb;
result += diffuseColor*reflColor;
return float4(result,1);
}
///// TECHNIQUES /////////////////////////////
RasterizerState DisableCulling
{
CullMode = NONE;
};
DepthStencilState DepthEnabling
{
DepthEnable = TRUE;
};
BlendState DisableBlend
{
BlendEnable[0] = FALSE;
};
technique10 Main10 <
string Script = "Pass=p0;";
> {
pass p0 <
string Script = "Draw=geometry;";
> {
SetVertexShader( CompileShader( vs_4_0, std_VS() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, std_PS() ) );
SetRasterizerState(DisableCulling);
SetDepthStencilState(DepthEnabling, 0);
SetBlendState(DisableBlend, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF);
}
}
technique Main <
string Script = "Pass=p0;";
> {
pass p0 <
string Script = "Draw=geometry;";
> {
VertexShader = compile vs_2_0 std_VS();
ZEnable = true;
ZWriteEnable = true;
ZFunc = LessEqual;
AlphaBlendEnable = false;
CullMode = None;
PixelShader = compile ps_2_a std_PS();
}
}
But nothing happened. I want to create a reflective terrain.
.fx
and Effect Files are obsolete. \$\endgroup\$