Is there an easy way to find out which object was hit?

Is there an easy way to know withing the closest hit or any hit program which object was hit? I’d prefer to use a single material for all but can encode the information into the material. I do however wish to use the same hit program to be flexible with the number of objects.

Is there an easy way to do this?

The idea is to do batch processing where I keep track of which object was hit and send the data to the appropriate buffer.


You can set varying material properties(in your case object ID) through the geometry instance of each object in your scene while using only 1 material hit program.

You should look at the OptiX SDK example on Progressive Photon Map. The cornell box walls have the same material but varying diffuse(Kd) values.

Particularly the function in ppm.cpp

GeometryInstance ProgressivePhotonScene::createParallelogram(…)

GeometryInstance gi = m_context->createGeometryInstance( parallelogram,
&m_material+1 );
gi[“Kd”]->setFloat( color );
gi[“Ks”]->setFloat( 0.0f, 0.0f, 0.0f );
gi[“use_grid”]->setUint( 0u );
gi[“grid_color”]->setFloat( make_float3( 0.0f ) );
gi[“emitted”]->setFloat( 0.0f, 0.0f, 0.0f );


I dug into that and worked on my code as well. Looks like its a bit harder after all.

I’m using objLoader to load a mesh file. I then create multiple objects by translating that object multiple times, and I need to know within the ray tracer which one of the transformed objects I’m looking at.

So I have
Group -> Transform(s) -> GeometryGroup -> Material + GeometryInstance -> Geometry

and I want to choose the output buffer based on the transform. Doesn’t seem like the transform has any variable support, or am I wrong? (I just need to know the transform index)

I guess that I could load the object multiple times, but it’s a lousy way to do it.


This should be pretty easy, if there aren’t any other circumstances involved.

  • If you have exactly one Transform node per object, let your Material have a variable rtDeclareVariable(unsigned int, objectID, , );
  • In your given node hierarchy you load the Geometry only once, but assign different Material parameters (=> objectID) per hierarchy path to each GeometryInstance (i.e. per Transform) to identify the Geometry as you wish.
  • To report back to the ray generation program that you hit a specific object, add a member
    unsigned int objectID; to your custom PerRayData payload.
  • Inside the closest-hit program write the objectID from the material into the objectID of the current ray payload.
  • Inside the ray generation program add a switch-case which writes the other PerRayData results you generated into the output buffer you select with the PerRayData objectID.

I have one object with one material transformed multiple times to different positions. The objects at the different positions do not interact with each other.

I don’t think that it’s relevant here, but I’m not reporting anything back as well, but rather writing the result out directly from the hit function itself, as each bounce creates an output. I’m doing time resolved imaging simulation, so output location depends on distance to hit point.

I’m looking to do batch processing at the moment, to compute the output resulting from putting the same object at different locations.