Jump to content

  • Log In with Google      Sign In   
  • Create Account

Smooth normals


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 xlope01   Members   -  Reputation: 174

Like
-2Likes
Like

Posted 09 November 2013 - 09:47 AM

How do you smooth normals?



Sponsor:

#2 Samith   Members   -  Reputation: 2256

Like
0Likes
Like

Posted 09 November 2013 - 12:30 PM

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;
}


#3 xlope01   Members   -  Reputation: 174

Like
0Likes
Like

Posted 09 November 2013 - 06:19 PM

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?



#4 Samith   Members   -  Reputation: 2256

Like
0Likes
Like

Posted 09 November 2013 - 06:33 PM

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.


Edited by Samith, 09 November 2013 - 06:35 PM.


#5 eppo   Crossbones+   -  Reputation: 2466

Like
0Likes
Like

Posted 10 November 2013 - 07:48 AM

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.



#6 Tom KQT   Members   -  Reputation: 1593

Like
0Likes
Like

Posted 10 November 2013 - 07:55 AM

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.


Edited by Tom KQT, 10 November 2013 - 08:01 AM.


#7 xlope01   Members   -  Reputation: 174

Like
0Likes
Like

Posted 10 November 2013 - 08:20 AM

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.



#8 Zaoshi Kaba   Crossbones+   -  Reputation: 4376

Like
2Likes
Like

Posted 10 November 2013 - 08:42 AM

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


Edited by Zaoshi Kaba, 10 November 2013 - 08:42 AM.


#9 xlope01   Members   -  Reputation: 174

Like
0Likes
Like

Posted 10 November 2013 - 10:46 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS