• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
angelmu88

Per pixel point light: interpolating vertex world pos

12 posts in this topic

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?
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
[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.
[/quote]
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!


1

Share this post


Link to post
Share on other sites



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


1

Share this post


Link to post
Share on other sites
[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.
[/quote]
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).
2

Share this post


Link to post
Share on other sites
[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.
[/quote]
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?
1

Share this post


Link to post
Share on other sites
[quote name='doesnotcompute' timestamp='1325229472' post='4898074']
[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.
[/quote]
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.
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
[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.
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='rdragon1' timestamp='1325276847' post='4898235']
[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.
[/quote]

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

Share this post


Link to post
Share on other sites
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 );
1

Share this post


Link to post
Share on other sites
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:

[url="http://img194.imageshack.us/i/rmpervertex.png/"][img]http://img194.imageshack.us/img194/4374/rmpervertex.th.png[/img][/url].


Per Pixel w/out normalize

[url="http://img850.imageshack.us/i/rmperpixelnonormalize.png/"][img]http://img850.imageshack.us/img850/7438/rmperpixelnonormalize.th.png[/img][/url]

Per Pixel w/normalize

[url="http://img27.imageshack.us/i/rmperpixel.png/"][img]http://img27.imageshack.us/img27/990/rmperpixel.th.png[/img][/url]
1

Share this post


Link to post
Share on other sites
Ok, you are right, in very strange situations you can see some differences:
Per-Vertex

[URL=http://imageshack.us/photo/my-images/6/cubevertex.jpg/][IMG]http://img6.imageshack.us/img6/6272/cubevertex.jpg[/IMG][/URL]

Uploaded with [URL=http://imageshack.us]ImageShack.us[/URL]

Per-Pixel

[URL=http://imageshack.us/photo/my-images/855/cubepixel.jpg/][IMG]http://img855.imageshack.us/img855/4513/cubepixel.jpg[/IMG][/URL]

Uploaded with [URL=http://imageshack.us]ImageShack.us[/URL]

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!
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0