Reputation: 597
I'm currently learning C++ and OpenGL and was wondering how the below code is able to map a texture to a 3D object within a scene. The code currently works, but I'm struggling to understand it.
//In the Pixel Shader.
float pi = 3.14159265359;
vec3 N = normalize (Normal);
vec3 L = normalize (vec3 (EyeSpaceLightPosition - EyeSpacePosition));
vec3 R = reflect (L, N);
vec3 V = normalize (vec3 (EyeSpacePosition) - vec3 (0, 0, 0));
//Specifically these parts here.
float theta = acos (dot ( N, L )) / pi;
float phi = acos (dot ( -R, V )) / pi;
fragColor = vec4 (texture (texture1, vec2 ( theta, phi )));
Upvotes: 1
Views: 142
Reputation: 48196
acos (dot ( N, L ))
gets the angle between the vectors N
and L
this will be in radians so it needs to be divided by pi
to get in the range texture()
expects.
All the actual texturing magic happens here
fragColor = vec4 (texture (texture1, vec2 ( theta, phi )));
texture()
will sample the texture in its first parameter at location defined by its second parameter and return the color value as a vec4 containing the color components (red, green, blue and alpha).
Upvotes: 2