What is wrong with this fx file?
I'm new to programming shaders in HLSL. Originally I had this shader doing normal mapping, but I want to do a diffuse texture on the object. If your not sure how diffuse works but can see the error with the code i'd be really happy just to know the syntax error if there is one, because I can't find it.
here is the code: (if you need more information then what i've provided please let me know, any help would be appreciated. thx :D
/*
Normal mapping shader
This shader performs a per-pixel dot3 bump mapping lighting effect with only the diffuse
light component calculated.
*/
// global matrices
uniform extern float4x4 g_matWVP;
uniform extern float4x4 g_matWI;
// light direction in world space
uniform extern float3 g_lightDir;
// textures
uniform extern texture g_texture;
uniform extern texture g_normalMap;
// colour texture
sampler sampTexture = sampler_state
{
Texture = <g_texture>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
// normal texture
sampler sampNormalMap = sampler_state
{
Texture = <g_normalMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
// vertex shader input that allows position and texture coordinates as well as TBN
struct VSInput
{
float3 pos: POSITION0;
float3 tan: TANGENT0;
float3 bin: BINORMAL0;
float3 nor: NORMAL0;
float2 tex: TEXCOORD0;
};
// vertex shader output
struct VSOutput
{
float4 pos: POSITION0;
float3 lightDir: TEXCOORD0;
float2 tex: TEXCOORD1;
};
// normal mapping vertex shader
VSOutput VS_NormalMapping(VSInput a_Input)
{
VSOutput output;
// calculate homogenous position
output.pos = mul(float4(a_Input.pos, 1.0f), g_matWVP);
// copy texture coordinates across
output.tex = a_Input.tex;
// calculate matrix to tangent space
float3x3 toTangentSpace = transpose(float3x3(a_Input.tan, a_Input.bin, a_Input.nor));
// calculate light's direction relative to tangent space
float3 lightDirL = mul(float4(g_lightDir, 0.0f), g_matWI).xyz;
output.lightDir = mul(lightDirL, toTangentSpace);
return output;
}
VSOutput VS_DiffuseTexture(VSInput a_input)
{
VSOutput output;
//in VS i believe i only need to calculate the vertex's normal since in the other pass the other vertex calulation is done
output.nor = mul(float4(a_input.nor,0.0f),g_matWI).xyz;
return output;
}
// normal mapping pixel shader
float4 PS_NormalMapping(VSOutput a_Input) : COLOR
{
// index into textures
float4 colour = tex2D(sampTexture, a_Input.tex);
float3 normal = tex2D(sampNormalMap, a_Input.tex);
// convert normal from (0 - 1) to (-1 - 1)
normal = (normal - 0.5f) * 2.0f;
// normalize light direction
float3 light = normalize(a_Input.lightDir);
// calculate diffuse component
float diffuse = max(dot(normal, light), 0.0f);
// return texture colour modified by diffuse component
return (colour * diffuse);
}
float4 PS_DiffuseTexture(VSOutput a_Input): COLOR
{
//diffuse properties
float3 dLightColor = {0.8,0.8,0.8};
float3 dMaterialColor = {0.7,0.6,0.5};
//make diffuse intensity matrix
float3 dIntensity = mul(dLightColor,dMaterialColor);
float dVariable = max(dot(g_lightDir,a_Input.nor),0.0f);
float3 diffuse = dVariable * dIntensity;
return float4(diffuse,a_Input.a);
}
technique NormalMappingTech
{
pass P0
{
vertexShader = compile vs_2_0 VS_NormalMapping();
pixelShader = compile ps_2_0 PS_NormalMapping();
}
pass P1
{
vertexShader = compile vs_2_0 VS_DiffuseTexture();
pixelShader = compile ps_2_0 PS_DiffuseTexture();
}
}
its this line
output.nor = mul(float4(a_input.nor,0.0f),g_matWI).xyz;
it's because you haven't declared any VSOutput.nor yet.
which is in "struct VSOutput"
output.nor = mul(float4(a_input.nor,0.0f),g_matWI).xyz;
it's because you haven't declared any VSOutput.nor yet.
which is in "struct VSOutput"
Yes, your VSOutput struct doesn't have a .nor member. Also, note that on these forums you should enclose large blocks of code in [ source ] [ /source ] tags.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement