texture_from_png.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 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 "texture_from_png.h"
  9. #ifndef IGL_NO_OPENGL
  10. #include <YImage.hpp>
  11. #include <igl/report_gl_error.h>
  12. IGL_INLINE bool igl::texture_from_png(const std::string png_file, GLuint & id)
  13. {
  14. YImage yimg;
  15. if(!yimg.load(png_file.c_str()))
  16. {
  17. return false;
  18. }
  19. // Why do I need to flip?
  20. //yimg.flip();
  21. glGenTextures(1, &id);
  22. glBindTexture(GL_TEXTURE_2D, id);
  23. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  24. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  25. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  26. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  27. glTexImage2D(
  28. GL_TEXTURE_2D, 0, GL_RGB,
  29. yimg.width(), yimg.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, yimg.data());
  30. glBindTexture(GL_TEXTURE_2D, 0);
  31. return true;
  32. }
  33. #endif