private void JTextureConstructorHelper(BitmapData dataSource, bool alpha, bool padToPowerOfTwo)
{
int texture;
int bpp;
Bitmap bitmapTarget;
_w = dataSource.Width;
_h = dataSource.Height;
_hasAlpha = _useAlpha = alpha;
isSubTexture = false;
GL.Enable(EnableCap.Texture2D);
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
GL.GenTextures(1, out texture);
GL.BindTexture(TextureTarget.Texture2D, texture);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp);
System.Drawing.Imaging.PixelFormat format1;
PixelInternalFormat format2;
OpenTK.Graphics.OpenGL.PixelFormat format3;
if (alpha)
{
format1 = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
format2 = PixelInternalFormat.Rgba;
format3 = OpenTK.Graphics.OpenGL.PixelFormat.Bgra;
bpp = 4;
}
else
{
format1 = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
format2 = PixelInternalFormat.Three;
format3 = OpenTK.Graphics.OpenGL.PixelFormat.Bgr;
bpp = 3;
}
int targetW, targetH;
if (!padToPowerOfTwo)
{
targetW = (int)_w;
targetH = (int)_h;
}
else
{
targetW = JMath.pot((int)_w);
targetH = JMath.pot((int)_h);
}
bitmapTarget = new Bitmap(targetW, targetH, format1);
bottomLeft = new Vector2(0f, 0f);
topRight = new Vector2((float)_w / targetW, (float)_h / targetH);
BitmapData dataTarget = bitmapTarget.LockBits(new Rectangle(0, 0, bitmapTarget.Width, bitmapTarget.Height),
ImageLockMode.ReadWrite, format1);
//copy source data into the target data, flipping it vertically in the process
unsafe
{
byte* sourcePtr = (byte*)(dataSource.Scan0);
byte* targetPtr = (byte*)(dataTarget.Scan0);
targetPtr += dataTarget.Stride * (dataSource.Height - 1); //target moves to start of last line
for (int i = 0; i < dataSource.Height; i++)
{
for (int j = 0; j < dataSource.Width; j++)
{
// write the logic implementation here
for (int k = 0; k < bpp; k++)
{
(*targetPtr) = (*sourcePtr);
sourcePtr++;
targetPtr++;
}
}
sourcePtr += dataSource.Stride - dataSource.Width * bpp; //move to the end of the line (past unused space)
targetPtr += dataTarget.Stride - dataSource.Width * bpp; //move to the end of the line (past unused space)
targetPtr -= dataTarget.Stride * 2; //move up a line
}
}
GL.TexImage2D(TextureTarget.Texture2D, 0, format2, dataTarget.Width, dataTarget.Height, 0,
format3, PixelType.UnsignedByte, dataTarget.Scan0);
bitmapTarget.UnlockBits(dataTarget);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
_GLTexID = texture;
}