render_to_file.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. {
  18. unsigned char * data = new unsigned char[4*width*height];
  19. glReadPixels(
  20. 0,
  21. 0,
  22. width,
  23. height,
  24. GL_RGBA,
  25. GL_UNSIGNED_BYTE,
  26. data);
  27. //img->flip();
  28. if(!alpha)
  29. {
  30. for(int i = 0;i<width;i++)
  31. for(int j = 0;j<height;j++)
  32. {
  33. data[4*(i+j*width)+3] = 255;
  34. }
  35. }
  36. const bool ret = igl::stb::write_image(filename,width,height,data);
  37. delete [] data;
  38. return ret;
  39. }
  40. #ifdef IGL_STATIC_LIBRARY
  41. // Explicit template instantiation
  42. #endif