Archived

This topic is now archived and is closed to further replies.

per vertex to per pixel conversion problems

This topic is 5144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Im just trying to convert a simple per vertex lighting program to a per pixel lighting program. For some reason, when I use my per pixel lighting program only half the polygons are lit correctly, and I think it is something to do with negative valued normals not rendering correctly. Here are some screenshots showing the problem: The first image shows the scene lit with the pervertex shader the second image shows the scene lit with the perpixel shader Notice how only a few of the polygons are lit correctly, when viewing the scene from underneath the "table", it is correctly lit with the vertex shader, but completely ''black'' with the perpixel shader. Here is the source code for the pervertex shader:
// define inputs from application
struct appin
{
    float4 Position     : POSITION;
	float3 Normal		: NORMAL;
    float2 TexCoord    : TEXCOORD0;
};

// define outputs from vertex shader
struct vertout
{
    float4 oPos: POSITION;
    float2 oTexCoord        : TEXCOORD0;
	float4 oCol: COLOR0;

};

vertout main(appin IN, 
             uniform float4x4 ModelViewProj,
			 uniform float3 lightPosition,
			 uniform float lightRange,
			 uniform float3 emmisive,
			 uniform float3 diffuse
			 )
{
	
	vertout OUT;
	OUT.oPos = mul(ModelViewProj, IN.Position);
	OUT.oTexCoord = IN.TexCoord;

	float3 N = normalize(IN.Normal);
	float3 L = normalize(lightPosition.xyz - IN.Position.xyz);
	
	//float diffuse_term = max(dot(N,L),0); 
	float diffuse_term = saturate(dot(N,L));

	OUT.oCol.rgb = emmisive + diffuse_term * diffuse;
	OUT.oCol.w = 1.0f;
	
	return OUT;
}
now the perpixel shaders:
// define inputs from application
struct appin
{
    float4 Position     : POSITION;
	float3 Normal		: NORMAL;
    float2 TexCoord    : TEXCOORD0;
};
// define outputs from vertex shader
struct vertout
{
    float4 oPos: POSITION;
    float2 oTexCoord        : TEXCOORD0;
	float4 oCol: COLOR0;

	float3 oPosTex : TEXCOORD1;
	float3 oNormalTex : TEXCOORD2;
};
vertout main(appin IN, 
             uniform float4x4 ModelViewProj,
			 uniform float3 lightPosition,
			 uniform float lightRange,
			 uniform float3 emmisive,
			 uniform float3 diffuse
			 )
{
	vertout OUT;
	OUT.oPos = mul(ModelViewProj, IN.Position);
	OUT.oTexCoord = IN.TexCoord;
	OUT.oCol.xyz = diffuse;
	OUT.oCol.w = 1.0f;
	OUT.oPosTex = normalize(lightPosition.xyz - IN.Position.xyz);
	OUT.oNormalTex = normalize(IN.Normal);
	return OUT;
}

//PIXEL SHADER:

float3 expand(float3 v) { return (v-0.5f) * 2;}

float4 FastNormalize(float4 inVec)
{
    float distSq = 1 - (dot(inVec.xyz , inVec.xyz));
    return mad(distSq, inVec * 0.5 , inVec );
}

float4 main(vertout I,
            uniform sampler2D colorMap,
			uniform samplerCUBE normalizeCube,
			uniform float3 emmisive,
			uniform float3 diffuse
			) : COLOR
{
	float4 oCol;
	float4 color = tex2D(colorMap, I.oTexCoord.xy);
	color.w = 1.0f;
	float3 N = I.oNormalTex;
	//float3 lt = texCUBE(normalizeCube, I.oPosTex).xyz;
	//float3 L = expand(lt);
	float4 lt;
	lt.xyz=I.oPosTex;
	lt.w=1.0f;

	float3 L = FastNormalize(lt);

	float diffuse_term = saturate(dot(N,L)); 
	
	oCol.rgb = emmisive + diffuse_term * diffuse;
	oCol.w = 1.0f;
	return color*oCol;			
}
The target profile is DX PS1.3 & VS1.1, I originaly thought the problem came from the normalizeation cube map, but I replaced that code with some code I stole from nVidia (I hope its right?) and I still get the same results, so now I am totally confused as to where the problem is coming from. I can post higher resolution screenshots if neccessary, I didnt want to attach any large images. If anyone can tell me where the problem is, or provide the correct translation that would be great! Thanks!

Share this post


Link to post
Share on other sites
Figured it out, two problems:
1) In the code I provided, the normalized normal is not renormalized. (hey, enough use of the word normal for you?) Because of the target specs, the nVidia normalization code can not be used (not enough temporary registers), so you need to go back to using cube maps.

2) This

Share this post


Link to post
Share on other sites