read_pixels.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "read_pixels.h"
  2. template <typename T>
  3. void igl::opengl::read_pixels(
  4. const GLuint width,
  5. const GLuint height,
  6. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & R,
  7. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & G,
  8. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & B,
  9. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & A,
  10. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & D)
  11. {
  12. R.resize(width,height);
  13. G.resize(width,height);
  14. B.resize(width,height);
  15. A.resize(width,height);
  16. D.resize(width,height);
  17. typedef typename std::conditional< std::is_floating_point<T>::value,GLfloat,GLubyte>::type GLType;
  18. GLenum type = std::is_floating_point<T>::value ? GL_FLOAT : GL_UNSIGNED_BYTE;
  19. GLType* pixels = (GLType*)calloc(width*height*4,sizeof(GLType));
  20. GLType * depth = (GLType*)calloc(width*height*1,sizeof(GLType));
  21. glReadPixels(0, 0,width, height,GL_RGBA, type, pixels);
  22. glReadPixels(0, 0,width, height,GL_DEPTH_COMPONENT, type, depth);
  23. int count = 0;
  24. for (unsigned j=0; j<height; ++j)
  25. {
  26. for (unsigned i=0; i<width; ++i)
  27. {
  28. R(i,j) = pixels[count*4+0];
  29. G(i,j) = pixels[count*4+1];
  30. B(i,j) = pixels[count*4+2];
  31. A(i,j) = pixels[count*4+3];
  32. D(i,j) = depth[count*1+0];
  33. ++count;
  34. }
  35. }
  36. // Clean up
  37. free(pixels);
  38. free(depth);
  39. }
  40. #ifdef IGL_STATIC_LIBRARY
  41. template void igl::opengl::read_pixels<unsigned char>(unsigned int, unsigned int, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&, Eigen::Matrix<unsigned char, -1, -1, 0, -1, -1>&);
  42. template void igl::opengl::read_pixels<double>(unsigned int, unsigned int, Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&);
  43. template void igl::opengl::read_pixels<float>(unsigned int, unsigned int, Eigen::Matrix<float, -1, -1, 0, -1, -1>&, Eigen::Matrix<float, -1, -1, 0, -1, -1>&, Eigen::Matrix<float, -1, -1, 0, -1, -1>&, Eigen::Matrix<float, -1, -1, 0, -1, -1>&, Eigen::Matrix<float, -1, -1, 0, -1, -1>&);
  44. #endif