* fixed some small bugs in format settings
[glBitmap.git] / glBitmap.pas
index 1e804e0..4934c14 100644 (file)
@@ -179,8 +179,6 @@ unit glBitmap;
   {$OPTIMIZATION ON}
 {$ENDIF}
 
-{$UNDEF GLB_LAZARUS}
-
 interface
 
 uses
@@ -253,9 +251,9 @@ type
     tfARGB4us1,                 //< 1 x unsigned short (4bit alpha, 4bit red, 4bit green, 4bit blue)
     tfRGB5A1us1,                //< 1 x unsigned short (5bit red, 5bit green, 5bit blue, 1bit alpha)
     tfA1RGB5us1,                //< 1 x unsigned short (1bit alpha, 5bit red, 5bit green, 5bit blue)
+    tfRGBA8ub4,                 //< 1 x unsigned byte (red), 1 x unsigned byte (green), 1 x unsigned byte (blue), 1 x unsigned byte (alpha)
     tfRGBA8ui1,                 //< 1 x unsigned int (8bit red, 8bit green, 8bit blue, 8 bit alpha)
     tfARGB8ui1,                 //< 1 x unsigned int (8 bit alpha, 8bit red, 8bit green, 8bit blue)
-    tfRGBA8ub4,                 //< 1 x unsigned byte (red), 1 x unsigned byte (green), 1 x unsigned byte (blue), 1 x unsigned byte (alpha)
     tfRGB10A2ui1,               //< 1 x unsigned int (10bit red, 10bit green, 10bit blue, 2bit alpha)
     tfA2RGB10ui1,               //< 1 x unsigned int (2bit alpha, 10bit red, 10bit green, 10bit blue)
     tfRGBA16us4,                //< 1 x unsigned short (red), 1 x unsigned short (green), 1 x unsigned short (blue), 1 x unsigned short (alpha)
@@ -276,9 +274,9 @@ type
     tfABGR4us1,                 //< 1 x unsigned short (4bit alpha, 4bit blue, 4bit green, 4bit red)
     tfBGR5A1us1,                //< 1 x unsigned short (5bit blue, 5bit green, 5bit red, 1bit alpha)
     tfA1BGR5us1,                //< 1 x unsigned short (1bit alpha, 5bit blue, 5bit green, 5bit red)
+    tfBGRA8ub4,                 //< 1 x unsigned byte (blue), 1 x unsigned byte (green), 1 x unsigned byte (red), 1 x unsigned byte (alpha)
     tfBGRA8ui1,                 //< 1 x unsigned int (8bit blue, 8bit green, 8bit red, 8bit alpha)
     tfABGR8ui1,                 //< 1 x unsigned int (8bit alpha, 8bit blue, 8bit green, 8bit red)
-    tfBGRA8ub4,                 //< 1 x unsigned byte (blue), 1 x unsigned byte (green), 1 x unsigned byte (red), 1 x unsigned byte (alpha)
     tfBGR10A2ui1,               //< 1 x unsigned int (10bit blue, 10bit green, 10bit red, 2bit alpha)
     tfA2BGR10ui1,               //< 1 x unsigned int (2bit alpha, 10bit blue, 10bit green, 10bit red)
     tfBGRA16us4,                //< 1 x unsigned short (blue), 1 x unsigned short (green), 1 x unsigned short (red), 1 x unsigned short (alpha)
@@ -390,6 +388,10 @@ type
 
     { @return @true if the format is a grayscale format, @false otherwise }
     function GetIsGrayscale: Boolean;
+
+    { @return @true if the format is supported by OpenGL, @false otherwise }
+    function GetHasOpenGLSupport: Boolean;
+
   protected
     fFormat:        TglBitmapFormat;  //< format this descriptor belongs to
     fWithAlpha:     TglBitmapFormat;  //< suitable format with alpha channel
@@ -442,6 +444,8 @@ type
     property HasColor:     Boolean read GetHasColor;      //< @true if the format has any color color channel, @false otherwise
     property IsGrayscale:  Boolean read GetIsGrayscale;   //< @true if the format is a grayscale format, @false otherwise
 
+    property HasOpenGLSupport: Boolean read GetHasOpenGLSupport; //< @true if the format is supported by OpenGL, @false otherwise
+
     function GetSize(const aSize: TglBitmapSize): Integer;     overload; virtual;
     function GetSize(const aWidth, aHeight: Integer): Integer; overload; virtual;
 
@@ -451,7 +455,12 @@ type
     { get the format descriptor by a given OpenGL internal format
         @param aInternalFormat  OpenGL internal format to get format descriptor for
         @returns                suitable format descriptor or tfEmpty-Descriptor }
-    class function GetByFormat(const aInternalFormat: GLenum): TglBitmapFormatDescriptor;
+    class function GetByFormat(const aInternalFormat: GLenum): TglBitmapFormatDescriptor; overload;
+
+    { get the format descriptor by the given format
+        @param aFormat  format to get descriptor for
+        @return         suitable format descriptor or tfEmpty-Descriptor }
+    class function GetByFormat(const aFormat: TglBitmapFormat): TglBitmapFormatDescriptor; overload;
   end;
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -1059,11 +1068,11 @@ type
   public
     { bind texture
         @param aEnableTextureUnit   enable texture unit for this texture (e.g. glEnable(GL_TEXTURE_2D)) }
