1
Graphics / sprite/texture/image bending
« on: October 23, 2011, 03:50:32 am »
Okay, I've implemented a simple bilinear filter, which makes it even more awesome.
Before:
After:
My implementation (assuming the surrounding code by Nexus):
I am pretty happy with the result!
Before:
After:
My implementation (assuming the surrounding code by Nexus):
Code: [Select]
sf::Vector2f pixelPos(source.GetWidth() * texCoords.x, source.GetHeight() * texCoords.y);
sf::Vector2i roundedPixelPos(pixelPos);
sf::Vector2f posFraction(pixelPos.x - roundedPixelPos.x, pixelPos.y - roundedPixelPos.y);
sf::Vector2f invertedPosFraction(1.0f - posFraction.x, 1.0f - posFraction.y);
sf::Color color1 = source.GetPixel(roundedPixelPos.x, roundedPixelPos.y );
sf::Color color2 = source.GetPixel(roundedPixelPos.x + 1 % source.GetWidth(), roundedPixelPos.y );
sf::Color color3 = source.GetPixel(roundedPixelPos.x, roundedPixelPos.y + 1 % source.GetHeight());
sf::Color color4 = source.GetPixel(roundedPixelPos.x + 1 % source.GetWidth(), roundedPixelPos.y + 1 % source.GetHeight());
sf::Color interpolatedColor;
// red
float r1 = invertedPosFraction.x * color1.r + posFraction.x * color2.r;
float r2 = invertedPosFraction.x * color3.r + posFraction.x * color4.r;
interpolatedColor.r = invertedPosFraction.y * r1 + posFraction.y * r2;
// green
float g1 = invertedPosFraction.x * color1.g + posFraction.x * color2.g;
float g2 = invertedPosFraction.x * color3.g + posFraction.x * color4.g;
interpolatedColor.g = invertedPosFraction.y * g1 + posFraction.y * g2;
// blue
float b1 = invertedPosFraction.x * color1.b + posFraction.x * color2.b;
float b2 = invertedPosFraction.x * color3.b + posFraction.x * color4.b;
interpolatedColor.b = invertedPosFraction.y * b1 + posFraction.y * b2;
// alpha
float a1 = invertedPosFraction.x * color1.a + posFraction.x * color2.a;
float a2 = invertedPosFraction.x * color3.a + posFraction.x * color4.a;
interpolatedColor.a = invertedPosFraction.y * a1 + posFraction.y * a2;
dest.SetPixel(x, y, interpolatedColor);
I am pretty happy with the result!