SuperCraggs

02-17-2009, 06:54 AM

Hi there. I have created a quick application in C# using CSGL as a testbed preview window for a raytracer.

I seem to be having a problem with ray generation.

I am using Molleur-Trombore 1995 as my ray-triangle intersection algorithm, and I am generating rays for picking, and for raytracing.

The raytracer is slow as hell, but i've not hacked in any acclerators yet.

Now, the problem is that when the camera is looking directly at world origin, then the rendering comes out correctly, however, when it shifts, the middle of the frustum renders correctly, while the edges don't.

I'm following the picking FAQ

http://www.opengl.org/resources/faq/technical/selection.htm

and am using exactly this method to generate all my rays.

public Ray CreateRay(int xPixel, int yPixel)

{

Ray Ray;

unsafe

{

Vector3 RayDirection;

Vector3 RayFar = new Vector3();

Vector3 RayNear = new Vector3();

double rx = 0;

double ry = 0;

double rz = 0;

double[] mProjection = new double[16];

double[] mModelView = new double[16];

int[] Viewport = new int[4];

fixed (double* pMView = &mModelView[0])

{

fixed (double* pProj = &mProjection[0])

{

fixed (int* pViewport = &Viewport[0])

{

// populate matricies and viewport data

GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX, mModelView);

GL.glGetDoublev(GL.GL_PROJECTION_MATRIX, mProjection);

GL.glGetIntegerv(GL.GL_VIEWPORT, Viewport);

// calculate ray direction by doing near plane intersection and far plane intersection

GLU.gluUnProject((double)xPixel, (double)(Viewport[3]-1)-yPixel, 0.0, pMView, pProj, pViewport, &rx, &ry, &rz);

RayNear = new Vector3(rx, ry, rz);

GLU.gluUnProject((double)xPixel, (double)(Viewport[3] - 1) - yPixel, 1.0, pMView, pProj, pViewport, &rx, &ry, &rz);

RayFar = new Vector3(rx, ry, rz);

// compute ray direction from far plane intersect - near plane, and remember to normalize!

RayDirection = RayFar - RayNear;

Vector3.Normalize(RayDirection);

// build ray from eye point as origin, raydirection as the ray point on the farplane - the point on the ray near plane.

Ray = new Ray(this.GetEye(), RayDirection);

}

}

}

}

return Ray;

}

Can anyone see any problems with the above ray generation function? If not, then I have some other ideas where there may be problems, but I don't want this post to be of epic length.

This is driving me crazy, please help! I can't put in any secondary rays until this damn problem is sorted :)

Craig.

I seem to be having a problem with ray generation.

I am using Molleur-Trombore 1995 as my ray-triangle intersection algorithm, and I am generating rays for picking, and for raytracing.

The raytracer is slow as hell, but i've not hacked in any acclerators yet.

Now, the problem is that when the camera is looking directly at world origin, then the rendering comes out correctly, however, when it shifts, the middle of the frustum renders correctly, while the edges don't.

I'm following the picking FAQ

http://www.opengl.org/resources/faq/technical/selection.htm

and am using exactly this method to generate all my rays.

public Ray CreateRay(int xPixel, int yPixel)

{

Ray Ray;

unsafe

{

Vector3 RayDirection;

Vector3 RayFar = new Vector3();

Vector3 RayNear = new Vector3();

double rx = 0;

double ry = 0;

double rz = 0;

double[] mProjection = new double[16];

double[] mModelView = new double[16];

int[] Viewport = new int[4];

fixed (double* pMView = &mModelView[0])

{

fixed (double* pProj = &mProjection[0])

{

fixed (int* pViewport = &Viewport[0])

{

// populate matricies and viewport data

GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX, mModelView);

GL.glGetDoublev(GL.GL_PROJECTION_MATRIX, mProjection);

GL.glGetIntegerv(GL.GL_VIEWPORT, Viewport);

// calculate ray direction by doing near plane intersection and far plane intersection

GLU.gluUnProject((double)xPixel, (double)(Viewport[3]-1)-yPixel, 0.0, pMView, pProj, pViewport, &rx, &ry, &rz);

RayNear = new Vector3(rx, ry, rz);

GLU.gluUnProject((double)xPixel, (double)(Viewport[3] - 1) - yPixel, 1.0, pMView, pProj, pViewport, &rx, &ry, &rz);

RayFar = new Vector3(rx, ry, rz);

// compute ray direction from far plane intersect - near plane, and remember to normalize!

RayDirection = RayFar - RayNear;

Vector3.Normalize(RayDirection);

// build ray from eye point as origin, raydirection as the ray point on the farplane - the point on the ray near plane.

Ray = new Ray(this.GetEye(), RayDirection);

}

}

}

}

return Ray;

}

Can anyone see any problems with the above ray generation function? If not, then I have some other ideas where there may be problems, but I don't want this post to be of epic length.

This is driving me crazy, please help! I can't put in any secondary rays until this damn problem is sorted :)

Craig.