render_to_file.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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_file.h"
  9. #include "../../stb/write_image.h"
  10. #include <stb_image_write.h>
  11. #include "../gl.h"
  12. IGL_INLINE bool igl::opengl::stb::render_to_file(
  13. const std::string filename,
  14. const int width,
  15. const int height,
  16. const bool alpha,
  17. const bool fast)
  18. {
  19. unsigned char * data = new unsigned char[4*width*height];
  20. glReadPixels(
  21. 0,
  22. 0,
  23. width,
  24. height,
  25. GL_RGBA,
  26. GL_UNSIGNED_BYTE,
  27. data);
  28. //img->flip();
  29. if(!alpha)
  30. {
  31. for(int i = 0;i<width;i++)
  32. for(int j = 0;j<height;j++)
  33. {
  34. data[4*(i+j*width)+3] = 255;
  35. }
  36. }
  37. const bool ret = igl::stb::write_image(filename,width,height,data);
  38. delete [] data;
  39. return ret;
  40. }
  41. #ifdef IGL_STATIC_LIBRARY
  42. // Explicit template instantiation
  43. #endif