Sign in to follow this  
knownonlyash

two sided light model with glsl

Recommended Posts

hi everyone hope someone can help me i've just recently started glsl as part of project and am using 3ds models which require a two sided light model. I have the book by randi rost and have put together a vertex shader but i have no idea if its right and no idea what goes into the fragment shader. This is what i have so far (taken from chapter 9 of the orange book) vertex shader

varying vec3 normal;

void DirectionalLight(in int i, in vec3 normal, inout vec4 ambient, inout vec4 diffuse, inout vec4 specular)
{	
float nDotVP;		// normal . light direction	
float nDotHV;		// normal . light half vector	
float pf;		// power factor	

nDotVP = max(0.0, dot(normal, vec3(gl_LightSource[0].position)));
nDotHV = max(0.0, dot(normal, vec3(gl_LightSource[0].halfVector)));	

if(nDotVP == 0.0)		
     pf = 0.0;	
else		
     pf = pow(nDotHV, gl_FrontMaterial.shininess);	

     ambient += gl_LightSource[0].ambient;	
     diffuse += gl_LightSource[0].diffuse;	
     specular += gl_LightSource[0].specular * pf;
}	

void main(void)
{	
// Vertex transformation	
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;	

// Normal transformation	
normal = gl_NormalMatrix * gl_Normal;	
normal = normalize(normal);		

vec4 amb;	
vec4 diff;	
vec4 spec;	
vec4 color;	

// Clear light intensity accumulators	
amb = vec4(0.0);	
diff = vec4(0.0);	
spec = vec4(0.0);	

if(gl_LightSource[0].position.w == 0.0)		
     DirectionalLight(0, normal, amb, diff, spec);	

color = gl_FrontLightModelProduct.sceneColor +	
amb * gl_FrontMaterial.ambient +		
diff * gl_FrontMaterial.diffuse;	

color += spec * gl_FrontMaterial.specular;	
gl_FrontColor = color;		

// Compute back sided surface	
normal = -normal;	

// CLear light intensity accumulators
amb = vec4(0.0);	
diff = vec4(0.0);	
spec = vec4(0.0);	

if(gl_LightSource[0].position.w == 0.0)		
    DirectionalLight(0, normal, amb, diff, spec);	

color = gl_BackLightModelProduct.sceneColor + amb * gl_BackMaterial.ambient +		diff * gl_BackMaterial.diffuse;	

color += spec * gl_BackMaterial.specular;	
gl_BackColor = color;}



fragment shader

varying vec3 normal;void main(void){}

any help would be much appreciated thanks in advance matt

Share this post


Link to post
Share on other sites
Try just compiling the vertex shader, and put it in a shader program without the fragment shader. See what the fixed function frag-processor does with it.

--Buzzy

Share this post


Link to post
Share on other sites
Hi, I just tried running that vertex shader in my game engine and it seemed to compile and run fine.. only problem is it crashed when I tried to use it on it's own without a fragment shader... are there any websites with glsl fragment shaders that replicate the fixed function pipeline? the only fragment shader I've got to test it with are silly ones using random colors and sine waves :)

Share this post


Link to post
Share on other sites
I'm pretty sure that the fixed function fragment processor just passes on the interpolated color from the vertex processor:
ie. void main(void){ gl_FragColor = gl_Color; }

Since you want two sided coloring, you'll have to enable it by calling glEnable with GL_VERTEX_PROGRAM_TWO_SIDE_ARB. This will make the fragment shader pick the right color based on whether the incoming fragment is from a front facing or back facing primitive.

Since you have the Orange Book, I'll just let you know that I'm getting this info from chapter 4, section 2 and 5.1. There's also a mention of a special input variable called gl_FrontFacing in 4.2.3 which you may find handy.

--Buzzy

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