mobeen

01-15-2011, 06:51 AM

Hi all,

I am trying to replicate the OpenGL fixed func. pipe. (FFP) lighting in a glsl shader. here is what I am doing right now.

#version 330

in vec2 vUV;

in vec3 vVertex;

in vec3 vNormal;

smooth out vec2 vTexCoord;

smooth out vec4 color;

uniform mat3 N;

uniform mat4 MV;

uniform mat4 MVP;

uniform vec4 lightPos;

uniform vec4 mat_ambient;

uniform vec4 mat_diffuse;

uniform vec4 mat_specular;

uniform float mat_shininess;

uniform vec4 light_ambient;

uniform vec4 light_diffuse;

uniform vec4 light_specular;

void main()

{

vTexCoord = vUV;

vec3 Nr = N*vNormal; //transform the normal vector by the normal matrix (inverse transpose of the modelview matrix)

vec4 eyePos = MV*vec4(vVertex,1);

vec3 L = (MV*normalize(lightPos-eyePos)).xyz;

vec4 A = mat_ambient*light_ambient;

float diffuse = max(dot(Nr,L),0.0);

vec3 R = normalize(2 * dot(L, Nr) * Nr - L);

vec3 V = L;

float RDotV = dot(R, V);

vec4 S = light_specular*mat_specular* max( pow(RDotV, mat_shininess), 0.);

vec4 D = diffuse*mat_diffuse*light_diffuse;

color = A + D + S;

gl_Position = MVP*vec4(vVertex,1);

}

But I dont get the same result as the fixed func. code see the attchmt).

I think the problem is the view vector calculation. I am taking the Light vector as the view vector how do i get the eye vector. Note that eyePos var. in the code means the vertex position in eye space.

In eye space, the eye position is (0,0,0,1) right then is my view vector V=normalize(vce4(0,0,0,1)-eyePos);

I am trying to replicate the OpenGL fixed func. pipe. (FFP) lighting in a glsl shader. here is what I am doing right now.

#version 330

in vec2 vUV;

in vec3 vVertex;

in vec3 vNormal;

smooth out vec2 vTexCoord;

smooth out vec4 color;

uniform mat3 N;

uniform mat4 MV;

uniform mat4 MVP;

uniform vec4 lightPos;

uniform vec4 mat_ambient;

uniform vec4 mat_diffuse;

uniform vec4 mat_specular;

uniform float mat_shininess;

uniform vec4 light_ambient;

uniform vec4 light_diffuse;

uniform vec4 light_specular;

void main()

{

vTexCoord = vUV;

vec3 Nr = N*vNormal; //transform the normal vector by the normal matrix (inverse transpose of the modelview matrix)

vec4 eyePos = MV*vec4(vVertex,1);

vec3 L = (MV*normalize(lightPos-eyePos)).xyz;

vec4 A = mat_ambient*light_ambient;

float diffuse = max(dot(Nr,L),0.0);

vec3 R = normalize(2 * dot(L, Nr) * Nr - L);

vec3 V = L;

float RDotV = dot(R, V);

vec4 S = light_specular*mat_specular* max( pow(RDotV, mat_shininess), 0.);

vec4 D = diffuse*mat_diffuse*light_diffuse;

color = A + D + S;

gl_Position = MVP*vec4(vVertex,1);

}

But I dont get the same result as the fixed func. code see the attchmt).

I think the problem is the view vector calculation. I am taking the Light vector as the view vector how do i get the eye vector. Note that eyePos var. in the code means the vertex position in eye space.

In eye space, the eye position is (0,0,0,1) right then is my view vector V=normalize(vce4(0,0,0,1)-eyePos);