render_to_png_async.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #ifndef IGL_NO_BOOST
  2. #include "render_to_png_async.h"
  3. #include <YImage.hpp>
  4. #ifdef __APPLE__
  5. # include <OpenGL/gl.h>
  6. #else
  7. # ifdef _WIN32
  8. # define NOMINMAX
  9. # include <Windows.h>
  10. # undef NOMINMAX
  11. # endif
  12. # include <GL/gl.h>
  13. #endif
  14. static IGL_INLINE bool render_to_png_async_helper(
  15. YImage * img,
  16. const std::string png_file,
  17. const bool alpha,
  18. const bool fast)
  19. {
  20. img->flip();
  21. const int width = img->width();
  22. const int height = img->height();
  23. if(!alpha)
  24. {
  25. for(int i = 0;i<width;i++)
  26. for(int j = 0;j<height;j++)
  27. {
  28. img->at(i,j).a = 255;
  29. }
  30. }
  31. return img->save(png_file.c_str(),fast);
  32. }
  33. IGL_INLINE boost::thread igl::render_to_png_async(
  34. const std::string png_file,
  35. const int width,
  36. const int height,
  37. const bool alpha,
  38. const bool fast)
  39. {
  40. // Part that should serial
  41. YImage * img = new YImage();
  42. img->resize(width,height);
  43. glReadPixels(
  44. 0,
  45. 0,
  46. width,
  47. height,
  48. GL_RGBA,
  49. GL_UNSIGNED_BYTE,
  50. img->data());
  51. // Part that should be asynchronous
  52. return boost::thread(render_to_png_async_helper,img,png_file,alpha,fast);
  53. }
  54. #endif