Thema: Delphi OpenGL Grundgerüst

Einzelnen Beitrag anzeigen

Benutzerbild von sniper_w
sniper_w

Registriert seit: 12. Dez 2004
Ort: Wien, Österriech
893 Beiträge
 
Delphi 6 Enterprise
 
#3

Re: OpenGL Grundgerüst

  Alt 30. Mär 2006, 15:14
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, dglOpenGl, glBitmap, ExtCtrls;

type
  TglPanel = class(TPanel)
    private
      FOnPaintEvent:TNotifyEvent;
    protected
      procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
      procedure WMEraseBkgnd(var Message: TWmEraseBkgnd); message WM_ERASEBKGND;
    published
      property OnPaint:TNotifyEvent read FOnPaintEvent write FOnPaintEvent;
  end;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormResize(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    myRc, myDc : Cardinal;
    GL_Texture:TglBitmap2D;
    Panel: TglPanel;
    procedure InitGl();
    procedure DeInitGl();
    procedure glRender();
  end;

var
  Form1: TForm1;
  e:TWinControl;

implementation

{$R *.dfm}

procedure TForm1.DeInitGl;
begin//
  DeactivateRenderingContext();
  DestroyRenderingContext(myRc);
  ReleaseDC(Panel.Handle,myDc);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin//
  OnResize := nil;
  Panel := TglPanel.Create(Self);
  with Panel do
  begin
    Caption := '';
    Align := alClient;
    OnPaint := FormPaint;
    Parent := Self;
  end;

  InitGl();
  Caption := 'OpenGL VCL Template by sniper (haris_201@hotmail.com)';
  KeyPreview := True;

  GL_Texture := TglBitmap2D.Create();
  GL_Texture.BuildMipMaps := False;
  GL_Texture.LoadFromFile('Back.bmp');
  GL_Texture.FreeDataAfterGenTexture := True;
  GL_Texture.GenTexture();

  OnResize := FormResize;
  Resize();
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin//
  GL_Texture.Free();
  DeInitGl();
end;

procedure TForm1.InitGl;
begin//
 InitOpenGL();
 myDc := GetDC(Panel.Handle);
 myRc := CreateRenderingContext(myDc,[opDoubleBuffered],24,16,8,0,0,0);
 ActivateRenderingContext(myDc,myRc);
 glClearColor(0, 0,0, 0);
 glShadeModel(GL_SMOOTH);
 glClearDepth(1.5);
 glEnable(GL_DEPTH_TEST);
 glDepthFunc (GL_LEQUAL);
 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
 glEnable(GL_TEXTURE_2D);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  // TODO : Draw Hier
  glRender();
  // End Drawing
  SwapBuffers(myDc);
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key=27 then Close();
end;

procedure TForm1.FormResize(Sender: TObject);
begin//
  if (ClientHeight=0) then
  begin
    ClientHeight := 1;
  end;
  glViewport(0, 0, ClientWidth, ClientHeight);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  // for 2d
  glOrtho(0, ClientWidth, ClientHeight,0, -10,10);
  // for 3d
  //gluPerspective(45, Width/Height, 0.1, 1000);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  FormPaint(nil);
end;

procedure TForm1.glRender();
begin
  GL_Texture.Bind();

  glLoadIdentity();
  glTranslatef( ClientWidth/2 - GL_Texture.Width/2, ClientHeight/2 - GL_Texture.Height/2, 0 );
  glScalef(GL_Texture.Width, GL_Texture.Height, 1);
  glBegin(GL_QUADS);
  glTexCoord2f(0,0);
  glVertex2f(0,0);
  glTexCoord2f(1,0);
  glVertex2f(1,0);
  glTexCoord2f(1,1);
  glVertex2f(1,1);
  glTexCoord2f(0,1);
  glVertex2f(0,1);
  glEnd();
end;

{ TglPanel }

procedure TglPanel.WMEraseBkgnd(var Message: TWmEraseBkgnd);
begin
  Message.Result := 1;
end;

procedure TglPanel.WMPaint(var Message: TWMPaint);
var
  PS:TPaintStruct;
begin
  BeginPaint( Handle, PS);
  if Assigned(FOnPaintEvent) then
    FOnPaintEvent(Self);
  EndPaint( Handle, PS);
  Message.Result := 0;
end;

end.
Zum Kompilieren benötigte Quellen:dglOpenGl.pasglBitmap.pas
Katura Haris
Es (ein gutes Wort) ist wie ein guter Baum, dessen Wurzel fest ist und dessen Zweige in den Himmel reichen.
  Mit Zitat antworten Zitat