unit OffsetTexture;

interface

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

type
  TOffsetTexture = class(TTexture2D)
  protected
    FInOutFormat                : GLenum;
    procedure SetImage(img: TImage); override;
    function GetImage: TImage; override;
  public
    constructor Create; override;
    destructor Destroy; override;
  end;

implementation

constructor TOffsetTexture.Create;
begin
  if glext_ExtensionSupported('GL_ATI_envmap_bumpmap') then
  begin
    FInternalFormat := GL_DUDV_ATI;
    FInOutFormat := GL_DUDV_ATI;
  end else
  if glext_ExtensionSupported('GL_NV_texture_shader') then
  begin
    FInternalFormat := GL_DSDT_NV;
    FInOutFormat := GL_DSDT_NV;
  end else
    raise Exception.Create('GL_ATI_envmap_bumpmap or GL_NV_texture_shader required!');

  inherited Create;
end;

destructor TOffsetTexture.Destroy;
begin
  inherited Destroy;
end;

procedure TOffsetTexture.SetImage;
begin
  Bind;
  glTexImage2D(FTarget, 0, FInternalFormat,
               img.Width, img.Height, 0, FInOutFormat, GL_UNSIGNED_BYTE, img.Data);
end;

function TOffsetTexture.GetImage;
begin
  Bind;
  result := TImage.Create(Width, Height, 16);
  glGetTexImage(FTarget, 0, FInOutFormat, GL_UNSIGNED_BYTE, result.Data);
end;

end.
