create_index_vbo.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. # 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 indices:
  16. // GL_ELEMENT_ARRAY_BUFFER_ARB for the triangle indices (F)
  17. namespace igl
  18. {
  19. // Inputs:
  20. // F #F by 3 eigen Matrix of face (triangle) indices
  21. // Outputs:
  22. // F_vbo_id buffer id for face indices
  23. //
  24. inline void create_index_vbo(
  25. const Eigen::MatrixXi & F,
  26. GLuint & F_vbo_id);
  27. }
  28. // Implementation
  29. // http://www.songho.ca/opengl/gl_vbo.html#create
  30. inline void igl::create_index_vbo(
  31. const Eigen::MatrixXi & F,
  32. GLuint & F_vbo_id)
  33. {
  34. // Generate Buffers
  35. glGenBuffersARB(1,&F_vbo_id);
  36. // Bind Buffers
  37. glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,F_vbo_id);
  38. // Copy data to buffers
  39. // We expect a matrix with each vertex position on a row, we then want to
  40. // pass this data to OpenGL reading across rows (row-major)
  41. if(F.Options & Eigen::RowMajor)
  42. {
  43. glBufferDataARB(
  44. GL_ELEMENT_ARRAY_BUFFER_ARB,
  45. sizeof(int)*F.size(),
  46. F.data(),
  47. GL_STATIC_DRAW_ARB);
  48. }else
  49. {
  50. // Create temporary copy of transpose
  51. Eigen::MatrixXi FT = F.transpose();
  52. // If its column major then we need to temporarily store a transpose
  53. glBufferDataARB(
  54. GL_ELEMENT_ARRAY_BUFFER_ARB,
  55. sizeof(int)*F.size(),
  56. FT.data(),
  57. GL_STATIC_DRAW);
  58. }
  59. // bind with 0, so, switch back to normal pointer operation
  60. glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
  61. }
  62. #endif