2
0

bind_vertex_attrib_array.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "bind_vertex_attrib_array.h"
  2. namespace igl{ namespace opengl{
  3. // This would be cleaner with C++17 if constexpr
  4. template <typename Scalar>
  5. IGL_INLINE void bind_vertex_attrib_array_helper(
  6. const GLint id,
  7. const int size,
  8. const int num_cols,
  9. const Scalar * data,
  10. const bool refresh);
  11. template <>
  12. IGL_INLINE void bind_vertex_attrib_array_helper<float>(
  13. const GLint id,
  14. const int size,
  15. const int num_cols,
  16. const float * data,
  17. const bool refresh)
  18. {
  19. if (refresh)
  20. glBufferData(GL_ARRAY_BUFFER, sizeof(float)*size, data, GL_DYNAMIC_DRAW);
  21. glVertexAttribPointer(id, num_cols, GL_FLOAT, GL_FALSE, 0, 0);
  22. };
  23. template <>
  24. IGL_INLINE void bind_vertex_attrib_array_helper<double>(
  25. const GLint id,
  26. const int size,
  27. const int num_cols,
  28. const double * data,
  29. const bool refresh)
  30. {
  31. // Are you really sure you want to use doubles? Are you going to change the
  32. // `in vec3` etc. in your vertex shader to `in dvec3` ?
  33. // Are you on a mac, where this will be emulated in software?
  34. if (refresh)
  35. glBufferData(GL_ARRAY_BUFFER, sizeof(double)*size, data, GL_DYNAMIC_DRAW);
  36. glVertexAttribLPointer(id, num_cols, GL_DOUBLE, 0, 0);
  37. };
  38. }}
  39. namespace igl{ namespace opengl{
  40. template <typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
  41. IGL_INLINE GLint bind_vertex_attrib_array(
  42. const GLuint program_shader,
  43. const std::string &name,
  44. GLuint bufferID,
  45. const Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime,Eigen::RowMajor> &M,
  46. const bool refresh)
  47. {
  48. GLint id = glGetAttribLocation(program_shader, name.c_str());
  49. if (id < 0)
  50. return id;
  51. if (M.size() == 0)
  52. {
  53. glDisableVertexAttribArray(id);
  54. return id;
  55. }
  56. glBindBuffer(GL_ARRAY_BUFFER, bufferID);
  57. bind_vertex_attrib_array_helper<Scalar>(
  58. id, M.size(), M.cols(), M.data(), refresh);
  59. glEnableVertexAttribArray(id);
  60. return id;
  61. }
  62. }}
  63. #ifdef IGL_STATIC_LIBRARY
  64. // Explicit template instantiation
  65. // generated by autoexplicit.sh
  66. template int igl::opengl::bind_vertex_attrib_array<float, -1, -1>(unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, Eigen::Matrix<float, -1, -1, 1, -1, -1> const&, bool);
  67. // generated by autoexplicit.sh
  68. template int igl::opengl::bind_vertex_attrib_array<float, -1, 3>(unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, Eigen::Matrix<float, -1, 3, 1, -1, 3> const&, bool);
  69. #endif