create_vector_vbo.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_ARB 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. // http://www.songho.ca/opengl/gl_vbo.html#create
  25. void igl::create_vector_vbo(
  26. const Eigen::MatrixXd & V,
  27. GLuint & V_vbo_id)
  28. {
  29. // Generate Buffers
  30. glGenBuffersARB(1,&V_vbo_id);
  31. // Bind Buffers
  32. glBindBufferARB(GL_ARRAY_BUFFER_ARB,V_vbo_id);
  33. // Copy data to buffers
  34. // We expect a matrix with each vertex position on a row, we then want to
  35. // pass this data to OpenGL reading across rows (row-major)
  36. if(V.Options & Eigen::RowMajor)
  37. {
  38. glBufferDataARB(
  39. GL_ARRAY_BUFFER_ARB,
  40. sizeof(double)*V.size(),
  41. V.data(),
  42. GL_STATIC_DRAW_ARB);
  43. }else
  44. {
  45. // Create temporary copy of transpose
  46. Eigen::MatrixXd VT = V.transpose();
  47. // If its column major then we need to temporarily store a transpose
  48. glBufferDataARB(
  49. GL_ARRAY_BUFFER_ARB,
  50. sizeof(double)*V.size(),
  51. VT.data(),
  52. GL_STATIC_DRAW_ARB);
  53. }
  54. // bind with 0, so, switch back to normal pointer operation
  55. glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
  56. }
  57. #endif