如何修复 OpenGL 中的“错误 0:60 采样器不能是结构或块成员”?

Posted

技术标签:

【中文标题】如何修复 OpenGL 中的“错误 0:60 采样器不能是结构或块成员”?【英文标题】:How can i fix "Error 0:60 samplers cannot be structure or block members" in OpenGL? 【发布时间】:2020-11-18 20:56:30 【问题描述】:

运行代码时,我收到错误“采样器不能是结构或块成员”。

我发现这是因为我的硬件不允许这些行:

struct Material

    sampler2D diffuse;
    sampler2D specular;
    float shininess;
;

我的直接解决方案是将两个 sampler2D 对象都声明为“统一”

uniform sampler2D Mdiffuse;
uniform sampler2D Mspecular;

然后将使用对象的每一行更改为它们的新名称(而不是 material.diffuse 和 material.specular)。虽然这确实消除了错误,但仍未绘制对象。

如果有必要,这里是完整的片段着色器(原始的,没有我的更改)。

#version 330 core

#define NUMBER_OF_POINT_LIGHTS 4

struct Material

    sampler2D diffuse;
    sampler2D specular;
    float shininess;
;

struct DirLight

    vec3 direction;
    
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
;

struct PointLight

    vec3 position;
    
    float constant;
    float linear;
    float quadratic;
    
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
;

struct SpotLight

    vec3 position;
    vec3 direction;
    float cutOff;
    float outerCutOff;
    
    float constant;
    float linear;
    float quadratic;
    
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
;

in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;

out vec4 color;

uniform vec3 viewPos;
uniform DirLight dirLight;
uniform PointLight pointLights[NUMBER_OF_POINT_LIGHTS];
uniform SpotLight spotLight;
uniform Material material;




// Function prototypes
vec3 CalcDirLight( DirLight light, vec3 normal, vec3 viewDir );
vec3 CalcPointLight( PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir );
vec3 CalcSpotLight( SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir );

void main( )

    // Properties
    vec3 norm = normalize( Normal );
    vec3 viewDir = normalize( viewPos - FragPos );
    
    // Directional lighting
    vec3 result = CalcDirLight( dirLight, norm, viewDir );
    
    // Point lights
    for ( int i = 0; i < NUMBER_OF_POINT_LIGHTS; i++ )
    
        result += CalcPointLight( pointLights[i], norm, FragPos, viewDir );
    
    
    // Spot light
    result += CalcSpotLight( spotLight, norm, FragPos, viewDir );
    
    
    /*color = vec4( result,texture( material.diffuse, TexCoords).a );
      if(color.a < 0.1)
        discard;*/


// Calculates the color when using a directional light.
vec3 CalcDirLight( DirLight light, vec3 normal, vec3 viewDir )

    vec3 lightDir = normalize( -light.direction );
    
    // Diffuse shading
    float diff = max( dot( normal, lightDir ), 0.0 );
    
    // Specular shading
    vec3 reflectDir = reflect( -lightDir, normal );
    float spec = pow( max( dot( viewDir, reflectDir ), 0.0 ), material.shininess );
    
    // Combine results
    vec3 ambient = light.ambient * vec3( texture( material.diffuse, TexCoords ).rgb );
    vec3 diffuse = light.diffuse * diff * vec3( texture( material.diffuse, TexCoords ).rgb );
    vec3 specular = light.specular * spec * vec3( texture( material.specular, TexCoords ).rgb );

    
    /*vec4 result= vec4(ambient + diffuse + specular,texture( material.diffuse, TexCoords).a) ;
      if(result.a < 0.1)
        discard;*/
            vec3 result=ambient + diffuse + specular;

    return (result);


// Calculates the color when using a point light.
vec3 CalcPointLight( PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir )

    vec3 lightDir = normalize( light.position - fragPos );
    
    // Diffuse shading
    float diff = max( dot( normal, lightDir ), 0.0 );
    
    // Specular shading
    vec3 reflectDir = reflect( -lightDir, normal );
    float spec = pow( max( dot( viewDir, reflectDir ), 0.0 ), material.shininess );
    
    // Attenuation
    float distance = length( light.position - fragPos );
    float attenuation = 1.0f / ( light.constant + light.linear * distance + light.quadratic * ( distance * distance ) );
    
    // Combine results
    vec3 ambient = light.ambient * vec3( texture( material.diffuse, TexCoords ).rgb );
    vec3 diffuse = light.diffuse * diff * vec3( texture( material.diffuse, TexCoords ).rgb );
    vec3 specular = light.specular * spec * vec3( texture( material.specular, TexCoords ).rgb );

    
    ambient *= attenuation;
    diffuse *= attenuation;
    specular *= attenuation;

  /* vec4 result= vec4(ambient + diffuse + specular,texture( material.diffuse, TexCoords).a) ;
      if(result.a < 0.1)
        discard;*/
        vec3 result=ambient + diffuse + specular;

    return (result);
    


// Calculates the color when using a spot light.
vec3 CalcSpotLight( SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir )

    vec3 lightDir = normalize( light.position - fragPos );
    
    // Diffuse shading
    float diff = max( dot( normal, lightDir ), 0.0 );
    
    // Specular shading
    vec3 reflectDir = reflect( -lightDir, normal );
    float spec = pow( max( dot( viewDir, reflectDir ), 0.0 ), material.shininess );
    
    // Attenuation
    float distance = length( light.position - fragPos );
    float attenuation = 1.0f / ( light.constant + light.linear * distance + light.quadratic * ( distance * distance ) );
    
    // Spotlight intensity
    float theta = dot( lightDir, normalize( -light.direction ) );
    float epsilon = light.cutOff - light.outerCutOff;
    float intensity = clamp( ( theta - light.outerCutOff ) / epsilon, 0.0, 1.0 );
    
    // Combine results
    vec3 ambient = light.ambient * vec3( texture( material.diffuse, TexCoords ).rgb );
    vec3 diffuse = light.diffuse * diff * vec3( texture( material.diffuse, TexCoords ).rgb );
    vec3 specular = light.specular * spec * vec3( texture( material.specular, TexCoords ).rgb );

    
    ambient *= attenuation * intensity;
    diffuse *= attenuation * intensity;
    specular *= attenuation * intensity;

    /*  vec4 result= vec4(ambient + diffuse + specular,texture( material.diffuse, TexCoords).a) ;
      if(result.a < 0.1)
        discard;*/

        vec3 result=ambient + diffuse + specular;

    return (result);
    

【问题讨论】:

它说采样器不能在块内。所以不要将采样器放在块内。而且你的着色器没有设置颜色,所以它怎么知道要画什么? @user253751 阅读完整的问题。 @Rabbid76 问题的第二部分是“没有绘制对象,为什么?” @user253751 抱歉,您的评论没有任何意义。 【参考方案1】:

检查是否将纹理(Mdiffuse/MSpecular)的用法替换为实际显示的硬编码颜色。如果是这样,请在绘制调用之前检查您是否正确加载和绑定纹理。

【讨论】:

以上是关于如何修复 OpenGL 中的“错误 0:60 采样器不能是结构或块成员”?的主要内容,如果未能解决你的问题,请参考以下文章

如何仅旋转特定对象而不影响opengl中的其他对象?

如何在 C++ 中使用 sdl、opengl 移动相机时修复奇怪的相机旋转

如何修复 google play console 不支持的设备(OpenGL ES 和框架版本)

如何在OpenGL中反转纹理颜色

OpenGL 4.3 程序 Ubuntu 14.04 x64 中的时间变量冲突问题

Qt 是不是使用 opengl=angle 修复支持不支持 opengl 2.0 的 Windows 客户端?