optix prime示例程序

//------------------------------------------------------------------------------
int main( int argc, char** argv )
{
  // set defaults
  RTPcontexttype contextType = RTP_CONTEXT_TYPE_CPU;
  RTPbuffertype bufferType = RTP_BUFFER_TYPE_HOST;

  // parse arguments
  for ( int i = 1; i < argc; ++i ) 
  { 
    std::string arg( argv[i] );
    if( arg == "-h" || arg == "--help" ) 
    {
      printUsageAndExit( argv[0] ); 
    } 
    else if( (arg == "-o" || arg == "--obj") && i+1 < argc ) 
    {
      objFilename = argv[++i];
    } 
    else if( ( arg == "-c" || arg == "--context" ) && i+1 < argc )
    {
      std::string param( argv[++i] );
      if( param == "cpu" )
        contextType = RTP_CONTEXT_TYPE_CPU;
      else if( param == "cuda" )
        contextType = RTP_CONTEXT_TYPE_CUDA;
      else
        printUsageAndExit( argv[0] );
    } 
    else if( ( arg == "-b" || arg == "--buffer" ) && i+1 < argc )
    {
      std::string param( argv[++i] );
      if( param == "host" )
        bufferType = RTP_BUFFER_TYPE_HOST;
      else if( param == "cuda" )
        bufferType = RTP_BUFFER_TYPE_CUDA_LINEAR;
      else
        printUsageAndExit( argv[0] );
    } 
    else if( (arg == "-w" || arg == "--width") && i+1 < argc ) 
    {
      width = atoi(argv[++i]);
    } 
    else 
    {
      std::cerr << "Bad option: '" << arg << "'" << std::endl;
      printUsageAndExit( argv[0] );
    }
  }


  //
  // Create Prime context
  //
  RTPcontext context;
  CHK_PRIME( rtpContextCreate( contextType, &context ) );

  float3 vertices[3];

  vertices[0] = make_float3(0.0f, 0.0f, 0.0f);
  vertices[1] = make_float3(2.0f, 2.0f, 0.0f);
  vertices[2] = make_float3(2.0f, 0.0f, 0.0f);

  //
  // Create buffers for geometry data 
  //
 
  RTPbufferdesc verticesDesc;
  CHK_PRIME( rtpBufferDescCreate(
        context,
        RTP_BUFFER_FORMAT_VERTEX_FLOAT3,
        RTP_BUFFER_TYPE_HOST,
        vertices, 
        &verticesDesc )
      );

  CHK_PRIME(rtpBufferDescSetRange(verticesDesc, 0, 3));

  //
  // Create the Model object
  //
  RTPmodel model;
  CHK_PRIME(rtpModelCreate(context, &model));
  CHK_PRIME(rtpModelSetTriangles(model, 0, verticesDesc));
  CHK_PRIME(rtpModelUpdate(model, 0));

  
  //
  // Create buffer for ray input 
  //
  const int rayCount = 2;

  //RTPbufferdesc raysDesc;
  //Ray *raysBuffer = new Ray[rayCount];
  
    RTPbufferdesc raysDesc;
  Buffer<Ray> raysBuffer( 0, bufferType, LOCKED ); 

  raysBuffer.alloc(rayCount);

  if (raysBuffer.type() == RTP_BUFFER_TYPE_HOST)
  {
	  Ray* rays = raysBuffer.ptr();

	  /*RTP_BUFFER_FORMAT_RAY_ORIGIN_TMIN_DIRECTION_TMAX*/
	  Ray r1 = { make_float3(1.0f, 0.2f, 1.0f), 0.1f, make_float3(0.0f, 0.0f, -1.0f), 10.0f };
	  rays[0] = r1;

	  Ray r2 = { make_float3(-1.0f, 0.2f, 1.0f), 0.1f, make_float3(0.0f, 0.0f, -1.0f), 10.0f };
	  rays[1] = r2;
  }


  CHK_PRIME( rtpBufferDescCreate( 
        context, 
        Ray::format, /*RTP_BUFFER_FORMAT_RAY_ORIGIN_TMIN_DIRECTION_TMAX*/ 
        raysBuffer.type(), 
        raysBuffer.ptr(), 
        &raysDesc )
      );

 CHK_PRIME( rtpBufferDescSetRange( raysDesc, 0, raysBuffer.count() ) );

  
  //
  // Create buffer for returned hit descriptions
  //
  RTPbufferdesc hitsDesc;
  Buffer<Hit> hitsBuffer( raysBuffer.count(), bufferType, LOCKED );
  CHK_PRIME( rtpBufferDescCreate( 
        context, 
        Hit::format, /*RTP_BUFFER_FORMAT_HIT_T_TRIID_U_V*/ 
        hitsBuffer.type(), 
        hitsBuffer.ptr(), 
        &hitsDesc )
      );

  CHK_PRIME( rtpBufferDescSetRange( hitsDesc, 0, hitsBuffer.count() ) );

  //
  // Execute query
  //
 RTPquery query;
  CHK_PRIME( rtpQueryCreate( model, RTP_QUERY_TYPE_CLOSEST, &query ) );
  CHK_PRIME( rtpQuerySetRays( query, raysDesc ) );
  CHK_PRIME( rtpQuerySetHits( query, hitsDesc ) );
  CHK_PRIME( rtpQueryExecute( query, 0 /* hints */ ) );

  std::cout << std::endl << "Baseline test results: " << std::endl;

  // Parse hits

  Hit* hits = hitsBuffer.ptr();

  for (int i = 0; i < rayCount; i++)
  {
	  Hit hit1 = hits[i];
	  float distance1 = hit1.t;
	  std::cout << "Ray Distance " << i + 1 << ": " << distance1 << std::endl;
	  
  }

  std::cout << std::endl;

  //CHK_PRIME(rtpModelDestroy(model));

  
  //
  // cleanup
  //
  CHK_PRIME( rtpContextDestroy( context ) );
}
发布了80 篇原创文章 · 获赞 14 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/novanova2009/article/details/90582348