unit shader;

interface
 uses  dglOpenGL;

function LoadFragmentandVertexShader(FFilename: string; VFilename: string): GLHandleARB;


implementation

uses
  Windows,
  Messages,
  SysUtils,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,

  OpenGL15_MainForm;


function CheckForErrors(glObject: GLHandleARB): String;
 var
  blen, slen: GLInt;
  InfoLog   : PGLCharARB;
begin
 glGetObjectParameterivARB(glObject, GL_OBJECT_INFO_LOG_LENGTH_ARB, @blen);
 if blen > 1 then
 begin
  GetMem(InfoLog, blen*SizeOf(GLCharARB));
  glGetInfoLogARB(glObject, blen , slen, InfoLog);
  Result:= PChar(InfoLog);
  Dispose(InfoLog);
 end;
end;  

function LoadFragmentandVertexShader(FFilename: string; VFilename: string): GLHandleARB;
 var
  ProgramObject, FragmentShaderObject, VertexShaderObject: GLHandleARB;
  FShader: TStringList;
  FShaderText: String;
  FShaderLength: Integer;
  VShader: TStringList;
  VShaderText: String;
  VShaderLength: Integer;
begin
if FileExists(FFilename)= True then
if FileExists(VFilename)= True then
begin
 ProgramObject:= glCreateProgramObjectARB;

 FragmentShaderObject:= glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
 VertexShaderObject:= glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
 
 FShader:= TStringList.Create;
 FShader.LoadFromFile(FFilename);
 FShaderText:= FShader.Text;
 FShaderLength:= Length(FShaderText);

 VShader:= TStringList.Create;
 VShader.LoadFromFile(VFilename);
 VShaderText:= VShader.Text;
 VShaderLength:= Length(VShaderText);

 glShaderSourceARB(VertexShaderObject, 1, @VShaderText, @VShaderLength);
 glShaderSourceARB(FragmentShaderObject, 1, @FShaderText, @FShaderLength);

 glCompileShaderARB(FragmentShaderObject);
 glCompileShaderARB(VertexShaderObject);
 
 glAttachObjectARB(ProgramObject, FragmentShaderObject);
 glAttachObjectARB(ProgramObject, VertexShaderObject);
 ShowMessage(CheckForErrors(FragmentShaderObject));
 ShowMessage(CheckForErrors(VertexShaderObject)); 
 
 glDeleteObjectARB(FragmentShaderObject);
 glDeleteObjectARB(VertexShaderObject);
 glLinkProgramARB(ProgramObject);

 Result:= ProgramObject;
end
else
 ShowMessage('Fehler: Shader-Datei konnte nicht gefunden werden!')
end;

end.
