1
\$\begingroup\$

My fragment shader compilers fine but the my app crashes when i use it.

My bug hunting deducted that the shader is linking fine. Also it's curious that striping down the core and removing everything and outputting just a colour is showing the model in one colour off course.

What is wrong?

Fragment shader:

#version 330 core
const float PI = 3.14159265359;
out vec4 coloroutfinal;


in vec3 fragpos;
uniform vec3 lightpos;
uniform vec3 camerapos;


vec3 wi;
vec3 normal;
float cosT;
uniform vec3 lightcolor;
vec3 incidentlight;


uniform float metallic;
uniform float roughness;
uniform vec3 albedo;
uniform float ao;

vec3 fresnel(float cosT,vec3 Fo);
float ND(vec3 H,vec3 N,float roughness);
float G(vec3 L,vec3 N,vec3 V,float roughness);

void main()
{
  wi=normalize(lightpos-fragpos);
  normal=normalize(fragpos);
  cosT=max(dot(normal,wi),0.0);
  float attenuation=1.0/pow(length(lightpos-fragpos),2);
  incidentlight=attenuation*cosT*lightcolor;
  vec3 V=normalize(camerapos-fragpos); 
  vec3 H=normalize(V+wi);


  vec3 Fo=vec3(0.04);
  Fo=mix(Fo,albedo,metallic);
  vec3 F=fresnel(max(dot(H,V),0.0),Fo);

  float ND=ND(H,normal,roughness);
  float G=G(wi,normal,V,roughness);

  vec3 BRDFn=F*ND*G;
  float BRDFd=4*max(dot(normal,wi),0.0)*max(dot(normal,V),0.0)+0.001;
  vec3 specular=BRDFn/BRDFd;

  vec3 KS=F;
  vec3 KD=1.0-KS;
  KD*=(1.0-metallic);

  vec3 lo=(KD*albedo/PI+specular)*incidentlight;
  vec3 colorout=lo+(ao*albedo*vec3(0.05));

  colorout=colorout/(colorout+1.0);
  colorout=pow(colorout,vec3(1/2.2));
  coloroutfinal=vec4(colorout,0.0);

}

vec3 fresnel(float cosT,vec3 Fo)
{
  return Fo+(1.0-Fo)*pow((1-cosT),5);
}

float ND(vec3 H,vec3 N,float roughness)
{
   float roughness4=pow(roughness,4);
   float cosT=max(dot(N,H),0.0);
   float cosT2=cosT*cosT;
   float num=roughness4;
   float denom=cosT2*(1.0-roughness4)+1.0;
   return num/denom;
}

float GP(float cosT,float roughness)
{
roughness-=1.0;
   float r2=roughness*roughness;
   float k=r2/8;

float num=cosT;
float denom=cosT*(1.0-k)+k;
 return num/denom;

}

float G(vec3 L,vec3 N,vec3 V,float roughness)
{
  float cosTL=max(dot(N,L),0.0);
  float cosTV=max(dot(N,V),0.0);

  float GL=GP(cosTL,roughness);
  float GV=GP(cosTV,roughness);
  return GL*GV;
}
\$\endgroup\$
1
  • \$\begingroup\$ You potentially have a divide by zero in the GP function if roughness == 1 and cosT == 0 \$\endgroup\$
    – megadan
    May 7 '17 at 20:40
1
\$\begingroup\$

I found the problem. The shaders reader had the buffer size set too small.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .