Hello,
I'm trying to raycast using nvidia CUDA and having problems.
I'm trying to raycast through voxel data held in a 'grid' (a 3D array to me). When the camera is point parallel to an axis everything seems to work out fine, however when I rotate it seems to give a 'zooming in' effect which I can't work out and obviously don't want.
I'm using the fast voxel traversal method by Amanatides and Woo (http://www.cse.yorku.ca/~amana/research/grid.pdf).
Below are two outputs, the first is 'parallel' and behaves as expected whilst the second has the camera rotated but gives this wierd zoom. Below that is the relevant code.
Please could you have a look over the code and tell me if I'm making any mistakes
Thankyou,
from Joshua.
p.s. yes its an ascii-raycaster because I haven't built the display yet
p.s.s. the object is a man
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. ..8............................................... ..................................................
.................................................. .................................................. ..88.............................................. ..................................................
.................................................. .................................................. .8888............................................. ..................................................
.................................................. .................................................. .8888............................................. ..................................................
.................................................. .................................................. 8.88.............................................. ..................................................
.................................................. .................................................. 8.88.............................................. ..................................................
.................................................. .................................................. ..88.............................................. ..................................................
.................................................. .................................................. ..88.............................................. ..................................................
.................................................. .................................................. ..88.............................................. ..................................................
.................................................. .................................................. ...8.............................................. ..................................................
.................................................. .................................................. .8.88............................................. ..................................................
.................................................. .................................................. ...88............................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
1: Rotate round Z axis
:: CAMERA
loc: 0.000000, 200.000000, 140.000000
dir: 0.342020, 0.939693, 0.000000
wdir: 0.939693, -0.342020, 0.000000
hdir: 0.000000, 0.000000, -1.000000
zoom: 10.000000
Screen at 3.420202, 209.396927, 140.000000
Top left at -90.549057, 243.598938, 190.000000
Screen to loc: 10.000001
Toplef to screen: 111.803398
sent
returned
.................................................. .................................................8 88................................................ ..................................................
.................................................. .................................................8 888............................................... ..................................................
.................................................. ................................................88 888............................................... ..................................................
.................................................. ................................................88 888............................................... ..................................................
.................................................. ................................................88 88888............................................. ..................................................
.................................................. ...............................................888 8888888........................................... ..................................................
.................................................. ..............................................8888 8888888........................................... ..................................................
.................................................. ..............................................8888 88888888.......................................... ..................................................
.................................................. .............................................88888 88888888.......................................... ..................................................
.................................................. ............................................888888 888888888......................................... ..................................................
.................................................. ............................................888888 888888888......................................... ..................................................
.................................................. ............................................888888 888888888......................................... ..................................................
.................................................. ...........................................8888888 888888888......................................... ..................................................
.................................................. ...........................................8888888 888888888......................................... ..................................................
.................................................. ...........................................8888888 888888888......................................... ..................................................
.................................................. ...........................................8888888 888888888......................................... ..................................................
.................................................. ..........................................88888888 888888888......................................... ..................................................
.................................................. ..........................................88.88888 888888888......................................... ..................................................
.................................................. ..........................................8..88888 888888888......................................... ..................................................
.................................................. .............................................88888 888888888......................................... ..................................................
.................................................. ..............................................8888 88888888.......................................... ..................................................
.................................................. .........................................8....8888 88888888.......................................... ..................................................
.................................................. .........................................8....8888 88888888.......................................... ..................................................
.................................................. .........................................8....8888 88888888.......................................... ..................................................
.................................................. .........................................8....8888 88888888.......................................... ..................................................
.................................................. ..............................................8888 88888888.......................................... ..................................................
.................................................. ..............................................8888 88888888.......................................... ..................................................
.................................................. ..............................................8888 88888888.......................................... ..................................................
.................................................. ..............................................8888 8888888........................................... ..................................................
.................................................. ..............................................8888 88888888.......................................... ..................................................
.................................................. ..............................................8888 8888888........................................... ..................................................
.................................................. ..............................................8888 88888888.......................................... ..................................................
.................................................. ..............................................8888 8.88888........................................... ..................................................
.................................................. ..............................................8888 8.888888.......................................... ..................................................
.................................................. ..............................................8888 8..8888........................................... ..................................................
.................................................. ..............................................8888 8..88888.......................................... ..................................................
.................................................. ...............................................888 8..88888.......................................... ..................................................
.................................................. ...............................................888 ...88888.......................................... ..................................................
.................................................. ..............................................8888 ...88888.......................................... ..................................................
.................................................. ..............................................8888 ...88888.......................................... ..................................................
.................................................. ..............................................8888 ...88888.......................................... ..................................................
.................................................. ..............................................8888 ...88888.......................................... ..................................................
.................................................. ..............................................8888 ....8888.......................................... ..................................................
.................................................. .............................................8888. ...88888.......................................... ..................................................
.................................................. ..............................................888. ...88888.......................................... ..................................................
.................................................. ..............................................888. ...88888.......................................... ..................................................
.................................................. ..............................................888. ...88888.......................................... ..................................................
.................................................. .............................................88888 ....88888......................................... ..................................................
.................................................. .............................................8888. ....8888.......................................... ..................................................
.................................................. ..............................................88.. ....88888......................................... ..................................................
.................................................. ..............................................88.. ....88888......................................... ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
.................................................. .................................................. .................................................. ..................................................
Code:// Setting where the 'topleft' corner (i.e. pixel 0,0) is in the world void SetCameraData() { VectorSet(camera.dir, 0.0f, 1.0f, 0.0f); VectorSet(camera.wdir, 1.0f, 0.0f, 0.0f); VectorSet(camera.hdir, 0.0f, 0.0f, -1.0f); int p; float rotationmatrix[3][3]; for (p = 0; p < 3; p++) { ConstructRotationMatrix(p, camera.rot[p], rotationmatrix); MatrixMultiplyVector(rotationmatrix, camera.dir, camera.dir); MatrixMultiplyVector(rotationmatrix, camera.wdir, camera.wdir); MatrixMultiplyVector(rotationmatrix, camera.hdir, camera.hdir); } vec3_t mid; VectorScale(camera.dir, camera.zoom, mid); VectorAdd(camera.loc, mid, screen_pos); vec3_t scale, scalew, scaleh; VectorScale(camera.wdir, -SCREENW/2, scalew); VectorScale(camera.hdir, -SCREENH/2, scaleh); VectorAdd(scalew, scaleh, scale); VectorAdd(screen_pos, scale, topleft_pos); } // Translate pixel into world position __device__ void TRAVERSER_GetVec(const int i, const int j, Camera *camera, vec3_t topleft, vec3_t output) { vec3_t w, h; CUDAMATH_VectorScale(camera->wdir, ((float) SCREENW*i)/(float) PIXELW, w); CUDAMATH_VectorScale(camera->hdir, ((float) SCREENH*j)/(float) PIXELH, h); CUDAMATH_VectorAdd(w, h, w); CUDAMATH_VectorAdd(w, topleft, output); } // for pixel i,j get its world position and define the ray going through it __device__ void TRAVERSER_ConstructRay(const int i, const int j, Camera *camera, vec3_t topleftloc, Ray *output) { vec3_t dir, loc; TRAVERSER_GetVec(i, j, camera, topleftloc, loc); CUDAMATH_VectorSubtract(loc, camera->loc, dir); CUDAMATH_VectorNormalize(dir, dir); InitRay(camera->loc, dir, output); } // Do the ray tracing through the grid __global__ void TRAVERSER_RayTrace(VOXEL* dev_world, Camera *camera, float tx, float ty, float tz, int* dev_screen) { const int x = threadIdx.x + blockIdx.x * blockDim.x; const int y = threadIdx.y + blockIdx.y * blockDim.y; Ray ray; dev_screen[x + y*blockDim.x*gridDim.x] = 0; vec3_t topleftloc; CUDAMATH_VectorSet(topleftloc, tx, ty, tz); TRAVERSER_ConstructRay(x, y, camera, topleftloc, &ray); while(IsValidVoxel(ray.X, ray.Y, ray.Z)) { if (ObjectPresent(ray.X, ray.Y, ray.Z, dev_world)) { dev_screen[x + y*blockDim.x*gridDim.x]++; break; } Traverse(&ray); } } typedef struct { int X; int Y; int Z; char stepX; char stepY; char stepZ; float tDeltaX; float tDeltaY; float tDeltaZ; float tMaxX; float tMaxY; float tMaxZ; } Ray; // Fast Voxel Travesal algorithm __device__ void Traverse(Ray *ray) { if (ray->tMaxX < ray->tMaxY) { if (ray->tMaxX < ray->tMaxZ) { ray->X += ray->stepX; ray->tMaxX += ray->tDeltaX; } else { ray->Z += ray->stepZ; ray->tMaxZ += ray->tDeltaZ; } } else { if (ray->tMaxY < ray->tMaxZ) { ray->Y += ray->stepY; ray->tMaxY += ray->tDeltaY; } else { ray->Z += ray->stepZ; ray->tMaxZ += ray->tDeltaZ; } } } // Init the ray and trace into the volume __device__ void InitRay(vec3_t origin, vec3_t dir, Ray *ray) { int x, y, z; GetVoxel(origin, &x, &y, &z); ray->X = x; ray->Y = y; ray->Z = z; ray->stepX = (dir[0] > 0.0f)?1:(dir[0] < 0.0f)?-1:0; ray->stepY = (dir[1] > 0.0f)?1:(dir[1] < 0.0f)?-1:0; ray->stepZ = (dir[2] > 0.0f)?1:(dir[2] < 0.0f)?-1:0; vec3_t nextVoxelx, nextVoxely, nextVoxelz; GetWorldPos(x+ray->stepX, y, z, nextVoxelx); GetWorldPos(x, y+ray->stepY, z, nextVoxely); GetWorldPos(x, y, z+ray->stepZ, nextVoxelz); vec3_t diffx, diffy, diffz; CUDAMATH_VectorSubtract(nextVoxelx, origin, diffx); CUDAMATH_VectorSubtract(nextVoxely, origin, diffy); CUDAMATH_VectorSubtract(nextVoxelz, origin, diffz); ray->tMaxX = abs(diffx[0])/dir[0]; ray->tMaxY = abs(diffy[1])/dir[1]; ray->tMaxZ = abs(diffz[2])/dir[2]; ray->tDeltaX = (float) WORLDVOXELRATIO/abs(dir[0]); ray->tDeltaY = (float) WORLDVOXELRATIO/abs(dir[1]); ray->tDeltaZ = (float) WORLDVOXELRATIO/abs(dir[2]); if ((x < 0 && dir[0] > 0) || (y < 0 && dir[1] > 0) || (z < 0 && dir[2] > 0) || (x > VOXELWIDTH && dir[0] < 0) || (y > VOXELLENGTH && dir[1] < 0) || (z > VOXELHEIGHT && dir[2] < 0)) { while (!IsValidVoxel(ray->X, ray->Y, ray->Z)) { Traverse(ray); } } }