PlyLoader::PlyLoader( const std::string&   filename,
                      optix::Context       context,
                      optix::GeometryGroup geometrygroup,
                      optix::Material      material,
                      const char* ASBuilder,
                      const char* ASTraverser,
                      bool large_geom )
: _filename( filename ),
  _context( context ),
  _geometrygroup( geometrygroup ),
  _material( material ),
  _large_geom( large_geom),
  _ASBuilder  (ASBuilder),
  _ASTraverser(ASTraverser)
{
  // Error checking on context and geometrygroup done in ModelLoader

  if( material.get() == 0 ) {
    const std::string ptx_path = std::string( sutilSamplesPtxDir() ) + "/cuda_compile_ptx_generated_phong.cu.ptx";
    _material = context->createMaterial();
    _material->setClosestHitProgram( 0, _context->createProgramFromPTXFile( ptx_path, "closest_hit_radiance" ) );
    _material->setAnyHitProgram    ( 1, _context->createProgramFromPTXFile( ptx_path, "any_hit_shadow" ) );
    _material[ "Kd"           ]->setFloat( 0.50f, 0.50f, 0.50f );
    _material[ "Ks"           ]->setFloat( 0.00f, 0.00f, 0.00f );
    _material[ "Ka"           ]->setFloat( 0.05f, 0.05f, 0.05f );
    _material[ "reflectivity" ]->setFloat( 0.00f, 0.00f, 0.00f );
    _material[ "phong_exp"    ]->setFloat( 1.00f );
  }
}
void PlyLoader::createGeometryInstance( unsigned nverts, optix::float3* verts, unsigned ntris, optix::int3* tris )
{
  if (_large_geom) {
    if( _ASBuilder == std::string("Sbvh") || _ASBuilder == std::string("KdTree")) {
      _ASBuilder = "MedianBvh";
      _ASTraverser = "Bvh";
    }


    RTgeometry geometry;
    unsigned int usePTX32InHost64 = 0;
    rtuCreateClusteredMesh( _context->get(), usePTX32InHost64, &geometry, nverts, 
                           (const float*)verts, ntris, (const unsigned int*)tris, 0 );
    optix::Geometry mesh = optix::Geometry::take(geometry);
   
    optix::GeometryInstance instance = _context->createGeometryInstance( mesh, &_material, &_material+1 );

    optix::Acceleration acceleration = _context->createAcceleration(_ASBuilder, _ASTraverser);
    acceleration->setProperty( "leaf_size", "1" );
    acceleration->markDirty();

    _geometrygroup->setAcceleration( acceleration );
    _geometrygroup->setChildCount( 1u );
    _geometrygroup->setChild( 0, instance );
  } else {
    optix::Buffer vertex_buffer = _context->createBuffer( RT_BUFFER_INPUT, RT_FORMAT_FLOAT3, nverts );
    memcpy( vertex_buffer->map(), reinterpret_cast<void*>( verts ), sizeof( optix::float3 )*nverts );
    vertex_buffer->unmap();

    optix::Buffer vindex_buffer = _context->createBuffer( RT_BUFFER_INPUT, RT_FORMAT_INT3, ntris );
    memcpy( vindex_buffer->map(), reinterpret_cast<void*>( tris ), sizeof( optix::int3 )*ntris );
    vindex_buffer->unmap();

    std::string ptx_path = std::string( sutilSamplesPtxDir() ) + "/cuda_compile_ptx_generated_triangle_mesh_small.cu.ptx";

    optix::Geometry mesh = _context->createGeometry();
    mesh->setPrimitiveCount( ntris );
    mesh->setIntersectionProgram( _context->createProgramFromPTXFile( ptx_path, "mesh_intersect" ) );
    mesh->setBoundingBoxProgram(  _context->createProgramFromPTXFile( ptx_path, "mesh_bounds" ) );
    mesh[ "vertex_buffer" ]->set( vertex_buffer );
    mesh[ "vindex_buffer" ]->set( vindex_buffer );

    optix::GeometryInstance instance = _context->createGeometryInstance( mesh, &_material, &_material+1 );

    optix::Acceleration acceleration = _context->createAcceleration(_ASBuilder, _ASTraverser);
    if ( _ASBuilder   == std::string("Sbvh")           ||
         _ASBuilder   == std::string("TriangleKdTree") ||
         _ASTraverser == std::string( "KdTree")        )
    {
      acceleration->setProperty( "vertex_buffer_name", "vertex_buffer" );
      acceleration->setProperty( "index_buffer_name", "vindex_buffer" );
    }
    acceleration->markDirty();

    _geometrygroup->setAcceleration( acceleration );
    _geometrygroup->setChildCount( 1u );
    _geometrygroup->setChild( 0, instance );
  }
}
Ejemplo n.º 3
0
int main(int argc, char* argv[])
{
    /* Primary OptiX objects */
    RTcontext          context;
    RTprogram          ray_gen_program;
    RTprogram          exception_program;
    RTprogram          miss_program;
    RTbuffer           buffer;
    RTselector         dummy_selector;
    RTgeometrygroup    dummy_group;
    RTvariable         dummy_object;
    RTacceleration     dummy_acceleration;
    RTprogram          selector_program;
    RTmaterial         material;
    RTprogram          closest_hit_program;
    RTprogram          any_hit_program;
    RTgeometryinstance instance;
    RTgeometry         geometry;
    RTprogram          intersection_program;
    RTprogram          bounding_box_program;

    /* Parameters */
    RTvariable result_buffer;

    char path_to_ptx[512];
    char outfile[512];

    unsigned int width  = 512u;
    unsigned int height = 384u;
    int i;

    outfile[0] = '\0';

    /* Process command line args */
    RT_CHECK_ERROR_NO_CONTEXT( sutilInitGlut( &argc, argv ) );
    for( i = 1; i < argc; ++i ) {
      if( strcmp( argv[i], "--help" ) == 0 || strcmp( argv[i], "-h" ) == 0 ) {
        printUsageAndExit( argv[0] );
      } else {
        fprintf( stderr, "Unknown option '%s'\n", argv[i] );
        printUsageAndExit( argv[0] );
      }
    }

    /* Create our objects and set state */
    RT_CHECK_ERROR( rtContextCreate( &context ) );
    RT_CHECK_ERROR( rtContextSetRayTypeCount( context, 1 ) );
    RT_CHECK_ERROR( rtContextSetEntryPointCount( context, 1 ) );

    RT_CHECK_ERROR( rtBufferCreate( context, RT_BUFFER_OUTPUT, &buffer ) );
    RT_CHECK_ERROR( rtBufferSetFormat( buffer, RT_FORMAT_FLOAT4 ) );
    RT_CHECK_ERROR( rtBufferSetSize2D( buffer, width, height ) );
    RT_CHECK_ERROR( rtContextDeclareVariable( context, "result_buffer", &result_buffer ) );
    RT_CHECK_ERROR( rtVariableSetObject( result_buffer, buffer ) );

    sprintf( path_to_ptx, "%s/%s", sutilSamplesPtxDir(), "exceptions_generated_exceptions_programs.cu.ptx" );
    RT_CHECK_ERROR( rtProgramCreateFromPTXFile( context, path_to_ptx, "buggy_draw_solid_color", &ray_gen_program ) );
    RT_CHECK_ERROR( rtContextSetRayGenerationProgram( context, 0, ray_gen_program ) );

    RT_CHECK_ERROR( rtProgramCreateFromPTXFile( context, path_to_ptx, "exception", &exception_program ) );
    RT_CHECK_ERROR( rtContextSetExceptionProgram( context, 0, exception_program ) );

    RT_CHECK_ERROR( rtProgramCreateFromPTXFile( context, path_to_ptx, "non_terminating_miss_program", &miss_program ) );
    RT_CHECK_ERROR( rtContextSetMissProgram( context, 0, miss_program ) );

    RT_CHECK_ERROR( rtSelectorCreate( context, &dummy_selector ) );
    RT_CHECK_ERROR( rtGeometryGroupCreate( context, &dummy_group ) );
    RT_CHECK_ERROR( rtSelectorSetChildCount( dummy_selector, 1 ) );
    RT_CHECK_ERROR( rtSelectorSetChild( dummy_selector, 0, dummy_group ) );

    RT_CHECK_ERROR( rtProgramCreateFromPTXFile( context, path_to_ptx, "visit", &selector_program ) );
    RT_CHECK_ERROR( rtSelectorSetVisitProgram( dummy_selector, selector_program ) );
    RT_CHECK_ERROR( rtContextDeclareVariable( context, "dummy_object", &dummy_object ) );
    RT_CHECK_ERROR( rtVariableSetObject( dummy_object, dummy_selector ) );

    RT_CHECK_ERROR( rtAccelerationCreate( context, &dummy_acceleration ) );
    RT_CHECK_ERROR( rtAccelerationSetBuilder( dummy_acceleration,"NoAccel") );
    RT_CHECK_ERROR( rtAccelerationSetTraverser( dummy_acceleration,"NoAccel") );
    RT_CHECK_ERROR( rtGeometryGroupSetAcceleration( dummy_group, dummy_acceleration) );


    RT_CHECK_ERROR( rtProgramCreateFromPTXFile( context, path_to_ptx, "closest_hit", &closest_hit_program ) );
    RT_CHECK_ERROR( rtProgramCreateFromPTXFile( context, path_to_ptx, "any_hit", &any_hit_program ) );
    RT_CHECK_ERROR( rtMaterialCreate( context, &material ) );
    RT_CHECK_ERROR( rtMaterialSetClosestHitProgram( material, 0, closest_hit_program ) );
    RT_CHECK_ERROR( rtMaterialSetAnyHitProgram( material, 0, any_hit_program ) );

    RT_CHECK_ERROR( rtGeometryInstanceCreate( context, &instance ) );
    RT_CHECK_ERROR( rtGeometryInstanceSetMaterialCount( instance, 1 ) );
    RT_CHECK_ERROR( rtGeometryInstanceSetMaterial( instance, 0, material ) );

    RT_CHECK_ERROR( rtGeometryCreate( context, &geometry ) );
    RT_CHECK_ERROR( rtGeometryInstanceSetGeometry( instance, geometry ) );
    RT_CHECK_ERROR( rtGeometryGroupSetChildCount( dummy_group, 1 ) );
    RT_CHECK_ERROR( rtGeometryGroupSetChild( dummy_group, 0, instance ) );
    RT_CHECK_ERROR( rtGeometrySetPrimitiveCount( geometry, 1 ) );

    RT_CHECK_ERROR( rtProgramCreateFromPTXFile( context, path_to_ptx, "bounds", &bounding_box_program ) );
    RT_CHECK_ERROR( rtGeometrySetBoundingBoxProgram( geometry, bounding_box_program ) );
    RT_CHECK_ERROR( rtProgramCreateFromPTXFile( context, path_to_ptx, "intersect", &intersection_program ) );
    RT_CHECK_ERROR( rtGeometrySetIntersectionProgram( geometry, intersection_program ) );


    /* Enable checking for all exceptions */
    RT_CHECK_ERROR( rtContextSetExceptionEnabled( context, RT_EXCEPTION_ALL, 1 ) );

    /* Enable printing so rtPrintExceptionDetails has an effect */
    RT_CHECK_ERROR( rtContextSetPrintEnabled( context, 1 ) );

    /* Run */
    RT_CHECK_ERROR( rtContextValidate( context ) );
    RT_CHECK_ERROR( rtContextCompile( context ) );
    RT_CHECK_ERROR( rtContextLaunch2D( context, 0 /* entry point */, width, height ) );

    /* Display image */
    if( strlen( outfile ) == 0 ) {
      RT_CHECK_ERROR( sutilDisplayBufferInGlutWindow( argv[0], buffer ) );
    } else {
      RT_CHECK_ERROR( sutilDisplayFilePPM( outfile, buffer ) );
    }

    /* Clean up */
    RT_CHECK_ERROR( rtBufferDestroy( buffer ) );
    RT_CHECK_ERROR( rtProgramDestroy( ray_gen_program ) );
    RT_CHECK_ERROR( rtContextDestroy( context ) );

    return( 0 );
}