create_vector_vbo.h 1.8 KB

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