Smooth normals

Started by
7 comments, last by xlope01 10 years, 5 months ago

How do you smooth normals?

Advertisement

Normals output from your vertex shader will get interpolated before being sent to the pixel shader. Inside the pixel shader you can normalize the interpolated normal and use it. Here's some very basic pseudo HLSL


struct VS_INPUT {
    float4 position : POSITION;
    float3 normal : NORMAL;
};

struct VS_OUTPUT {
    float4 position : SV_Position;
    float3 normal : NORMAL;          // will get interpolated
};

VS_OUTPUT VS(const VS_INPUT &Input)
{
    VS_OUTPUT Output;
    Output.position = mul(Input.position, modelviewprojection);
    Output.normal = mul(Input.normal, modelview);
    return Output;
}

float4 PS(const VS_OUTPUT &Input) : SV_Target
{
    float4 color;
    // Input.normal has been interpolated,
    // we use it here in a simple fake lighting calculation
    color.rgb = float3(dot(normalize(Input.normal), float3(0,1,0)));
    color.a = 1;
    return color;
}

That is pretty much what I was doing. with the difference that i use the worldMatrix instead of the ViewMatrix like you did in the VS. Still no smooth normals.sad.png

There's a way to change the angle of smoothing to see what happens?

That is pretty much what I was doing. with the difference that i use the worldMatrix instead of the ViewMatrix like you did in the VS. Still no smooth normals.sad.png

There's a way to change the angle of smoothing to see what happens?

Would you mind posting some code and perhaps a screenshot so we can get a better picture of your problem?

I don't know exactly what you mean by "change the angle of smoothing." There are some interpolation modifiers (see http://msdn.microsoft.com/en-us/library/windows/desktop/bb509668(v=vs.85).aspx) but I think it's somewhat unlikely that the interpolation modifiers are going to solve your problem.

EDIT: also, just in case you were wondering, using worldMatrix vs worldViewMatrix shouldn't have any effect. I chose to transform the normal into view space in my example because I typically do lighting in view space, but it is perfectly acceptable to do it in world space as well.

How do you come up with the normals you are using? Shaders use smooth Gouraud interpolation by default, so I doubt that's where to problem lies.

How do you smooth normals?

You should be much more specific about your problem. That must have been one of the shortests first posts on a technical forum ever :O
What you are trying to do? How does it look and how did you want it to look? How did you get the mesh (normals) data?

IF the mesh was generated with flat shading (each triangle has its own normals perpendicular to the triangle surface), you won't get smoothed rendering just by the automatic interpolating done between vertex and pixel shaders, because it will interpolate three identical normals which will give again the same normal.

But that's a huge IF, because we really cannot know.

cb11.png


VS...
output.normal = mul(input.normal, (float3x3)worldMatrix);

PS....
float4 color;
float c = dot(normalize(input.normal), float3(0,1,0));
color = float4(c,c,c, 1);
return color;

mesh came from sketchup.

My guess is that exporter didn't smooth them. There might be an option during export or in editor to create smooth normals.

My guess is that exporter didn't smooth them. There might be an option during export or in editor to create smooth normals.

That was the problem.

In the mesh loading function i add this


for(int i=0; i<vertexCount; i++)
	{
		for(int g=0; g<vertexCount; g++)
		{
			if(model[i].x == model[g].x && model[i].y == model[g].y && model[i].z == model[g].z)
			{
				model[i].nx = (model[g].nx+model[i].nx)/2;
				model[i].ny = (model[g].ny+model[i].ny)/2;
				model[i].nz = (model[g].nz+model[i].nz)/2;
			}
		}
	}

add it fix the problem, now it is smooth.

This topic is closed to new replies.

Advertisement