Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions sr/MathUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ namespace sr
const Vector<T, 3> side2{c.v[1] - a.v[1], b.v[1] - a.v[1], a.v[1] - p.v[1]};
const auto u = side1.cross(side2);

if (std::abs(u.v[2]) < std::numeric_limits<T>::min()) // degenerate triangle (all three points in a line)
return Vector<T, 3>{T(-1), T(1), T(1)};

return Vector<T, 3>{T(1) - (u.v[0] + u.v[1]) / u.v[2], u.v[1] / u.v[2], u.v[0] / u.v[2]};
return (std::abs(u.v[2]) < std::numeric_limits<T>::min()) // degenerate triangle (all three points in a line)
? Vector<T, 3>{T(-1), T(1), T(1)}
: Vector<T, 3>{T(1) - (u.v[0] + u.v[1]) / u.v[2], u.v[1] / u.v[2], u.v[0] / u.v[2]};
}
}

Expand Down
7 changes: 2 additions & 5 deletions sr/Rect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,8 @@ namespace sr
const T width, const T height) const noexcept
{
T t;
if ((t = x - position.v[0]) > size.v[0] || -t > width)
return false;
if ((t = y - position.v[1]) > size.v[1] || -t > height)
return false;
return true;
return !((t = x - position.v[0]) > size.v[0] || -t > width) &&
!((t = y - position.v[1]) > size.v[1] || -t > height);
}

constexpr bool intersects(const Rect& r) const noexcept
Expand Down
116 changes: 57 additions & 59 deletions sr/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,26 @@ namespace sr
{
const auto pixelSize = getPixelSize(pixelFormat);

if (pixelSize > 0 && width > 0 && height > 0)
{
levels.push_back(std::vector<std::uint8_t>(width * height * pixelSize));
if (pixelSize <= 0 || width <= 0 || height <= 0)
return;

if (mipMaps)
{
std::size_t newWidth = width;
std::size_t newHeight = height;
levels.push_back(std::vector<std::uint8_t>(width * height * pixelSize));

while (newWidth > 1 || newHeight > 1)
{
newWidth >>= 1;
newHeight >>= 1;
if (!mipMaps)
return;

std::size_t newWidth = width;
std::size_t newHeight = height;

if (newWidth < 1) newWidth = 1;
if (newHeight < 1) newHeight = 1;
while (newWidth > 1 || newHeight > 1)
{
newWidth >>= 1;
newHeight >>= 1;

levels.push_back(std::vector<std::uint8_t>(newWidth * newHeight * pixelSize));
}
}
if (newWidth < 1) newWidth = 1;
if (newHeight < 1) newHeight = 1;

levels.push_back(std::vector<std::uint8_t>(newWidth * newHeight * pixelSize));
}
}

Expand All @@ -86,20 +86,20 @@ namespace sr
levels.push_back(std::vector<std::uint8_t>(width * height * pixelSize));

if (mipMaps)
{
newWidth = width;
newHeight = height;
return;

while (newWidth > 1 || newHeight > 1)
{
newWidth >>= 1;
newHeight >>= 1;
newWidth = width;
newHeight = height;

if (newWidth < 1) newWidth = 1;
if (newHeight < 1) newHeight = 1;
while (newWidth > 1 || newHeight > 1)
{
newWidth >>= 1;
newHeight >>= 1;

levels.push_back(std::vector<std::uint8_t>(newWidth * newHeight * pixelSize));
}
if (newWidth < 1) newWidth = 1;
if (newHeight < 1) newHeight = 1;

levels.push_back(std::vector<std::uint8_t>(newWidth * newHeight * pixelSize));
}
}

Expand Down Expand Up @@ -188,38 +188,36 @@ namespace sr
const auto textureY = static_cast<std::size_t>(std::round(v));
return getPixel(textureX, textureY, 0);
}
else
{
auto textureX0 = static_cast<std::size_t>(u - 0.5F);
auto textureX1 = textureX0 + 1;
auto textureY0 = static_cast<std::size_t>(v - 0.5F);
auto textureY1 = textureY0 + 1;

textureX0 = clamp(textureX0, static_cast<std::size_t>(0U), width - 1);
textureX1 = clamp(textureX1, static_cast<std::size_t>(0U), width - 1);
textureY0 = clamp(textureY0, static_cast<std::size_t>(0U), height - 1);
textureY1 = clamp(textureY1, static_cast<std::size_t>(0U), height - 1);

// TODO: calculate mip level
const Color color[4] = {
getPixel(textureX0, textureY0, 0),
getPixel(textureX1, textureY0, 0),
getPixel(textureX0, textureY1, 0),
getPixel(textureX1, textureY1, 0)
};

const auto x0 = u - (textureX0 + 0.5F);
const auto y0 = v - (textureY0 + 0.5F);
const auto x1 = (textureX0 + 1.5F) - u;
const auto y1 = (textureY0 + 1.5F) - v;

return Color{
color[0].r * x1 * y1 + color[1].r * x0 * y1 + color[2].r * x1 * y0 + color[3].r * x0 * y0,
color[0].g * x1 * y1 + color[1].g * x0 * y1 + color[2].g * x1 * y0 + color[3].g * x0 * y0,
color[0].b * x1 * y1 + color[1].b * x0 * y1 + color[2].b * x1 * y0 + color[3].b * x0 * y0,
color[0].a * x1 * y1 + color[1].a * x0 * y1 + color[2].a * x1 * y0 + color[3].a * x0 * y0
};
}

auto textureX0 = static_cast<std::size_t>(u - 0.5F);
auto textureX1 = textureX0 + 1;
auto textureY0 = static_cast<std::size_t>(v - 0.5F);
auto textureY1 = textureY0 + 1;

textureX0 = clamp(textureX0, static_cast<std::size_t>(0U), width - 1);
textureX1 = clamp(textureX1, static_cast<std::size_t>(0U), width - 1);
textureY0 = clamp(textureY0, static_cast<std::size_t>(0U), height - 1);
textureY1 = clamp(textureY1, static_cast<std::size_t>(0U), height - 1);

// TODO: calculate mip level
const Color color[4] = {
getPixel(textureX0, textureY0, 0),
getPixel(textureX1, textureY0, 0),
getPixel(textureX0, textureY1, 0),
getPixel(textureX1, textureY1, 0)
};

const auto x0 = u - (textureX0 + 0.5F);
const auto y0 = v - (textureY0 + 0.5F);
const auto x1 = (textureX0 + 1.5F) - u;
const auto y1 = (textureY0 + 1.5F) - v;

return Color{
color[0].r * x1 * y1 + color[1].r * x0 * y1 + color[2].r * x1 * y0 + color[3].r * x0 * y0,
color[0].g * x1 * y1 + color[1].g * x0 * y1 + color[2].g * x1 * y0 + color[3].g * x0 * y0,
color[0].b * x1 * y1 + color[1].b * x0 * y1 + color[2].b * x1 * y0 + color[3].b * x0 * y0,
color[0].a * x1 * y1 + color[1].a * x0 * y1 + color[2].a * x1 * y0 + color[3].a * x0 * y0
};
}

return Color();
Expand Down