Sampler2D.cpp 996 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Sampler2D.h"
  7. #include <LibGL/Tex/Texture2D.h>
  8. #include <math.h>
  9. namespace GL {
  10. static constexpr float wrap_repeat(float value)
  11. {
  12. return value - floorf(value);
  13. }
  14. FloatVector4 Sampler2D::sample(FloatVector2 const& uv) const
  15. {
  16. // FIXME: Calculate the correct mipmap level here, need to receive uv derivatives for that
  17. unsigned lod = 0;
  18. MipMap const& mip = m_texture.mipmap(lod);
  19. float x = uv.x();
  20. float y = uv.y();
  21. switch (m_wrap_s_mode) {
  22. case GL_REPEAT:
  23. x = wrap_repeat(x);
  24. break;
  25. default:
  26. VERIFY_NOT_REACHED();
  27. }
  28. switch (m_wrap_t_mode) {
  29. case GL_REPEAT:
  30. y = wrap_repeat(y);
  31. break;
  32. default:
  33. VERIFY_NOT_REACHED();
  34. }
  35. x *= mip.width() - 1;
  36. y *= mip.height() - 1;
  37. return mip.texel(static_cast<unsigned>(x), static_cast<unsigned>(y));
  38. }
  39. }