首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修正OpenGL中的“错误0:60样本不能是结构或块成员”?

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

Stack Overflow用户
提问于 2020-11-18 20:56:30
回答 1查看 108关注 0票数 0

在运行代码时,我得到错误"samplers不能是结构或块成员“。

我发现这是因为我的硬件不允许这样做:

代码语言:javascript
复制
struct Material
{
    sampler2D diffuse;
    sampler2D specular;
    float shininess;
};

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

代码语言:javascript
复制
uniform sampler2D Mdiffuse;
uniform sampler2D Mspecular;

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

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

代码语言:javascript
复制
#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);
    
}
EN

回答 1

Stack Overflow用户

发布于 2020-11-23 23:42:47

检查一下,如果你用硬编码的颜色代替纹理的用法( that /MSpecular),那么就会发现一些东西。如果是这样的话,检查您正在加载和绑定纹理正确之前,您的抽签调用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64901246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档