abhishek bansal

12-19-2011, 08:41 AM

Hello people,

I am trying to develop a basic Ray Tracer. So far i have calculated intersection with a plane and blinn-phong shading. My problem is that i m getting some specular highlights on plane which i think should not be there (i am not sure of).

i am working on a 500*500 window and my primary ray generation code is as follows

vec3 rayDirection = vec3( gl_FragCoord.x-250.0,250.0-gl_FragCoord.y , 10.0);

code for finding out intersection of ray with plane..

int planeIntersect(vec3 rayDir, vec3 rayOrigin, out float t1)

{

vec3 planeNormal = vec3(0.0,1.0,0.0);

vec3 planePoint = vec3(0.0,-10.0,0.0);

// rayDir= normalize(rayDir);

if(dot(planeNormal, rayDir ) > 0)

{

t1 = -dot(planeNormal, (rayOrigin - planePoint)) / dot(planeNormal,rayDir);

return 1;

}

else

return 0; //no intersection or intersection behind camera

}

then if a intersection is found i compute final color as follows...

if(planeIntersect(rayDirection, camera, t1) == 1)

{

//calculating point of intersection

vec3 iPoint,iNormal;

iPoint.x = camera.x + rayDirection.x * t1;

iPoint.y = camera.y + rayDirection.y * t1;

iPoint.z = camera.z + rayDirection.z * t1;

//calculating normal at point of intersection

iNormal = vec3 (0.0,1.0,0.0);

lightColor = computeLight(iPoint, iNormal);

if(textureEnabled==1)

wTex = texture2D(wallTex,gl_TexCoord[0].st).rgb;

//lightColor = computeLight();

if(textureEnabled==1)

lightColor = lightColor * wTex;

gl_FragColor = vec4(lightColor,1.0);

// gl_FragColor = vec4(1.0,0.0,0.0,1.0);

}

else {

//discard;

gl_FragColor = vec4(0.0,0.0,1.0,1.0);

}

Finally light computation.....

vec3 computeLight(vec3 iPoint, vec3 iNormal)

{

vec3 normal,lightDir,halfVector;

vec3 n,halfV,viewV,ldir;

float NdotL,NdotHV;

vec4 diffuse1, ambient1;

lightDir = normalize(lightPosition - iPoint);

viewV = normalize (camera - iPoint);

halfVector = normalize(lightDir + viewV);

/* Compute the diffuse, ambient and globalAmbient terms */

diffuse1 = mat_diffuse* gl_LightSource[0].diffuse;

ambient1 = mat_ambient * gl_LightSource[0].ambient;

ambient1 += gl_LightModel.ambient * mat_ambient;

/* a fragment shader can't write a verying variable, hence we need

a new variable to store the normalized interpolated normal */

n = normalize(iNormal);

vec4 color = ambient1;

/* compute the dot product between normal and ldir */

NdotL = max(dot(n,lightDir),0.0);

if (NdotL > 0.0) {

// halfV = normalize(halfVector);

NdotHV = max(dot(n,halfVector),0.0);

color += mat_specular * gl_LightSource[0].specular * pow(NdotHV,shininess);

color += diffuse1 * NdotL;

}

return color.rgb;

}

material and light properties are as follows..

vec3 lightPosition = vec3 (0.0, 15.0, 0.0);

vec4 mat_ambient = vec4( 0.7, 0.0, 0.0, 1.0 );

vec4 mat_diffuse = vec4( 0.7, 0.0, 0.0, 1.0 );

vec4 mat_specular = vec4(0.0,1.0,0.0,1.0);

vec3 camera = vec3(0.0,5.0,30.0);

float shininess = 10;

//light properties

GLfloat specular[] = { 0.0, 1.0, 0.0, 1.0 };

GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };

GLfloat diffuse[] = { 0.7, 0.7, 0.7, 1.0 };

Please tell me if my output is even right or wrong..!!

Please find my output attached for reference..

