**0**

# Per pixel point light: interpolating vertex world pos

###
#1
Members - Reputation: **228**

Posted 24 December 2011 - 11:34 AM

I'm working on a point light per-pixel based shader. I've read a lot of info about this topic, and most of the time people compute light direction in the vertex shader and pass it to the pixel shader along with vertex normal. The problem is that I want to add multiple point lights, so I am limited by the number of registers the vertex shader can pass to the pixel shader. I've seen another web example and I think that I could propably pass the vertex world position to the pixel shader and compute the light Direction in the pixel shader, but I don't know if it results in a good per-pixel light effect, I mean, I am no longer interpolating the light direction, instead I am interpolating vertex position in worldspace. Does anyone know if it's ok?

My blog: http://graphicprogramming.wordpress.com/

###
#3
Members - Reputation: **228**

Posted 26 December 2011 - 07:26 AM

Out of curiosity, wich kind of data can I interpolate?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.

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!

My blog: http://graphicprogramming.wordpress.com/

###
#5
Members - Reputation: **823**

Posted 29 December 2011 - 11:44 PM

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).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.

http://realtimecollisiondetection.net/blog/

###
#6
Members - Reputation: **258**

Posted 30 December 2011 - 01:17 AM

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).

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.

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?

###
#7
Members - Reputation: **228**

Posted 30 December 2011 - 06:34 AM

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).

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.

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.

My blog: http://graphicprogramming.wordpress.com/

###
#8
Members - Reputation: **258**

Posted 30 December 2011 - 01:55 PM

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.

###
#9
Crossbones+ - Reputation: **1200**

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.

###
#10
Members - Reputation: **258**

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.

###
#12
Members - Reputation: **258**

Posted 30 December 2011 - 05:06 PM

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

###
#13
Members - Reputation: **228**

Posted 31 December 2011 - 04:11 AM

Per-Vertex

Uploaded with ImageShack.us

Per-Pixel

Uploaded with ImageShack.us

Anyway I think if I only use diffuse component, per-pixel isn't worth it, because I'm not going to use a mesh like the one above (a cube deformed with only two triangles per face).

Thanks everybody!

My blog: http://graphicprogramming.wordpress.com/