Per pixel point light: interpolating vertex world pos

Started by
11 comments, last by angelmu88 12 years, 3 months ago
Hi!
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 3D graphic engine: http://graphicprogramming.wordpress.com/rebirthengine/
My blog: http://graphicprogramming.wordpress.com/
Advertisement
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.

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!


My 3D graphic engine: http://graphicprogramming.wordpress.com/rebirthengine/
My blog: http://graphicprogramming.wordpress.com/



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



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

[quote name='MJP' timestamp='1324752019' post='4897124']
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).
[/quote]

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?

[quote name='Christer Ericson' timestamp='1325223848' post='4898063']
[quote name='MJP' timestamp='1324752019' post='4897124']
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).
[/quote]

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?
[/quote]

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 3D graphic engine: http://graphicprogramming.wordpress.com/rebirthengine/
My blog: http://graphicprogramming.wordpress.com/
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.

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.

[quote name='doesnotcompute' timestamp='1325274943' post='4898227']
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.
[/quote]

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.

This topic is closed to new replies.

Advertisement