From 7964d52cb7747906b6d450afbed3ff5b807ad780 Mon Sep 17 00:00:00 2001 From: Bergmann89 Date: Sat, 16 Nov 2013 18:16:15 +0100 Subject: [PATCH] * native OpenGL Support (dynamic or static linked) --- glBitmap.pas | 845 ++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 515 insertions(+), 330 deletions(-) diff --git a/glBitmap.pas b/glBitmap.pas index e1e591d..1a547ac 100644 --- a/glBitmap.pas +++ b/glBitmap.pas @@ -218,58 +218,51 @@ unit glBitmap; // Please uncomment the defines below to configure the glBitmap to your preferences. // If you have configured the unit you can uncomment the warning above. -// ###### Start of preferences ################################################ - -{$DEFINE GLB_NO_NATIVE_GL} -// To enable the dglOpenGL.pas Header -// With native GL then bindings are staticlly declared to support other headers -// or use the glBitmap inside of DLLs (minimize codesize). +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Preferences /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// activate to enable build-in OpenGL support with statically linked methods +// use dglOpenGL.pas if not enabled +{$DEFINE GLB_NATIVE_OGL_STATIC} +// activate to enable build-in OpenGL support with dynamically linked methods +// use dglOpenGL.pas if not enabled +{$DEFINE GLB_NATIVE_OGL_DYNAMIC} +// activate to enable the support for SDL_surfaces {.$DEFINE GLB_SDL} -// To enable the support for SDL_surfaces +// activate to enable the support for TBitmap from Delphi (not lazarus) {.$DEFINE GLB_DELPHI} -// To enable the support for TBitmap from Delphi (not lazarus) -// *** image libs *** - -{.$DEFINE GLB_SDL_IMAGE} -// To enable the support of SDL_image to load files. (READ ONLY) +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// activate to enable the support of SDL_image to load files. (READ ONLY) // If you enable SDL_image all other libraries will be ignored! +{.$DEFINE GLB_SDL_IMAGE} - -{.$DEFINE GLB_PNGIMAGE} -// to enable png support with the unit pngimage. You can download it from http://pngdelphi.sourceforge.net/ +// activate to enable png support with the unit pngimage. You can download it from http://pngdelphi.sourceforge.net/ // if you enable pngimage the libPNG will be ignored +{.$DEFINE GLB_PNGIMAGE} -{.$DEFINE GLB_LIB_PNG} -// to use the libPNG http://www.libpng.org/ +// activate to use the libPNG http://www.libpng.org/ // You will need an aditional header. // http://www.opengl24.de/index.php?cat=header&file=libpng +{.$DEFINE GLB_LIB_PNG} -{.$DEFINE GLB_DELPHI_JPEG} // if you enable delphi jpegs the libJPEG will be ignored +{.$DEFINE GLB_DELPHI_JPEG} -{.$DEFINE GLB_LIB_JPEG} -// to use the libJPEG http://www.ijg.org/ +// activateto use the libJPEG http://www.ijg.org/ // You will need an aditional header. // http://www.opengl24.de/index.php?cat=header&file=libjpeg +{.$DEFINE GLB_LIB_JPEG} -// ###### End of preferences ################################################## - - -// ###### PRIVATE. Do not change anything. #################################### -// *** old defines for compatibility *** -{$IFDEF NO_NATIVE_GL} - {$DEFINE GLB_NO_NATIVE_GL} -{$ENDIF} -{$IFDEF pngimage} - {$definde GLB_PNGIMAGE} -{$ENDIF} -// *** Delphi Versions *** +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// PRIVATE: DO not change anything! ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Delphi Versions {$IFDEF fpc} {$MODE Delphi} @@ -283,7 +276,20 @@ unit glBitmap; {$ENDIF} {$ENDIF} -// *** checking define combinations *** +// Operation System +{$IF DEFINED(WIN32) or DEFINED(WIN64)} + {$DEFINE GLB_WIN} +{$ELSEIF DEFINED(LINUX)} + {$DEFINE GLB_LINUX} +{$IFEND} + +// native OpenGL Support +{$IF DEFINED(GLB_NATIVE_OGL_STATIC) OR DEFINED(GLB_NATIVE_OGL_DYNAMIC)} + {$DEFINE GLB_NATIVE_OGL} +{$IFEND} + +// checking define combinations +//SDL Image {$IFDEF GLB_SDL_IMAGE} {$IFNDEF GLB_SDL} {$MESSAGE warn 'SDL_image won''t work without SDL. SDL will be activated.'} @@ -310,6 +316,7 @@ unit glBitmap; {$DEFINE GLB_SUPPORT_JPEG_READ} {$ENDIF} +// PNG Image {$IFDEF GLB_PNGIMAGE} {$IFDEF GLB_LIB_PNG} {$MESSAGE warn 'The library libPNG will be ignored if you are using pngimage.'} @@ -320,11 +327,13 @@ unit glBitmap; {$DEFINE GLB_SUPPORT_PNG_WRITE} {$ENDIF} +// libPNG {$IFDEF GLB_LIB_PNG} {$DEFINE GLB_SUPPORT_PNG_READ} {$DEFINE GLB_SUPPORT_PNG_WRITE} {$ENDIF} +// JPEG Image {$IFDEF GLB_DELPHI_JPEG} {$IFDEF GLB_LIB_JPEG} {$MESSAGE warn 'The library libJPEG will be ignored if you are using the unit JPEG.'} @@ -335,12 +344,18 @@ unit glBitmap; {$DEFINE GLB_SUPPORT_JPEG_WRITE} {$ENDIF} +// libJPEG {$IFDEF GLB_LIB_JPEG} {$DEFINE GLB_SUPPORT_JPEG_READ} {$DEFINE GLB_SUPPORT_JPEG_WRITE} {$ENDIF} -// *** general options *** +// native OpenGL +{$IF DEFINED(GLB_NATIVE_OGL_STATIC) AND DEFINED(GLB_NATIVE_OGL_DYNAMIC)} + {$MESSAGE warn 'GLB_NATIVE_OGL_STATIC will be ignored because you enabled GLB_NATIVE_OGL_DYNAMIC'} +{$ENDIF} + +// general options {$EXTENDEDSYNTAX ON} {$LONGSTRINGS ON} {$ALIGN ON} @@ -351,18 +366,21 @@ unit glBitmap; interface uses - {$IFDEF GLB_NO_NATIVE_GL} dglOpenGL, {$ENDIF} + {$IFNDEF GLB_NATIVE_OGL} dglOpenGL, {$ENDIF} + {$IF DEFINED(GLB_WIN) AND + DEFINED(GLB_NATIVE_OGL)} windows, {$IFEND} + + {$IFDEF GLB_SDL} SDL, {$ENDIF} + {$IFDEF GLB_DELPHI} Dialogs, Graphics, {$ENDIF} - {$IFDEF GLB_SDL} SDL, {$ENDIF} - {$IFDEF GLB_DELPHI} Dialogs, Windows, Graphics, {$ENDIF} + {$IFDEF GLB_SDL_IMAGE} SDL_image, {$ENDIF} - {$IFDEF GLB_SDL_IMAGE} SDL_image, {$ENDIF} + {$IFDEF GLB_PNGIMAGE} pngimage, {$ENDIF} + {$IFDEF GLB_LIB_PNG} libPNG, {$ENDIF} - {$IFDEF GLB_PNGIMAGE} pngimage, {$ENDIF} - {$IFDEF GLB_LIB_PNG} libPNG, {$ENDIF} + {$IFDEF GLB_DELPHI_JPEG} JPEG, {$ENDIF} + {$IFDEF GLB_LIB_JPEG} libJPEG, {$ENDIF} - {$IFDEF GLB_DELPHI_JPEG} JPEG, {$ENDIF} - {$IFDEF GLB_LIB_JPEG} libJPEG, {$ENDIF} Classes, SysUtils; {$IFNDEF GLB_DELPHI} @@ -379,209 +397,285 @@ type end; {$ENDIF} -(* TODO dglOpenGL -{$IFNDEF GLB_NO_NATIVE_GL} -// Native OpenGL Implementation -type - PByteBool = ^ByteBool; - -{$IFDEF GLB_DELPHI} -var - gLastContext: HGLRC; -{$ENDIF} - +{$IFDEF GLB_NATIVE_OGL} const - // Generell - GL_VERSION = $1F02; + GL_TRUE = 1; + GL_FALSE = 0; + + GL_VERSION = $1F02; GL_EXTENSIONS = $1F03; - GL_TRUE = 1; - GL_FALSE = 0; - - GL_TEXTURE_1D = $0DE0; - GL_TEXTURE_2D = $0DE1; - - GL_MAX_TEXTURE_SIZE = $0D33; - GL_PACK_ALIGNMENT = $0D05; - GL_UNPACK_ALIGNMENT = $0CF5; - - // Textureformats - GL_RGB = $1907; - GL_RGB4 = $804F; - GL_RGB8 = $8051; - GL_RGBA = $1908; - GL_RGBA4 = $8056; - GL_RGBA8 = $8058; - GL_BGR = $80E0; - GL_BGRA = $80E1; - GL_ALPHA4 = $803B; - GL_ALPHA8 = $803C; - GL_LUMINANCE4 = $803F; - GL_LUMINANCE8 = $8040; - GL_LUMINANCE4_ALPHA4 = $8043; - GL_LUMINANCE8_ALPHA8 = $8045; - GL_DEPTH_COMPONENT = $1902; - - GL_UNSIGNED_BYTE = $1401; - GL_ALPHA = $1906; - GL_LUMINANCE = $1909; - GL_LUMINANCE_ALPHA = $190A; - - GL_TEXTURE_WIDTH = $1000; - GL_TEXTURE_HEIGHT = $1001; - GL_TEXTURE_INTERNAL_FORMAT = $1003; - GL_TEXTURE_RED_SIZE = $805C; - GL_TEXTURE_GREEN_SIZE = $805D; - GL_TEXTURE_BLUE_SIZE = $805E; - GL_TEXTURE_ALPHA_SIZE = $805F; - GL_TEXTURE_LUMINANCE_SIZE = $8060; - - // Dataformats - GL_UNSIGNED_SHORT_5_6_5 = $8363; - GL_UNSIGNED_SHORT_5_6_5_REV = $8364; + GL_TEXTURE_1D = $0DE0; + GL_TEXTURE_2D = $0DE1; + GL_TEXTURE_RECTANGLE = $84F5; + + GL_TEXTURE_WIDTH = $1000; + GL_TEXTURE_HEIGHT = $1001; + GL_TEXTURE_INTERNAL_FORMAT = $1003; + + GL_ALPHA = $1906; + GL_ALPHA4 = $803B; + GL_ALPHA8 = $803C; + GL_ALPHA12 = $803D; + GL_ALPHA16 = $803E; + + GL_LUMINANCE = $1909; + GL_LUMINANCE4 = $803F; + GL_LUMINANCE8 = $8040; + GL_LUMINANCE12 = $8041; + GL_LUMINANCE16 = $8042; + + GL_LUMINANCE_ALPHA = $190A; + GL_LUMINANCE4_ALPHA4 = $8043; + GL_LUMINANCE6_ALPHA2 = $8044; + GL_LUMINANCE8_ALPHA8 = $8045; + GL_LUMINANCE12_ALPHA4 = $8046; + GL_LUMINANCE12_ALPHA12 = $8047; + GL_LUMINANCE16_ALPHA16 = $8048; + + GL_RGB = $1907; + GL_BGR = $80E0; + GL_R3_G3_B2 = $2A10; + GL_RGB4 = $804F; + GL_RGB5 = $8050; + GL_RGB565 = $8D62; + GL_RGB8 = $8051; + GL_RGB10 = $8052; + GL_RGB12 = $8053; + GL_RGB16 = $8054; + + GL_RGBA = $1908; + GL_BGRA = $80E1; + GL_RGBA2 = $8055; + GL_RGBA4 = $8056; + GL_RGB5_A1 = $8057; + GL_RGBA8 = $8058; + GL_RGB10_A2 = $8059; + GL_RGBA12 = $805A; + GL_RGBA16 = $805B; + + GL_DEPTH_COMPONENT = $1902; + GL_DEPTH_COMPONENT16 = $81A5; + GL_DEPTH_COMPONENT24 = $81A6; + GL_DEPTH_COMPONENT32 = $81A7; + + GL_COMPRESSED_RGB = $84ED; + GL_COMPRESSED_RGBA = $84EE; + GL_COMPRESSED_RGB_S3TC_DXT1_EXT = $83F0; + GL_COMPRESSED_RGBA_S3TC_DXT1_EXT = $83F1; + GL_COMPRESSED_RGBA_S3TC_DXT3_EXT = $83F2; + GL_COMPRESSED_RGBA_S3TC_DXT5_EXT = $83F3; + + GL_UNSIGNED_BYTE = $1401; + GL_UNSIGNED_BYTE_3_3_2 = $8032; + GL_UNSIGNED_BYTE_2_3_3_REV = $8362; + + GL_UNSIGNED_SHORT = $1403; + GL_UNSIGNED_SHORT_5_6_5 = $8363; + GL_UNSIGNED_SHORT_4_4_4_4 = $8033; + GL_UNSIGNED_SHORT_5_5_5_1 = $8034; + GL_UNSIGNED_SHORT_5_6_5_REV = $8364; GL_UNSIGNED_SHORT_4_4_4_4_REV = $8365; GL_UNSIGNED_SHORT_1_5_5_5_REV = $8366; - GL_UNSIGNED_INT_2_10_10_10_REV = $8368; - // Filter - GL_NEAREST = $2600; - GL_LINEAR = $2601; + GL_UNSIGNED_INT = $1405; + GL_UNSIGNED_INT_8_8_8_8 = $8035; + GL_UNSIGNED_INT_10_10_10_2 = $8036; + GL_UNSIGNED_INT_8_8_8_8_REV = $8367; + GL_UNSIGNED_INT_2_10_10_10_REV = $8368; + + { Texture Filter } + GL_TEXTURE_MAG_FILTER = $2800; + GL_TEXTURE_MIN_FILTER = $2801; + GL_NEAREST = $2600; GL_NEAREST_MIPMAP_NEAREST = $2700; - GL_LINEAR_MIPMAP_NEAREST = $2701; - GL_NEAREST_MIPMAP_LINEAR = $2702; - GL_LINEAR_MIPMAP_LINEAR = $2703; - GL_TEXTURE_MAG_FILTER = $2800; - GL_TEXTURE_MIN_FILTER = $2801; - - // Wrapmodes - GL_TEXTURE_WRAP_S = $2802; - GL_TEXTURE_WRAP_T = $2803; - GL_CLAMP = $2900; - GL_REPEAT = $2901; - GL_CLAMP_TO_EDGE = $812F; - GL_CLAMP_TO_BORDER = $812D; - GL_TEXTURE_WRAP_R = $8072; - - GL_MIRRORED_REPEAT = $8370; - - // Border Color + GL_NEAREST_MIPMAP_LINEAR = $2702; + GL_LINEAR = $2601; + GL_LINEAR_MIPMAP_NEAREST = $2701; + GL_LINEAR_MIPMAP_LINEAR = $2703; + + { Texture Wrap } + GL_TEXTURE_WRAP_S = $2802; + GL_TEXTURE_WRAP_T = $2803; + GL_TEXTURE_WRAP_R = $8072; + GL_CLAMP = $2900; + GL_REPEAT = $2901; + GL_CLAMP_TO_EDGE = $812F; + GL_CLAMP_TO_BORDER = $812D; + GL_MIRRORED_REPEAT = $8370; + + { Other } + GL_GENERATE_MIPMAP = $8191; GL_TEXTURE_BORDER_COLOR = $1004; + GL_MAX_TEXTURE_SIZE = $0D33; + GL_PACK_ALIGNMENT = $0D05; + GL_UNPACK_ALIGNMENT = $0CF5; - // Texgen - GL_NORMAL_MAP = $8511; - GL_REFLECTION_MAP = $8512; - GL_S = $2000; - GL_T = $2001; - GL_R = $2002; - GL_TEXTURE_GEN_MODE = $2500; - GL_TEXTURE_GEN_S = $0C60; - GL_TEXTURE_GEN_T = $0C61; - GL_TEXTURE_GEN_R = $0C62; - - // Cubemaps - GL_MAX_CUBE_MAP_TEXTURE_SIZE = $851C; - GL_TEXTURE_CUBE_MAP = $8513; - GL_TEXTURE_BINDING_CUBE_MAP = $8514; - GL_TEXTURE_CUBE_MAP_POSITIVE_X = $8515; - GL_TEXTURE_CUBE_MAP_NEGATIVE_X = $8516; - GL_TEXTURE_CUBE_MAP_POSITIVE_Y = $8517; - GL_TEXTURE_CUBE_MAP_NEGATIVE_Y = $8518; - GL_TEXTURE_CUBE_MAP_POSITIVE_Z = $8519; - GL_TEXTURE_CUBE_MAP_NEGATIVE_Z = $851A; - - GL_TEXTURE_RECTANGLE_ARB = $84F5; - - // GL_SGIS_generate_mipmap - GL_GENERATE_MIPMAP = $8191; - - // GL_EXT_texture_compression_s3tc - GL_COMPRESSED_RGB_S3TC_DXT1_EXT = $83F0; - GL_COMPRESSED_RGBA_S3TC_DXT1_EXT = $83F1; - GL_COMPRESSED_RGBA_S3TC_DXT3_EXT = $83F2; - GL_COMPRESSED_RGBA_S3TC_DXT5_EXT = $83F3; - - // GL_EXT_texture_filter_anisotropic - GL_TEXTURE_MAX_ANISOTROPY_EXT = $84FE; + GL_TEXTURE_MAX_ANISOTROPY_EXT = $84FE; GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT = $84FF; - // GL_ARB_texture_compression - GL_COMPRESSED_RGB = $84ED; - GL_COMPRESSED_RGBA = $84EE; - GL_COMPRESSED_ALPHA = $84E9; - GL_COMPRESSED_LUMINANCE = $84EA; - GL_COMPRESSED_LUMINANCE_ALPHA = $84EB; +{$ifdef LINUX} + libglu = 'libGLU.so.1'; + libopengl = 'libGL.so.1'; +{$else} + libglu = 'glu32.dll'; + libopengl = 'opengl32.dll'; +{$endif} + +type + GLboolean = BYTEBOOL; + GLint = Integer; + GLsizei = Integer; + GLuint = Cardinal; + GLfloat = Single; + GLenum = Cardinal; + + PGLvoid = Pointer; + PGLboolean = ^GLboolean; + PGLint = ^GLint; + PGLuint = ^GLuint; + PGLfloat = ^GLfloat; + + TglCompressedTexImage1D = procedure(target: GLenum; level: GLint; internalformat: GLenum; width: GLsizei; border: GLint; imageSize: GLsizei; const data: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglCompressedTexImage2D = procedure(target: GLenum; level: GLint; internalformat: GLenum; width: GLsizei; height: GLsizei; border: GLint; imageSize: GLsizei; const data: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglGetCompressedTexImage = procedure(target: GLenum; level: GLint; img: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + +{$IF DEFINED(GLB_NATIVE_OGL_DYNAMIC)} + TglEnable = procedure(cap: GLenum); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglDisable = procedure(cap: GLenum); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + + TglGetString = function(name: GLenum): PAnsiChar; {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglGetIntegerv = procedure(pname: GLenum; params: PGLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + + TglTexParameteri = procedure(target: GLenum; pname: GLenum; param: GLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglTexParameterfv = procedure(target: GLenum; pname: GLenum; const params: PGLfloat); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglGetTexParameteriv = procedure(target: GLenum; pname: GLenum; params: PGLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglGetTexParameterfv = procedure(target: GLenum; pname: GLenum; params: PGLfloat); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglGetTexLevelParameteriv = procedure(target: GLenum; level: GLint; pname: GLenum; params: PGLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglGetTexLevelParameterfv = procedure(target: GLenum; level: GLint; pname: GLenum; params: PGLfloat); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + + TglGenTextures = procedure(n: GLsizei; textures: PGLuint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglBindTexture = procedure(target: GLenum; texture: GLuint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglDeleteTextures = procedure(n: GLsizei; const textures: PGLuint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + + TglAreTexturesResident = function(n: GLsizei; const textures: PGLuint; residences: PGLboolean): GLboolean; {$IFDEF DGL_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglReadPixels = procedure(x: GLint; y: GLint; width: GLsizei; height: GLsizei; format: GLenum; _type: GLenum; pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglPixelStorei = procedure(pname: GLenum; param: GLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + + TglTexImage1D = procedure(target: GLenum; level: GLint; internalformat: GLint; width: GLsizei; border: GLint; format: GLenum; _type: GLenum; const pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglTexImage2D = procedure(target: GLenum; level: GLint; internalformat: GLint; width: GLsizei; height: GLsizei; border: GLint; format: GLenum; _type: GLenum; const pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TglGetTexImage = procedure(target: GLenum; level: GLint; format: GLenum; _type: GLenum; pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + + TgluBuild1DMipmaps = function(target: GLEnum; components, width: GLint; format, atype: GLEnum; const data: Pointer): GLint; {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + TgluBuild2DMipmaps = function(target: GLEnum; components, width, height: GLint; format, atype: GLEnum; const Data: Pointer): GLint; {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} + + {$IFDEF GLB_LINUX} + TglXGetProcAddress = function(ProcName: PAnsiChar): Pointer; cdecl; + {$ELSE} + TwglGetProcAddress = function (ProcName: PAnsiChar): Pointer; stdcall; + {$ENDIF} + +{$ELSEIF DEFINED(GLB_NATIVE_OGL_STATIC)} + procedure glEnable(cap: GLenum); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + procedure glDisable(cap: GLenum); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + + function glGetString(name: GLenum): PAnsiChar; {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + procedure glGetIntegerv(pname: GLenum; params: PGLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + + procedure glTexParameteri(target: GLenum; pname: GLenum; param: GLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + procedure glTexParameterfv(target: GLenum; pname: GLenum; const params: PGLfloat); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + procedure glGetTexParameteriv(target: GLenum; pname: GLenum; params: PGLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + procedure glGetTexParameterfv(target: GLenum; pname: GLenum; params: PGLfloat); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + procedure glGetTexLevelParameteriv(target: GLenum; level: GLint; pname: GLenum; params: PGLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + procedure glGetTexLevelParameterfv(target: GLenum; level: GLint; pname: GLenum; params: PGLfloat); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + + procedure glGenTextures(n: GLsizei; textures: PGLuint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + procedure glBindTexture(target: GLenum; texture: GLuint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + procedure glDeleteTextures(n: GLsizei; const textures: PGLuint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + + function glAreTexturesResident(n: GLsizei; const textures: PGLuint; residences: PGLboolean): GLboolean; {$IFDEF DGL_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + procedure glReadPixels(x: GLint; y: GLint; width: GLsizei; height: GLsizei; format: GLenum; _type: GLenum; pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + procedure glPixelStorei(pname: GLenum; param: GLint); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + + procedure glTexImage1D(target: GLenum; level: GLint; internalformat: GLint; width: GLsizei; border: GLint; format: GLenum; _type: GLenum; const pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + procedure glTexImage2D(target: GLenum; level: GLint; internalformat: GLint; width: GLsizei; height: GLsizei; border: GLint; format: GLenum; _type: GLenum; const pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + procedure glGetTexImage(target: GLenum; level: GLint; format: GLenum; _type: GLenum; pixels: PGLvoid); {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libopengl; + + function gluBuild1DMipmaps(target: GLEnum; components, width: GLint; format, atype: GLEnum; const data: Pointer): GLint; {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libglu; + function gluBuild2DMipmaps(target: GLEnum; components, width, height: GLint; format, atype: GLEnum; const Data: Pointer): GLint; {$IFDEF GLB_WIN}stdcall; {$ELSE}cdecl; {$ENDIF} external libglu; +{$ENDIF} - // Extensions var GL_VERSION_1_2, GL_VERSION_1_3, GL_VERSION_1_4, GL_VERSION_2_0, + GL_SGIS_generate_mipmap, + GL_ARB_texture_border_clamp, - GL_ARB_texture_cube_map, - GL_ARB_texture_compression, - GL_ARB_texture_non_power_of_two, - GL_ARB_texture_rectangle, GL_ARB_texture_mirrored_repeat, - GL_EXT_bgra, - GL_EXT_texture_edge_clamp, - GL_EXT_texture_cube_map, - GL_EXT_texture_compression_s3tc, - GL_EXT_texture_filter_anisotropic, - GL_EXT_texture_rectangle, - GL_NV_texture_rectangle, + GL_ARB_texture_rectangle, + GL_ARB_texture_non_power_of_two, + GL_IBM_texture_mirrored_repeat, - GL_SGIS_generate_mipmap: Boolean; -const -{$IFDEF LINUX} - libglu = 'libGLU.so.1'; - libopengl = 'libGL.so.1'; -{$else} - libglu = 'glu32.dll'; - libopengl = 'opengl32.dll'; -{$ENDIF} + GL_NV_texture_rectangle, -{$IFDEF LINUX} - function glXGetProcAddress(ProcName: PAnsiChar): Pointer; cdecl; external libopengl; -{$else} - function wglGetProcAddress(ProcName: PAnsiChar): Pointer; stdcall; external libopengl; + GL_EXT_texture_edge_clamp, + GL_EXT_texture_rectangle, + GL_EXT_texture_filter_anisotropic: Boolean; + + glCompressedTexImage1D: TglCompressedTexImage1D; + glCompressedTexImage2D: TglCompressedTexImage2D; + glGetCompressedTexImage: TglGetCompressedTexImage; + +{$IFDEF GLB_NATIVE_OGL_DYNAMIC} + glEnable: TglEnable; + glDisable: TglDisable; + + glGetString: TglGetString; + glGetIntegerv: TglGetIntegerv; + + glTexParameteri: TglTexParameteri; + glTexParameterfv: TglTexParameterfv; + glGetTexParameteriv: TglGetTexParameteriv; + glGetTexParameterfv: TglGetTexParameterfv; + glGetTexLevelParameteriv: TglGetTexLevelParameteriv; + glGetTexLevelParameterfv: TglGetTexLevelParameterfv; + + glGenTextures: TglGenTextures; + glBindTexture: TglBindTexture; + glDeleteTextures: TglDeleteTextures; + + glAreTexturesResident: TglAreTexturesResident; + glReadPixels: TglReadPixels; + glPixelStorei: TglPixelStorei; + + glTexImage1D: TglTexImage1D; + glTexImage2D: TglTexImage2D; + glGetTexImage: TglGetTexImage; + + gluBuild1DMipmaps: TgluBuild1DMipmaps; + gluBuild2DMipmaps: TgluBuild2DMipmaps; + + {$IF DEFINED(GLB_WIN)} + wglGetProcAddress: TwglGetProcAddress; + {$ELSEIF DEFINED(GLB_LINUX)} + glXGetProcAddress: TglXGetProcAddress; + glXGetProcAddressARB: TglXGetProcAddressARB; + {$ENDIF} {$ENDIF} - function glGetString(name: Cardinal): PAnsiChar; {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - - procedure glEnable(cap: Cardinal); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - procedure glDisable(cap: Cardinal); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - procedure glGetIntegerv(pname: Cardinal; params: PInteger); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - - procedure glTexImage1D(target: Cardinal; level, internalformat, width, border: Integer; format, atype: Cardinal; const pixels: Pointer); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - procedure glTexImage2D(target: Cardinal; level, internalformat, width, height, border: Integer; format, atype: Cardinal; const pixels: Pointer); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - - procedure glGenTextures(n: Integer; Textures: PCardinal); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - procedure glBindTexture(target: Cardinal; Texture: Cardinal); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - procedure glDeleteTextures(n: Integer; const textures: PCardinal); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - - procedure glReadPixels(x, y: Integer; width, height: Integer; format, atype: Cardinal; pixels: Pointer); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - procedure glPixelStorei(pname: Cardinal; param: Integer); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - procedure glGetTexImage(target: Cardinal; level: Integer; format: Cardinal; _type: Cardinal; pixels: Pointer); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - - function glAreTexturesResident(n: Integer; const Textures: PCardinal; residences: PByteBool): ByteBool; {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - procedure glTexParameteri(target: Cardinal; pname: Cardinal; param: Integer); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - procedure glTexParameterfv(target: Cardinal; pname: Cardinal; const params: PSingle); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - procedure glGetTexLevelParameteriv(target: Cardinal; level: Integer; pname: Cardinal; params: PInteger); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - procedure glTexGeni(coord, pname: Cardinal; param: Integer); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libopengl; - - function gluBuild1DMipmaps(Target: Cardinal; Components, Width: Integer; Format, atype: Cardinal; Data: Pointer): Integer; {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libglu; - function gluBuild2DMipmaps(Target: Cardinal; Components, Width, Height: Integer; Format, aType: Cardinal; Data: Pointer): Integer; {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} external libglu; - +(* +{$IFDEF GLB_DELPHI} var - glCompressedTexImage2D : procedure(target: Cardinal; level: Integer; internalformat: Cardinal; width, height: Integer; border: Integer; imageSize: Integer; const data: Pointer); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} - glCompressedTexImage1D : procedure(target: Cardinal; level: Integer; internalformat: Cardinal; width: Integer; border: Integer; imageSize: Integer; const data: Pointer); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} - glGetCompressedTexImage : procedure(target: Cardinal; level: Integer; img: Pointer); {$IFDEF WINDOWS}stdcall; {$else}cdecl; {$ENDIF} + gLastContext: HGLRC; {$ENDIF} *) +{$ENDIF} + type //////////////////////////////////////////////////////////////////////////////////////////////////// EglBitmapException = class(Exception); @@ -1104,7 +1198,7 @@ type function HasAlpha: Boolean; virtual; function MaskMatch(const aRedMask, aGreenMask, aBlueMask, aAlphaMask: UInt64): Boolean; virtual; - procedure PreparePixel(var aPixel: TglBitmapPixelData); virtual; + procedure PreparePixel(out aPixel: TglBitmapPixelData); virtual; constructor Create; virtual; public @@ -1609,6 +1703,8 @@ end; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function FormatGetSupportedFiles(const aFormat: TglBitmapFormat): TglBitmapFileTypes; begin + result := []; + if (aFormat in [ //4 bbp tfLuminance4, @@ -1739,6 +1835,7 @@ begin LUMINANCE_WEIGHT_B * aPixel.Data.b); end; +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function DepthWeight(const aPixel: TglBitmapPixelData): Cardinal; begin result := Trunc( @@ -1747,11 +1844,137 @@ begin DEPTH_WEIGHT_B * aPixel.Data.b); end; -//TODO check _ARB functions and constants +{$IFDEF GLB_NATIVE_OGL} +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//OpenGLInitialization/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +var + GL_LibHandle: Pointer = nil; + +function glbGetProcAddress(aProcName: PChar; aLibHandle: Pointer = nil): Pointer; +begin + result := nil; + + if not Assigned(aLibHandle) then + aLibHandle := GL_LibHandle; + +{$IF DEFINED(GLB_WIN)} + result := GetProcAddress(HMODULE(aLibHandle), aProcName); + if Assigned(result) then + exit; + + if Assigned(wglGetProcAddress) then + result := wglGetProcAddress(aProcName); +{$ELSEIF DEFINED(GLB_LINUX)} + if Assigned(glXGetProcAddress) then begin + result := glXGetProcAddress(aProcName); + if Assigned(result) then + exit; + end; + + if Assigned(glXGetProcAddressARB) then begin + result := glXGetProcAddressARB(aProcName); + if Assigned(result) then + exit; + end; + + result := dlsym(aLibHandle, aProcName); +{$ENDIF} + if not Assigned(result) then + raise EglBitmapException.Create('unable to load procedure form library: ' + aProcName); +end; + +{$IFDEF GLB_NATIVE_OGL_DYNAMIC} +var + GLU_LibHandle: Pointer = nil; + OpenGLInitialized: Boolean; + InitOpenGLCS: TCriticalSection; + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure glbInitOpenGL; + + //////////////////////////////////////////////////////////////////////////////// + function glbLoadLibrary(const aName: PChar): Pointer; + begin + {$IF DEFINED(GLB_WIN)} + result := Pointer(LoadLibrary(aName)); + {$ELSEIF DEFINED(GLB_LINUX)} + result := dlopen(Name, RTLD_LAZY); + {$ELSE} + result := nil; + {$ENDIF} + end; + + //////////////////////////////////////////////////////////////////////////////// + function glbFreeLibrary(const aLibHandle: Pointer): Boolean; + begin + result := false; + if not Assigned(aLibHandle) then + exit; + + {$IF DEFINED(GLB_WIN)} + Result := FreeLibrary(HINST(aLibHandle)); + {$ELSEIF DEFINED(GLB_LINUX)} + Result := dlclose(aLibHandle) = 0; + {$ENDIF} + end; -(* GLB_NO_NATIVE_GL -{$IFNDEF GLB_NO_NATIVE_GL} -procedure ReadOpenGLExtensions; +var + p: Pointer; +begin + if Assigned(GL_LibHandle) then + glbFreeLibrary(GL_LibHandle); + + if Assigned(GLU_LibHandle) then + glbFreeLibrary(GLU_LibHandle); + + GL_LibHandle := glbLoadLibrary(libopengl); + if not Assigned(GL_LibHandle) then + raise EglBitmapException.Create('unable to load library: ' + libopengl); + + GLU_LibHandle := glbLoadLibrary(libglu); + if not Assigned(GLU_LibHandle) then + raise EglBitmapException.Create('unable to load library: ' + libglu); + + try + {$IF DEFINED(GLB_WIN)} + wglGetProcAddress := glbGetProcAddress('wglGetProcAddress'); + {$ELSEIF DEFINED(GLB_LINUX)} + glXGetProcAddress := glbGetProcAddress('glXGetProcAddress'); + glXGetProcAddressARB := dglGetProcAddress('glXGetProcAddressARB'); + {$ENDIF} + + glEnable := glbGetProcAddress('glEnable'); + glDisable := glbGetProcAddress('glDisable'); + glGetString := glbGetProcAddress('glGetString'); + glGetIntegerv := glbGetProcAddress('glGetIntegerv'); + glTexParameteri := glbGetProcAddress('glTexParameteri'); + glTexParameterfv := glbGetProcAddress('glTexParameterfv'); + glGetTexParameteriv := glbGetProcAddress('glGetTexParameteriv'); + glGetTexParameterfv := glbGetProcAddress('glGetTexParameterfv'); + glGetTexLevelParameteriv := glbGetProcAddress('glGetTexLevelParameteriv'); + glGetTexLevelParameterfv := glbGetProcAddress('glGetTexLevelParameterfv'); + glGenTextures := glbGetProcAddress('glGenTextures'); + glBindTexture := glbGetProcAddress('glBindTexture'); + glDeleteTextures := glbGetProcAddress('glDeleteTextures'); + glAreTexturesResident := glbGetProcAddress('glAreTexturesResident'); + glReadPixels := glbGetProcAddress('glReadPixels'); + glPixelStorei := glbGetProcAddress('glPixelStorei'); + glTexImage1D := glbGetProcAddress('glTexImage1D'); + glTexImage2D := glbGetProcAddress('glTexImage2D'); + glGetTexImage := glbGetProcAddress('glGetTexImage'); + + gluBuild1DMipmaps := glbGetProcAddress('gluBuild1DMipmaps', GLU_LibHandle); + gluBuild2DMipmaps := glbGetProcAddress('gluBuild2DMipmaps', GLU_LibHandle); + finally + glbFreeLibrary(GL_LibHandle); + glbFreeLibrary(GLU_LibHandle); + end; +end; +{$ENDIF} + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure glbReadOpenGLExtensions; var {$IFDEF GLB_DELPHI} Context: HGLRC; @@ -1759,7 +1982,7 @@ var Buffer: AnsiString; MajorVersion, MinorVersion: Integer; - + /////////////////////////////////////////////////////////////////////////////////////////// procedure TrimVersionString(Buffer: AnsiString; var Major, Minor: Integer); var Separator: Integer; @@ -1768,7 +1991,6 @@ var Major := 0; Separator := Pos(AnsiString('.'), Buffer); - if (Separator > 1) and (Separator < Length(Buffer)) and (Buffer[Separator - 1] in ['0'..'9']) and (Buffer[Separator + 1] in ['0'..'9']) then begin @@ -1791,36 +2013,35 @@ var end; end; - + /////////////////////////////////////////////////////////////////////////////////////////// function CheckExtension(const Extension: AnsiString): Boolean; var ExtPos: Integer; begin ExtPos := Pos(Extension, Buffer); result := ExtPos > 0; - if result then result := ((ExtPos + Length(Extension) - 1) = Length(Buffer)) or not (Buffer[ExtPos + Length(Extension)] in ['_', 'A'..'Z', 'a'..'z']); end; - - function glLoad (aFunc: pAnsiChar): pointer; - begin - {$IFDEF LINUX} - result := glXGetProcAddress(aFunc); - {$else} - result := wglGetProcAddress(aFunc); - {$ENDIF} +begin +{$IFDEF GLB_NATIVE_OGL_DYNAMIC} + InitOpenGLCS.Enter; + try + if not OpenGLInitialized then begin + glbInitOpenGL; + OpenGLInitialized := true; + end; + finally + InitOpenGLCS.Leave; end; +{$ENDIF} - -begin - {$IFDEF GLB_DELPHI} +{$IFDEF GLB_DELPHI} Context := wglGetCurrentContext; - - if Context <> gLastContext then begin + if (Context <> gLastContext) then begin gLastContext := Context; - {$ENDIF} +{$ENDIF} // Version Buffer := glGetString(GL_VERSION); @@ -1830,21 +2051,16 @@ begin GL_VERSION_1_3 := false; GL_VERSION_1_4 := false; GL_VERSION_2_0 := false; - if MajorVersion = 1 then begin - if MinorVersion >= 1 then begin - if MinorVersion >= 2 then - GL_VERSION_1_2 := true; + if MinorVersion >= 2 then + GL_VERSION_1_2 := true; - if MinorVersion >= 3 then - GL_VERSION_1_3 := true; + if MinorVersion >= 3 then + GL_VERSION_1_3 := true; - if MinorVersion >= 4 then - GL_VERSION_1_4 := true; - end; - end; - - if MajorVersion >= 2 then begin + if MinorVersion >= 4 then + GL_VERSION_1_4 := true; + end else if MajorVersion >= 2 then begin GL_VERSION_1_2 := true; GL_VERSION_1_3 := true; GL_VERSION_1_4 := true; @@ -1854,41 +2070,30 @@ begin // Extensions Buffer := glGetString(GL_EXTENSIONS); GL_ARB_texture_border_clamp := CheckExtension('GL_ARB_texture_border_clamp'); - GL_ARB_texture_cube_map := CheckExtension('GL_ARB_texture_cube_map'); - GL_ARB_texture_compression := CheckExtension('GL_ARB_texture_compression'); GL_ARB_texture_non_power_of_two := CheckExtension('GL_ARB_texture_non_power_of_two'); GL_ARB_texture_rectangle := CheckExtension('GL_ARB_texture_rectangle'); GL_ARB_texture_mirrored_repeat := CheckExtension('GL_ARB_texture_mirrored_repeat'); - GL_EXT_bgra := CheckExtension('GL_EXT_bgra'); GL_EXT_texture_edge_clamp := CheckExtension('GL_EXT_texture_edge_clamp'); - GL_EXT_texture_cube_map := CheckExtension('GL_EXT_texture_cube_map'); - GL_EXT_texture_compression_s3tc := CheckExtension('GL_EXT_texture_compression_s3tc'); GL_EXT_texture_filter_anisotropic := CheckExtension('GL_EXT_texture_filter_anisotropic'); GL_EXT_texture_rectangle := CheckExtension('GL_EXT_texture_rectangle'); GL_NV_texture_rectangle := CheckExtension('GL_NV_texture_rectangle'); GL_IBM_texture_mirrored_repeat := CheckExtension('GL_IBM_texture_mirrored_repeat'); GL_SGIS_generate_mipmap := CheckExtension('GL_SGIS_generate_mipmap'); - // Funtions if GL_VERSION_1_3 then begin - // Loading Core - glCompressedTexImage1D := glLoad('glCompressedTexImage1D'); - glCompressedTexImage2D := glLoad('glCompressedTexImage2D'); - glGetCompressedTexImage := glLoad('glGetCompressedTexImage'); - end else - - begin - // Try loading Extension - glCompressedTexImage1D := glLoad('glCompressedTexImage1DARB'); - glCompressedTexImage2D := glLoad('glCompressedTexImage2DARB'); - glGetCompressedTexImage := glLoad('glGetCompressedTexImageARB'); + glCompressedTexImage1D := glbGetProcAddress('glCompressedTexImage1D'); + glCompressedTexImage2D := glbGetProcAddress('glCompressedTexImage2D'); + glGetCompressedTexImage := glbGetProcAddress('glGetCompressedTexImage'); + end else begin + glCompressedTexImage1D := glbGetProcAddress('glCompressedTexImage1DARB'); + glCompressedTexImage2D := glbGetProcAddress('glCompressedTexImage2DARB'); + glGetCompressedTexImage := glbGetProcAddress('glGetCompressedTexImageARB'); end; - {$IFDEF GLB_DELPHI} +{$IFDEF GLB_DELPHI} end; - {$ENDIF} +{$ENDIF} end; {$ENDIF} -*) (* TODO GLB_DELPHI {$IFDEF GLB_DELPHI} @@ -2266,7 +2471,7 @@ begin end; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -procedure TFormatDescriptor.PreparePixel(var aPixel: TglBitmapPixelData); +procedure TFormatDescriptor.PreparePixel(out aPixel: TglBitmapPixelData); begin FillChar(aPixel, SizeOf(aPixel), 0); aPixel.Data := fRange; @@ -3756,6 +3961,7 @@ begin end; end; +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure TbmpColorTableFormat.Unmap(var aData: PByte; var aPixel: TglBitmapPixelData; var aMapData: Pointer); type PUInt64 = ^UInt64; @@ -3802,6 +4008,21 @@ end; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //TglBitmap - Helper////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +procedure glBitmapConvertPixel(var aPixel: TglBitmapPixelData; const aSourceFD, aDestFD: TFormatDescriptor); +var + i: Integer; +begin + for i := 0 to 3 do begin + if (aSourceFD.Range.arr[i] <> aDestFD.Range.arr[i]) then begin + if (aSourceFD.Range.arr[i] > 0) then + aPixel.Data.arr[i] := Round(aPixel.Data.arr[i] / aSourceFD.Range.arr[i] * aDestFD.Range.arr[i]) + else + aPixel.Data.arr[i] := aDestFD.Range.arr[i]; + end; + end; +end; + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// procedure glBitmapConvertCopyFunc(var aFuncRec: TglBitmapFunctionRec); begin with aFuncRec do begin @@ -3851,10 +4072,7 @@ end; procedure glBitmapInvertFunc(var aFuncRec: TglBitmapFunctionRec); begin with aFuncRec do begin - Dest.Data.r := Source.Data.r; - Dest.Data.g := Source.Data.g; - Dest.Data.b := Source.Data.b; - Dest.Data.a := Source.Data.a; + Dest.Data := Source.Data; if (Args and $1 > 0) then begin Dest.Data.r := Dest.Data.r xor Dest.Range.r; Dest.Data.g := Dest.Data.g xor Dest.Range.g; @@ -4935,7 +5153,7 @@ begin result := false; FormatDesc := TFormatDescriptor.Get(Format); if Assigned(Data) then begin - if not ({FormatDesc.IsUncompressed or }FormatDesc.HasAlpha) then + if FormatDesc.IsCompressed or not FormatDesc.HasAlpha then raise EglBitmapUnsupportedFormatFormat.Create('RemoveAlpha - ' + UNSUPPORTED_FORMAT); result := ConvertTo(FormatDesc.WithoutAlpha); end; @@ -5246,8 +5464,8 @@ end; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// constructor TglBitmap.Create; begin -{$IFNDEF GLB_NO_NATIVE_GL} - ReadOpenGLExtensions; +{$IFDEF GLB_NATIVE_OGL} + glbReadOpenGLExtensions; {$ENDIF} if (ClassType = TglBitmap) then raise EglBitmapException.Create('Don''t create TglBitmap directly. Use one of the deviated classes (TglBitmap2D) instead.'); @@ -6322,16 +6540,7 @@ var SpecialFormat.PreparePixel(Pixel); for i := 0 to Info.biWidth-1 do begin SpecialFormat.Unmap(aLineBuf, Pixel, SourceMD); - with FormatDesc do begin - //TODO: use convert function - for j := 0 to 3 do - if (SpecialFormat.Range.arr[j] <> Range.arr[j]) then begin - if (SpecialFormat.Range.arr[j] > 0) then - Pixel.Data.arr[j] := Round(Pixel.Data.arr[j] / SpecialFormat.Range.arr[j] * Range.arr[j]) - else - Pixel.Data.arr[j] := 0; - end; - end; + glBitmapConvertPixel(Pixel, SpecialFormat, FormatDesc); FormatDesc.Map(Pixel, aData, DestMD); end; end; @@ -6563,16 +6772,7 @@ begin dstData := ConvertBuffer; for PixelIdx := 0 to Info.biWidth-1 do begin FormatDesc.Unmap(srcData, Pixel, SourceFD); - with FormatDesc do begin - //TODO use convert function - for i := 0 to 3 do - if (Converter.Range.arr[i] <> Range.arr[i]) then begin - if (Range.arr[i] > 0) then - Pixel.Data.arr[i] := Round(Pixel.Data.arr[i] / Range.arr[i] * Converter.Range.arr[i]) - else - Pixel.Data.arr[i] := 0; - end; - end; + glBitmapConvertPixel(Pixel, FormatDesc, Converter); Converter.Map(Pixel, dstData, DestFD); end; aStream.Write(ConvertBuffer^, wbLineSize); @@ -6618,15 +6818,12 @@ type end; const - TGA_UNCOMPRESSED_COLOR_TABLE = 1; - TGA_UNCOMPRESSED_RGB = 2; - TGA_UNCOMPRESSED_GRAY = 3; - TGA_COMPRESSED_COLOR_TABLE = 9; - TGA_COMPRESSED_RGB = 10; - TGA_COMPRESSED_GRAY = 11; + TGA_UNCOMPRESSED_RGB = 2; + TGA_UNCOMPRESSED_GRAY = 3; + TGA_COMPRESSED_RGB = 10; + TGA_COMPRESSED_GRAY = 11; - TGA_NONE_COLOR_TABLE = 0; - TGA_COLOR_TABLE = 1; + TGA_NONE_COLOR_TABLE = 0; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function TglBitmap.LoadTGA(const aStream: TStream): Boolean; @@ -6998,34 +7195,20 @@ const DDSD_CAPS = $00000001; DDSD_HEIGHT = $00000002; DDSD_WIDTH = $00000004; - DDSD_PITCH = $00000008; DDSD_PIXELFORMAT = $00001000; - DDSD_MIPMAPCOUNT = $00020000; - DDSD_LINEARSIZE = $00080000; - DDSD_DEPTH = $00800000; // DDS_header.sPixelFormat.dwFlags DDPF_ALPHAPIXELS = $00000001; DDPF_ALPHA = $00000002; DDPF_FOURCC = $00000004; - DDPF_INDEXED = $00000020; DDPF_RGB = $00000040; DDPF_LUMINANCE = $00020000; // DDS_header.sCaps.dwCaps1 - DDSCAPS_COMPLEX = $00000008; DDSCAPS_TEXTURE = $00001000; - DDSCAPS_MIPMAP = $00400000; // DDS_header.sCaps.dwCaps2 DDSCAPS2_CUBEMAP = $00000200; - DDSCAPS2_CUBEMAP_POSITIVEX = $00000400; - DDSCAPS2_CUBEMAP_NEGATIVEX = $00000800; - DDSCAPS2_CUBEMAP_POSITIVEY = $00001000; - DDSCAPS2_CUBEMAP_NEGATIVEY = $00002000; - DDSCAPS2_CUBEMAP_POSITIVEZ = $00004000; - DDSCAPS2_CUBEMAP_NEGATIVEZ = $00008000; - DDSCAPS2_VOLUME = $00200000; D3DFMT_DXT1 = $31545844; D3DFMT_DXT3 = $33545844; @@ -7193,14 +7376,7 @@ begin aStream.Read(SrcData^, RowSize); for x := 0 to Header.dwWidth-1 do begin Converter.Unmap(SrcData, Pixel, SourceMD); - //TODO use converter function - for j := 0 to 3 do - if (Converter.Range.arr[j] <> FormatDesc.Range.arr[j]) then begin - if (Converter.Range.arr[j] > 0) then - Pixel.Data.arr[j] := Round(Pixel.Data.arr[j] / Converter.Range.arr[j] * FormatDesc.Range.arr[j]) - else - Pixel.Data.arr[j] := 0; - end; + glBitmapConvertPixel(Pixel, Converter, FormatDesc); FormatDesc.Map(Pixel, TmpData, DestMD); end; end; @@ -7461,7 +7637,7 @@ begin raise EglBitmapSizeToLargeException.Create('TglBitmap2D.GenTexture - The size for the texture is to large. It''s may be not conform with the Hardware.'); PotTex := IsPowerOfTwo(Height) and IsPowerOfTwo(Width); - TexRec := (GL_ARB_texture_rectangle or GL_EXT_texture_rectangle or GL_NV_texture_rectangle) and (Target = GL_TEXTURE_RECTANGLE_ARB); + TexRec := (GL_ARB_texture_rectangle or GL_EXT_texture_rectangle or GL_NV_texture_rectangle) and (Target = GL_TEXTURE_RECTANGLE); if not (PotTex or GL_ARB_texture_non_power_of_two or GL_VERSION_2_0 or TexRec) then raise EglBitmapNonPowerOfTwoException.Create('TglBitmap2D.GenTexture - Rendercontex dosn''t support non power of two texture.'); @@ -8132,8 +8308,17 @@ initialization TFormatDescriptor.Init; +{$IFDEF GLB_NATIVE_OGL_DYNAMIC} + OpenGLInitialized := false; + InitOpenGLCS := TCriticalSection.Create; +{$ENDIF} + finalization TFormatDescriptor.Finalize; +{$IFDEF GLB_NATIVE_OGL_DYNAMIC} + FreeAndNil(InitOpenGLCS); +{$ENDIF} + end. -- 2.1.4