render_to_png.cpp 692 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "render_to_png.h"
  2. #include <YImage.hpp>
  3. #ifdef __APPLE__
  4. # include <OpenGL/gl.h>
  5. #else
  6. # ifdef _WIN32
  7. # define NOMINMAX
  8. # include <Windows.h>
  9. # undef NOMINMAX
  10. # endif
  11. # include <GL/gl.h>
  12. #endif
  13. IGL_INLINE bool igl::render_to_png(
  14. const std::string png_file,
  15. const int width,
  16. const int height,
  17. const bool alpha,
  18. const bool fast)
  19. {
  20. YImage img;
  21. img.resize(width,height);
  22. glReadPixels(
  23. 0,
  24. 0,
  25. width,
  26. height,
  27. GL_RGBA,
  28. GL_UNSIGNED_BYTE,
  29. img.data());
  30. img.flip();
  31. if(!alpha)
  32. {
  33. for(int i = 0;i<width;i++)
  34. for(int j = 0;j<height;j++)
  35. {
  36. img.at(i,j).a = 255;
  37. }
  38. }
  39. return img.save(png_file.c_str(),fast);
  40. }