I'm currently working on a small Raytracer in Python+CUDA and I'm kinda stuck right now.
Basically the raytracer should just display some Boxes for the beginning, but I'm already having trouble with this.
The Camera is currently always looking at (0,0,0) and if the camera is at e.g. (8,8,4) (like in the first Screenshot) everything is working fine, but
when the camera is at (-8,-8,-4) (second screenshot) it renders the last box and not the first as I want it to.
And here is the Code for shooting the Ray (this one gets called for each pixel):
__device__ Color3i shootRay(int x, int y) {
Ray ray;
ray.org = make_float3(cam.x, cam.y, cam.z);
ray.t = 999999;
// Calculate Direction
ray.dir.x = ( 2.0f * ((x/(float)resX - 0.5)*aspect) * xAxis.x)
+ ( 2.0f * (y/(float)resY - 0.5) * yAxis.x)
+ (focus * zAxis.x);
ray.dir.y = ( 2.0f * ((x/(float)resX - 0.5)*aspect) * xAxis.y)
+ ( 2.0f * (y/(float)resY - 0.5) * yAxis.y)
+ (focus * zAxis.y);
ray.dir.z = ( 2.0f * ((x/(float)resX - 0.5)*aspect) * xAxis.z)
+ ( 2.0f * (y/(float)resY - 0.5) * yAxis.z)
+ (focus * zAxis.z);
Voxel* hit;
bool hasHit = false;
for(unsigned int i=0;i<VOXELCOUNT;i++) {
if(rayIntersectsVoxel(ray, voxels)) {
hit = &voxels;
hasHit = true;
}
}
if(hasHit)
return hit->color;
Color3i c;
c.r = 0;
c.g = 0;
c.b = 0;
return c;
}
And the algorithm for checking ray against a box did I get from here:
http://www.siggraph....ce/rtinter3.htm
And here is my code for it:
__device__ bool rayIntersectsVoxel(Ray ray, Voxel v) {
// http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter3.htm
// check for intersection with voxel
float tnear = -9999999;
float tfar = 9999999;
if(ray.dir.x == 0) {
// Parallel
if(ray.org.x < v.min.x || ray.org.x > v.max.x )
return false;
}else{
float t1 = (v.min.x - ray.org.x) / ray.dir.x;
float t2 = (v.max.x - ray.org.x) / ray.dir.x;
if(t1 > t2) {
// Swap
float tmp = t1;
t1 = t2;
t2 = tmp;
}
if(t1 > tnear) tnear = t1;
if(t2 < tfar) tfar = t2;
if(tnear > tfar)
return false; // Box missed
if(tfar < 0)
return false;
}
if(ray.dir.y == 0) {
// Parallel
if(ray.org.y < v.min.y || ray.org.y > v.max.y )
return false;
}else{
float t1 = (v.min.y - ray.org.y) / ray.dir.y;
float t2 = (v.max.y - ray.org.y) / ray.dir.y;
if(t1 > t2) {
// Swap
float tmp = t1;
t1 = t2;
t2 = tmp;
}
if(t1 > tnear) tnear = t1;
if(t2 < tfar) tfar = t2;
if(tnear > tfar)
return false; // Box missed
if(tfar < 0)
return false;
}
if(ray.dir.z == 0) {
// Parallel
if(ray.org.z < v.min.z || ray.org.z > v.max.z )
return false;
}else{
float t1 = (v.min.z - ray.org.z) / ray.dir.z;
float t2 = (v.max.z - ray.org.z) / ray.dir.z;
if(t1 > t2) {
// Swap
float tmp = t1;
t1 = t2;
t2 = tmp;
}
if(t1 > tnear) tnear = t1;
if(t2 < tfar) tfar = t2;
if(tnear > tfar)
return false; // Box missed
if(tfar < 0)
return false;
}
if(tnear > ray.t)
return false;
ray.t = tnear;
return true;
}
I hope you can help me, because I've been stuck with this bug for quite some time now and I just can't figure it out.
Greets,
Dennis