render_to_file_async.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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_async.h"
  9. #include "../../stb/write_image.h"
  10. #include "../gl.h"
  11. #include <stb_image_write.h>
  12. static IGL_INLINE bool render_to_file_async_helper(
  13. unsigned char * img, int width, int height,
  14. const std::string filename,
  15. const bool alpha,
  16. const bool fast)
  17. {
  18. //img->flip();
  19. if(!alpha)
  20. {
  21. for(int i = 0;i<width;i++)
  22. for(int j = 0;j<height;j++)
  23. {
  24. img[4*(i+j*width)+3] = 255;
  25. }
  26. }
  27. const bool ret = igl::stb::write_image(filename,width,height,img);
  28. delete [] img;
  29. return ret;
  30. }
  31. IGL_INLINE std::thread igl::opengl::stb::render_to_file_async(
  32. const std::string filename,
  33. const int width,
  34. const int height,
  35. const bool alpha,
  36. const bool fast)
  37. {
  38. // Part that should serial
  39. unsigned char * data = new unsigned char[width*height];
  40. glReadPixels(
  41. 0,
  42. 0,
  43. width,
  44. height,
  45. GL_RGBA,
  46. GL_UNSIGNED_BYTE,
  47. data);
  48. // Part that should be asynchronous
  49. std::thread t(render_to_file_async_helper,data,width,height,filename,alpha,fast);
  50. t.detach();
  51. return t;
  52. }