create_vector_vbo.cpp 1.8 KB

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