I'm trying to downsample an image (for mipmap generation purposes) but I'm having some problems.
The problem is that each miplevel looks like it's being sucked towards the bottom right corner of the image.
I've got a quick video on how it looks when switching through the miplevels:
Does anyone have an idea what I'm doing wrong ?
My mipmap creation code looks like this:
void CreateMipMaps()
{
// Create 8 mipmap levels
for (unsigned short i = 0; i < 8; ++i)
{
width[i + 1] = static_cast<unsigned int>(std::ceil(width[i] / 2.0f));
height[i + 1] = static_cast<unsigned int>(std::ceil(height[i] / 2.0f));
this->BuildMipMap(this->pixels[i], this->pixels[i+1],
width[i], height[i], width[i + 1], height[i + 1]);
}
}
void BuildMipMap(std::vector<Rgb> &source, std::vector<Rgb> &dest,
const unsigned int srcWidth, const unsigned int srcHeight,
const unsigned int destWidth, const unsigned int destHeight)
{
dest.resize(destWidth * destHeight);
double x_ratio = srcWidth / (double)destWidth;
double y_ratio = srcHeight / (double)destHeight;
double px, py;
for (unsigned int y = 0; y < destHeight; ++y)
{
for (unsigned int x = 0; x < destWidth; ++x)
{
px = std::floor(x * x_ratio);
py = std::floor(y * y_ratio);
dest[x + y * destWidth] =
source[(unsigned int)(px + py * srcWidth)];
}
}
}
And this is my texture (point) sampler function (sry for the bad format):
static Vector4_SSE SampleTex2DPoint(Texture2D* tex, const Vector4_SSE &uv,
const unsigned int miplevel = 0)
{
const unsigned int texWidth = tex->width[miplevel];
const unsigned int texHeight = tex->height[miplevel];
const unsigned int sampleX = std::min<unsigned int>(static_cast<unsigned int>(uv.X() * texWidth),
texWidth - 1); // floor of x
const unsigned int sampleY = std::min<unsigned int>(static_cast<unsigned int>(uv.Y() * texHeight),
texHeight - 1); // floor of y
return Vector4_SSE(tex->pixels[miplevel][sampleX + sampleY * texWidth].r,
tex->pixels[miplevel][sampleX + sampleY * texWidth].g,
tex->pixels[miplevel][sampleX + sampleY * texWidth].b);
}