Results 1 to 4 of 4

Thread: RayTracing in GLSL Help Needed !!!

  1. #1
    Member Newbie
    Join Date
    Sep 2010
    Location
    India
    Posts
    47

    RayTracing in GLSL Help Needed !!!

    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
    Code :
    vec3 rayDirection  =   vec3( gl_FragCoord.x-250.0,250.0-gl_FragCoord.y , 10.0);

    code for finding out intersection of ray with plane..
    Code :
    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...
    Code :
    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.....
    Code :
    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

  2. #2
    Senior Member Regular Contributor strattonbrazil's Avatar
    Join Date
    Jun 2007
    Location
    Los Angeles, CA
    Posts
    305

    Re: RayTracing in GLSL Help Needed !!!

    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.

  3. #3
    Member Newbie
    Join Date
    Sep 2010
    Location
    India
    Posts
    47

    Re: RayTracing in GLSL Help Needed !!!

    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..
    Code :
    	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..!!

  4. #4
    Member Newbie
    Join Date
    Sep 2010
    Location
    India
    Posts
    47

    Re: RayTracing in GLSL Help Needed !!!

    OK i found answers to some of my doubts..

    Quote Originally Posted by abhishek bansal
    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.
    Quote Originally Posted by abhishek bansal
    I doubt if my primary ray generation method is right.. can you please help?? right now my ray generation code is..
    Code :
    	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...!!
    Quote Originally Posted by abhishek bansal
    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

    I am also attaching image file of my end result.

Similar Threads

  1. Pointer needed to sample GLSL code
    By iapx86 in forum OpenGL: GLSL
    Replies: 7
    Last Post: 07-08-2011, 06:06 AM
  2. GL_NORMALIZE and is it needed with GLSL
    By Mars_999 in forum OpenGL: GLSL
    Replies: 4
    Last Post: 07-10-2008, 05:37 PM
  3. Raytracing with GLSL
    By jlukas in forum OpenGL: GLSL
    Replies: 3
    Last Post: 07-22-2004, 11:39 AM
  4. Raytracing...
    By MrShoe in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 09-17-2001, 06:28 PM
  5. raytracing
    By prashantkatre in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 12-13-2000, 06:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean