Thanks a lot for the replies.
I managed to make some Transforms work, but I believe something is off with my translation.
To the left is the box translated, to the right it’s in its original position without the translation. The box was intersecting some spheres in its original position and apparently this intersection was… kept, even with the translation, but that’s not what I was expecting. My matrix function would be:
optix::Matrix4x4 translateMatrix(vec3f offset){
float floatM[16] = {
1.0f, 0.0f, 0.0f, offset.x,
0.0f, 1.0f, 0.0f, offset.y,
0.0f, 0.0f, 1.0f, offset.z,
0.0f, 0.0f, 0.0f, 1.0f
};
optix::Matrix4x4 mm(floatM);
return mm;
}
The function I use to set up the translate transform is:
optix::Transform translate(optix::GeometryInstance gi, vec3f& translate, optix::Context &g_context){
optix::Matrix4x4 matrix = translateMatrix(translate);
optix::GeometryGroup d_world = g_context->createGeometryGroup();
d_world->setAcceleration(g_context->createAcceleration("NoAccel"));
d_world->setChildCount(1);
d_world->setChild(0, gi);
optix::Transform translateTransform = g_context->createTransform();
translateTransform->setChild(d_world);
translateTransform->setMatrix(false, matrix.getData(), matrix.inverse().getData());
return translateTransform;
}
Finally, my ray-box intersection program is:
static __device__ float3 boxnormal(float t, float3 t0, float3 t1) {
float3 neg = make_float3(t == t0.x ? 1 : 0, t == t0.y ? 1 : 0, t == t0.z ? 1 : 0);
float3 pos = make_float3(t == t1.x ? 1 : 0, t == t1.y ? 1 : 0, t == t1.z ? 1 : 0);
return pos - neg;
}
// Program that performs the ray-box intersection
RT_PROGRAM void hit_box(int pid) {
float3 t0 = (boxmin - ray.origin) / ray.direction;
float3 t1 = (boxmax - ray.origin) / ray.direction;
float tmin = max_component(min_vec(t0, t1));
float tmax = min_component(max_vec(t0, t1));
if(tmin <= tmax) {
bool check_second = true;
if(rtPotentialIntersection(tmin)) {
hit_rec_p = ray.origin + tmin * ray.direction;
hit_rec_u = 0.f;
hit_rec_v = 0.f;
hit_rec_normal = boxnormal(tmin, t0, t1);
if(rtReportIntersection(0))
check_second = false;
}
if(check_second) {
if(rtPotentialIntersection(tmax)) {
hit_rec_p = ray.origin + tmax * ray.direction;
hit_rec_u = 0.f;
hit_rec_v = 0.f;
hit_rec_normal = boxnormal(tmax, t0, t1);
rtReportIntersection(0);
}
}
}
}
Any idea what could be wrong?
Should I change the object normals or other parameters in any way if I apply a transform? What about the incoming ray parameters?