Mukund

10-19-2011, 09:20 AM

Hello everyone,

i wrote this simple shader for some lighting. The calculation of the specular component is borrowed from the Orange book.

Here is the vertex shader:

I'm trying a simple per-vertex lighting. I'm trying to implement a directional light.

Light Direction = (1.0, 0.0, 0.0, 0.0)

uniform vec4 lAmbient;

uniform vec4 mAmbient;

uniform vec4 lDiffuse;

uniform vec4 mDiffuse;

uniform vec4 lightPos;

uniform float waveAmp;

varying vec4 color;

const float SpecularContribution = 0.1;

void main(void)

{

float spec = 0.0;

vec3 normal = normalize(gl_NormalMatrix * gl_Normal);

float lambertTerm = dot( vec4(normal, 0.0), lightPos );

vec4 ambColor = lAmbient * mAmbient;

vec4 diffColor = lDiffuse * mDiffuse * lambertTerm;

vec3 viewVec = normalize(-gl_Vertex.xyz);

vec3 lightVec = normalize(vec3(lightPos)-

gl_Vertex.xyz);

vec3 reflectVec = reflect(-lightVec, normal);

if (lambertTerm > 0.0) {

spec = max(dot(reflectVec, viewVec), 0.0);

spec = pow(spec, 16.0);

}

color = (ambColor + diffColor)

+ (vec4(1.0, 1.0, 1.0, 1.0) * spec);

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

}

The problem is, when i rotate the object, the light source seems to move too. Also, i'm not able to get good specular highlights. Any tips on improving it?

Here is the PIC (http://imageshack.us/photo/my-images/80/unledyq.png/)

Thanks!

i wrote this simple shader for some lighting. The calculation of the specular component is borrowed from the Orange book.

Here is the vertex shader:

I'm trying a simple per-vertex lighting. I'm trying to implement a directional light.

Light Direction = (1.0, 0.0, 0.0, 0.0)

uniform vec4 lAmbient;

uniform vec4 mAmbient;

uniform vec4 lDiffuse;

uniform vec4 mDiffuse;

uniform vec4 lightPos;

uniform float waveAmp;

varying vec4 color;

const float SpecularContribution = 0.1;

void main(void)

{

float spec = 0.0;

vec3 normal = normalize(gl_NormalMatrix * gl_Normal);

float lambertTerm = dot( vec4(normal, 0.0), lightPos );

vec4 ambColor = lAmbient * mAmbient;

vec4 diffColor = lDiffuse * mDiffuse * lambertTerm;

vec3 viewVec = normalize(-gl_Vertex.xyz);

vec3 lightVec = normalize(vec3(lightPos)-

gl_Vertex.xyz);

vec3 reflectVec = reflect(-lightVec, normal);

if (lambertTerm > 0.0) {

spec = max(dot(reflectVec, viewVec), 0.0);

spec = pow(spec, 16.0);

}

color = (ambColor + diffColor)

+ (vec4(1.0, 1.0, 1.0, 1.0) * spec);

gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

}

The problem is, when i rotate the object, the light source seems to move too. Also, i'm not able to get good specular highlights. Any tips on improving it?

Here is the PIC (http://imageshack.us/photo/my-images/80/unledyq.png/)

Thanks!