HELP: GLSL Per Pixel Lighting

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
clarks
Posts: 35
Joined: Sat Jul 28, 2012 5:23 am

HELP: GLSL Per Pixel Lighting

Post by clarks »

I have a model of a vehicle that I am trying to shade using per pixel lighting but its not working.

I have the following vertex shader

Code: Select all

// bring in data
uniform vec3 lightPos;
 
// send to fragment shader
out vec3 Position;
out vec3 Normal;
out vec4 Color;
 
void main()
{
    // compute the normal
    Normal = normalize( gl_NormalMatrix * gl_Normal );
    
    // compute the position
    Position = (gl_ModelViewMatrix * gl_Vertex).xyz;
    
    // get the color
    Color = gl_Color;
    // gl_FrontColor = gl_Color;
    
    // calculate the position
    // transform the vertex
    // gl_Position = ftransform();
    gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
}
I have the following fragment shader

Code: Select all

// uniform sampler2D reflMap;
uniform vec3 lightPos;
 
in vec3 Position;
in vec3 Normal;
in vec4 Color;
 
// write the ads function
vec3 ads()
{
    vec3 n = normalize( Normal );
    vec3 s = normalize( lightPos - Position.xyz );
    vec3 v = normalize( vec3(-Position) );
    vec3 r = reflect( -s, n );
    
    // get the ambient
    // vec3 ambient = gl_LightSource[0].ambient.xyz * gl_FrontMaterial.ambient.xyz;
    //ambient
    // get the diffuse
    /*vec3 diffuse = gl_LightSource[0].diffuse.xyz * gl_FrontMaterial.diffuse.xyz *
        max( dot( s, n ), 0.0 );*/
    vec3 diffuse = Color.xyz * max( dot( s, n ), 0.0 );
        
    // get the specular
    /*vec3 specular = gl_LightSource[0].specular.xyz * gl_FrontMaterial.specular.xyz *
        pow( max( dot(r,v), 0.0 ), gl_FrontMaterial.shininess );*/
    vec3 specular = vec3(1.0,1.0,1.0)*pow( max( dot(r,v), 0.0 ), 10 );
        
    // get the light intensity
    // return (ambient + diffuse + specular)*gl_Color.xyz;
    return diffuse + specular;
}
 
void main()
{
    gl_FragColor = vec4(ads(),1.0);
}
My vertex card currently supports GLSL 1.5, I know that somethings have been deprecated. Any help is appreciated. I have a vehicle that I want to perform per pixel lighting on. GLSL spec says that the shade model affects how values are interpolated (smooth or flat) in the fragment shader. If the mesh has smooth shading enabled it still does not look like per pixel lighting. I do not understand what Open GL is doing in the background.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: HELP: GLSL Per Pixel Lighting

Post by mongoose7 »

You haven't said what the problem is. Is 'lightPos' in world or view coordinates? I'm guessing it is world coordinates. But 'Position' is in view coordinates!

BTW Your shaders are written in GLSL 1.10, so the fact that your card supports GLSL 1.5 is neither here nor there.
clarks
Posts: 35
Joined: Sat Jul 28, 2012 5:23 am

Re: HELP: GLSL Per Pixel Lighting

Post by clarks »

Well thanks for responding. lightPos is in world coordinates as a directional light pointing down. Here is the problem: I am using blender to export an obj file. If I export with flat shading then the model is rendered flat shaded. If I export with smooth shading, the model is rendered smooth. The vertex normals are different depending on whether or not the mesh is flat or smooth. When it gets rendered, its as if the shader has to effect. The per pixel lighting does not seem to work. I know that somewhere in the irrlicht GLDriver, its sets the shade model using glShadeModel, but I do not want that to determine whether or not my model is flat or smooth, because it can get confusing when trying to debug problems. I am trying to aim for a GT5 type shader look. Any help would be appreciated, let me know if you need more info.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: HELP: GLSL Per Pixel Lighting

Post by mongoose7 »

Try setting gl_FragColor = vec4(0, 1, 0, 1).

Alternatively, try replacing the shader in Example 10 with your own.
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Re: HELP: GLSL Per Pixel Lighting

Post by ent1ty »

If you have recently updated to the 1.9 trunk from 1.8, beware that shader callbacks are now done differently
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Re: HELP: GLSL Per Pixel Lighting

Post by christianclavet »

Hi, what are thoses?

Code: Select all

// send to fragment shader
out vec3 Position;
out vec3 Normal;
out vec4 Color;
I would normally do this in GLSL

Code: Select all

varying vec3 Position;
varying vec3 Normal;
varying vec4 Color;
I am trying to shade using per pixel lighting but its not working.
What is not working? Rendering or compilation?
I am trying to aim for a GT5 type shader look.
Looking at the screenshots of GT5; your shader will need to support reflection of some sort (cubemap / spherical harmonics), and you will need to have a post process shader to create "bloom"
Post Reply