0

Here, I'm trying to add ray-casting into a real 3D scene. As we know, in ray-casting, in order to cast the ray, we need to get the direction of ray. The first point in the ray is the start point of ray which I can get from glLookAt. The second point in the ray is each point in the screen. In the original CUDA ray-casting SDK, it gets the screen point in the Kernel Function by the code below:

uint x = blockIdx.x*blockDim.x + threadIdx.x;
uint y = blockIdx.y*blockDim.y + threadIdx.y;

(x,y) is the screen space coordinate right? So now I need to translate (x, y) into a world space coordinate. A nice guy told me use gluUnProject to do this job. Good thinking! However I cannot call OpenGL function in the CUDA kernel.

Maybe I can use a invertible matrix to do the translation. But I'm not sure about the right way. Is there anybody could give me a hint about how to translate screen coordinate to world coordinate in the CUDA kernel function?

genpfault
  • 51,148
  • 11
  • 85
  • 139
TonyLic
  • 647
  • 1
  • 13
  • 26
  • 3
    It isn't all that obvious what you want help with. Is it the mathematics of the transformation, or is it the CUDA implementation of the mathematics or what? Perhaps you could start by writing a serial piece of host code which performs the transformation and confirm it is correct, and then look at the problem of how to map the calculation into the CUDA execution model. – talonmies May 25 '12 at 05:38
  • 1
    You could look at the source for `gluUnproject()` and implement it in the CUDA code. This question is completely orthogonal to CUDA -- what you need is just mathematics and then C code that will get executed by each thread. – harrism May 28 '12 at 00:02

1 Answers1

1

My solution is like this: In CUDA kernel, it gets the screen coordinates in this way:

uint x = blockIdx.x*blockDim.x + threadIdx.x;
uint y = blockIdx.y*blockDim.y + threadIdx.y;

Actually it stands for another coordinate space where the screen is perpendicular to the Z axis. From the gluPerspective, we can also get the distance between the screen and the view position. So as we know the view position, we can calculate the Z coordinate of the screen. So the coordinate of each point in the screen is (x,y,z) under this particular coordinate space.

In this particular coordinate space, the view direction is parallel to the Z axis. Also, we know the view direction in the world space. So we just need to calculate the transformation matrix that transform the Z-Axis-aligned view direction (in particular coordinate space above) to the real view direction (in world space).

Then we use this matrix to transform the each point in the screen (x,y,z) to a world space. So that's the world space coordinate.

I'm now trying to implement this, I think this idea is OK, after implemented I'll accept this answer.

TonyLic
  • 647
  • 1
  • 13
  • 26