Different Results on GLSL SandBox and PC

Hello all,
I was trying to develop a basic Ray Tracer in GLSL on my PC. Then i came to know about GLSL SandBox.I started my shader development in that as it appeared awesome.

I have a plane and two spheres in my scene. Now my problem is that one of the sphere won’t show up when i run same shader in my own C++ OpenGL code or in Render Monkey on my PC. Basically that sphere become a ghost while its reflections and shadows are visible. Is it any bug or what ?

please find both outputs in attached image files with post.
also link to my shader is RayTrace

My graphic card is ATI Radeon 5470 Mobility. it supports Shader Model 3.0.

Hi,
Your sphere intersect func needs some tuning. Try this func instead and tell us if it still does not work.


int sphereIntersect(vec3 rayDir, vec3 rayOrigin, out float t1, in vec3 sCenter, in float sRadius)
{
	t1=1000000.0;
	rayDir = normalize(rayDir);
	float B = 2.0 *( ( rayDir.x * (rayOrigin.x - sCenter.x ) )+  ( rayDir.y * (rayOrigin.y - sCenter.y )) + ( rayDir.z * (rayOrigin.z - sCenter.z ) ));
	float C = pow((rayOrigin.x - sCenter.x),2.0) + pow((rayOrigin.y - sCenter.y),2.0) + pow((rayOrigin.z - sCenter.z),2.0) - pow(sRadius,2.0);
	
	float D = B*B - 4.0*C ;
	
	if(D>0.0)
	{
		t1= (-B - pow(D, .5)) / 2.0;
		if (t1 > 0.0)
		{
		        return 1;  
			 
		}
		
	}
	 
		return 0; //since determinant of quadratic equation <0 so no solution and hence no intersection
}

That worked like charm for smoothing out Reflections…thank you very much for that… but it did not helped in different output problem i.e. i am still getting different outputs (same as before) on sandbox and OpenGL Code/C++ .

But in your function you did not calculate second root of quadratic equation… why is that ?? and why that work more accurately ??

Thanks for your great help.!!

but it did not helped in different output problem i.e. i am still getting different outputs (same as before) on sandbox and OpenGL Code/C++ .

Well there must be something wrong in your c++ side. Could u show us how u r doing it.

But in your function you did not calculate second root of quadratic equation… why is that ?? and why that work more accurately ??

The solution of this quadratic eq. gives two roots, one t where the ray enters the sphere and the other t where it leaves it. My code takes the nearest t value only since that is the value we need. Since you were taking both of these values and because u initialized the local t value with 0 and not a big value, some values were taken from the back side of the sphere thus u got those grains in the reflection. Usually I see people calculating both t values when infact only the nearest t value is needed.

well thank you very much again for insights.

I am pasting here relevant portions of my c++ code and my vertex shader.

vertex shader (since render monkey also behaving in same way)


void main()
{	
 	gl_Position = ftransform();
} 

display function


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glClearColor(0.0,0.0,0.0,1.0);
	
	glLoadIdentity();
	glBindTexture(GL_TEXTURE_2D,floorTex);
	rayTrace.abSetUniformTexture("tex0",0);

	rayTrace.abUseProgram();  
	
	glBegin(GL_QUADS);
	glVertex3f(-1.5, 1.5, -1);
	glVertex3f(1.5, 1.5, -1);
	glVertex3f(1.5, -1.5, -1);
	glVertex3f(-1.5, -1.5, -1);
	glEnd();
	
	rayTrace.abStopProgram();
	
	glFlush();
	glutSwapBuffers();

reshape function


void reshape(int w, int h)
{
	// Prevent a divide by zero, when window is too short
	// (you cant make a window of zero width).
	if(h == 0)
		h = 1;

//	ratio = 1.0f * w / h;
	// Reset the coordinate system before modifying
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	// Set the viewport to be the entire window
    glViewport(0, 0, w, h);

	// Set the clipping volume
	gluPerspective(100,1.0f * w / h,1,2000);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

}

I need to ask you a couple of things

  1. Why do u use perspective projection? You only need to draw a full screen quad that makes sure that the raytrace fragment shader runs for the whole screen. The projection code is controlled by the raytrace fragment shader.

I dont know if this helps in solving your problem but I would instead use an orthographic projection like this


glOrtho(0,1,0,1,0,1);

Then in my render function, I would specify the quad as follows.


glBegin(GL_QUADS);
   glVertex2f(0, 0);
   glVertex2f(1, 0);
   glVertex2f(1, 1);
   glVertex2f(0, 1);
glEnd();

Other than that I think the rest of the code seems ok to me. See if this helps.

When i started with ray tracer i didn’t know that i will be needing a full screen quad. I thought that i ll be applying ray tracer on OpenGL primitives.

I changed my projection type and quad code as per your suggestion but still it is producing same results… :frowning:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.