PDA

View Full Version : Physically based rendering in GLM?



paul_g_griffiths
02-08-2017, 07:19 AM
Any code/examples/tutorials of physically based rendering in GLSL/GLM vector library that I can include in my engine, I've searched google but can't find anything.

Don't want it to bee too complicated though.

Thanks.

paul_g_griffiths
02-08-2017, 08:29 AM
I've found the following code, can someone please tell me what the varying variable is for ? varying vec3 varNormal;

http://ruh.li/GraphicsCookTorrance.html

thanks.


precision highp float; //set default precision in glsl es 2.0

uniform vec3 lightDirection;

varying vec3 varNormal;
varying vec3 varEyeDir;

void main()
{
// set important material values
float roughnessValue = 0.3; // 0 : smooth, 1: rough
float F0 = 0.8; // fresnel reflectance at normal incidence
float k = 0.2; // fraction of diffuse reflection (specular reflection = 1 - k)
vec3 lightColor = vec3(0.9, 0.1, 0.1);

// interpolating normals will change the length of the normal, so renormalize the normal.
vec3 normal = normalize(varNormal);

// do the lighting calculation for each fragment.
float NdotL = max(dot(normal, lightDirection), 0.0);

float specular = 0.0;
if(NdotL > 0.0)
{
vec3 eyeDir = normalize(varEyeDir);

// calculate intermediary values
vec3 halfVector = normalize(lightDirection + eyeDir);
float NdotH = max(dot(normal, halfVector), 0.0);
float NdotV = max(dot(normal, eyeDir), 0.0); // note: this could also be NdotL, which is the same value
float VdotH = max(dot(eyeDir, halfVector), 0.0);
float mSquared = roughnessValue * roughnessValue;

// geometric attenuation
float NH2 = 2.0 * NdotH;
float g1 = (NH2 * NdotV) / VdotH;
float g2 = (NH2 * NdotL) / VdotH;
float geoAtt = min(1.0, min(g1, g2));

// roughness (or: microfacet distribution function)
// beckmann distribution function
float r1 = 1.0 / ( 4.0 * mSquared * pow(NdotH, 4.0));
float r2 = (NdotH * NdotH - 1.0) / (mSquared * NdotH * NdotH);
float roughness = r1 * exp(r2);

// fresnel
// Schlick approximation
float fresnel = pow(1.0 - VdotH, 5.0);
fresnel *= (1.0 - F0);
fresnel += F0;

specular = (fresnel * geoAtt * roughness) / (NdotV * NdotL * 3.14);
}

vec3 finalValue = lightColor * NdotL * (k + specular * (1.0 - k);
gl_FragColor = vec4(finalValue, 1.0);
}

Silence
02-08-2017, 11:04 PM
Any code/examples/tutorials of physically based rendering in GLSL/GLM vector library

PBR is not related to GLM. GLM is just a mathematical library whereas PBR are ways to simulate more accurately how light is interacting with matter, following some real physical laws, instead of doing it the empirical way (Phong, Blinn models).

paul_g_griffiths
02-09-2017, 03:33 AM
I did also state glsl in above post, just if it also used vectors etc I was using glm.

paul_g_griffiths
02-09-2017, 06:40 AM
I've found an example for BRDF here, it's in german or something but the code is English: http://www.mathematik.uni-marburg.de/~thormae/lectures/graphics1/graphics_11_1_ger_web.html#1

Edit: just compared the code with what ive already have at learnopengl.com and it's the same implementation.