int h = Texture.nearestPower(height);
if (h > maxSize)
h = maxSize;
// Get current glPixelStore state
PixelStoreState pss = new PixelStoreState();
// set pixel packing
GL11.glPixelStorei(GL11.GL_PACK_ROW_LENGTH, 0);
GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
GL11.glPixelStorei(GL11.GL_PACK_SKIP_ROWS, 0);
GL11.glPixelStorei(GL11.GL_PACK_SKIP_PIXELS, 0);
ByteBuffer image;
boolean done = false;
if (w != width || h != height)
{
// Must rescale image to get "top" mipmap texture image
image = BufferUtils.createByteBuffer((w + 4) * h * bpp);
if (!Texture.gluScaleImage(format, width, height, type, data, w, h,
type, image))
done = true;
/* set pixel unpacking */
GL11.glPixelStorei(GL11.GL_UNPACK_ROW_LENGTH, 0);
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
GL11.glPixelStorei(GL11.GL_UNPACK_SKIP_ROWS, 0);
GL11.glPixelStorei(GL11.GL_UNPACK_SKIP_PIXELS, 0);
} else
{
image = data;
}
ByteBuffer bufferA = null;
ByteBuffer bufferB = null;
int level = 0;
while (!done)
{
if (image != data)
{
/* set pixel unpacking */
GL11.glPixelStorei(GL11.GL_UNPACK_ROW_LENGTH, 0);
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
GL11.glPixelStorei(GL11.GL_UNPACK_SKIP_ROWS, 0);
GL11.glPixelStorei(GL11.GL_UNPACK_SKIP_PIXELS, 0);
}
GL11.glTexImage2D(target, level, components, w, h, 0, format, type,
image);
if (w == 1 && h == 1)
break;
final int newW = (w < 2) ? 1 : w >> 1;
final int newH = (h < 2) ? 1 : h >> 1;
final ByteBuffer newImage;
if (bufferA == null)
newImage = (bufferA = BufferUtils.createByteBuffer((newW + 4)
* newH * bpp));
else if (bufferB == null)
newImage = (bufferB = BufferUtils.createByteBuffer((newW + 4)
* newH * bpp));
else
newImage = bufferB;
if (!Texture.gluScaleImage(format, w, h, type, image, newW, newH,
type, newImage))
done = true;
image = newImage;
if (bufferB != null)
bufferB = bufferA;
w = newW;
h = newH;
level++;
}
// Restore original glPixelStore state
pss.save();
}