3DPrgmer

02-05-2017, 11:57 AM

Hi,

i wrote my shader by following different tutorials (mainly from https://learnopengl.com/)

But now i don't know how to continue.

#ifdef GL_ES

// Set default precision to medium

precision mediump int;

precision mediump float;

#endif

uniform mat3 n_matrix;

uniform vec3 cameraPosition;

uniform sampler2D texture;

uniform sampler2D secondTexture;

uniform float materialShininess;

uniform vec3 materialSpecularColor;

uniform bool b_transparent;

uniform bool b_specular;

uniform bool b_normalmap;

uniform bool b_light;

uniform struct Light {

vec4 position;

vec3 intensities;

float attenuationFactor;

float ambientCoefficient;

} light;

attribute vec3 a_polyNorm;

attribute vec3 a_polyTan;

attribute vec3 a_polyBiTan;

varying vec2 v_surfaceUV;

varying vec3 v_surfacePosition;

varying vec3 v_surfaceNormal;

void main()

{

if(b_light)

{

// some values

mat3 tbn = transpose(mat3(a_polyTan, a_polyBiTan, a_polyNorm));

vec3 finalNormal = normalize(n_matrix * v_surfaceNormal);

if(b_normalmap)

{

finalNormal = texture2D(secondTexture, v_surfaceUV).rgb;

finalNormal = normalize(finalNormal * 2.0 -1.0)

}

vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));

surfaceColor.rgb = pow(surfaceColor.rgb, vec3(2.2));

vec3 surfaceToLight;

float attenuation;

// directional light

if(light.position.w == 0.0f)

{

surfaceToLight = normalize(light.position.xyz);

}

// point light

else

{

surfaceToLight = normalize(light.position.xyz - v_surfacePosition);

}

float distanceToLight = length(light.position.xyz - v_surfacePosition);

attenuation = 1.0 / (1.0 + light.attenuationFactor * pow(distanceToLight, 2));

vec3 surfaceToCamera = normalize(cameraPosition - v_surfacePosition);

// ambient

vec3 ambient = light.ambientCoefficient * surfaceColor.rgb * light.intensities;

// diffuse

float diffuseCoefficient = max(0.0, dot(finalNormal, surfaceToLight));

vec3 diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities;

// specular

float specularCoefficient = 0.0;

if(diffuseCoefficient > 0.0)

specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, finalNormal))), materialShininess);

vec3 specColor;

if(b_specular)

specColor = vec3(surfaceColor.a);

else

specColor = materialSpecularColor;

vec3 specular = specularCoefficient * specColor * light.intensities;

// linear color before gamma correction)

vec3 linearColor = ambient + attenuation * (diffuse + specular);

// final color after gama correction

vec3 gamma = vec3(1.0/2.2);

if(!b_transparent)

surfaceColor.a = 1.0f;

gl_FragColor = vec4(pow(linearColor, gamma), surfaceColor.a);

}

else

{

vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));

if(!b_transparent)

surfaceColor.a = 1.0f;

gl_FragColor = surfaceColor;

}

}

as you can see there are different options.

1) light on/off if the light is not turned on, there won't be specular, and so on.

2) if a specular map is enabled the diffuse alpha channel is used as intensity.

3) now i want to use normalmapping if it is enabled.

i tried to use this tutorial: https://learnopengl.com/#!Advanced-Lighting/Normal-Mapping

But this example code is that unreadable in my eyes. I hope someone can point me the right way to get normal mapping enabled for my shader.

The whole code can be found here: https://git.rwth-aachen.de/carstenf/OpenGL/tree/f47e1cc76a59b8748a766f3f475f8acbb0ebc435/QtMeshViewer

i wrote my shader by following different tutorials (mainly from https://learnopengl.com/)

But now i don't know how to continue.

#ifdef GL_ES

// Set default precision to medium

precision mediump int;

precision mediump float;

#endif

uniform mat3 n_matrix;

uniform vec3 cameraPosition;

uniform sampler2D texture;

uniform sampler2D secondTexture;

uniform float materialShininess;

uniform vec3 materialSpecularColor;

uniform bool b_transparent;

uniform bool b_specular;

uniform bool b_normalmap;

uniform bool b_light;

uniform struct Light {

vec4 position;

vec3 intensities;

float attenuationFactor;

float ambientCoefficient;

} light;

attribute vec3 a_polyNorm;

attribute vec3 a_polyTan;

attribute vec3 a_polyBiTan;

varying vec2 v_surfaceUV;

varying vec3 v_surfacePosition;

varying vec3 v_surfaceNormal;

void main()

{

if(b_light)

{

// some values

mat3 tbn = transpose(mat3(a_polyTan, a_polyBiTan, a_polyNorm));

vec3 finalNormal = normalize(n_matrix * v_surfaceNormal);

if(b_normalmap)

{

finalNormal = texture2D(secondTexture, v_surfaceUV).rgb;

finalNormal = normalize(finalNormal * 2.0 -1.0)

}

vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));

surfaceColor.rgb = pow(surfaceColor.rgb, vec3(2.2));

vec3 surfaceToLight;

float attenuation;

// directional light

if(light.position.w == 0.0f)

{

surfaceToLight = normalize(light.position.xyz);

}

// point light

else

{

surfaceToLight = normalize(light.position.xyz - v_surfacePosition);

}

float distanceToLight = length(light.position.xyz - v_surfacePosition);

attenuation = 1.0 / (1.0 + light.attenuationFactor * pow(distanceToLight, 2));

vec3 surfaceToCamera = normalize(cameraPosition - v_surfacePosition);

// ambient

vec3 ambient = light.ambientCoefficient * surfaceColor.rgb * light.intensities;

// diffuse

float diffuseCoefficient = max(0.0, dot(finalNormal, surfaceToLight));

vec3 diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities;

// specular

float specularCoefficient = 0.0;

if(diffuseCoefficient > 0.0)

specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, finalNormal))), materialShininess);

vec3 specColor;

if(b_specular)

specColor = vec3(surfaceColor.a);

else

specColor = materialSpecularColor;

vec3 specular = specularCoefficient * specColor * light.intensities;

// linear color before gamma correction)

vec3 linearColor = ambient + attenuation * (diffuse + specular);

// final color after gama correction

vec3 gamma = vec3(1.0/2.2);

if(!b_transparent)

surfaceColor.a = 1.0f;

gl_FragColor = vec4(pow(linearColor, gamma), surfaceColor.a);

}

else

{

vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));

if(!b_transparent)

surfaceColor.a = 1.0f;

gl_FragColor = surfaceColor;

}

}

as you can see there are different options.

1) light on/off if the light is not turned on, there won't be specular, and so on.

2) if a specular map is enabled the diffuse alpha channel is used as intensity.

3) now i want to use normalmapping if it is enabled.

i tried to use this tutorial: https://learnopengl.com/#!Advanced-Lighting/Normal-Mapping

But this example code is that unreadable in my eyes. I hope someone can point me the right way to get normal mapping enabled for my shader.

The whole code can be found here: https://git.rwth-aachen.de/carstenf/OpenGL/tree/f47e1cc76a59b8748a766f3f475f8acbb0ebc435/QtMeshViewer