render_to_file_async.cpp 1.3 KB

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