unit OpenGLPanel;
{$mode Delphi}
interface
uses
Classes, SysUtils, Controls, Graphics, GR32, XVector, XMatrix, dglOpenGL, OpenGLContext;
type
TOpenGLPanel = class(TCustomControl)
strict private
var
FContext : TOpenGLContext;
FGlColor : TXVector;
FGlMatrix : TXMatrix;
protected
procedure SetColor(Value: TColor); override;
procedure Paint; override;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
property Context: TOpenGLContext read FContext;
property Matrix : TXMatrix read FGlMatrix;
end;
implementation
constructor TOpenGLPanel.Create;
var i: GLint;
begin
inherited Create(TheOwner);
Parent := TheOwner as TWinControl;
Visible := True;
DoubleBuffered := False; // True = does not render
BringToFront;
FContext := TOpenGLContext.CreateCore3D(Handle, False, True);
FGlMatrix := TXMatrix.CreateOrtho2D(0, Width, Height, 0);
FContext.EnableDebugToStdOut;
end;
destructor TOpenGLPanel.Destroy;
begin
FContext.Free;
inherited;
end;
procedure TOpenGLPanel.Paint;
begin
FContext.Activate;
FGlMatrix:= TXMatrix.CreateOrtho2D(0, Width, Height, 0);
glFrontFace(GL_CCW); // Y axis is flipped
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glViewport(0, 0, Width, Height);
glClearColor(FGlColor.X, FGlColor.Y, FGlColor.Z, FGlColor.W);
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT or GL_STENCIL_BUFFER_BIT);
inherited;
FContext.SwapBuffer;
end;
procedure TOpenGLPanel.SetColor;
var c: TColor32Entry;
begin
c := TColor32Entry(Color32(Value));
FGlColor:= TXVector.Create(c.R, c.G, c.B, c.A) / TXVector.Create(255.0);
inherited;
end;
end.