create_vector_vbo.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef IGL_CREATE_VECTOR_VBO
  2. #define IGL_CREATE_VECTOR_VBO
  3. // NOTE: It wouldn't be so hard to template this using Eigen's templates
  4. #include <Eigen/Core>
  5. #if __APPLE__
  6. # include <OpenGL/gl.h>
  7. #else
  8. # ifdef _WIN32
  9. # define NOMINMAX
  10. # include <Windows.h>
  11. # undef NOMINMAX
  12. # endif
  13. # include <GL/gl.h>
  14. #endif
  15. // Create a VBO (Vertex Buffer Object) for a list of vectors:
  16. // GL_ARRAY_BUFFER for the vectors (V)
  17. namespace igl
  18. {
  19. // Templates:
  20. // T should be a eigen matrix primitive type like int or double
  21. // Inputs:
  22. // V m by n eigen Matrix of type T values
  23. // Outputs:
  24. // V_vbo_id buffer id for vectors
  25. //
  26. template <typename T>
  27. inline void create_vector_vbo(
  28. const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & V,
  29. GLuint & V_vbo_id);
  30. }
  31. // Implementation
  32. #include <cassert>
  33. // http://www.songho.ca/opengl/gl_vbo.html#create
  34. template <typename T>
  35. inline void igl::create_vector_vbo(
  36. const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & V,
  37. GLuint & V_vbo_id)
  38. {
  39. //// Expects that input is list of 3D vectors along rows
  40. //assert(V.cols() == 3);
  41. // Generate Buffers
  42. glGenBuffers(1,&V_vbo_id);
  43. // Bind Buffers
  44. glBindBuffer(GL_ARRAY_BUFFER,V_vbo_id);
  45. // Copy data to buffers
  46. // We expect a matrix with each vertex position on a row, we then want to
  47. // pass this data to OpenGL reading across rows (row-major)
  48. if(V.Options & Eigen::RowMajor)
  49. {
  50. glBufferData(
  51. GL_ARRAY_BUFFER,
  52. sizeof(T)*V.size(),
  53. V.data(),
  54. GL_STATIC_DRAW);
  55. }else
  56. {
  57. // Create temporary copy of transpose
  58. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> VT = V.transpose();
  59. // If its column major then we need to temporarily store a transpose
  60. glBufferData(
  61. GL_ARRAY_BUFFER,
  62. sizeof(T)*V.size(),
  63. VT.data(),
  64. GL_STATIC_DRAW);
  65. }
  66. // bind with 0, so, switch back to normal pointer operation
  67. glBindBuffer(GL_ARRAY_BUFFER, 0);
  68. }
  69. #endif