Started by Dec 24 2011 11:34 AM

,
12 replies to this topic

Posted 24 December 2011 - 11:34 AM

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?

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/

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

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

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

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

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.

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.

Christer Ericson

http://realtimecollisiondetection.net/blog/

http://realtimecollisiondetection.net/blog/

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?

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

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

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

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

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.

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.

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.

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.

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

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

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

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

Posted 31 December 2011 - 04:11 AM

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

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!

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/