• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

BRDF gone wrong

3 posts in this topic

Hi all, 


After reading 'bout BRDF I tried to implement it in a shader, but things apparently goes wrong, but don't know why. It's my first attempt to go beyond a basic phong shading, so sorry if the following is a silly question.



First of all here's the model with just the diffusion component:





Now if I activate the specular component as well the result is strange:




The right side of the face is completely blackened and overall it looks strange.  This was with the beckmann distribution, if I switch to Blinn:




Same issue. 



My frag shader (relevant part) is:

float beckmannD(float roughness,float NdotH){
	float roughness2 = roughness*roughness;
	float roughness4 = roughness2*roughness2;
	float NdotH2 = NdotH*NdotH;

	float term1 = 1/(roughness4*NdotH2*NdotH2);
	float expTerm = exp((NdotH2 - 1)/(roughness4*NdotH2));
	return term1*expTerm;

float blinnD(float roughness, float NdotH){
	return (roughness + 2)/2 * pow(NdotH, roughness);

float kelemenG(float NdotL, float NdotV, float LdotV){
	float numerator = 2*NdotL*NdotV;
	float denominator = 1+LdotV;
	return numerator/denominator;

float implicitG(float NdotL, float NdotV){
	return NdotL*NdotV;


void main(void){

vec3 N = texture2D(uNormalSampler, vTextureCoord).xyz;
N =  N*2.0 - 1.0; 
N = normalize(N*uNMatrix);
vec3 L = normalize(uLightPosition-vPosition.xyz);
vec3 V = normalize(-vPosition.xyz);


vec3 H = normalize(V+L);
float NdotH = max(dot(N,H),0.0);
float NdotL = max(dot(N,L),0.0);
float NdotV = max(dot(N,V),0.0);

D = beckmannD(roughness, NdotH);
// or 
D = blinnD(roughness,NdotH);

float LdotV = max(dot(L,V),0.0);
G = kelemenG(NdotL, NdotV, LdotV);
G = implicitG(NdotL,NdotV);

// Note I'm not considering Fresnel yet
specularWeighting = D*G/(3.14*NdotL*NdotV);
vec3 diffuseColor = vec3(1.0,0.6,0.6);
vec3 diffuseReflection = uLightColor * diffuseColor * diffuseWeighting;
vec3 specularReflection = specularColor * specularWeighting;

fragmentColor = vec4(uAmbientColor+diffuseReflection+specularReflection,1.0);


What can be? Thank you very much, and sorry for the probably silly issue.


Share this post

Link to post
Share on other sites

The most common cause of NaN in a shader is division by 0. In your case you will get division by 0 whenever NdotL or NdotV is 0, since your denominator has those terms in in it. To make that work with your current setup, you would need to wrap your specular calculations in an if statement that checks if both N dot L and N dot V are greater than 0. However in many cases it's possible to write your code in such a way that there's no chance of division by 0. For instance, take your "implicit G" function. This is meant to cancel out the NdotL * NdotV in the denominator by putting the same terms in the numerator. So in that case, it would be better if you canceled it out in your code by removing the implicitG function and then also removing the N dot L and N dot V from the denominator. 


Also I should point out another common mistake that you're making, which is that you need to multiply your entire BRDF by NdotL. If you look up the definition of the BRDF, you'll find that it's the ratio of lighting scattered towards the eye (which is the value you're computing in your fragment shader) relative to the irradiance incident to the surface. When you're dealing point lights/spot lights/directional lights/etc. the irradiance is equal to LightIntensity * LightAttenuation * Shadowing * NdotL. In your case you don't have shadows and you don't seem to be using an attenuation factor (which is fine), you 'll want to multiply your specular by (uLightColor * NdotL). A lot of people tend to associate the NdotL with diffuse, but really it's not part of the diffuse BRDF. A lambertian diffuse BRDF is actually just a constant value, the NdotL is part of the irradiance calculations.


Oh thank you very much! I indeed re-wrote everything (that snippet is an extract) so to  avoid division as much as possible and it now works like a charm. As for the NdotL issue thank you for pointing out, I did know that on back of my head but I probably got confused by what was in a old piece of code of mine! 


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0