PDA

View Full Version : RayTracing in GLSL Help Needed !!!



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

strattonbrazil
12-19-2011, 11:15 AM
Hrmm, I think those highlights are a little strong, but here are a few tips I'd suggest for debugging. First, move the light one unit off the plane so you can get a better idea of what's reflecting there. Second, use gl_FragColor to output values in your shader like lightDir and see if they make sense. For example, in this case most of them should be greenish, right? I'd also try hardcoding some of those attributes in your shader to make sure they're being set correctly. If you haven't already, I'd switch back to a basic Lambertian to make sure that is working correctly.

You can also try making the plane really big and turning off texturing until you get it the non-textured part working.

abhishek bansal
12-19-2011, 08:11 PM
first of all thank u for your kind reply..!!

I tried shifting light off the plane.. the highlight moves with the light but very slowly. For example if i move light to edge of plane highlight only shift a little from center.. but when i move light very far away from edge it completely covers that side of the plane and become stronger!!

material properties are already hardcoded and i am accessing light properties with help of inbuilt GLSL structures.

I doubt if my primary ray generation method is right.. can you please help?? right now my ray generation code is..


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


and origin i am taking as camera coordinates.

my one more doubt is that do we need to make geometry in open gl code while ray tracing ? as in in my case does the plane needs to be rendered in opengl code too OR raytracer won't need it ?

i know i am full of doubts but please help..!!

abhishek bansal
12-23-2011, 12:08 PM
OK i found answers to some of my doubts..


I tried shifting light off the plane.. the highlight moves with the light but very slowly. For example if i move light......

the approach was completely wrong.. i was drawing primitives and trying to raytrace them in shader which is incorrect.


I doubt if my primary ray generation method is right.. can you please help?? right now my ray generation code is..


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


and origin i am taking as camera coordinates.


that was kind of right...!!


my one more doubt is that do we need to make geometry in open gl code while ray tracing ? as in in my case does the plane needs to be rendered in opengl code too OR raytracer won't need it ?


and finally yes one need to make a full screen quad in OpenGl Code in order to RayTrace in GLSL.

BUT now i have another problem.....I have already implemented shadows and phong. I am working with spheres and planes only. Now when i was implementing reflections i am having strange problem in which i get proper reflections of sphere in the plane, but when it comes to plane's reflection in spheres and mutual reflection on spheres it only produces some kind of noise.

Interesting fact is that shape of this noise is exactly same as reflection's should be. I cant figure out the problem despite of too much efforts in debugging....please help !!

link to GLSL sandbox containing my shader....
RayTrace SandBox (http://glsl.heroku.com/e#808.11 )

I am also attaching image file of my end result.