Browse Source

vertex array from matryoshka

Former-commit-id: c0e54bfb3bf847112ec10e962fb067e8b711f353
Alec Jacobson 7 years ago
parent
commit
9059465ffd
2 changed files with 99 additions and 0 deletions
  1. 61 0
      include/igl/opengl/vertex_array.cpp
  2. 38 0
      include/igl/opengl/vertex_array.h

+ 61 - 0
include/igl/opengl/vertex_array.cpp

@@ -0,0 +1,61 @@
+#include "vertex_array.h"
+#include <igl/opengl/report_gl_error.h>
+
+template <
+  typename DerivedV,
+  typename DerivedF>
+IGL_INLINE void igl::opengl::vertex_array(
+  const Eigen::PlainObjectBase<DerivedV> & V,
+  const Eigen::PlainObjectBase<DerivedF> & F,
+  GLuint & va_id,
+  GLuint & ab_id,
+  GLuint & eab_id)
+{
+  // Inputs should be in RowMajor storage. If not, we have no choice but to
+  // create a copy.
+  if(!(V.Options & Eigen::RowMajor))
+  {
+    Eigen::Matrix<
+      typename DerivedV::Scalar,
+      DerivedV::RowsAtCompileTime,
+      DerivedV::ColsAtCompileTime,
+      Eigen::RowMajor> VR = V;
+    return vertex_array(VR,F,va_id,ab_id,eab_id);
+  }
+  if(!(F.Options & Eigen::RowMajor))
+  {
+    Eigen::Matrix<
+      typename DerivedF::Scalar,
+      DerivedF::RowsAtCompileTime,
+      DerivedF::ColsAtCompileTime,
+      Eigen::RowMajor> FR = F;
+    return vertex_array(V,FR,va_id,ab_id,eab_id);
+  }
+  // Generate and attach buffers to vertex array
+  glGenVertexArrays(1, &va_id);
+  glGenBuffers(1, &ab_id);
+  glGenBuffers(1, &eab_id);
+  glBindVertexArray(va_id);
+  glBindBuffer(GL_ARRAY_BUFFER, ab_id);
+  const auto size_VScalar = sizeof(typename DerivedV::Scalar);
+  const auto size_FScalar = sizeof(typename DerivedF::Scalar);
+  glBufferData(GL_ARRAY_BUFFER,size_VScalar*V.size(),V.data(),GL_STATIC_DRAW);
+  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eab_id);
+  assert(sizeof(GLuint) == size_FScalar && "F type does not match GLuint");
+  glBufferData(
+    GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*F.size(), F.data(), GL_STATIC_DRAW);
+  glVertexAttribPointer(
+    0,
+    V.cols(),
+    size_VScalar==sizeof(float)?GL_FLOAT:GL_DOUBLE,
+    GL_FALSE,
+    V.cols()*size_VScalar,
+    (GLvoid*)0);
+  glEnableVertexAttribArray(0);
+  glBindBuffer(GL_ARRAY_BUFFER, 0); 
+  glBindVertexArray(0);
+}
+
+#ifdef IGL_STATIC_LIBRARY
+template void igl::opengl::vertex_array<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, unsigned int&, unsigned int&, unsigned int&);
+#endif

+ 38 - 0
include/igl/opengl/vertex_array.h

@@ -0,0 +1,38 @@
+#ifndef IGL_OPENGL_VERTEX_ARRAY_H
+#define IGL_OPENGL_VERTEX_ARRAY_H
+#include <igl/opengl/../igl_inline.h>
+#include <igl/opengl/gl.h>
+#include <Eigen/Core>
+namespace igl
+{
+  namespace opengl
+  {
+    // Create a GL_VERTEX_ARRAY for a given mesh (V,F)
+    //
+    // Inputs:
+    //   V  #V by dim list of mesh vertex positions
+    //   F  #F by 3 list of mesh triangle indices into V
+    // Outputs:
+    //   va_id  id of vertex array
+    //   ab_id  id of array buffer (vertex buffer object)
+    //   eab_id  id of element array buffer (element/face buffer object)
+    //
+    template <
+      typename DerivedV,
+      typename DerivedF>
+    IGL_INLINE void vertex_array(
+      // Note: Unlike most libigl functions, the **input** Eigen matrices must
+      // be `Eigen::PlainObjectBase` because we want to directly access it's
+      // underlying storage. It cannot be `Eigen::MatrixBase` (see
+      // http://stackoverflow.com/questions/25094948/)
+      const Eigen::PlainObjectBase<DerivedV> & V,
+      const Eigen::PlainObjectBase<DerivedF> & F,
+      GLuint & va_id,
+      GLuint & ab_id,
+      GLuint & eab_id);
+  }
+}
+#ifndef IGL_STATIC_LIBRARY
+#  include "vertex_array.cpp"
+#endif
+#endif