unit Texture3D;

interface

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

type
  TTexture3D = class(TTexture)
  protected
    procedure SetImage(img: TImage); override;
    function GetImage: TImage; override;
  public
    constructor Create; reintroduce;
  end;

implementation

constructor TTexture3D.Create;
begin
  if not Load_GL_EXT_texture3D then
    raise Exception.Create('GL_EXT_texture3D required!');

  inherited Create(GL_TEXTURE_3D);
end;

procedure TTexture3D.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('TTexture3D.SetImage: Invalid bytes per pel');
  end;
  glTexImage3D(FTarget, 0, FInternalFormat,
               img.Width, img.Height, TImage3D(img).Depth, 0,
               fmt, GL_UNSIGNED_BYTE, img.Data);
end;

function TTexture3D.GetImage;
var
  fmt: cardinal;
begin
  Bind;
  result := TImage3D.Create(Width, Height, Depth, 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('TTexture3D.GetImage: Invalid bytes per pel');
  end;
  glGetTexImage(FTarget, 0, fmt, GL_UNSIGNED_BYTE, result.Data);
end;

end.
