球面贴图(Sphere Map)

         球面贴图用于环境反射(reflection), 纹理坐标的生成与cube map的reflection mode类似:

        

 推导如下:

顶点处理器:

// Sphere Mapping Shader
// Vertex Shader
// Richard S. Wright Jr.
// OpenGL SuperBible
#version 130

// Incoming per vertex... position and normal
in vec4 vVertex;
in vec3 vNormal;

uniform mat4   mvpMatrix;
uniform mat4   mvMatrix;
uniform mat3   normalMatrix;

// Color to fragment program
smooth out vec2 vVaryingTexCoord;


// Generates a sphere map texture coordinate based on the eyespace surface normal
// and the eye space vertex.
vec2 sphereMap(in vec3 normal, in vec3 ecPosition3)
   {
   float m;
   vec3 r, u;
   u = normalize(ecPosition3);
   r = reflect(u, normal);
   m = 2.0 * sqrt(r.x * r.x + r.y * r.y + (r.z + 1.0) * (r.z + 1.0));
   return vec2 (r.x / m + 0.5, r.y / m + 0.5);
   }




void main(void) 
    {
    // Normal in Eye Space
    vec3 vEyeNormal = normalMatrix * vNormal;
    
    // Vertex position in Eye Space
    vec4 vVert4 = mvMatrix * vVertex;
    vec3 vEyeVertex = vVert4.xyz / vVert4.w;
    
    
    // Pass on the texture coordinates 
    vVaryingTexCoord = sphereMap(vEyeNormal, vEyeVertex);
    
    

    // Don't forget to transform the geometry!
    gl_Position = mvpMatrix * vVertex;
    }


片断着色器:

// ADS Point lighting Shader
// Fragment Shader
// Richard S. Wright Jr.
// OpenGL SuperBible
#version 130

out vec4 vFragColor;

uniform sampler2D  sphereMap;

smooth in vec2 vVaryingTexCoord;

void main(void)
    { 
    vFragColor = texture(sphereMap, vVaryingTexCoord);
    }


用于球面贴图的纹理图像:

              在一幅平面纹理图像中对各个方向的颜色进行编码就相当于把一个擦得锃亮的完美球体放在环境的中央,然后在极远处用长焦镜头对它进行拍照。需要编码的区域就是覆盖整个纹理图像的一个圆形区域,它与纹理图像的顶、底、左、右边缘相切。这个圆形区域之外的纹理值不会对结果产生影响,因为它们不会在环境纹理中使用。

              如下图:

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值