create_vector_vbo.h 1.6 KB

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