vertex_array.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "vertex_array.h"
  2. #include <igl/opengl/report_gl_error.h>
  3. template <
  4. typename DerivedV,
  5. typename DerivedF>
  6. IGL_INLINE void igl::opengl::vertex_array(
  7. const Eigen::PlainObjectBase<DerivedV> & V,
  8. const Eigen::PlainObjectBase<DerivedF> & F,
  9. GLuint & va_id,
  10. GLuint & ab_id,
  11. GLuint & eab_id)
  12. {
  13. // Inputs should be in RowMajor storage. If not, we have no choice but to
  14. // create a copy.
  15. if(!(V.Options & Eigen::RowMajor))
  16. {
  17. Eigen::Matrix<
  18. typename DerivedV::Scalar,
  19. DerivedV::RowsAtCompileTime,
  20. DerivedV::ColsAtCompileTime,
  21. Eigen::RowMajor> VR = V;
  22. return vertex_array(VR,F,va_id,ab_id,eab_id);
  23. }
  24. if(!(F.Options & Eigen::RowMajor))
  25. {
  26. Eigen::Matrix<
  27. typename DerivedF::Scalar,
  28. DerivedF::RowsAtCompileTime,
  29. DerivedF::ColsAtCompileTime,
  30. Eigen::RowMajor> FR = F;
  31. return vertex_array(V,FR,va_id,ab_id,eab_id);
  32. }
  33. // Generate and attach buffers to vertex array
  34. glGenVertexArrays(1, &va_id);
  35. glGenBuffers(1, &ab_id);
  36. glGenBuffers(1, &eab_id);
  37. glBindVertexArray(va_id);
  38. glBindBuffer(GL_ARRAY_BUFFER, ab_id);
  39. const auto size_VScalar = sizeof(typename DerivedV::Scalar);
  40. const auto size_FScalar = sizeof(typename DerivedF::Scalar);
  41. glBufferData(GL_ARRAY_BUFFER,size_VScalar*V.size(),V.data(),GL_STATIC_DRAW);
  42. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eab_id);
  43. assert(sizeof(GLuint) == size_FScalar && "F type does not match GLuint");
  44. glBufferData(
  45. GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*F.size(), F.data(), GL_STATIC_DRAW);
  46. glVertexAttribPointer(
  47. 0,
  48. V.cols(),
  49. size_VScalar==sizeof(float)?GL_FLOAT:GL_DOUBLE,
  50. GL_FALSE,
  51. V.cols()*size_VScalar,
  52. (GLvoid*)0);
  53. glEnableVertexAttribArray(0);
  54. glBindBuffer(GL_ARRAY_BUFFER, 0);
  55. glBindVertexArray(0);
  56. }
  57. #ifdef IGL_STATIC_LIBRARY
  58. 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&);
  59. #endif