-    procedure Bind(const aEnableTextureUnit: Boolean = true); virtual;
+    procedure Bind({$IFNDEF OPENGL_ES}const aEnableTextureUnit: Boolean = true{$ENDIF}); virtual;
 
     { bind texture
         @param aDisableTextureUnit  disable texture unit for this texture (e.g. glEnable(GL_TEXTURE_2D)) }
-    procedure Unbind(const aDisableTextureUnit: Boolean = true); virtual;
+    procedure Unbind({$IFNDEF OPENGL_ES}const aDisableTextureUnit: Boolean = true{$ENDIF}); virtual;
 
     { upload texture data from given data object to video card
         @param aData        texture data object that contains the actual data
@@ -1176,12 +1185,12 @@ type
     { bind texture
         @param aEnableTexCoordsGen  enable cube map generator
         @param aEnableTextureUnit   enable texture unit }
-    procedure Bind({$IFNDEF OPENGL_ES}const aEnableTexCoordsGen: Boolean = true;{$ENDIF} const aEnableTextureUnit: Boolean = true); reintroduce; virtual;
+    procedure Bind({$IFNDEF OPENGL_ES}const aEnableTexCoordsGen: Boolean = true; const aEnableTextureUnit: Boolean = true{$ENDIF}); reintroduce; virtual;
 
     { unbind texture
         @param aDisableTexCoordsGen   disable cube map generator
         @param aDisableTextureUnit    disable texture unit }
-    procedure Unbind({$IFNDEF OPENGL_ES}const aDisableTexCoordsGen: Boolean = true;{$ENDIF} const aDisableTextureUnit: Boolean = true); reintroduce; virtual;
+    procedure Unbind({$IFNDEF OPENGL_ES}const aDisableTexCoordsGen: Boolean = true; const aDisableTextureUnit: Boolean = true{$ENDIF}); reintroduce; virtual;
   end;
 {$IFEND}
 
@@ -1726,9 +1735,9 @@ const
     TfdARGB4us1,
     TfdRGB5A1us1,
     TfdA1RGB5us1,
+    TfdRGBA8ub4,
     TfdRGBA8ui1,
     TfdARGB8ui1,
-    TfdRGBA8ub4,
     TfdRGB10A2ui1,
     TfdA2RGB10ui1,
     TfdRGBA16us4,
@@ -1749,9 +1758,9 @@ const
     TfdABGR4us1,
     TfdBGR5A1us1,
     TfdA1BGR5us1,
+    TfdBGRA8ub4,
     TfdBGRA8ui1,
     TfdABGR8ui1,
-    TfdBGRA8ub4,
     TfdBGR10A2ui1,
     TfdA2BGR10ui1,
     TfdBGRA16us4,
@@ -3054,7 +3063,7 @@ begin
   fPrecision        := glBitmapRec4ub( 4,  4,  4,  4);
   fShift            := glBitmapRec4ub(12,  8,  4,  0);
   fglFormat         := GL_RGBA;
-  fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGBA8{$ELSE}GL_RGBA{$IFEND};
+  fglInternalFormat := {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}GL_RGBA4{$ELSE}GL_RGBA{$IFEND};
   fglDataFormat     := GL_UNSIGNED_SHORT_4_4_4_4;
 end;
 
@@ -3837,6 +3846,11 @@ begin
   result := (Mask.r = Mask.g) and (Mask.g = Mask.b) and (Mask.r > 0);
 end;
 
+function TglBitmapFormatDescriptor.GetHasOpenGLSupport: Boolean;
+begin
+  result := (OpenGLFormat = Format);
+end;
+
 procedure TglBitmapFormatDescriptor.SetValues;
 begin
   fFormat       := tfEmpty;
@@ -3912,6 +3926,14 @@ begin
 end;
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+class function TglBitmapFormatDescriptor.GetByFormat(const aFormat: TglBitmapFormat): TglBitmapFormatDescriptor;
+begin
+  result := TFormatDescriptor.Get(aFormat);
+  if not Assigned(result) then
+    result := TFormatDescriptor.Get(tfEmpty);
+end;
+
+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //TFormatDescriptor///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 class procedure TFormatDescriptor.Init;
@@ -8101,7 +8123,7 @@ begin
 {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_EXT)}
     if GL_EXT_texture_filter_anisotropic then begin
       if fAnisotropic > 0 then begin
-        Bind(false);
+        Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
         glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, @MaxAnisotropic);
         if aValue > MaxAnisotropic then
           fAnisotropic := MaxAnisotropic;
@@ -8122,7 +8144,7 @@ begin
   if (ID <> 0) then
     glDeleteTextures(1, @fID);
   glGenTextures(1, @fID);
-  Bind(false);
+  Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
 end;
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -8144,14 +8166,14 @@ begin
   aBuildWithGlu := false;
   if (MipMap = mmMipmap) then begin
     if (GL_VERSION_1_4 or GL_SGIS_generate_mipmap) then
