Looking for simple code to load obj file into OptiX

I’m playing with learning OptiX, and the most daunting task seems to be to actually create a geometry. I’ve gone through the examples and they do seem to load a triangle based geometry (from Obj file) in a few places, but the process seems very well hidden.

Is there some good public code or example on how to do that?

Thanks

Please have a look through the examples again.
For example the procedural example has only one single OBJ file and the code to load it is really just this:

// Load OBJ scene 
  GeometryGroup geometry_group = m_context->createGeometryGroup();
  ObjLoader loader( m_filename.c_str(), m_context, geometry_group, material); 
  loader.load();
  m_context[ "top_object" ]->set( geometry_group );

Just use the debugger and single-step through the ObjLoader constructor and the ObjLoader::load() functions to see what’s happening. After that you should also be able to generate triangle data yourself.

If you want to load multiple OBJ files, look at the glass example.

Does this “procedural example” you mentioned still exist in OptiX 4.1.0? I can’t find it.

I am currently trying to figure out why OptiXMesh.cpp from the MeshViewer example requires two ray types to run. I am trying to set up my geometry with this and then continue from there …

extern const char* const meshFile;
std::string meshPath = std::string(sutil::samplesDir()) + std::string(meshFile);
	
OptiXMesh mesh;
mesh.context = context;
mesh.context->setRayTypeCount(2); // // otherwise OptiXMesh.cpp won't run, somehow requires two ray types
mesh.context["shadow_ray_type"]->setUint(1u); // // -''-
loadMesh(meshPath, mesh);

aabb.set(mesh.bbox_min, mesh.bbox_max);

GeometryGroup geometry_group = context->createGeometryGroup();
geometry_group->addChild(mesh.geom_instance);
geometry_group->setAcceleration(context->createAcceleration("Trbvh"));
context["top_object"]->set(geometry_group);
context["top_shadower"]->set(geometry_group); // for phong

from OptiXMesh.cpp

void loadMesh(
    const std::string&          filename,
    OptiXMesh&                  optix_mesh, 
    const optix::Matrix4x4&     load_xform
    ) {

optix::Context context = optix_mesh.context;

Mesh mesh;
MeshLoader loader( filename );
loader.scanMesh( mesh );

MeshBuffers buffers;
setupMeshLoaderInputs( context, buffers, mesh );

loader.loadMesh( mesh, load_xform.getData() );

translateMeshToOptiX( mesh, buffers, optix_mesh );

unmap( buffers, mesh );
}

OptiX 4.1.0, CUDA 8.0, VS 2015, Quadro K1100M

The mesh loader used in optixMeshViewer is a good place to start. The procedural sample was removed, and was not an improvement on the current mesh loader anyway.

As far as why you’re having to declare shadow rays, the anyhit program for the shadow ray type is set in optix::Material::createOptiXMaterial in OptiXMesh.cpp:

mat->setClosestHitProgram( 0u, closest_hit );
  mat->setAnyHitProgram( 1u, any_hit );

This is what requires two ray types. If you remove the setAnyHitProgram() line and change to a single ray type, you’ll also need to find any places in the shaders (phong.h in this case) that shoot shadow rays, and remove those as well. You should be able to get a cow without shadows if you do this correctly in optixMeshViewer.

Thank you very much for the helpful explanation! This makes sense and works for me now since I don’t use a phon shader …