unit TextureCubeMap;

interface

uses Windows, SysUtils, GL, GLu, GLext, WGLext, GLtools, Images, Textures;

type
  TTextureCubeMap = class(TTexture)
  protected
    procedure SetImage(i: cardinal; img: TImage); reintroduce; virtual;
    function GetImage(i: cardinal): TImage; reintroduce; virtual;
  public
    constructor Create; reintroduce; virtual;
    property Image[i: cardinal]: TImage read GetImage write SetImage;
  end;

implementation

constructor TTextureCubeMap.Create;
begin
  if not glext_ExtensionSupported('GL_ARB_texture_cube_map') then
    raise Exception.Create('GL_ARB_texture_cube_map required!');

  inherited Create(GL_TEXTURE_CUBE_MAP_ARB);
end;

procedure TTextureCubeMap.SetImage;
var
  fmt: cardinal;
begin
  Bind;
  case img.BytesPerPel of
    1: fmt := GL_RED;
    2: fmt := GL_LUMINANCE_ALPHA;
    3: fmt := GL_BGR;
    4: fmt := GL_BGRA;
    else raise Exception.Create('TTextureCubeMap.SetImage: Invalid bytes per pel');
  end;
  glTexImage2D(i, 0, FInternalFormat,
               img.Width, img.Height, 0, fmt, GL_UNSIGNED_BYTE, img.Data);
end;

function TTextureCubeMap.GetImage;
var
  fmt: cardinal;
begin
  Bind;
  result := TImage.Create(Width, Height, BytesPerPel * 8);
  case BytesPerPel of
    1: fmt := GL_RED;
    2: fmt := GL_LUMINANCE_ALPHA;
    3: fmt := GL_BGR;
    4: fmt := GL_BGRA;
    else raise Exception.Create('TTextureCubeMap.GetImage: Invalid bytes per pel');
  end;
  glGetTexImage(i, 0, fmt, GL_UNSIGNED_BYTE, result.Data);
end;

end.
