create_vector_vbo.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "create_vector_vbo.h"
  9. #ifndef IGL_NO_OPENGL
  10. #include <cassert>
  11. // http://www.songho.ca/opengl/gl_vbo.html#create
  12. template <typename T>
  13. IGL_INLINE void igl::create_vector_vbo(
  14. const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & V,
  15. GLuint & V_vbo_id)
  16. {
  17. //// Expects that input is list of 3D vectors along rows
  18. //assert(V.cols() == 3);
  19. // Generate Buffers
  20. glGenBuffers(1,&V_vbo_id);
  21. // Bind Buffers
  22. glBindBuffer(GL_ARRAY_BUFFER,V_vbo_id);
  23. // Copy data to buffers
  24. // We expect a matrix with each vertex position on a row, we then want to
  25. // pass this data to OpenGL reading across rows (row-major)
  26. if(V.Options & Eigen::RowMajor)
  27. {
  28. glBufferData(
  29. GL_ARRAY_BUFFER,
  30. sizeof(T)*V.size(),
  31. V.data(),
  32. GL_STATIC_DRAW);
  33. }else
  34. {
  35. // Create temporary copy of transpose
  36. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> VT = V.transpose();
  37. // If its column major then we need to temporarily store a transpose
  38. glBufferData(
  39. GL_ARRAY_BUFFER,
  40. sizeof(T)*V.size(),
  41. VT.data(),
  42. GL_STATIC_DRAW);
  43. }
  44. // bind with 0, so, switch back to normal pointer operation
  45. glBindBuffer(GL_ARRAY_BUFFER, 0);
  46. }
  47. #ifndef IGL_HEADER_ONLY
  48. // Explicit template specialization
  49. // generated by autoexplicit.sh
  50. template void igl::create_vector_vbo<int>(Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, unsigned int&);
  51. // generated by autoexplicit.sh
  52. template void igl::create_vector_vbo<double>(Eigen::Matrix<double, -1, -1, 0, -1, -1> const&, unsigned int&);
  53. #endif
  54. #endif