PDA

View Full Version : Different Results on GLSL SandBox and PC



abhishek bansal
12-23-2011, 04:37 PM
Hello all,
I was trying to develop a basic Ray Tracer in GLSL on my PC. Then i came to know about GLSL SandBox (http://glsl.heroku.com/e#808.11).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 (http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CB8Q FjAA&url=http%3A%2F%2Fdeveloper.amd.com%2Farchive% 2Fgpu%2Frendermonkey%2Fpages%2Fdefault.aspx&ei=yxz 1Tue_F83QrQf32vED&usg=AFQjCNEgCqaAqXp4MAAbiRxxWID9 huTUsQ&sig2=MgsqNzqI5iLbe4niyr9E4w) 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 (http://glsl.heroku.com/e#808.11)

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

mobeen
12-23-2011, 07:31 PM
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
}

abhishek bansal
12-24-2011, 04:04 AM
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.!!

mobeen
12-24-2011, 04:42 AM
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.

abhishek bansal
12-24-2011, 04:51 AM
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();

}

mobeen
12-24-2011, 06:11 AM
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.

abhishek bansal
12-24-2011, 07:26 AM
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.


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... :(