PDA

View Full Version : Conceptual question

Tomy
08-19-2005, 10:35 PM
Hello everybody,

I am still a novice in OGL shading, and there is something I could not understand...

If I want to apply light to a 2d texture, I would most probably define the texture in my application using 4 glVertex calls. Would this mean that my vertex shader would by applied 4 times(once for every vertex)?

If so, if we suppose that my light direction is perpendicular to the center of the texture, how could the fragment shader figure out that the maximum light intensity would be at the center of the texture, if light calculations at the input vertices would give the same low intensity?

Thank you :)

Jan
08-20-2005, 12:34 AM
Originally posted by Tomy:
Hello everybody,
If I want to apply light to a 2d texture, I would most probably define the texture in my application using 4 glVertex calls. Would this mean that my vertex shader would by applied 4 times(once for every vertex)?
Yes.

If so, if we suppose that my light direction is perpendicular to the center of the texture, how could the fragment shader figure out that the maximum light intensity would be at the center of the texture, if light calculations at the input vertices would give the same low intensity?
It couldn't. The vertex-shader can only work on the vertices, so it is not able to calculate stuff in between the vertices. The calculations at each vertex simply get interpolated across the triangle. As long as your function comes close to this, this is ok, but usually functions are too complex to be done in the vertex-shader alone.

And that's where the pixel-shader comes in. The pixel-shader gets some input-data, which was computed by the vertex-shader and then computes the rest for each pixel. This way you can calculate your lighting for each pixel individually. And viola! you get the bright light at the center of your quad.

Jan.

Jan
08-20-2005, 12:41 AM

Well, the thing is, if you want to do per-pixel-lighting, you don't compute the light in the vertex-shader, but completely in the pixel-shader.
All the vertex-shader does, is to precalculate stuff, that can be interpolated across the triangle without errors (or with only very low errors).

For example, at each vertex you calculate the "light-to-vertex" vector. This one can perfectly be interplolated. Now, the pixel-shader needs this vector for its lighting-calculations, but it would be insane, if it would compute the vector by itself. So you leave that to the vertex-shader, let it pass the vector to the pixel-shader and the pixel-shader then uses that information to compute the lighting-function.

In general it is this way: If you want to have per-pixel correct results, the pixel-shader needs to do everything. But then, look at your function and check, which parts of the function you can take out of the pixel-shader and put into the vertex-shader, without actually changing the function.

Hope that helps,
Jan.

Tomy
08-20-2005, 01:44 AM
Thank You Jan, this was very helpful :D