From: John Tsiombikas <nuclear@me...>  20120609 14:57:26

On Sat, Jun 09, 2012 at 09:34:18AM +0200, Sylvain wrote: > > Ok world space, go on... are you going to present the benefit of doing > > lighting in world space then, instead of view space where it's much > > simpler and efficient? > > I personnaly prefer world space because I find it easier to understand > and debug, when everything is done in a reference space that doesn't > move. Ok, so we went from "best current practice" to "I personally prefer because I understand it better" :) So let me explain why everyone does shading in view space (unless required to do it in some other weirdo space like tangent space :) First of all OpenGL doesn't have a world space, it's applicationspecific where you draw that line. With shaders you can definitely introduce a worldspace by having two uniforms instead of a single modelview matrix, but that's a far cry from standard practice, and you don't really gain anything by doing that. But most importantly shading in view space simplifies calculations that deal with the view direction (which is required for specular calculations). In view space the view direction is always: vdir = normalize(pos), with pos being the position of the point in space we're shading, the vertex position if we're doing it in the vertex shader, or the interpolated vertex position otherwise. You can also do the even better simplification: vdir = vec3(0, 0, 1), which allows you to drop an expensive normalization. This is equivalent ot setting glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 0); in the fixed function pipeline (which is the default). In world space you would first need to locate the viewer position which is in an arbitrary position in space, by inversetransforming (with the inverse WORLD matrix, reminder: there's no such thing as a world matrix in opengl, you have to make it up) the origin, and then do subtraction and normalization: vdir = normalize(vpos  pos); Bottom line: don't try to teach people things you don't know. If you need a phong shader for your example rewrite, I'll write you a *simple* one without necessary backandforths. After all examples are supposed to be readable. > AFAIK the Unity 3D framework does that. I highly doubt that. All modern games do shading in tangent space, in order to use tangentspace normal maps. I can write you an example for that too if you like. But it can't use the glut shapes, because it would need tangent vectors pervertex.  John Tsiombikas http://nuclear.mutantstargoat.com/ 