Browse Source

eigen unproject to plane, draw floor with lines

Former-commit-id: 58840f731d3fff6af6aede06833518e94339c905
Alec Jacobson (jalec 11 years ago
parent
commit
bc93395f68

+ 50 - 0
include/igl/draw_floor.cpp

@@ -7,8 +7,10 @@ IGL_INLINE void igl::draw_floor(const float * colorA, const float * colorB)
 {
   // old settings
   int old_lighting=0,old_color_material=0;
+  float old_line_width =0;
   glGetIntegerv(GL_LIGHTING,&old_lighting);
   glGetIntegerv(GL_COLOR_MATERIAL,&old_color_material);
+  glGetFloatv(GL_LINE_WIDTH,&old_line_width);
   glDisable(GL_LIGHTING);
   glColorMaterial( GL_FRONT, GL_EMISSION);
   glEnable(GL_COLOR_MATERIAL);
@@ -31,6 +33,8 @@ IGL_INLINE void igl::draw_floor(const float * colorA, const float * colorB)
 
   int GridSizeX = 100;
   int GridSizeY = 100;
+  //int GridSizeX = 5;
+  //int GridSizeY = 5;
   float SizeX = 0.5f;
   float SizeY = 0.5f;
 
@@ -54,8 +58,54 @@ IGL_INLINE void igl::draw_floor(const float * colorA, const float * colorB)
     }
   }
   glEnd();
+
+  glLineWidth(2.0f);
+  glBegin(GL_LINES);
+  for (int x =-GridSizeX/2;x<=GridSizeX/2;++x)
+  {
+    if(x!=(GridSizeX/2))
+    {
+      for(int s = -1;s<2;s+=2)
+      {
+        int y = s*(GridSizeY/2);
+        int cy = y==(GridSizeY/2) ? y-1 : y;
+        if ((x+cy)&0x00000001) //modulo 2
+        {
+          glColor4fv(colorA);
+          //glColor3f(1,0,0);
+        }else
+        {
+          glColor4fv(colorB);
+          //glColor3f(0,0,1);
+        }
+        glVertex3f((x+1)*SizeX,0,y*SizeY);
+        glVertex3f(    x*SizeX,0,y*SizeY);
+      }
+    }
+    if(x==-(GridSizeX/2) || x==(GridSizeX/2))
+    {
+      int cx = x==(GridSizeX/2) ? x-1 : x;
+      for (int y =-GridSizeY/2;y<GridSizeY/2;++y)
+      {
+        if ((cx+y)&0x00000001) //modulo 2
+        {
+          glColor4fv(colorA);
+          //glColor3f(1,0,0);
+        }else
+        {
+          glColor4fv(colorB);
+          //glColor3f(0,0,1);
+        }
+        glVertex3f(x*SizeX,0,(y+1)*SizeY);
+        glVertex3f(x*SizeX,0,    y*SizeY);
+      }
+    }
+  }
+  glEnd();
+
   (old_lighting ? glEnable(GL_LIGHTING) : glDisable(GL_LIGHTING));
   (old_color_material? glEnable(GL_COLOR_MATERIAL) : glDisable(GL_COLOR_MATERIAL));
+  glLineWidth(old_line_width);
 }
 
 IGL_INLINE void igl::draw_floor()

+ 16 - 0
include/igl/unproject_to_zero_plane.cpp

@@ -18,4 +18,20 @@ IGL_INLINE int igl::unproject_to_zero_plane(
   return igl::unproject(winX, winY, winOrigin[2], objX, objY, objZ);
 }
 
+template <typename Derivedwin, typename Derivedobj>
+IGL_INLINE int igl::unproject_to_zero_plane(
+  const Eigen::PlainObjectBase<Derivedwin> & win,
+  Eigen::PlainObjectBase<Derivedobj> & obj)
+{
+  return unproject_to_zero_plane(win(0),win(1),
+      &obj.data()[0],
+      &obj.data()[1],
+      &obj.data()[2]);
+}
+
+#ifndef IGL_HEADER_ONLY
+// Explicit template instanciation
+template int igl::unproject_to_zero_plane<Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, 3, 1, 0, 3, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&);
+#endif
+
 #endif

+ 5 - 0
include/igl/unproject_to_zero_plane.h

@@ -1,6 +1,7 @@
 #ifndef IGL_UNPROJECT_TO_ZERO_PLANE_H
 #define IGL_UNPROJECT_TO_ZERO_PLANE_H
 #include "igl_inline.h"
+#include <Eigen/Core>
 namespace igl
 {
   // Wrapper for gluUnproject that uses the current GL_MODELVIEW_MATRIX,
@@ -17,6 +18,10 @@ namespace igl
     double* objX,
     double* objY,
     double* objZ);
+  template <typename Derivedwin, typename Derivedobj>
+  IGL_INLINE int unproject_to_zero_plane(
+    const Eigen::PlainObjectBase<Derivedwin> & win,
+    Eigen::PlainObjectBase<Derivedobj> & obj);
 }
 
 #ifdef IGL_HEADER_ONLY