render_to_png_async.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_async.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. static IGL_INLINE bool render_to_png_async_helper(
  21. unsigned char * img, int width, int height,
  22. const std::string png_file,
  23. const bool alpha,
  24. const bool fast)
  25. {
  26. //img->flip();
  27. if(!alpha)
  28. {
  29. for(int i = 0;i<width;i++)
  30. for(int j = 0;j<height;j++)
  31. {
  32. img[4*(i+j*width)+3] = 255;
  33. }
  34. }
  35. bool ret = stbi_write_png(png_file.c_str(), width, height, 4, img, width*sizeof(unsigned char));
  36. delete [] img;
  37. return ret;
  38. }
  39. IGL_INLINE std::thread igl::png::render_to_png_async(
  40. const std::string png_file,
  41. const int width,
  42. const int height,
  43. const bool alpha,
  44. const bool fast)
  45. {
  46. // Part that should serial
  47. unsigned char * data = new unsigned char[width*height];
  48. glReadPixels(
  49. 0,
  50. 0,
  51. width,
  52. height,
  53. GL_RGBA,
  54. GL_UNSIGNED_BYTE,
  55. data);
  56. // Part that should be asynchronous
  57. std::thread t(render_to_png_async_helper,data,width,height,png_file,alpha,fast);
  58. t.detach();
  59. return t;
  60. }