Sign in to follow this  
Creativ

Raytracing a Box

Recommended Posts

Creativ    103
hi,

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.
[img]http://www.ld-host.de/uploads/images/091e6ace9e847a6be61a48b0c173295f.png[/img] [img]http://www.ld-host.de/uploads/images/98bc57e47bea545489118a125d06e08a.png[/img]


And here is the Code for shooting the Ray (this one gets called for each pixel):

[code]__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[i])) {
hit = &voxels[i];
hasHit = true;
}
}

if(hasHit)
return hit->color;

Color3i c;
c.r = 0;
c.g = 0;
c.b = 0;
return c;
}[/code]

And the algorithm for checking ray against a box did I get from here:
[url="http://www.siggraph.org/education/materials/HyperGraph/raytrace/rtinter3.htm"]http://www.siggraph....ce/rtinter3.htm[/url]

And here is my code for it:
[code]__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;
}[/code]


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

Share this post


Link to post
Share on other sites
Creativ    103
I think I'm doing it.
rayIntersectsVoxel only returns true if it's the closest hit voxel:
if(tnear > ray.t)
return false;

and so hit should only contain the closest hit voxel, doesn't it?

Share this post


Link to post
Share on other sites
Creativ    103
Thank you so much!

You can't believe how much time I spend trying to fix this freaking bug.
I didn't work with C or C++ for quite some time so I just forgot that I have to use a pointer....

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this