Jump to content
  • Advertisement
Sign in to follow this  
PDX_Catalyst

Incorrect Interpolation from Vertex Shader to Pixel Shader

This topic is 3670 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

I'm getting what looks like poor interpolation in the shading of my terrain. You can see the lines/diamonds in the picture below. I made the terrain pure white to better show them. On a related? note: I'm curious if this same issue might also explain why it seems like the interpolation of the textures is also poor so that the textures blend together poorly in the diagonal directions. Two consecutive vertices with different textures look ok if they're both on the x or the y axis, but vertices diagonal from each other look bad if they have different textures. I'm guessing it's related to the shading problem. [Edited by - PDX_Catalyst on July 27, 2008 2:24:11 PM]

Share this post


Link to post
Share on other sites
Advertisement
By the way, I moved the calculations to the pixel shader and it looked exactly the same. Here's what I'm doing in the vertex shader...

GVS_OUTPUT vs_ground(float4 inPos: POSITION, float3 inNormal: NORMAL, float2 inTexCoords: TEXCOORD0)
{
GVS_OUTPUT Out = (GVS_OUTPUT)0;

Out.Pos = mul(inPos, ViewProjMx);
inNormal = normalize(inNormal);
vector SunVc = mul(SunPos, InvWorldMx);
vector SunDir = normalize(SunVc - inPos);
Out.Color = dot(inNormal, SunDir) * (1 - Ambient) + Ambient;
Out.TexUV = inTexCoords;
return Out;
}

If it's not a problem with the interpolation or this shader code then maybe my normals aren't quite right, but they should be.

Share this post


Link to post
Share on other sites
Just a little clarification:

Is ViewProjMx just view*projection, or is it world*view*projection?

Is SunPos a world position?

Share this post


Link to post
Share on other sites
Yes, just view and projection. The world is always centered on 0, 0 just because of the way the game works, so the world matrix is always equal to Matrix.Identity. Glad you pointed that out because I didn't need to multiply the SunPos by InvWorldMx since it doesn't do anything.

SunPos is in world coordinates.

All that should be ok... the interpolation just seems to be bad. You can see in the picture that all four vertices surrounding a pair of triangles are darker than the triangles, which makes no sense since the fill should be no darker or lighter than the darkest or lightest vertex. I'm stumped.

Share this post


Link to post
Share on other sites
Okay. I thought maybe you were mixing apples and oranges by dotting the unmodified normal with whatever the SunVC would've been. [smile]

Quote:
maybe my normals aren't quite right, but they should be.


Are you using 3 vertices per face, or shared vertices for your mesh?

If 3 vertices per face, are you sure that the normals for vertices at the same position, but on different faces, are identical?

Also, assuming that, in your world, the sun is far enough away from your vertices, you might try using a sun direction, rather than SunPos-inPos. It would save you 1 calculation and would avoid subtracting a very small number (inPos) from (I assume) a very large one (SunPos), which, possibly, could be a problem.

Share this post


Link to post
Share on other sites
You're right, the SunPos - inPos was unnecessary. I just took out the subtraction and it works the same.

I'm using shared vertices.

Here's how I'm getting the vertice normals:

First I store the normals of each triangle ...

plane = new Plane(groundVertices[x + y * SIZE_V].Position, groundVertices[x + (y + 1) * SIZE_V].Position, groundVertices[x + 1 + y * SIZE_V].Position);

triNorm[x + y * SIZE_Q, 0] = plane.Normal;

... then I go through and average the normals for all 6 triangles connected to each vertice ...

groundVertices[x + y * SIZE_V].Normal =
(
triNorm[x - 1 + (y - 1) * SIZE_Q, 1] +
triNorm[x + y * SIZE_Q, 0] +
triNorm[x + (y - 1) * SIZE_Q, 0] +
triNorm[x + (y - 1) * SIZE_Q, 1] +
triNorm[x - 1 + y * SIZE_Q, 0] +
triNorm[x - 1 + y * SIZE_Q, 1]
)
/ 6f;

... so hopefully that's done properly.

Share this post


Link to post
Share on other sites
For an unweighted average, your calc is correct. A long time ago, I tried:

((x-1,y-1),0) +
((x,y-1),0)*0.5 +
((x,y-1),1)*0.5 +
((x,y),0) +
((x-1,y-1),0)*0.5 +
((x-1,y-1),1)*0.5

Then divide by 4 and normalize.

That seemed to help.

Might be worth a try.

Share this post


Link to post
Share on other sites
i don't think you need to average the normals, just normalize the normal for every vertex -divide x,y & z components of the normal by the lenght of the normal

Share this post


Link to post
Share on other sites
Quote:
just normalize the normal for every vertex..

The code he is posting is the process to determine the normal for each vertex. The vertex normal doesn't exist until this process is done. [smile]

Share this post


Link to post
Share on other sites
That's funny, using the logic that the ".5" triangles are "less attached", I tried exactly that before and it didn't seem to do anything about the patterns. I wrote the normal code by thinking out what needed to happen, not by learning it somewhere, so I wasn't absolutely sure I'd done it right. Glad to know I was on the right track. I'll try tweaking those numbers more extremely and see how that works out, because my feeling was also that something was weighted incorrectly. HOWEVER, regardless of what the normals surrounding a triangle are it still doesn't make sense to me that the triangle could be lighter than all three vertices. I mean, logically, if you interpolate between .5, .8, and .3 how could you ever get .9 or .2?

So we can at least agree this shading appearance is not normal, right? It SHOULD look smooth, right? I spent a bunch of time looking for examples of someone else's terrain (or any mesh really) with the same pattern in the shading, but didn't find anything to show either way.

I appreciate the help, BTW =)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!