c++ - 如何修复 OpenGL 中的“错误 0:60 采样器不能是结构或块成员”?
问题描述
运行代码时,我收到错误“采样器不能是结构或块成员”。
我发现这是因为我的硬件不允许这些行:
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);
}
解决方案
检查是否将纹理(Mdiffuse/MSpecular)的用法替换为实际显示的硬编码颜色。如果是这样,请在绘制调用之前检查您是否正确加载和绑定纹理。
推荐阅读
- php - laravel BelongsToMany 分页
- sql - 如何将多行汇总成单行和单列
- c# - 'SQLite Error 1: 'near "(": syntax error'.' with cross apply in query?
- flutter - 如何在父窗口小部件的回调函数中使用 Navigator.of(context)?
- excel - Multiple criteria formula should be returning multiple matches. But is only returning the first match
- scala - 第一个 jar 创建和执行问题
- kubernetes - Pull images from local (insecure) registry on kind cluster
- r - 在 R 脚本中识别多余的 library() 的快速方法?
- reactjs - 页面刷新时如何加载选定的语言
- mybatis - 如果元素,如何添加不需要的条件子句?