• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Per pixel point light: interpolating vertex world pos

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.

12 replies to this topic

### #1angelmu88  Members

Posted 24 December 2011 - 11:34 AM

Hi!

### #2MJP  Moderators

Posted 24 December 2011 - 12:40 PM

You will get the same results by interpolating the positing and then computing the light direction in the pixel shader...this is because vertex positions can be linearly interpolated. So you don't have anything to worry about.

### #3angelmu88  Members

Posted 26 December 2011 - 07:26 AM

You will get the same results by interpolating the positing and then computing the light direction in the pixel shader...this is because vertex positions can be linearly interpolated. So you don't have anything to worry about.

Out of curiosity, wich kind of data can I interpolate?
Because I was thinking of interpolating tangents too for normal mapping, for the same reason I stated earlier (add as many lights as possible)
Thanks!

### #4Litheon  Members

Posted 26 December 2011 - 11:02 AM

Tangents, normals, binormals can be interpolated, just make sure that you normalize them again in your pixel shader.

### #5Christer Ericson  Members

Posted 29 December 2011 - 11:44 PM

You will get the same results by interpolating the positing and then computing the light direction in the pixel shader...this is because vertex positions can be linearly interpolated. So you don't have anything to worry about.

I don't think you meant to say "same results", Matt. Interpolating the vertex positions and computing a light direction in the pixel shader will not give the same result as computing a light direction at each vertex and interpolating these vectors over the triangle. The former is correct while the latter is incorrect, in the sense of the resulting vector accurately pointing at the light source. You might say "similar results" in that interpolating the direction vectors isn't grossly inaccurate (for some definition of grossly).

### #6doesnotcompute  Members

Posted 30 December 2011 - 01:17 AM

You will get the same results by interpolating the positing and then computing the light direction in the pixel shader...this is because vertex positions can be linearly interpolated. So you don't have anything to worry about.

I don't think you meant to say "same results", Matt. Interpolating the vertex positions and computing a light direction in the pixel shader will not give the same result as computing a light direction at each vertex and interpolating these vectors over the triangle. The former is correct while the latter is incorrect, in the sense of the resulting vector accurately pointing at the light source. You might say "similar results" in that interpolating the direction vectors isn't grossly inaccurate (for some definition of grossly).

Let's say we were interpolating along a line between points P1 and P2 rather than across a triangle (I'm a bit fuzzy on how to parameterize the triangle)

Let L0 = light position

The light direction at P1 is L1 = P1 - L0 and likewise L2 = P2 - L0.

The interpolated position at some intermediate point is P' = P1 + t * (P2 - P1) and the light position at this point is L' = P' - L0 = P1 + t * (P2 - P1) - L0

The interpolated light position at some point between P1 and P2 is L' = L1 + t * (L2 - L1) = P1 - L0 + t * (P2 - L0 - P1 + L0) = P1 + t * (P2 - P1) - L0, which is the same as the first result.

So they are the same right?

### #7angelmu88  Members

Posted 30 December 2011 - 06:34 AM

You will get the same results by interpolating the positing and then computing the light direction in the pixel shader...this is because vertex positions can be linearly interpolated. So you don't have anything to worry about.

I don't think you meant to say "same results", Matt. Interpolating the vertex positions and computing a light direction in the pixel shader will not give the same result as computing a light direction at each vertex and interpolating these vectors over the triangle. The former is correct while the latter is incorrect, in the sense of the resulting vector accurately pointing at the light source. You might say "similar results" in that interpolating the direction vectors isn't grossly inaccurate (for some definition of grossly).

Let's say we were interpolating along a line between points P1 and P2 rather than across a triangle (I'm a bit fuzzy on how to parameterize the triangle)

Let L0 = light position

The light direction at P1 is L1 = P1 - L0 and likewise L2 = P2 - L0.

The interpolated position at some intermediate point is P' = P1 + t * (P2 - P1) and the light position at this point is L' = P' - L0 = P1 + t * (P2 - P1) - L0

