Browse Source

Added double_sided flag to embree renderer (#1765)

Vladimir S. FONOV 4 years ago
parent
commit
c37fa050f9
2 changed files with 26 additions and 5 deletions
  1. 19 4
      include/igl/embree/EmbreeRenderer.cpp
  2. 7 1
      include/igl/embree/EmbreeRenderer.h

+ 19 - 4
include/igl/embree/EmbreeRenderer.cpp

@@ -36,7 +36,6 @@ IGL_INLINE void igl::embree::EmbreeRenderer::init_view()
   orthographic = false;
   
 
-  uC << 1,0,0;
 }
 
 IGL_INLINE igl::embree::EmbreeRenderer::EmbreeRenderer()
@@ -47,6 +46,8 @@ IGL_INLINE igl::embree::EmbreeRenderer::EmbreeRenderer()
   device(igl::embree::EmbreeDevice::get_device())
 {
   init_view();
+  uC << 1,0,0;
+  double_sided = false;
 }
 
 IGL_INLINE igl::embree::EmbreeRenderer::EmbreeRenderer(
@@ -304,19 +305,21 @@ igl::embree::EmbreeRenderer
 
       if(this->intersect_ray(s,dir,hit))
       {
-        if ( dir.dot(hit.N) > 0.0f)
+        if ( double_sided || dir.dot(hit.N) > 0.0f )
         {
           // TODO: interpolate normals ?
           hit.N.normalize();
+
           // cos between ray and face normal
-          float face_proj=dir.dot(hit.N);
+          // negative projection will indicate back side
+          float face_proj = fabs(dir.dot(hit.N));
 
           Eigen::RowVector3f c;
 
           if(this->uniform_color)
           {
             // same color for the whole mesh
-            c=uC;
+            c=this->uC;
           } else if(this->face_based) {
             // flat color per face
             c=this->C.row(hit.id);
@@ -329,6 +332,11 @@ igl::embree::EmbreeRenderer
           R(x,y) = clamp(face_proj*c(0));
           G(x,y) = clamp(face_proj*c(1));
           B(x,y) = clamp(face_proj*c(2));
+        } else {
+          // backface?
+          R(x,y)=0;
+          G(x,y)=0;
+          B(x,y)=0;
         }
         // give the same alpha to all points with something behind
         A(x,y)=255; 
@@ -419,5 +427,12 @@ igl::embree::EmbreeRenderer::set_orthographic(bool o)
   this->orthographic=o;
 }
 
+IGL_INLINE void 
+igl::embree::EmbreeRenderer::set_double_sided(bool d)
+{
+  this->double_sided=d;
+}
+
+
 #ifdef IGL_STATIC_LIBRARY
 #endif //IGL_STATIC_LIBRARY

+ 7 - 1
include/igl/embree/EmbreeRenderer.h

@@ -50,7 +50,6 @@ namespace igl
       typedef Eigen::Matrix<float,Eigen::Dynamic,3> PointMatrixType;
       typedef Eigen::Matrix<float,Eigen::Dynamic,3> ColorMatrixType;
       typedef Eigen::Matrix<int,  Eigen::Dynamic,3> FaceMatrixType;
-
       typedef Eigen::Matrix<unsigned char,Eigen::Dynamic,Eigen::Dynamic> PixelMatrixType;
 
     public:
@@ -121,6 +120,12 @@ namespace igl
       //    f - orthographic or perspective projection
       void set_orthographic(bool f );
 
+
+      // Render both sides of triangles
+      // Inputs:
+      //    f - double sided
+      void set_double_sided(bool f);
+
       // render full buffer
       // Outputs:
       //   all outputs should have the same size (size of the output picture)
@@ -200,6 +205,7 @@ namespace igl
 
       bool face_based;
       bool uniform_color;
+      bool double_sided ;
 
       // Camera parameters
       float camera_base_zoom;