Using images in OpenGL (as textures and as normal images).
For non-OpenGL image management, see CastleImages and TextureImages units. They contain functions and classes to load, save and process images.
This unit has functions and classes to:
Load images as OpenGL textures. You usually do not use these directly, instead TCastleScene automatically uses these to load and render textures as part of 3D models.
A lot of utilities included: for 2D textures (see LoadGLTexture), cube maps (see glTextureCubeMap), 3D textures (see glTextureImage3D). These functions wrap OpenGL calls like glTexImage2D to handle our images (TEncodedImage (and descendant TCastleImage), TDDSImage), and to automatically set texture parameters, mipmaps and such.
Load and draw images in 2D. This is useful to implement various 2D controls. See TGLImage class and friends.
Render to texture, see TGLRenderToTexture class. This is our abstraction over OpenGL framebuffer (or glCopyTexSubImage for ancient GPUs).
This unit hides from your some details about OpenGL images handling. For example, you don't have to worry about "pixel store alignment", we handle it here internally when transferring images between memory and GPU. You also don't have to worry about texture sizes being power of 2, or about maximum texture sizes — we will resize textures if necessary.
The suffix "NoFlush" is there to remind you that this function grabs the current buffer contents. Usually you want to redraw the screen to the back buffer, and call this function to capture back buffer before swapping, since this is the only reliable way to capture OpenGL screen. Just use TCastleWindowCustom.SaveScreen to do it automatically.
Version with ImageClass can save to any image format from PixelsImageClasses.
Version with TCastleImage instance just uses this instance to save the image. You must pass here already created TCastleImage instance, it's class, Width and Height will be used when saving.
Resize the image to a size accepted as GL_TEXTURE_2D texture size for OpenGL. It tries to resize to a larger size, not smaller, to avoid losing image information.
It also makes texture have power-of-two size, if Sizing <> tsAny (or if GLFeatures.TextureNonPowerOfTwo = False). This is a must for normal textures, used for 3D rendering (with mipmapping and such). Using OpenGL non-power-of-2 textures is not good for such usage case, some OpenGLs crash (ATI), some are ultra slow (NVidia), some cause artifacts (Mesa). OpenGL ES explicitly limits what you can do with non-power-of-2. Sample model using non-power-of-2 is in inlined_textures.wrl.
Use Sizing = tsAny only for textures that you plan to use for drawing GUI images by TGLImage.
Does image have proper size for 2D OpenGL texture. See ResizeForTextureSize. Note that this checks glGet(GL_MAX_TEXTURE_SIZE), so requires initialized OpenGL context.
function IsTextureSized(const Width, Height: Cardinal; const Sizing: TTextureSizing): boolean;
function IsCubeMapTextureSized(const Size: Cardinal): boolean;
function ResizeToCubeMapTextureSize(const Size: Cardinal): Cardinal;
Load new texture to OpenGL. Generates new texture number by glGenTextures, then binds this texture, and loads it's data.
Takes care of UNPACK_ALIGNMENT inside (if needed, we'll change it and later revert back, so that the texture is correctly loaded).
Sets texture minification, magnification filters and wrap parameters.
Changes currently bound texture to this one (returned).
If mipmaps will be needed (this is decided looking at Filter.Minification) we will load them too.
As a first try, if DDSForMipmaps is non-nil and has mipmaps (DDSForMipmaps.Mipmaps), we will load these mipmaps. DDSForMipmaps must be a normal 2D texture (DDSType = dtTexture).
Otherwise, we'll try to generate mipmaps, using various OpenGL mechanisms.
We will try using GenerateMipmap functionality to generate mipmaps on GPU. If not available, for uncompressed textures, we will generate mipmaps on CPU. For compressed textures, we will change minification filter to simple minLinear and make OnWarning.
If texture cannot be loaded for whatever reason. This includes ECannotLoadS3TCTexture if the S3TC texture cannot be loaded for whatever reason. This includes EInvalidImageForOpenGLTexture if Image class is invalid for an OpenGL texture.
Load OpenGL texture into already reserved texture number. It uses existing OpenGL texture number (texnum). Everything else works exactly the same as LoadGLTexture.
You can also use this to set "default unnamed OpenGL texture" parameters by passing TexNum = 0.
Comfortably load all six cube map texture images. Think about this as doing glTexImage2D(Side, ...) for each cube side. It takes care of (almost?) everything you need to prepare OpenGL cube map texture.
It automatically takes care to adjust the texture size to appropriate size, honoring the "power of two" requirement and the GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB limit of OpenGL. So texture image may be resized (preferably up) internally before loading. Although, if texture is S3TC compressed, we cannot resize it — so ECannotLoadS3TCTexture will be raised if texture is not appropriate size.
It takes care about OpenGL unpack parameters. Just don't worry about it.
If mipmaps are requested:
First of all, if DDSForMipmaps is non-nil and has mipmaps defined, we will load them from this DDS image. DDSForMipmaps must have DDSType = dtCubeMap.
Otherwise, we'll try to generate images using OpenGL GenerateMipmap.
As a last resort, if GenerateMipmap is not available, we will fallback to generating mipmaps on CPU by good old gluBuild2DMipmaps call.
If texture cannot be loaded for whatever reason. This includes ECannotLoadS3TCTexture if the S3TC texture cannot be loaded for whatever reason (not availble S3TC extensions, not correct texture size, mipmaps requested and DDSForMipmaps/glGenerateMipmap not available). This includes EInvalidImageForOpenGLTexture if Image class is invalid for an OpenGL texture.
Comfortably load a 3D texture. Think about this as doing glTexImage3D(...) for you. It also sets texture minification, magnification filters and creates mipmaps if necessary.
It checks OpenGL 3D texture size requirements, and throws exceptions if not satisfied.
It takes care about OpenGL unpack parameters. Just don't worry about it.
If Filter uses mipmaps, then all mipmap levels will be loaded.
As a first try, if DDSForMipmaps is non-nil and has mipmaps (DDSForMipmaps.Mipmaps), we will load these mipmaps. DDSForMipmaps must be a 3D texture (DDSType = dtVolume).
Otherwise, we'll generate mipmaps.
GenerateMipmap functionality will be required for this. When it is not available on this OpenGL implementation, we will change minification filter to simple linear and make OnWarning. So usually you just don't have to worry about this.
If texture cannot be loaded for whatever reason, for example it's size is not correct for OpenGL 3D texture (we cannot automatically resize 3D textures, at least for now). Or it's compressed (although we support here TEncodedImage, OpenGL doesn't have any 3D texture compression available.)
If no glGenerateMipmap version is available on this OpenGL version. If you don't want to get this exception, you can always check HasGenerateMipmap before calling this.
Call glTexParameterf to set GL_TEXTURE_MAX_ANISOTROPY_EXT on given texture target.
Takes care to check for appropriate OpenGL extension (if not present, does nothing), and to query OpenGL limit for Anisotropy (eventually clamping provided Anisotropy down).
———————————————————————- Adjusting image sizes to load them as textures. Usually you don't need these functions, LoadGLTexture* and TGLImage and such call it automatically when needed.
Values
tsAny: Texture size does not have to be a power of two (unless GLFeatures.TextureNonPowerOfTwo = False, in which case all textures must have power-of-two, and then tsAny may be scaled to satisfy it (but it still will not be scaled for GLTextureScale)). It is not affected by GLTextureScale.
tsRequiredPowerOf2: Texture size must be a power of two. It is not affected by GLTextureScale, because we cannot scale it.
tsScalablePowerOf2: Texture size must be a power of two. It is affected by GLTextureScale, we can scale it.
Scaling for all textures loaded to OpenGL. This allows you to conserve GPU memory. Each size (width, height, and (for 3D textures) depth) is scaled by 1 / 2ˆGLTextureScale. So value = 1 means no scaling, value = 2 means that each size is 1/2 (texture area is 1/4), value = 3 means that each size is 1/4 and so on.
Note that textures used for GUI, by TGLImage (more precisely: all non-power-of-2 textures) avoid this scaling entirely.
GLTextureMinSize: Cardinal = 16;
Contraints the scaling done by GLTextureScale. Scaling caused by GLTextureScale cannot scale texture to something less than GLTextureMinSize. If texture size was already < GLTextureMinSize, it is not scaled at all by GLTextureScale. This must be a power of two.