I'm writing a 2D software renderer and when rotating in certain angles I will get a sort of "checkerboarding" effect with my sprite pixels:
To try and get proper rotation, I tried rounding/flooring/ceiling the rotated pixels, but that did nothing to fix it.
It seems the pixels get set to the wrong position and overlap each other. Not exactly sure why this happens? I presume this may be a precision issue or something, maybe, really not sure..
Relevant portion of my code:
uint width = image.width;
uint height = image.height;
uint originX = width / 2;
uint originY = height;
float radians = angleDegrees * PI / 180.0f;
float sine = sin(radians);
float cosine = cos(radians);
for (uint r = 0; r < height; r++)
{
for (uint p = 0; p < width; p++)
{
int translatedR = r - originY;
int translatedP = p - originX;
int rotatedR = round(translatedP * sine + translatedR * cosine) + originY;
int rotatedP = round(translatedP * cosine - translatedR * sine) + originX;
size_t index = (x + rotatedP + (frameBufferWidth * y)) + (frameBufferWidth * rotatedR);
size_t index2 = p + (image.width * r);
if (!image.pixels[index2].a)
continue;
frameBuffer[index].r = image.pixels[index2].r;
frameBuffer[index].g = image.pixels[index2].g;
frameBuffer[index].b = image.pixels[index2].b;
}
}
