render_to_png.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "render_to_png.h"
  9. #include <stb_image_write.h>
  10. #ifdef __APPLE__
  11. # include <OpenGL/gl.h>
  12. #else
  13. # ifdef _WIN32
  14. # define NOMINMAX
  15. # include <Windows.h>
  16. # undef NOMINMAX
  17. # endif
  18. # include <GL/gl.h>
  19. #endif
  20. IGL_INLINE bool igl::png::render_to_png(
  21. const std::string png_file,
  22. const int width,
  23. const int height,
  24. const bool alpha,
  25. const bool fast)
  26. {
  27. unsigned char * data = new unsigned char[width*height];
  28. glReadPixels(
  29. 0,
  30. 0,
  31. width,
  32. height,
  33. GL_RGBA,
  34. GL_UNSIGNED_BYTE,
  35. data);
  36. //img->flip();
  37. if(!alpha)
  38. {
  39. for(int i = 0;i<width;i++)
  40. for(int j = 0;j<height;j++)
  41. {
  42. data[4*(i+j*width)+3] = 255;
  43. }
  44. }
  45. bool ret = stbi_write_png(png_file.c_str(), width, height, 4, data, width*sizeof(unsigned char));
  46. delete [] data;
  47. return ret;
  48. }
  49. #ifdef IGL_STATIC_LIBRARY
  50. // Explicit template specialization
  51. #endif