Omnimaga

General Discussion => Other Discussions => Math and Science => Topic started by: 3rik on August 26, 2011, 04:52:03 pm

Title: Understanding Shading
Post by: 3rik on August 26, 2011, 04:52:03 pm
I am working on a Lua function that creates a TI Image of a 3D object made of triangles but I don't know how to determine what shade the pixel should be.
I have looked at some websites but they either didn't get into detail or they used notation or syntax I'm not familiar with.

This is the relevant information I have:
camera position
light source position
the points that make up the triangle
the equation of the plane the triangle lies in
point where the camera view intersects the plane
the original color of the triangle

If anyone understands how Phong shading (or something similar) works, explaining it at a precalculus level would be much appreciated.

Something like this is what I am thinking of but I don't know how it works.
(http://i395.photobucket.com/albums/pp36/jnosof/reflexion.png)

I know about vectors but I don't understand what R is.

Thank you
Title: Re: Understanding Shading
Post by: Eiyeron on August 26, 2011, 04:53:55 pm
Ray. Of light of course
Title: Re: Understanding Shading
Post by: 3rik on August 26, 2011, 04:56:54 pm
But how do I find it and what should I do with it once I have it and why? This is beyond what we covered in math class.
Title: Re: Understanding Shading
Post by: ben_g on August 26, 2011, 04:57:53 pm
The amount of light that hits the surface is the cosine of the angle between the normal vector (N) and the direction of the light (L). Or ta least it had something to do with the cosine between two vectors.
Title: Re: Understanding Shading
Post by: 3rik on August 26, 2011, 05:01:10 pm
I think I want the angle between -R and V but I'm not sure how to get R.
Title: Re: Understanding Shading
Post by: ben_g on August 26, 2011, 05:04:36 pm
If you only need to do simple lighting, then you only need to find out the angle between the direction the light hits the surface and the normal vector.

EDIT: I checked it: it's the cosine of the angle between the normal and the light direction. That's all you need if you just want the most simple concept of lighting.
Title: Re: Understanding Shading
Post by: 3rik on August 26, 2011, 05:12:04 pm
In my function the Light isn't necessarily in the same position as the camera so the Light might be directly over the object but the camera is at a low angle above the object. It would be a big difference.

If I can't come up with what I'm looking for then that sounds like a good method. Thanks.
Title: Re: Understanding Shading
Post by: ben_g on August 26, 2011, 05:15:45 pm
If you want to know more abouth how lighting is calculated in 3D computer programs, you can take a look at this: http://www.lighthouse3d.com/tutorials/glsl-tutorial/directional-lights-i/
I know the code in it will be useless to you, but you can use the formula's. They are all explained. And if you don't understand something related to 3D programming, feel free to ask.
Title: Re: Understanding Shading
Post by: 3rik on August 26, 2011, 05:52:40 pm
Thanks! I think I will use this. It seems realistic enough. Hopefully I'll have it finished later today.

Edit: The very next page had what I was looking for! Thank you.
Title: Re: Understanding Shading
Post by: ben_g on August 26, 2011, 06:25:40 pm
Thanks! I think I will use this. It seems realistic enough. Hopefully I'll have it finished later today.

Edit: The very next page had what I was looking for! Thank you.
You're welcome. And keep in mind that while this is for directional lights (like the sun), you can also let the light come from one point, and then calculate the direction the rays of light hit he surface.

Also: You get the best graphical results when you doo all of the calculations per pixel, instaed of per triangle. This would be slower, but as you said it will be used for creating images of that, I don't think that would be a problem.

good luck
Title: Re: Understanding Shading
Post by: 3rik on August 26, 2011, 11:18:26 pm
I realized I needed more information and found http://www.faculty.iu-bremen.de/llinsen/teaching/320322/lecture11.pdf

It clearly explains everything with pictures and examples before getting into the OpenGL stuff.