create_vector_vbo.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "create_vector_vbo.h"
  2. #ifndef IGL_NO_OPENGL
  3. #include <cassert>
  4. // http://www.songho.ca/opengl/gl_vbo.html#create
  5. template <typename T>
  6. IGL_INLINE void igl::create_vector_vbo(
  7. const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & V,
  8. GLuint & V_vbo_id)
  9. {
  10. //// Expects that input is list of 3D vectors along rows
  11. //assert(V.cols() == 3);
  12. // Generate Buffers
  13. glGenBuffers(1,&V_vbo_id);
  14. // Bind Buffers
  15. glBindBuffer(GL_ARRAY_BUFFER,V_vbo_id);
  16. // Copy data to buffers
  17. // We expect a matrix with each vertex position on a row, we then want to
  18. // pass this data to OpenGL reading across rows (row-major)
  19. if(V.Options & Eigen::RowMajor)
  20. {
  21. glBufferData(
  22. GL_ARRAY_BUFFER,
  23. sizeof(T)*V.size(),
  24. V.data(),
  25. GL_STATIC_DRAW);
  26. }else
  27. {
  28. // Create temporary copy of transpose
  29. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> VT = V.transpose();
  30. // If its column major then we need to temporarily store a transpose
  31. glBufferData(
  32. GL_ARRAY_BUFFER,
  33. sizeof(T)*V.size(),
  34. VT.data(),
  35. GL_STATIC_DRAW);
  36. }
  37. // bind with 0, so, switch back to normal pointer operation
  38. glBindBuffer(GL_ARRAY_BUFFER, 0);
  39. }
  40. #ifndef IGL_HEADER_ONLY
  41. // Explicit template specialization
  42. // generated by autoexplicit.sh
  43. template void igl::create_vector_vbo<int>(Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, unsigned int&);
  44. // generated by autoexplicit.sh
  45. template void igl::create_vector_vbo<double>(Eigen::Matrix<double, -1, -1, 0, -1, -1> const&, unsigned int&);
  46. #endif
  47. #endif