Explorar o código

templates, viewer opengl code extraction

Former-commit-id: 2d9f56c925c913460e51850c5d96bd8cb96b8572
Alec Jacobson %!s(int64=10) %!d(string=hai) anos
pai
achega
a1c8fcb85c

+ 3 - 12
examples/scene-rotation/example.cpp

@@ -32,7 +32,6 @@
 
 #ifdef __APPLE__
 #include <GLUT/glut.h>
-#include <Carbon/Carbon.h>
 #else
 #include <GL/glut.h>
 #endif
@@ -483,14 +482,6 @@ void init_relative()
 }
 
 
-KeyMap keyStates ;
-bool IS_KEYDOWN( uint16_t vKey )
-{
-  uint8_t index = vKey / 32 ;
-  uint8_t shift = vKey % 32 ;
-  return keyStates[index].bigEndianValue & (1 << shift) ;
-}
-
 
 void undo()
 {
@@ -519,9 +510,9 @@ void key(unsigned char key, int mouse_x, int mouse_y)
   using namespace std;
   using namespace igl;
   using namespace Eigen;
-  GetKeys(keyStates);
-  const bool command_down = IS_KEYDOWN(kVK_Command);
-  const bool shift_down = IS_KEYDOWN(kVK_Shift);
+  const int mod = glutGetModifiers();
+  const bool command_down = mod | GLUT_ACTIVE_COMMAND;
+  const bool shift_down = mod | GLUT_ACTIVE_SHIFT;
   switch(key)
   {
     // ESC

+ 1 - 0
include/igl/all_edges.cpp

@@ -48,4 +48,5 @@ IGL_INLINE void igl::all_edges(
 template void igl::all_edges<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
 template void igl::all_edges<Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 2, 0, -1, 2> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> >&);
 template void igl::all_edges<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 2, 0, -1, 2> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> >&);
+template void igl::all_edges<Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
 #endif

+ 21 - 0
include/igl/frustum.cpp

@@ -0,0 +1,21 @@
+#include "frustum.h"
+template < typename DerivedP>
+IGL_INLINE void igl::frustum(
+  const typename DerivedP::Scalar left,
+  const typename DerivedP::Scalar right,
+  const typename DerivedP::Scalar bottom,
+  const typename DerivedP::Scalar top,
+  const typename DerivedP::Scalar nearVal,
+  const typename DerivedP::Scalar farVal,
+  Eigen::PlainObjectBase<DerivedP> & P)
+{
+  P.setConstant(4,4,0.);
+  P(0,0) = (2.0 * nearVal) / (right - left);
+  P(1,1) = (2.0 * nearVal) / (top - bottom);
+  P(0,2) = (right + left) / (right - left);
+  P(1,2) = (top + bottom) / (top - bottom);
+  P(2,2) = -(farVal + nearVal) / (farVal - nearVal);
+  P(3,2) = -1.0;
+  P(2,3) = -(2.0 * farVal * nearVal) / (farVal - nearVal);
+}
+

+ 44 - 0
include/igl/frustum.h

@@ -0,0 +1,44 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+// 
+// Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
+// 
+// This Source Code Form is subject to the terms of the Mozilla Public License 
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
+// obtain one at http://mozilla.org/MPL/2.0/.
+#ifndef IGL_FRUSTUM_H
+#define IGL_FRUSTUM_H
+#include "igl_inline.h"
+
+#include <Eigen/Dense>
+
+namespace igl 
+{
+  // Implementation of the deprecated glFrustum function.
+  //
+  // Inputs:
+  //   left  coordinate of left vertical clipping plane
+  //   right  coordinate of right vertical clipping plane
+  //   bottom  coordinate of bottom vertical clipping plane
+  //   top  coordinate of top vertical clipping plane
+  //   nearVal  distance to near plane
+  //   farVal  distance to far plane
+  // Outputs:
+  //   P  4x4 perspective matrix
+  template < typename DerivedP>
+  IGL_INLINE void frustum(
+    const typename DerivedP::Scalar left,
+    const typename DerivedP::Scalar right,
+    const typename DerivedP::Scalar bottom,
+    const typename DerivedP::Scalar top,
+    const typename DerivedP::Scalar nearVal,
+    const typename DerivedP::Scalar farVal,
+    Eigen::PlainObjectBase<DerivedP> & P);
+}
+
+#ifndef IGL_STATIC_LIBRARY
+#  include "frustum.cpp"
+#endif
+
+#endif
+
+

+ 2 - 1
include/igl/internal_angles.cpp

@@ -48,7 +48,7 @@ IGL_INLINE void igl::internal_angles(
   #  define IGL_OMP_MIN_VALUE 1000
   #endif
   #pragma omp parallel for if (m>IGL_OMP_MIN_VALUE)
-  for(long int f = 0;f<m;f++)
+  for(size_t f = 0;f<m;f++)
   {
     for(size_t d = 0;d<3;d++)
     {
@@ -64,4 +64,5 @@ IGL_INLINE void igl::internal_angles(
 // Explicit template specialization
 template void igl::internal_angles<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
 template void igl::internal_angles<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
+template void igl::internal_angles<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
 #endif

+ 2 - 0
include/igl/list_to_matrix.cpp

@@ -152,4 +152,6 @@ template bool igl::list_to_matrix<int, Eigen::PlainObjectBase<Eigen::Matrix<int,
 template bool igl::list_to_matrix<double, Eigen::Matrix<double, 1, -1, 1, 1, -1> >(std::vector<double, std::allocator<double> > const&, Eigen::Matrix<double, 1, -1, 1, 1, -1>&);
 template bool igl::list_to_matrix<unsigned long, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > >(std::vector<unsigned long, std::allocator<unsigned long> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
 template bool igl::list_to_matrix<float, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > >(std::vector<std::vector<float, std::allocator<float> >, std::allocator<std::vector<float, std::allocator<float> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&);
+template bool igl::list_to_matrix<double, Eigen::Matrix<double, -1, 2, 0, -1, 2> >(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > const&, Eigen::Matrix<double, -1, 2, 0, -1, 2>&);
+template bool igl::list_to_matrix<double, Eigen::Matrix<double, -1, 3, 0, -1, 3> >(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > const&, Eigen::Matrix<double, -1, 3, 0, -1, 3>&);
 #endif

+ 31 - 0
include/igl/look_at.cpp

@@ -0,0 +1,31 @@
+#include "look_at.h"
+
+template <
+  typename Derivedeye,
+  typename Derivedcenter,
+  typename Derivedup,
+  typename DerivedR>
+IGL_INLINE void igl::look_at(
+  const Eigen::PlainObjectBase<Derivedeye> & eye,
+  const Eigen::PlainObjectBase<Derivedcenter> & center,
+  const Eigen::PlainObjectBase<Derivedup> & up,
+  Eigen::PlainObjectBase<DerivedR> & R)
+{
+  typedef Eigen::Matrix<typename DerivedR::Scalar,3,1> Vector3S;
+  Vector3S f = (center - eye).normalized();
+  Vector3S s = f.cross(up).normalized();
+  Vector3S u = s.cross(f);
+  R = Eigen::Matrix<typename DerivedR::Scalar,4,4>::Identity();
+  R(0,0) = s(0);
+  R(0,1) = s(1);
+  R(0,2) = s(2);
+  R(1,0) = u(0);
+  R(1,1) = u(1);
+  R(1,2) = u(2);
+  R(2,0) =-f(0);
+  R(2,1) =-f(1);
+  R(2,2) =-f(2);
+  R(0,3) =-s.transpose() * eye;
+  R(1,3) =-u.transpose() * eye;
+  R(2,3) = f.transpose() * eye;
+}

+ 42 - 0
include/igl/look_at.h

@@ -0,0 +1,42 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+// 
+// Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
+// 
+// This Source Code Form is subject to the terms of the Mozilla Public License 
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
+// obtain one at http://mozilla.org/MPL/2.0/.
+#ifndef IGL_LOOK_AT_H
+#define IGL_LOOK_AT_H
+#include "igl_inline.h"
+
+#include <Eigen/Dense>
+
+namespace igl 
+{
+  // Implementation of the deprecated gluLookAt function.
+  //
+  // Inputs:
+  //   eye  3-vector of eye position
+  //   center  3-vector of center reference point
+  //   up  3-vector of up vector
+  // Outputs:
+  //   R  4x4 rotation matrix
+  //
+  template <
+    typename Derivedeye,
+    typename Derivedcenter,
+    typename Derivedup,
+    typename DerivedR >
+  IGL_INLINE void look_at(
+    const Eigen::PlainObjectBase<Derivedeye> & eye,
+    const Eigen::PlainObjectBase<Derivedcenter> & center,
+    const Eigen::PlainObjectBase<Derivedup> & up,
+    Eigen::PlainObjectBase<DerivedR> & R);
+}
+
+#ifndef IGL_STATIC_LIBRARY
+#  include "look_at.cpp"
+#endif
+
+#endif
+

+ 20 - 0
include/igl/ortho.cpp

@@ -0,0 +1,20 @@
+#include "ortho.h"
+
+template < typename DerivedP>
+IGL_INLINE void igl::ortho(
+  const typename DerivedP::Scalar left,
+  const typename DerivedP::Scalar right,
+  const typename DerivedP::Scalar bottom,
+  const typename DerivedP::Scalar top,
+  const typename DerivedP::Scalar nearVal,
+  const typename DerivedP::Scalar farVal,
+  Eigen::PlainObjectBase<DerivedP> & P)
+{
+  P.setIdentity();
+  P(0,0) = 2. / (right - left);
+  P(1,1) = 2. / (top - bottom);
+  P(2,2) = - 2./ (farVal - nearVal);
+  P(0,3) = - (right + left) / (right - left);
+  P(1,3) = - (top + bottom) / (top - bottom);
+  P(2,3) = - (farVal + nearVal) / (farVal - nearVal);
+}

+ 42 - 0
include/igl/ortho.h

@@ -0,0 +1,42 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+// 
+// Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
+// 
+// This Source Code Form is subject to the terms of the Mozilla Public License 
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
+// obtain one at http://mozilla.org/MPL/2.0/.
+#ifndef IGL_ORTHO_H
+#define IGL_ORTHO_H
+#include "igl_inline.h"
+
+#include <Eigen/Dense>
+
+namespace igl 
+{
+  // Implementation of the deprecated glOrtho function.
+  //
+  // Inputs:
+  //   left  coordinate of left vertical clipping plane
+  //   right  coordinate of right vertical clipping plane
+  //   bottom  coordinate of bottom vertical clipping plane
+  //   top  coordinate of top vertical clipping plane
+  //   nearVal  distance to near plane
+  //   farVal  distance to far plane
+  // Outputs:
+  //   P  4x4 perspective matrix
+  template < typename DerivedP>
+  IGL_INLINE void ortho(
+    const typename DerivedP::Scalar left,
+    const typename DerivedP::Scalar right,
+    const typename DerivedP::Scalar bottom,
+    const typename DerivedP::Scalar top,
+    const typename DerivedP::Scalar nearVal,
+    const typename DerivedP::Scalar farVal,
+    Eigen::PlainObjectBase<DerivedP> & P);
+}
+
+#ifndef IGL_STATIC_LIBRARY
+#  include "ortho.cpp"
+#endif
+
+#endif

+ 2 - 0
include/igl/per_vertex_normals.cpp

@@ -103,4 +103,6 @@ IGL_INLINE void igl::per_vertex_normals(
 // Explicit template specialization
 template void igl::per_vertex_normals<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
 template void igl::per_vertex_normals<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
+template void igl::per_vertex_normals<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&);
+template void igl::per_vertex_normals<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&);
 #endif

+ 1 - 0
include/igl/triangle_triangle_adjacency.cpp

@@ -207,4 +207,5 @@ template <
 // Explicit template specialization
 template void igl::triangle_triangle_adjacency<Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<long, -1, 1, 0, -1, 1>, long, long, long>(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> > const&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> > const&, std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > > const&, bool, std::vector<std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > >, std::allocator<std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > > > >&, std::vector<std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > >, std::allocator<std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > > > >&);
 template void igl::triangle_triangle_adjacency<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
+template void igl::triangle_triangle_adjacency<Eigen::Matrix<int, -1, -1, 0, -1, -1>, long, long>(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, std::vector<std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > >, std::allocator<std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > > > >&, std::vector<std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > >, std::allocator<std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > > > >&);
 #endif

+ 2 - 0
include/igl/unique_edge_map.cpp

@@ -40,4 +40,6 @@ IGL_INLINE void igl::unique_edge_map(
 // Explicit template specialization
 template void igl::unique_edge_map<Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<long, -1, 1, 0, -1, 1>, long>(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&, std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > >&);
 template void igl::unique_edge_map<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<long, -1, 1, 0, -1, 1>, long>(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&, std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > >&);
+template void igl::unique_edge_map<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, int>(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&);
+template void igl::unique_edge_map<Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, int>(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&);
 #endif 

+ 1 - 0
include/igl/view_axis.cpp

@@ -37,6 +37,7 @@ IGL_INLINE void igl::view_axis(Eigen::PlainObjectBase<DerivedV> & V)
 #ifdef IGL_STATIC_LIBRARY
 // Explicit template instanciation
 template void igl::view_axis<Eigen::Matrix<double, 3, 1, 0, 3, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&);
+template void igl::view_axis<Eigen::Matrix<double, 1, 3, 1, 1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&);
 #endif
 
 #endif

+ 4 - 2
include/igl/view_axis.h

@@ -13,6 +13,8 @@
 namespace igl
 {
   // Determines the view axis or depth axis of the current gl matrix
+  // Inputs:
+  //   mv pointer to modelview matrix
   // Outputs:
   //   x  pointer to x-coordinate in scene coordinates of the un-normalized
   //     viewing axis 
@@ -20,11 +22,11 @@ namespace igl
   //     viewing axis 
   //   z  pointer to z-coordinate in scene coordinates of the un-normalized
   //     viewing axis
-  //   mv pointer to modelview matrix
   //
   // Note: View axis is returned *UN-normalized*
-  IGL_INLINE void view_axis(double * x, double * y, double * z);
   IGL_INLINE void view_axis(const double * mv, double * x, double * y, double * z);
+  // Extract mv from current GL state.
+  IGL_INLINE void view_axis(double * x, double * y, double * z);
   template <typename DerivedV>
   IGL_INLINE void view_axis(Eigen::PlainObjectBase<DerivedV> & V);
 };

+ 10 - 92
include/igl/viewer/ViewerCore.cpp

@@ -8,95 +8,14 @@
 
 #include "ViewerCore.h"
 #include <igl/quat_to_mat.h>
+#include <igl/look_at.h>
+#include <igl/frustum.h>
+#include <igl/ortho.h>
 #include <igl/massmatrix.h>
 #include <igl/barycenter.h>
 #include <Eigen/Geometry>
 #include <iostream>
 
-
-IGL_INLINE Eigen::Matrix4f lookAt (
-                        const Eigen::Vector3f& eye,
-                        const Eigen::Vector3f& center,
-                        const Eigen::Vector3f& up)
-{
-  Eigen::Vector3f f = (center - eye).normalized();
-  Eigen::Vector3f s = f.cross(up).normalized();
-  Eigen::Vector3f u = s.cross(f);
-
-  Eigen::Matrix4f Result = Eigen::Matrix4f::Identity();
-  Result(0,0) = s(0);
-  Result(0,1) = s(1);
-  Result(0,2) = s(2);
-  Result(1,0) = u(0);
-  Result(1,1) = u(1);
-  Result(1,2) = u(2);
-  Result(2,0) =-f(0);
-  Result(2,1) =-f(1);
-  Result(2,2) =-f(2);
-  Result(0,3) =-s.transpose() * eye;
-  Result(1,3) =-u.transpose() * eye;
-  Result(2,3) = f.transpose() * eye;
-  return Result;
-}
-
-IGL_INLINE Eigen::Matrix4f ortho(
-                       const float left,
-                       const float right,
-                       const float bottom,
-                       const float top,
-                       const float zNear,
-                       const float zFar
-                       )
-{
-  Eigen::Matrix4f Result = Eigen::Matrix4f::Identity();
-  Result(0,0) = 2.0f / (right - left);
-  Result(1,1) = 2.0f / (top - bottom);
-  Result(2,2) = - 2.0f / (zFar - zNear);
-  Result(0,3) = - (right + left) / (right - left);
-  Result(1,3) = - (top + bottom) / (top - bottom);
-  Result(2,3) = - (zFar + zNear) / (zFar - zNear);
-  return Result;
-}
-
-IGL_INLINE Eigen::Matrix4f frustum(
-                         const float left,
-                         const float right,
-                         const float bottom,
-                         const float top,
-                         const float nearVal,
-                         const float farVal)
-{
-  Eigen::Matrix4f Result = Eigen::Matrix4f::Zero();
-  Result(0,0) = (2.0f * nearVal) / (right - left);
-  Result(1,1) = (2.0f * nearVal) / (top - bottom);
-  Result(0,2) = (right + left) / (right - left);
-  Result(1,2) = (top + bottom) / (top - bottom);
-  Result(2,2) = -(farVal + nearVal) / (farVal - nearVal);
-  Result(3,2) = -1.0f;
-  Result(2,3) = -(2.0f * farVal * nearVal) / (farVal - nearVal);
-  return Result;
-}
-
-IGL_INLINE Eigen::Matrix4f scale(const Eigen::Matrix4f& m,
-                       const Eigen::Vector3f& v)
-{
-  Eigen::Matrix4f Result;
-  Result.col(0) = m.col(0).array() * v(0);
-  Result.col(1) = m.col(1).array() * v(1);
-  Result.col(2) = m.col(2).array() * v(2);
-  Result.col(3) = m.col(3);
-  return Result;
-}
-
-IGL_INLINE Eigen::Matrix4f translate(
-                          const Eigen::Matrix4f& m,
-                          const Eigen::Vector3f& v)
-{
-  Eigen::Matrix4f Result = m;
-  Result.col(3) = m.col(0).array() * v(0) + m.col(1).array() * v(1) + m.col(2).array() * v(2) + m.col(3).array();
-  return Result;
-}
-
 #ifdef ENABLE_SERIALIZATION
 #include <igl/serialize.h>
 namespace igl {
@@ -237,9 +156,7 @@ IGL_INLINE void igl::ViewerCore::draw(ViewerData& data, OpenGL_state& opengl)
   proj  = Eigen::Matrix4f::Identity();
 
   // Set view
-  view = lookAt(Eigen::Vector3f(camera_eye[0], camera_eye[1], camera_eye[2]),
-                Eigen::Vector3f(camera_center[0], camera_center[1], camera_center[2]),
-                Eigen::Vector3f(camera_up[0], camera_up[1], camera_up[2]));
+  look_at( camera_eye, camera_center, camera_up, view);
 
   float width  = viewport(2);
   float height = viewport(3);
@@ -249,13 +166,13 @@ IGL_INLINE void igl::ViewerCore::draw(ViewerData& data, OpenGL_state& opengl)
   {
     float length = (camera_eye - camera_center).norm();
     float h = tan(camera_view_angle/360.0 * M_PI) * (length);
-    proj = ortho(-h*width/height, h*width/height, -h, h, camera_dnear, camera_dfar);
+    ortho(-h*width/height, h*width/height, -h, h, camera_dnear, camera_dfar,proj);
   }
   else
   {
     float fH = tan(camera_view_angle / 360.0 * M_PI) * camera_dnear;
     float fW = fH * (double)width/(double)height;
-    proj = frustum(-fW, fW, -fH, fH, camera_dnear, camera_dfar);
+    frustum(-fW, fW, -fH, fH, camera_dnear, camera_dfar,proj);
   }
   // end projection
 
@@ -267,9 +184,10 @@ IGL_INLINE void igl::ViewerCore::draw(ViewerData& data, OpenGL_state& opengl)
     for (unsigned j=0;j<4;++j)
       model(i,j) = mat[i+4*j];
 
-  model = scale(model, Eigen::Vector3f(camera_zoom,camera_zoom,camera_zoom));
-  model = scale(model, Eigen::Vector3f(model_zoom,model_zoom,model_zoom));
-  model = translate(model, Eigen::Vector3f(model_translation[0],model_translation[1],model_translation[2]));
+  // Why not just use Eigen::Transform<double,3,Projective> for model...?
+  model.topLeftCorner(3,3)*=camera_zoom;
+  model.topLeftCorner(3,3)*=model_zoom;
+  model.col(3).head(3) += model.topLeftCorner(3,3)*model_translation;
 
   // Send transformations to the GPU
   GLint modeli = opengl.shader_mesh.uniform("model");