2015-04-28 129 views
1

我目前正在学习C++和OpenGL,并想知道下面的代码如何能够将纹理映射到场景中的3D对象。代码目前可行,但我很难理解它。纹理如何映射到3D对象?

//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))); 

回答

2

acos (dot (N, L))获取矢量NL这将是弧度,因此需要由pi被划分在范围texture()期望得到之间的角度。

所有实际纹理魔术在这里发生

fragColor = vec4 (texture (texture1, vec2 (theta, phi))); 

texture()将通过其第二参数定义的位置采样纹理在其第一参数和返回的颜色值作为vec4包含颜色分量(红,绿,蓝色和alpha)。