-      glTexParameteri(Target, GL_GENERATE_MIPMAP, GL_TRUE)
+      glTexParameteri(Target, GL_GENERATE_MIPMAP, GLint(GL_TRUE))
     else
       aBuildWithGlu := true;
   end else if (MipMap = mmMipmapGlu) then
     aBuildWithGlu := true;
 {$ELSE}
   if (MipMap = mmMipmap) then
-    glTexParameteri(Target, GL_GENERATE_MIPMAP, GL_TRUE);
+    glGenerateMipmap(Target);
 {$ENDIF}
 end;
 
@@ -8234,7 +8256,7 @@ begin
 
   //apply filter
   if (ID > 0) then begin
-    Bind(false);
+    Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
     glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, fFilterMag);
 
     if (MipMap = mmNone) {$IFNDEF OPENGL_ES}or (Target = GL_TEXTURE_RECTANGLE){$ENDIF} then begin
@@ -8306,7 +8328,7 @@ begin
   CheckAndSetWrap(R, fWrapR);
 
   if (ID > 0) then begin
-    Bind(false);
+    Bind({$IFNDEF OPENGL_ES}false{$ENDIF});
     glTexParameteri(Target, GL_TEXTURE_WRAP_S, fWrapS);
     glTexParameteri(Target, GL_TEXTURE_WRAP_T, fWrapT);
 {$IF NOT DEFINED(OPENGL_ES) OR DEFINED(OPENGL_ES_3_0)}
@@ -8357,19 +8379,23 @@ end;
 {$IFEND}
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-procedure TglBitmap.Bind(const aEnableTextureUnit: Boolean);
+procedure TglBitmap.Bind({$IFNDEF OPENGL_ES}const aEnableTextureUnit: Boolean{$ENDIF});
 begin
+{$IFNDEF OPENGL_ES}
   if aEnableTextureUnit then
     glEnable(Target);
+{$ENDIF}
   if (ID > 0) then
     glBindTexture(Target, ID);
 end;
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-procedure TglBitmap.Unbind(const aDisableTextureUnit: Boolean);
+procedure TglBitmap.Unbind({$IFNDEF OPENGL_ES}const aDisableTextureUnit: Boolean{$ENDIF});
 begin
+{$IFNDEF OPENGL_ES}
   if aDisableTextureUnit then
     glDisable(Target);
+{$ENDIF}
   glBindTexture(Target, 0);
 end;
 
@@ -8414,6 +8440,7 @@ begin
   FormatDesc := TFormatDescriptor.Get(IntFormat);
   GetMem(Temp, FormatDesc.GetSize(TempWidth, TempHeight));
   try
+    glPixelStorei(GL_PACK_ALIGNMENT, 1);
     if FormatDesc.IsCompressed then begin
       if not Assigned(glGetCompressedTexImage) then
         raise EglBitmap.Create('compressed formats not supported by video adapter');
@@ -8510,7 +8537,7 @@ end;
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //TglBitmap2D/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-procedure TglBitmap2D.UploadDataIntern(const aDataObj: TglBitmapData; const aTarget: GLenum; const aBuildWithGlu: Boolean);
+procedure TglBitmap2D.UploadDataIntern(const aDataObj: TglBitmapData; const aTarget: GLenum{$IFNDEF OPENGL_ES}; const aBuildWithGlu: Boolean{$ENDIF});
 var
   fd: TglBitmapFormatDescriptor;
 begin
@@ -8675,9 +8702,9 @@ begin
 end;
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-procedure TglBitmapCubeMap.Bind({$IFNDEF OPENGL_ES}const aEnableTexCoordsGen: Boolean;{$ENDIF} const aEnableTextureUnit: Boolean);
+procedure TglBitmapCubeMap.Bind({$IFNDEF OPENGL_ES}const aEnableTexCoordsGen: Boolean; const aEnableTextureUnit: Boolean{$ENDIF});
 begin
-  inherited Bind (aEnableTextureUnit);
+  inherited Bind({$IFNDEF OPENGL_ES}aEnableTextureUnit{$ENDIF});
 {$IFNDEF OPENGL_ES}
   if aEnableTexCoordsGen then begin
     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, fGenMode);
@@ -8691,9 +8718,9 @@ begin
 end;
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-procedure TglBitmapCubeMap.Unbind({$IFNDEF OPENGL_ES}const aDisableTexCoordsGen: Boolean;{$ENDIF} const aDisableTextureUnit: Boolean);
+procedure TglBitmapCubeMap.Unbind({$IFNDEF OPENGL_ES}const aDisableTexCoordsGen: Boolean; const aDisableTextureUnit: Boolean{$ENDIF});
 begin
-  inherited Unbind(aDisableTextureUnit);
+  inherited Unbind({$IFNDEF OPENGL_ES}aDisableTextureUnit{$ENDIF});
 {$IFNDEF OPENGL_ES}
   if aDisableTexCoordsGen then begin
     glDisable(GL_TEXTURE_GEN_S);