Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: Different Results on GLSL SandBox and PC

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

    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.

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Singapore
    Posts
    802

    Re: Different Results on GLSL SandBox and PC

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

    Code :
    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
    }
    Regards,
    Mobeen

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

    Re: Different Results on GLSL SandBox and PC

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

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Singapore
    Posts
    802

    Re: Different Results on GLSL SandBox and PC

    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.
    Regards,
    Mobeen

  5. #5
    Intern Newbie
    Join Date
    Sep 2010
    Location
    India
    Posts
    47

    Re: Different Results on GLSL SandBox and PC

    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)
    Code :
    void main()
    {	
     	gl_Position = ftransform();
    }

    display function
    Code :
    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
    Code :
    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();
     
    }

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Singapore
    Posts
    802

    Re: Different Results on GLSL SandBox and PC

    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
    Code :
    glOrtho(0,1,0,1,0,1);
    Then in my render function, I would specify the quad as follows.
    Code :
    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.
    Regards,
    Mobeen

  7. #7
    Intern Newbie
    Join Date
    Sep 2010
    Location
    India
    Posts
    47

    Re: Different Results on GLSL SandBox and PC

    Quote Originally Posted by mobeen
    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •