create_index_vbo.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #ifndef IGL_CREATE_INDEX_VBO
  2. #define IGL_CREATE_INDEX_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 indices:
  11. // GL_ELEMENT_ARRAY_BUFFER_ARB for the triangle indices (F)
  12. namespace igl
  13. {
  14. // Inputs:
  15. // F #F by 3 eigen Matrix of face (triangle) indices
  16. // Outputs:
  17. // F_vbo_id buffer id for face indices
  18. //
  19. inline void create_index_vbo(
  20. const Eigen::MatrixXi & F,
  21. GLuint & F_vbo_id);
  22. }
  23. // Implementation
  24. // http://www.songho.ca/opengl/gl_vbo.html#create
  25. inline void igl::create_index_vbo(
  26. const Eigen::MatrixXi & F,
  27. GLuint & F_vbo_id)
  28. {
  29. // Generate Buffers
  30. glGenBuffersARB(1,&F_vbo_id);
  31. // Bind Buffers
  32. glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,F_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(F.Options & Eigen::RowMajor)
  37. {
  38. glBufferDataARB(
  39. GL_ELEMENT_ARRAY_BUFFER_ARB,
  40. sizeof(int)*F.size(),
  41. F.data(),
  42. GL_STATIC_DRAW_ARB);
  43. }else
  44. {
  45. // Create temporary copy of transpose
  46. Eigen::MatrixXi FT = F.transpose();
  47. // If its column major then we need to temporarily store a transpose
  48. glBufferDataARB(
  49. GL_ELEMENT_ARRAY_BUFFER_ARB,
  50. sizeof(int)*F.size(),
  51. FT.data(),
  52. GL_STATIC_DRAW);
  53. }
  54. // bind with 0, so, switch back to normal pointer operation
  55. glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
  56. }
  57. #endif