Util.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "Util.h"
  2. #include "stb_image.h"
  3. Util::Util()
  4. {
  5. }
  6. Util::~Util()
  7. {
  8. }
  9. GLuint Util::loadTexture(const std::string &filename)
  10. {
  11. int width, height, bpp;
  12. stbi_set_flip_vertically_on_load(true);
  13. unsigned char* imgData = stbi_load(filename.c_str(), &width, &height, &bpp, 4);
  14. GLuint num;
  15. glGenTextures(1, &num);
  16. glBindTexture(GL_TEXTURE_2D, num);
  17. glTexImage2D(GL_TEXTURE_2D,
  18. 0, //level
  19. GL_RGBA, //internal format
  20. width, //width
  21. height, //height
  22. 0, //border
  23. GL_RGBA, //data format
  24. GL_UNSIGNED_BYTE, //data type
  25. imgData); //data
  26. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  27. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  28. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  29. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  30. stbi_image_free(imgData);
  31. return num;
  32. }
  33. void Util::glutBitmapString(std::string str, int x, int y)
  34. {
  35. glRasterPos2f(x, y);
  36. for (int i = 0; i < str.size(); i++)
  37. {
  38. glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, str[i]);
  39. }
  40. }
  41. int Util::glutTextWidth(const std::string str)
  42. {
  43. int total = 0;
  44. for (int i = 0; i < str.size(); i++)
  45. {
  46. total += glutBitmapWidth(GLUT_BITMAP_HELVETICA_18, str[i]);
  47. }
  48. return total;
  49. }
  50. //int Util::glutTextHeight()
  51. //{
  52. // return glutBitmapHeight(GLUT_BITMAP_HELVETICA_18);
  53. //}