You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
970 B

  1. #include "Texture.h"
  2. #include "util.h"
  3. TextureRGB::TextureRGB()
  4. {
  5. mImageFileContent = nullptr;
  6. mRGBPixel = nullptr;
  7. mWidth = 0;
  8. mHeight = 0;
  9. }
  10. TextureRGB::~TextureRGB()
  11. {
  12. if (mImageFileContent) {
  13. delete[] mImageFileContent;
  14. }
  15. }
  16. void TextureRGB::Set(const char * image_path)
  17. {
  18. int file_size = 0;
  19. mImageFileContent = LoadFileContent(image_path, file_size);
  20. mRGBPixel = DecodeBMP(mImageFileContent, mWidth, mHeight);
  21. }
  22. Vector3 TextureRGB::Sample(float u, float v)
  23. {
  24. int x = int(u * mWidth);
  25. int y = int(u * mHeight);
  26. x = x < 0 ? 0 : x;
  27. y = y < 0 ? 0 : y;
  28. x = x > (mWidth - 1) ? (mWidth - 1) : x;
  29. y = y > (mHeight - 1) ? (mHeight - 1) : y;
  30. int pixel_data_start_index = 3 * (x + y * mWidth);
  31. float r = float(mRGBPixel[pixel_data_start_index]) / 255.0f;
  32. float g = float(mRGBPixel[pixel_data_start_index + 1]) / 255.0f;
  33. float b = float(mRGBPixel[pixel_data_start_index + 2]) / 255.0f;
  34. return Vector3(r, g, b);
  35. }