• FEATURED
• FEATURED
• FEATURED
• FEATURED
• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Raytracing a Box

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1Creativ  Members

Posted 19 October 2011 - 04:34 AM

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.

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[i])) {
hit = &voxels[i];
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

### #2NDIR  Members

Posted 19 October 2011 - 04:44 AM

You should return the color of the NEAREST hit voxel. (if that doesn't already happen )

### #3Creativ  Members

Posted 19 October 2011 - 04:49 AM

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?

### #4NDIR  Members

Posted 19 October 2011 - 05:04 AM

then you should do &ray or *ray

### #5Creativ  Members

Posted 19 October 2011 - 05:15 AM

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....

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.