The interpolated light position at some point between P1 and P2 is L' = L1 + t * (L2 - L1) = P1 - L0 + t * (P2 - L0 - P1 + L0) = P1 + t * (P2 - P1) - L0, which is the same as the first result.

So they are the same right?

Your calculations are correct, at least for a linear interpolation across a line.
One thing I wish to add: I've already implemented a per pixel lighting shader (with directional lights),and there is some difference in the specular component (I mean between the per-vertex and the per-pixel version) but regarding diffuse component you can hardly notice the difference (sometimes there is no difference at all). Is this ok?
For now I'm only using the diffuse component because I'm programming outdoor enviroments. In the future I will add specular map support but without specular maps, enviroments look unreal with specular component as you probably know.

### #8doesnotcompute  Members

Posted 30 December 2011 - 01:55 PM

I think you can make the same argument that the diffuse lighting calculation should be the same whether it's done per pixel or per vertex. In the per-vertex case you're computing the N*L dot product at the vertex and interpolating that to the each pixel. In the per pixel case you're interpolating the normal and computing the dot product per pixel but the dot product is a linear operation so it interpolates the same.

With specular lighting you have a non-linear term (a value raised to the specular exponent) which does not interpolate the same. Which is why vertex-lit meshes usually have weird looking specular highlights.

### #9rdragon1  Members

Posted 30 December 2011 - 02:27 PM

I think you can make the same argument that the diffuse lighting calculation should be the same whether it's done per pixel or per vertex. In the per-vertex case you're computing the N*L dot product at the vertex and interpolating that to the each pixel. In the per pixel case you're interpolating the normal and computing the dot product per pixel but the dot product is a linear operation so it interpolates the same.

With specular lighting you have a non-linear term (a value raised to the specular exponent) which does not interpolate the same. Which is why vertex-lit meshes usually have weird looking specular highlights.

Definitely not. You're talking about the difference between vertex lighting and per-pixel lighting. Calculating NdotL at each vertex and interpolating the result is not the same as interpolating the normal and calculating NdotL at each pixel.

Think of a quad with vertex normals pointing away from the center, and a point light source directly above the center of the quad.

### #10doesnotcompute  Members

Posted 30 December 2011 - 03:49 PM

I think you can make the same argument that the diffuse lighting calculation should be the same whether it's done per pixel or per vertex. In the per-vertex case you're computing the N*L dot product at the vertex and interpolating that to the each pixel. In the per pixel case you're interpolating the normal and computing the dot product per pixel but the dot product is a linear operation so it interpolates the same.

With specular lighting you have a non-linear term (a value raised to the specular exponent) which does not interpolate the same. Which is why vertex-lit meshes usually have weird looking specular highlights.

Definitely not. You're talking about the difference between vertex lighting and per-pixel lighting. Calculating NdotL at each vertex and interpolating the result is not the same as interpolating the normal and calculating NdotL at each pixel.

Think of a quad with vertex normals pointing away from the center, and a point light source directly above the center of the quad.

Yeah you're right (I just confirmed it RenderMonkey), I thought that seemed a little suspect as I was writing it. I don't quite see how it fails an interpolation calculation like the one I did above though.

### #11rdragon1  Members

Posted 30 December 2011 - 03:56 PM

per vertex lighting:

NdotL0 = dot(N0, L)
NdotL1 = dot(N1, L)
V = NdotL0 + t (NdotL1 - NdotL0)

per pixel lighting:

N = normalize( N0 + t( N1 - N0 ) );
V = dot( N, L );

### #12doesnotcompute  Members

Posted 30 December 2011 - 05:06 PM

So it's the renormalization that introduces the "non-linearity" I guess. Because otherwise you could just distribute the dot product into that expression for 'N' and get the same result as in the per-vertex case.

Here are some screen shots from RenderMoneky showing per-vertex nDotL along with per-pixel with and without the renormalization

Per Vertex:

.

Per Pixel w/out normalize

Per Pixel w/normalize

### #13angelmu88  Members

Posted 31 December 2011 - 04:11 AM

Ok, you are right, in very strange situations you can see some differences:
Per-Vertex

Per-Pixel