Exemplo n.º 1
0
void WhittedScene::createGeometry()
{
  // Create glass sphere geometry
  std::string shell_ptx( ptxpath( "whitted", "sphere_shell.cu" ) ); 
  Geometry glass_sphere = m_context->createGeometry();
  glass_sphere->setPrimitiveCount( 1u );
  glass_sphere->setBoundingBoxProgram( m_context->createProgramFromPTXFile( shell_ptx, "bounds" ) );
  glass_sphere->setIntersectionProgram( m_context->createProgramFromPTXFile( shell_ptx, "intersect" ) );
  glass_sphere["center"]->setFloat( 4.0f, 2.3f, -4.0f );
  glass_sphere["radius1"]->setFloat( 0.96f );
  glass_sphere["radius2"]->setFloat( 1.0f );
  
  // Metal sphere geometry
  std::string sphere_ptx( ptxpath( "whitted", "sphere.cu" ) ); 
  Geometry metal_sphere = m_context->createGeometry();
  metal_sphere->setPrimitiveCount( 1u );
  metal_sphere->setBoundingBoxProgram( m_context->createProgramFromPTXFile( sphere_ptx, "bounds" ) );
  metal_sphere->setIntersectionProgram( m_context->createProgramFromPTXFile( sphere_ptx, "robust_intersect" ) );
  metal_sphere["sphere"]->setFloat( 2.0f, 1.5f, -2.5f, 1.0f );

  // Floor geometry
  std::string pgram_ptx( ptxpath( "whitted", "parallelogram.cu" ) );
  Geometry parallelogram = m_context->createGeometry();
  parallelogram->setPrimitiveCount( 1u );
  parallelogram->setBoundingBoxProgram( m_context->createProgramFromPTXFile( pgram_ptx, "bounds" ) );
  parallelogram->setIntersectionProgram( m_context->createProgramFromPTXFile( pgram_ptx, "intersect" ) );
  float3 anchor = make_float3( -16.0f, 0.01f, -8.0f );
  float3 v1 = make_float3( 32.0f, 0.0f, 0.0f );
  float3 v2 = make_float3( 0.0f, 0.0f, 16.0f );
  float3 normal = cross( v1, v2 );
  normal = normalize( normal );
  float d = dot( normal, anchor );
  v1 *= 1.0f/dot( v1, v1 );
  v2 *= 1.0f/dot( v2, v2 );
  float4 plane = make_float4( normal, d );
  parallelogram["plane"]->setFloat( plane );
  parallelogram["v1"]->setFloat( v1 );
  parallelogram["v2"]->setFloat( v2 );
  parallelogram["anchor"]->setFloat( anchor );

  // Glass material
  Program glass_ch = m_context->createProgramFromPTXFile( ptxpath( "whitted", "glass.cu" ), "closest_hit_radiance" );
  Program glass_ah = m_context->createProgramFromPTXFile( ptxpath( "whitted", "glass.cu" ), "any_hit_shadow" );
  Material glass_matl = m_context->createMaterial();
  glass_matl->setClosestHitProgram( 0, glass_ch );
  glass_matl->setAnyHitProgram( 1, glass_ah );

  glass_matl["importance_cutoff"]->setFloat( 1e-2f );
  glass_matl["cutoff_color"]->setFloat( 0.034f, 0.055f, 0.085f );
  glass_matl["fresnel_exponent"]->setFloat( 3.0f );
  glass_matl["fresnel_minimum"]->setFloat( 0.1f );
  glass_matl["fresnel_maximum"]->setFloat( 1.0f );
  glass_matl["refraction_index"]->setFloat( 1.4f );
  glass_matl["refraction_color"]->setFloat( 1.0f, 1.0f, 1.0f );
  glass_matl["reflection_color"]->setFloat( 1.0f, 1.0f, 1.0f );
  glass_matl["refraction_maxdepth"]->setInt( 10 );
  glass_matl["reflection_maxdepth"]->setInt( 5 );
  float3 extinction = make_float3(.83f, .83f, .83f);
  glass_matl["extinction_constant"]->setFloat( log(extinction.x), log(extinction.y), log(extinction.z) );
  glass_matl["shadow_attenuation"]->setFloat( 0.6f, 0.6f, 0.6f );

  // Metal material
  Program phong_ch = m_context->createProgramFromPTXFile( ptxpath( "whitted", "phong.cu" ), "closest_hit_radiance" );
  Program phong_ah = m_context->createProgramFromPTXFile( ptxpath( "whitted", "phong.cu" ), "any_hit_shadow" );

  Material metal_matl = m_context->createMaterial();
  metal_matl->setClosestHitProgram( 0, phong_ch );
  metal_matl->setAnyHitProgram( 1, phong_ah );
  metal_matl["Ka"]->setFloat( 0.2f, 0.5f, 0.5f );
  metal_matl["Kd"]->setFloat( 0.2f, 0.7f, 0.8f );
  metal_matl["Ks"]->setFloat( 0.9f, 0.9f, 0.9f );
  metal_matl["phong_exp"]->setFloat( 64 );
  metal_matl["reflectivity"]->setFloat( 0.5f,  0.5f,  0.5f);

  // Checker material for floor
  Program check_ch = m_context->createProgramFromPTXFile( ptxpath( "whitted", "checker.cu" ), "closest_hit_radiance" );
  Program check_ah = m_context->createProgramFromPTXFile( ptxpath( "whitted", "checker.cu" ), "any_hit_shadow" );
  Material floor_matl = m_context->createMaterial();
  floor_matl->setClosestHitProgram( 0, check_ch );
  floor_matl->setAnyHitProgram( 1, check_ah );

  floor_matl["Kd1"]->setFloat( 0.8f, 0.3f, 0.15f);
  floor_matl["Ka1"]->setFloat( 0.8f, 0.3f, 0.15f);
  floor_matl["Ks1"]->setFloat( 0.0f, 0.0f, 0.0f);
  floor_matl["Kd2"]->setFloat( 0.9f, 0.85f, 0.05f);
  floor_matl["Ka2"]->setFloat( 0.9f, 0.85f, 0.05f);
  floor_matl["Ks2"]->setFloat( 0.0f, 0.0f, 0.0f);
  floor_matl["inv_checker_size"]->setFloat( 32.0f, 16.0f, 1.0f );
  floor_matl["phong_exp1"]->setFloat( 0.0f );
  floor_matl["phong_exp2"]->setFloat( 0.0f );
  floor_matl["reflectivity1"]->setFloat( 0.0f, 0.0f, 0.0f);
  floor_matl["reflectivity2"]->setFloat( 0.0f, 0.0f, 0.0f);

  // Create GIs for each piece of geometry
  std::vector<GeometryInstance> gis;
  gis.push_back( m_context->createGeometryInstance( glass_sphere, &glass_matl, &glass_matl+1 ) );
  gis.push_back( m_context->createGeometryInstance( metal_sphere,  &metal_matl,  &metal_matl+1 ) );
  gis.push_back( m_context->createGeometryInstance( parallelogram, &floor_matl,  &floor_matl+1 ) );

  // Place all in group
  GeometryGroup geometrygroup = m_context->createGeometryGroup();
  geometrygroup->setChildCount( static_cast<unsigned int>(gis.size()) );
  geometrygroup->setChild( 0, gis[0] );
  geometrygroup->setChild( 1, gis[1] );
  geometrygroup->setChild( 2, gis[2] );
  geometrygroup->setAcceleration( m_context->createAcceleration("NoAccel","NoAccel") );

  m_context["top_object"]->set( geometrygroup );
  m_context["top_shadower"]->set( geometrygroup );
}