Here is my GL Shader objects unit (used with the SDL OpenGL extensions, etc.) if anyone is interested
Code:
Unit GL_Shaders;
{.......................................................}
// created by Paul Nicholls
// Copyright 2006
// Use for anything that you want, but
// please email me with any improvements that you may make :)
//
// Email:
// paul_nicholls.hotmail.com
{.......................................................}
Interface
Uses
gl,
glext;
Type
TGL_Shader = Class
Private
Protected
FHandle: GLuint;
Public
Function LoadAndCompileShaderSource(AFileName: String): Boolean; Virtual;
Function GetInfoLog: String; Virtual;
Property Handle: GLuint Read FHandle;
End;
TGL_ARB_Shader = Class(TGL_Shader)
Protected
Public
Constructor Create; Virtual;
Destructor Destroy; Override;
Function LoadAndCompileShaderSource(AFileName: String): Boolean; Override;
Function GetInfoLog: String; Override;
End;
TGL_ARB_Vertex_Shader = Class(TGL_ARB_Shader)
Private
Public
Constructor Create; Override;
Destructor Destroy; Override;
End;
TGL_ARB_Fragment_Shader = Class(TGL_ARB_Shader)
Private
Public
Constructor Create; Override;
Destructor Destroy; Override;
End;
TGL_Program = Class
Private
Protected
FHandle: GLuint;
Public
Constructor Create; Virtual;
Destructor Destroy; Override;
Procedure AttachShader(Const AShader: TGL_Shader); Virtual;
Procedure Link; Virtual;
Procedure Enable; Virtual;
Procedure Disable; Virtual;
Procedure SetParameter(Const AName: String;
Const AValue: GLint); Overload; Virtual;
Procedure SetParameter(Const AName: String;
Const AValues: Array Of GLfloat); Overload; Virtual;
End;
TGL_ARB_Program = Class(TGL_Program)
Public
Constructor Create; Override;
Destructor Destroy; Override;
Procedure AttachShader(Const AShader: TGL_Shader); Override;
Procedure Link; Override;
Procedure Enable; Override;
Procedure Disable; Override;
Procedure SetParameter(Const AName: String;
Const AValue: GLint); Overload; Override;
Procedure SetParameter(Const AName: String;
Const AValues: Array Of GLfloat); Overload; Override;
End;
Implementation
Uses
SysUtils,Classes;
Function TGL_Shader.LoadAndCompileShaderSource(AFileName: String): Boolean;
Begin
Result := False;
End;
{.......................................................}
{.......................................................}
Function TGL_Shader.GetInfoLog: String;
Begin
Result := '';
End;
{.......................................................}
{.......................................................}
Constructor TGL_ARB_Shader.Create;
Begin
Inherited Create;
FHandle := 0;
End;
{.......................................................}
{.......................................................}
Destructor TGL_ARB_Shader.Destroy;
Begin
If (FHandle <> 0) Then
glDeleteObjectARB(FHandle);
Inherited Destroy;
End;
{.......................................................}
{.......................................................}
Function TGL_ARB_Shader.LoadAndCompileShaderSource(AFileName: String): Boolean;
Var
ShaderSource : TStringList;
Source : String;
LinkStatus : GLint;
CompileStatus : GLint;
SourceLength : Integer;
Begin
Result := False;
If (Not FileExists(AFileName)) Then
Exit;
ShaderSource := TStringList.Create;
Try
ShaderSource.LoadFromFile(AFileName);
If (ShaderSource.Text = '') Then
Exit;
Source := ShaderSource.Text;
SourceLength := Length(Source);
glShaderSourceARB(FHandle,1,@Source,@SourceLength);
glCompileShaderARB(FHandle);
glGetObjectParameterivARB(FHandle,GL_OBJECT_LINK_STATUS_ARB , @LinkStatus);
glGetObjectParameterivARB(FHandle,GL_OBJECT_COMPILE_STATUS_ARB, @CompileStatus);
Result := (LinkStatus = 1) And (CompileStatus = 1);
Finally
ShaderSource.Free;
End;
End;
{.......................................................}
{.......................................................}
Function TGL_ARB_Shader.GetInfoLog: String;
Var
InfoLog : PGLcharARB;
InfoLogLength : GLint;
Begin
glGetObjectParameterivARB(FHandle,GL_OBJECT_INFO_LOG_LENGTH_ARB,@InfoLogLength);
GetMem(InfoLog,InfoLogLength + 1);
glGetInfoLogARB(FHandle,InfoLogLength,@InfoLogLength,InfoLog);
Result := String(InfoLog);
FreeMem(InfoLog,InfoLogLength + 1);
End;
{.......................................................}
{.......................................................}
Constructor TGL_ARB_Vertex_Shader.Create;
Begin
Inherited Create;
FHandle := glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
End;
{.......................................................}
{.......................................................}
Destructor TGL_ARB_Vertex_Shader.Destroy;
Begin
Inherited Destroy;
End;
{.......................................................}
{.......................................................}
Constructor TGL_ARB_Fragment_Shader.Create;
Begin
Inherited Create;
FHandle := glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
End;
{.......................................................}
{.......................................................}
Destructor TGL_ARB_Fragment_Shader.Destroy;
Begin
Inherited Destroy;
End;
{.......................................................}
{.......................................................}
Constructor TGL_Program.Create;
Begin
Inherited Create;
FHandle := 0;
End;
{.......................................................}
{.......................................................}
Destructor TGL_Program.Destroy;
Begin
If (FHandle <> 0) Then
glDeleteObjectARB(FHandle);
Inherited Destroy;
End;
{.......................................................}
{.......................................................}
Procedure TGL_Program.AttachShader(Const AShader: TGL_Shader);
Begin
End;
{.......................................................}
{.......................................................}
Procedure TGL_Program.Link;
Begin
End;
{.......................................................}
{.......................................................}
Procedure TGL_Program.Enable;
Begin
End;
{.......................................................}
{.......................................................}
Procedure TGL_Program.Disable;
Begin
End;
{.......................................................}
{.......................................................}
Procedure TGL_Program.SetParameter(Const AName: String;
Const AValue: GLint);
Begin
End;
{.......................................................}
{.......................................................}
Procedure TGL_Program.SetParameter(Const AName: String;
Const AValues: Array Of GLfloat);
Begin
End;
{.......................................................}
{.......................................................}
Constructor TGL_ARB_Program.Create;
Begin
Inherited Create;
FHandle := glCreateprogramObjectARB;
End;
{.......................................................}
{.......................................................}
Destructor TGL_ARB_Program.Destroy;
Begin
Inherited Destroy;
End;
{.......................................................}
{.......................................................}
Procedure TGL_ARB_Program.AttachShader(Const AShader: TGL_Shader);
Begin
glAttachObjectARB(FHandle,AShader.Handle);
End;
{.......................................................}
{.......................................................}
Procedure TGL_ARB_Program.Link;
Begin
glLinkProgramARB(FHandle);
End;
{.......................................................}
{.......................................................}
Procedure TGL_ARB_Program.Enable;
Begin
glUseProgramObjectARB(FHandle);
End;
{.......................................................}
{.......................................................}
Procedure TGL_ARB_Program.Disable;
Begin
glUseProgramObjectARB(0);
End;
{.......................................................}
{.......................................................}
Procedure TGL_ARB_Program.SetParameter(Const AName: String;
Const AValue: GLint);
Begin
glUniform1iARB(glGetUniformLocationARB(FHandle,PGLCharArb(AName)),
AValue);
End;
{.......................................................}
{.......................................................}
Procedure TGL_ARB_Program.SetParameter(Const AName: String;
Const AValues: Array Of GLfloat);
Var
ParamCount: Integer;
NameArb : PGLCharArb;
Begin
ParamCount := Length(AValues);
NameArb := PGLCharArb(AName);
Case ParamCount Of
1: glUniform1fARB(glGetUniformLocationARB(FHandle, NameArb),
AValues[0]);
2: glUniform2fARB(glGetUniformLocationARB(FHandle, NameArb),
AValues[0],AValues[1]);
3: glUniform3fARB(glGetUniformLocationARB(FHandle, NameArb),
AValues[0],AValues[1],AValues[2]);
4: glUniform4fARB(glGetUniformLocationARB(FHandle, NameArb),
AValues[0],AValues[1],AValues[2],AValues[3]);
Else
End;
End;
{.......................................................}
{.......................................................}
End.
Bookmarks