unit textures3d;
interface
uses opengl,types3d,graphics,ogl3d;

function LoadTextureFromResources(ID: GLuint; var Where: TTexture; Clamp: Boolean): GLint;
function LoadTextureAlphaFromResources(ID,MaskID: GLint; var Where: TTexture; Clamp: Boolean): GLint;
procedure GenHalo(var _: TTexture; Negative: BOOLEAN);

implementation


function LoadTextureFromResources(ID: GLuint; var Where: TTexture; Clamp: Boolean): GLint;
VAR I,J: GLint;
    Row: RGBLine;
    Tmp: TBitmap;
begin
Tmp:=TBitmap.Create;
TRY

  Tmp.LoadFromResourceID(hInstance,ID); Tmp.PixelFormat:=pf24bit;

  Where.Width:=Tmp.Width; Where.Height:=Tmp.Height; Where.BPP:=3; SetLength(Where.Data,Where.Width*Where.Height*Where.BPP);
    For I:=0 to Where.Height-1 do begin
     Row:=Tmp.ScanLine[I];
    For J:=0 to Where.Width-1 do begin
     Where.Data[0+3*(I*Where.Width+J)]:=Row[J].R;
     Where.Data[1+3*(I*Where.Width+J)]:=Row[J].G;
     Where.Data[2+3*(I*Where.Width+J)]:=Row[J].B;
    end;
    end;
    CreateTexture(Where,Clamp);

FINALLY
 Tmp.Free;
END;
  Result:=0;
end;

function LoadTextureAlphaFromResources(ID,MaskID: GLint; var Where: TTexture; Clamp: Boolean): GLint;
VAR I,J: GLint;
    Row1,Row2: RGBLine;
    Tmp1,Tmp2: TBitmap;
begin
Tmp1:=TBitmap.Create;
Tmp2:=TBitmap.Create;
TRY
  Tmp1.LoadFromResourceID(hInstance,ID); Tmp1.PixelFormat:=pf24bit;
  if MaskID>=0 then begin Tmp2.LoadFromResourceID(hInstance,MaskID); Tmp2.PixelFormat:=pf24bit; end;

  Where.Width:=Tmp1.Width; Where.Height:=Tmp1.Height; Where.BPP:=4; SetLength(Where.Data,Where.Width*Where.Height*Where.BPP);
  Row2:=nil;
    For I:=0 to Where.Height-1 do begin
      Row1:=Tmp1.ScanLine[I];
      if MaskID>=0 then Row2:=Tmp2.ScanLine[I];
    For J:=0 to Where.Width-1 do begin
      if MaskID>=0 then begin
       Where.Data[0+4*(I*Where.Width+J)]:=Row1[J].R;
       Where.Data[1+4*(I*Where.Width+J)]:=Row1[J].G;
       Where.Data[2+4*(I*Where.Width+J)]:=Row1[J].B;
       Where.Data[3+4*(I*Where.Width+J)]:=Row2[J].B;
      end else begin
       Where.Data[0+4*(I*Where.Width+J)]:=255;
       Where.Data[1+4*(I*Where.Width+J)]:=255;
       Where.Data[2+4*(I*Where.Width+J)]:=255;
       Where.Data[3+4*(I*Where.Width+J)]:=Row1[J].B;
      end;
    end;
    end;
    CreateTextureAlpha(Where,Clamp);
FINALLY
 Tmp1.Free;
 Tmp2.Free;
END;
  Result:=0;
end;

////////////////////////////////////////////////////////////////////////////////

procedure GenHalo(var _: TTexture; Negative: BOOLEAN);
var C,E,M: GLint;
    Dist: GLfloat;
begin
   For C:= 0 to _.Width-1 do
    For E:= 0 to _.Width-1 do
     For M:= 1 to _.BPP do begin
       Dist:=SQRT( SQR(E-_.Width/2) + SQR(C-_.Width/2) );

       if M=_.BPP then begin
        if Dist<_.Width/2 then
          _.Data[(M-1) + (C*_.Width+E)*_.BPP]:=255-Round( Dist/(_.Width/2+0.5) * 256 )
        else _.Data[(M-1) + (C*_.Width+E)*_.BPP]:=0
       end else _.Data[(M-1) + (C*_.Width+E)*_.BPP]:=255;

       if Negative then _.Data[(M-1) + (C*_.Width+E)*_.BPP]:=255-_.Data[(M-1) + (C*_.Width+E)*_.BPP];
     end;
end;

end.
