片段着色器中的纹理函数给出错误

时间:2019-06-15 11:17:48

标签: opengl glsl

这是我的片段着色器,每当我尝试在纹理函数中使用Cubemap时,都会出现错误消息:

  

0.54未找到匹配函数(使用隐式转换)

     

0.54纹理功能未知。

纹理功能与texture2D一起使用。

#version 330 core
out vec4 FragColor;

 struct Light {
  vec3 direction;     
  vec3 ambient;
  vec3 diffuse;
  vec3 specular;
};

struct Material {
  vec3 ambient;
  vec3 diffuse;
  vec3 specular;
  float shininess;
  float opacity; 
}; uniform Material material;

uniform vec3 viewPos;
uniform Light light;
in vec3 FragPos;  
in vec3 Normal; 
in vec2 TexCoord; 
uniform bool UseColorMap;
uniform sampler2D texture1;
uniform samplerCube texturecubeMap;

void main()
 {  
    vec3 ambient = 0.2 * (light.ambient * material.ambient );

 // diffuse
 vec3 norm = normalize( Normal );
 vec3 lightDir = normalize( -light.direction );
 float diff = max( dot( norm, lightDir) , 0.0 );
 vec3 diffuse = light.diffuse * diff * material.diffuse;

 // specular
  vec3 viewDir = normalize( viewPos - FragPos );
  vec3 reflectDir = reflect( -lightDir , norm );
  float spec = pow(max(dot(viewDir, reflectDir), 0.0), 
  material.shininess);
  vec3 specular = light.specular * spec * material.specular;
  vec3 result = ambient + diffuse + specular;
  vec3 texDiffuseColor =  texture( texture1 , TexCoord ).rgb;

if( !UseColorMap )
 {
    FragColor =  vec4( result ,  material.opacity / 100.0 );
 }
else
 {
 //FragColor =  texture( texture1 , TexCoord )  * vec4( result , 
  material.opacity / 100.0 );  // This works fine
  FragColor =   texture(  texturecubeMap , TexCoord ); // Get error here

 }
};

1 个答案:

答案 0 :(得分:2)

在使用samplerCube采样器的情况下,纹理坐标必须为3维,因为纹理坐标被视为从立方体的中心发出的方向向量(rx ry rz)
造成编译错误是因为TexCoord是二维的。

幸运的是,您已经计算了世界空间中的视角方向:

vec3 viewDir = normalize(viewPos - FragPos);

viewDir是环境图的正确方向向量:

FragColor = texture(texturecubeMap, viewDir);