Thanks in Advance..:)

Edit: File Added

I am trying to develop a basic Ray Tracer. So far i have calculated intersection with a plane and blinn-phong shading. My problem is that i m getting some specular highlights on plane which i think should not be there (i am not sure of).

i am working on a 500*500 window and my primary ray generation code is as follows

vec3 rayDirection = vec3( gl_FragCoord.x-250.0,250.0-gl_FragCoord.y , 10.0);

code for finding out intersection of ray with plane..

int planeIntersect(vec3 rayDir, vec3 rayOrigin, out float t1)

{

vec3 planeNormal = vec3(0.0,1.0,0.0);

vec3 planePoint = vec3(0.0,-10.0,0.0);

// rayDir= normalize(rayDir);

if(dot(planeNormal, rayDir ) > 0)

{

t1 = -dot(planeNormal, (rayOrigin - planePoint)) / dot(planeNormal,rayDir);

return 1;

}

else

return 0; //no intersection or intersection behind camera

}

then if a intersection is found i compute final color as follows...

if(planeIntersect(rayDirection, camera, t1) == 1)

{

//calculating point of intersection

vec3 iPoint,iNormal;

iPoint.x = camera.x + rayDirection.x * t1;

iPoint.y = camera.y + rayDirection.y * t1;

iPoint.z = camera.z + rayDirection.z * t1;

//calculating normal at point of intersection

iNormal = vec3 (0.0,1.0,0.0);

lightColor = computeLight(iPoint, iNormal);

if(textureEnabled==1)

wTex = texture2D(wallTex,gl_TexCoord[0].st).rgb;

//lightColor = computeLight();

if(textureEnabled==1)

lightColor = lightColor * wTex;

gl_FragColor = vec4(lightColor,1.0);

// gl_FragColor = vec4(1.0,0.0,0.0,1.0);

}

else {

//discard;

gl_FragColor = vec4(0.0,0.0,1.0,1.0);

}

Finally light computation.....

vec3 computeLight(vec3 iPoint, vec3 iNormal)

{

vec3 normal,lightDir,halfVector;

vec3 n,halfV,viewV,ldir;

float NdotL,NdotHV;

vec4 diffuse1, ambient1;

lightDir = normalize(lightPosition - iPoint);

viewV = normalize (camera - iPoint);

halfVector = normalize(lightDir + viewV);

/* Compute the diffuse, ambient and globalAmbient terms */

diffuse1 = mat_diffuse* gl_LightSource[0].diffuse;

ambient1 = mat_ambient * gl_LightSource[0].ambient;

ambient1 += gl_LightModel.ambient * mat_ambient;

/* a fragment shader can't write a verying variable, hence we need

a new variable to store the normalized interpolated normal */

n = normalize(iNormal);

vec4 color = ambient1;

/* compute the dot product between normal and ldir */

NdotL = max(dot(n,lightDir),0.0);

if (NdotL > 0.0) {

// halfV = normalize(halfVector);

NdotHV = max(dot(n,halfVector),0.0);

color += mat_specular * gl_LightSource[0].specular * pow(NdotHV,shininess);

color += diffuse1 * NdotL;

}

return color.rgb;

}

material and light properties are as follows..

vec3 lightPosition = vec3 (0.0, 15.0, 0.0);

vec4 mat_ambient = vec4( 0.7, 0.0, 0.0, 1.0 );

vec4 mat_diffuse = vec4( 0.7, 0.0, 0.0, 1.0 );

vec4 mat_specular = vec4(0.0,1.0,0.0,1.0);

vec3 camera = vec3(0.0,5.0,30.0);

float shininess = 10;

//light properties

GLfloat specular[] = { 0.0, 1.0, 0.0, 1.0 };

GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };

GLfloat diffuse[] = { 0.7, 0.7, 0.7, 1.0 };

Please tell me if my output is even right or wrong..!!

Please find my output attached for reference..

Thanks in Advance..:)

Edit: File Added