vertex_array.cpp 2.1 KB

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