function CreateTexture(aWidth, aHeight, aFormat : Cardinal; aMipmaps : Boolean; aData : Pointer) : Cardinal;
begin
glGenTextures(1, @result);
glBindTexture(GL_TEXTURE_2D, result);
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
if aFormat = GL_RGBA then
begin
if aMipMaps then gluBuild2DMipmaps(GL_TEXTURE_2D, 4, aWidth, aHeight, aFormat, GL_UNSIGNED_BYTE, aData)
else glTexImage2D(GL_TEXTURE_2D, 4, 4, aWidth, aHeight, 0, aFormat, GL_UNSIGNED_BYTE, aData);
end
else if aFormat = GL_RGB then
begin
if aMipmaps then gluBuild2DMipmaps(GL_TEXTURE_2D, 3, aWidth, aHeight, aFormat, GL_UNSIGNED_BYTE, aData)
else glTexImage2D(GL_TEXTURE_2D, 0, 3, aWidth, aHeight, 0, aFormat, GL_UNSIGNED_BYTE, aData);
end
else if aFormat = GL_RGB16 then
begin
if aMipmaps then gluBuild2DMipmaps(GL_TEXTURE_2D, 2, aWidth, aHeight, aFormat, GL_UNSIGNED_BYTE, aData)
else glTexImage2D(GL_TEXTURE_2D, 0, 2, aWidth, aHeight, 0, aFormat, GL_UNSIGNED_BYTE, aData);
end
else if (aFormat = GL_RGB8) or (aFormat = GL_LUMINANCE) or (aFORMAT = GL_LUMINANCE_ALPHA) then
begin
if aMipmaps then gluBuild2DMipmaps(GL_TEXTURE_2D, 1, aWidth, aHeight, aFormat, GL_UNSIGNED_BYTE, aData)
else glTexImage2D(GL_TEXTURE_2D, 0, 1, aWidth, aHeight, 0, aFormat, GL_UNSIGNED_BYTE, aData);
end
else
begin
ShowMessage('invalid or unknown texture format');
result := 0;
end;
end;
// note that we specify format, cause several of the same bitmap pixeltypes can have diff gl formats
function CreateTexture(aBitmap : TBitmap; aFormat : Cardinal; aMipmaps : Boolean) : Cardinal; overload;
var
aData : Pointer;
begin
aData := aBitmap.ScanLine[aBitmap.Height-1];
result := CreateTexture(aBitmap.Width, aBitmap.Height, aFormat, aMipmaps, aBitmap.ScanLine[aBitmap.Height-1])
end;