View Full Version : Interpolating reflection with defeuse in fragment shader

02-06-2016, 03:17 PM
I am trying to implement a Blinn-Phong, Cool-Torrance, and GGX light models. I have implemented the first 2 light models but now I trying to fix and figure out how to do interpolation between diffuse and shininess.

My scene will consists of 3 rows with 10 spheres where each sphere has shininess factor .1 bigger and a cube map as reflection/diffuse texture.

I am trying to find something useful on how to do this interpolation but i guess i am googling the wrong thing now.

My fragment shader code now looks like this. PS, I am sorry for all the comments left in the code but I believe it will help you see what I was trying to do.

#version 410 core

#include "../Global/GlobalShader.inc"
#include "../Global/GlobalMesh.inc"
#include "../Global/GlobalLight.inc"

in vec3 Position;
in vec3 Normal;
in vec2 TexCoord;

uniform vec3 cameraPosition;
uniform samplerCube skybox;
uniform DirectionalLight directionalLight;

float specularStrength = 16.0; // to be impelemented

out vec4 gl_FragColor;

vec4 calculateDirectionalLight1(Light light, vec3 direction, vec3 normal, vec3 worldPosition, float specularIntensity, vec3 eyePosition, Material material, vec2 texCoord, vec4 diffuse)
vec3 diffuseFactor = (light.color * diffuse * (light.intensity * clamp(dot(normal, direction), 0.0, 1.0)));

vec3 viewDir = normalize(eyePosition - worldPosition);
vec3 reflectDir = normalize(reflect(-direction, normal));

float shininess = (material.shininess / 1000.0);
float specularFactor = pow(clamp(dot(viewDir, reflectDir), 0.0, 1.0), specularIntensity);
vec3 specularColor = (light.color * material.specular) * (specularFactor * shininess);

//return vec4(pow((diffuseFactor + specularColor + light.ambient), GAMMA), 1.0);
return vec4(pow((diffuseFactor + specularColor), GAMMA), 1.0);

//return specularColor;
//return diffuseFactor;
//return shininess;
//return diffuse;

void main() {

vec4 tempColor = vec4(0.2);
//vec4 tempColor = vec4(1.0, 0.0, 0.0, 0.0);

//vec4 diffuse = vec4(texture(material.texture.diffuse, TexCoord.st).rgb, 1.0); // *material.diffuse) - vec4(1.0 - material.specular);

vec3 I = normalize(Position - cameraPosition);
vec3 R = reflect(I, normalize(Normal));
vec4 reflection = texture(skybox, R);

// fix blending/interpolation for light
float shininess = (material.shininess / 1000.0);
//gl_FragColor = diffuse * (reflection * shininess) * material.specular;
//vec4 tempFinalDiffuse = mix(diffuse, reflection, shininess);
vec4 tempFinalDiffuse = mix(tempColor, reflection, shininess);

vec4 light = vec4(1.0);
light = calculateDirectionalLight1(directionalLight.light, directionalLight.position, Normal, Position, specularStrength, cameraPosition, material, TexCoord, tempFinalDiffuse);

//gl_FragColor = mix(tempFinalDiffuse, light, shininess);
//gl_FragColor = diffuse;
//gl_FragColor = vec4(shininess);
//gl_FragColor = tempFinalDiffuse * light;
//gl_FragColor = reflection;
gl_FragColor = light;
//gl_FragColor = tempFinalDiffuse;