【问题标题】:How to use Image and material together in the shader如何在着色器中同时使用图像和材质
【发布时间】:2019-06-11 05:32:16
【问题描述】:

这是我的纹理着色器程序

 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;
}; uniform Material material;

uniform vec3 viewPos;
uniform Light light;
in vec3 FragPos;  
in vec3 Normal; 
in vec2 TexCoord; 
uniform sampler2D texture1;

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;
FragColor =  texture( texture1 , TexCoord )  * vec4( result , 1.0 );

}

一切都按预期进行。 我的问题是,如果我不发送任何图像数据,对象会呈现黑色,我将遇到对象没有附加纹理的情况。

他们在 glsl 中有什么方法可以解决这个问题,还是我应该向着色器发送一个 bool 变量并应用 if 条件

【问题讨论】:

  • 将白色空纹理绑定到那些 .... 或使用不同的着色器或使用统一来检测无纹理,在这种情况下使用 FragColor = vec4( result , 1.0 ); 或者您可以通过传递渲染面的颜色来检测无纹理然后使用FragColor = uniform_color*vec4( result , 1.0 );

标签: opengl glsl


【解决方案1】:

对于材料的各个方面,您必须以不同的方式修改纹理,例如,如果您有颜色吸收元素,则必须根据此值更改颜色,并且您对材料的各个方面都执行类似的操作,请检查纹理是否存在,如果不存在,就不要渲染它

【讨论】:

    猜你喜欢
    • 2020-08-28
    • 2019-03-08
    • 2021-12-04
    • 2019-03-01
    • 2015-02-16
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多