lighttransport / nanort

NanoRT, single header only modern ray tracing kernel.
MIT License
1.07k stars 89 forks source link

RayTracer with Color #53

Closed rezaradmanesh90 closed 4 years ago

rezaradmanesh90 commented 4 years ago

Hello, I am using the nanort for generating a depth map. I want to implement RGB color of the vertices in this too (in my function nanort::TriangleIntersector<>). Can I extract the color of vertices from nanort and add it to this function. Below is the code I have developed. Thanks. ` void RTracer::construct_scene(const ShapeIO& shio, const pcl::PointCloud::Ptr cloud) { const pcl::PolygonMesh& pmesh = shio.mesh();

vertices.resize(cloud->size() * 3);
faces.resize(pmesh.polygons.size() * 3);

for (int i = 0; i < cloud->size(); ++i)
{
  vertices.at(i * 3 + 0) = cloud->at(i).x;
  vertices.at(i * 3 + 1) = cloud->at(i).y;
  vertices.at(i * 3 + 2) = cloud->at(i).z;
}

for (int i=0; i<pmesh.polygons.size(); ++i)
{
  faces.at(i * 3 + 0) = pmesh.polygons.at(i).vertices.at(0);
  faces.at(i * 3 + 1) = pmesh.polygons.at(i).vertices.at(1);
  faces.at(i * 3 + 2) = pmesh.polygons.at(i).vertices.at(2);
}

nanort::TriangleMesh<float> tmesh(
                                  vertices.data(),
                                  faces.data(),
                                  3 * sizeof(float));
nanort::TriangleSAHPred<float> tpred(
                                     vertices.data(),
                                     faces.data(),
                                     3 * sizeof(float));

nanort::BVHBuildOptions<float> opts;
opts.cache_bbox = false;

bvh.Build(pmesh.polygons.size(), tmesh, tpred, opts);

if 0

nanort::BVHBuildStatistics stats = bvh.GetStatistics();
NUKLEI_LOG("Raytracer BVH statistics:\n" <<
           "# of leaf   nodes  : " << stats.num_leaf_nodes << "\n" <<
           "# of branch nodes  : " << stats.num_branch_nodes << "\n" <<
           "Max tree depth   : " << stats.max_tree_depth);

endif

}

// // \brief Cast a ray and return the distance to mesh collision // returns 0 in case of no collision // float RTracer::cast_ray(float viewpoint[3], float viewdir[3]) const { nanort::Ray ray; // compiler converts float[3] in argument to float // casting float to float[3] afterwards violates some dumb ISO rule // workaround to disabling -fpermissive std::memcpy(ray.org, viewpoint, sizeof(float) 3); std::memcpy(ray.dir, viewdir, sizeof(float) 3); ray.min_t = 0.0f; ray.max_t = 2RADIUS; nanort::TriangleIntersector<> intersector(vertices.data(), faces.data(), 3 sizeof(float)); nanort::TriangleIntersection<> hit; bool did_hit = bvh.Traverse(ray, intersector, &hit); return did_hit ? hit.t : 0.0; }`

syoyo commented 4 years ago

https://github.com/lighttransport/nanort/blob/1af671e81563610e87622fedfa2672013b5d69e7/examples/gui/render.cc#L1011