//------------------------------------------------------------------------------
//
//  Portal Adventure - 2nd PGD Challenge: The Journey
//  Copyright (C) 2012 by Jim Valavanis
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
//  02111-1307, USA.
//
//------------------------------------------------------------------------------
//  E-Mail: jimmyvalavanis@yahoo.gr - jvalavanis@gmail.com
//------------------------------------------------------------------------------

{$I portal.inc}

unit gl_shaders;

interface

uses
  dglOpenGL;

function gld_CreateShader(const vShaderF, fShaderF: string): GLHandleARB;

procedure gld_InitShaders;

procedure gld_DoneShaders;

var
  bumpshader: glHandleARB;

implementation

uses
  d_delphi,
  w_pak;

function gld_CreateShader(const vShaderF, fShaderF: string): GLHandleARB;
var
  VShader : GLHandleARB;
  FShader : GLHandleARB;
  SSource : TDStringList;
  Source  : String;
  SLen    : Integer;
  strm: TPakStream;
begin
  result := glCreateProgramObjectARB;
  VShader := glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
  FShader := glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);


  SSource := TDStringList.Create;

  strm := TPakStream.Create(vShaderF, pm_short);
  if strm.IOResult = 0 then
  begin
    SSource.LoadFromStream(strm);
  end;
  strm.Free;

  Source := SSource.Text;
  SLen := Length(Source);
  glShaderSourceARB(VShader, 1, @Source, @SLen);

  strm := TPakStream.Create(fShaderF, pm_short);
  if strm.IOResult = 0 then
  begin
    SSource.LoadFromStream(strm);
  end;
  strm.Free;

  Source := SSource.Text;
  SLen := Length(Source);
  glShaderSourceARB(FShader, 1, @Source, @SLen);
  SSource.Free;
  glCompileShaderARB(VShader);
  glCompileShaderARB(FShader);
  glAttachObjectARB(result, VShader);
  glAttachObjectARB(result, FShader);
  glDeleteObjectARB(VShader);
  glDeleteObjectARB(FShader);
  glLinkProgramARB(result);
end;

procedure gld_InitShaders;
begin
//  bumpshader := gld_CreateShader('bump_vs.txt', 'bump_fs.txt');
end;

procedure gld_DoneShaders;
begin

end;

end.
