- unit frmMain;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, DGLOpenGL, StdCtrls, ExtCtrls, OpenGLTools, glBitmap, libPNG;
- type
- TTGAHEADER = packed record
- tfType : Byte;
- tfColorMapType : Byte;
- tfImageType : Byte;
- tfColorMapSpec : Array[0..4] of Byte;
- tfOrigX : Word; //Array [0..1] of Byte;
- tfOrigY : Word;
- tfWidth : Word;
- tfHeight : Word;
- tfBpp : Byte;
- tfImageDes : Byte;
- end;
- type TRGBAColor = Array[0..3] of Single;
- type
- TForm1 = class(TForm)
- btnRenderToTga: TButton;
- btnRender: TButton;
- Image1: TImage;
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure btnRenderToTgaClick(Sender: TObject);
- procedure btnRenderClick(Sender: TObject);
- private
- procedure SetupGL;
- procedure Render;
- { Private-Deklarationen }
- public
- { Public-Deklarationen }
- end;
- var
- Form1: TForm1;
- DC, RC:HDC;
- FontLists: Cardinal;
- Font: TFont;
- const NearClipping = 1;
- FarClipping = 1000;
- screenWidth = 1920;
- screenHeight = 1080;
- implementation
- uses StdVCL, frmDC;
- {$R *.dfm}
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- DoubleBuffered := True;
- DCForm := TDCForm.Create(Form1);
- DCForm.Width := 1920;
- DCForm.Height := 1080;
- DCForm.Visible := false;
- DC:= GetDC(DCForm.Handle);
- if not InitOpenGL then Application.Terminate;
- RC:= CreateRenderingContext( DC,
- [opDoubleBuffered],
- 32,
- 24,
- 0,0,0,
- 0);
- ActivateRenderingContext(DC, RC);
- SetupGL;
- end;
- procedure TForm1.SetupGL;
- begin
- //Texture wird vorher gebunden
- glDisable(GL_DEPTH_TEST);
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA,GL_ONE);
- //Render...
- glDisable(GL_BLEND);
- glEnable(GL_DEPTH_TEST);
- end;
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- DeactivateRenderingContext;
- DestroyRenderingContext(RC);
- ReleaseDC(Handle, DC);
- end;
- procedure TForm1.Render;
- var
- bmp: Tbitmap;
- begin
- SetupGL;
- glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glViewport(0, 0, screenWidth, screenHeight);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glOrtho(0, screenWidth, 0, screenHeight, 1000, -1000); // Origin in lower-left corner
- //blaues 4-eck
- glBegin(GL_QUADS);
- glColor3f(0, 0, 1); glVertex2f(800, 100);
- glColor3f(0, 0, 1); glVertex2f(1000, 100);
- glColor3f(0, 0, 1); glVertex2f(1000, 300);
- glColor3f(0, 0, 1); glVertex2f(800, 300);
- glEnd;
- {bunte box mit verschiedenen transparenzen}
- glBegin(GL_QUADS);
- glColor4f(0, 1, 1, 1); glVertex2f(600, 500);
- glColor4f(1, 0, 1, 0.75); glVertex2f(800, 500);
- glColor4f(1, 1, 0, 0.5); glVertex2f(800, 800);
- glColor4f(1, 0, 0, 0); glVertex2f(600, 800);
- glEnd;
- glDisable(GL_BLEND);
- glEnable(GL_DEPTH_TEST);
- SwapBuffers(DC);
- bmp := TBitmap.Create;
- try
- GetBitmapFromOpenGL(bmp);
- Image1.Picture.Assign(bmp);
- finally
- FreeAndNil(bmp);
- end;
- end;
- procedure ScreenShot(const Name : string);
- var
- DataBuffer : array of Byte;
- f : file;
- tgaHeader : TTGAHEADER;
- width, height : integer;
- DataSize:Integer;
- viewport : Array[0..3] of integer;
- begin
- //Viewport-Größe lesen
- glGetIntegerv(GL_VIEWPORT, @viewport);
- width := viewport[2];
- height := viewport[3];
- //Größe der Daten berechnen
- DataSize := Width * Height * 4;
- //Größe des Puffers festlegen (Speicher reservieren)
- SetLength(DataBuffer,DataSize);
- // TGA Kopf mit Daten füllen
- ZeroMemory(@tgaHeader, SizeOf(tgaHeader));
- tgaHeader.tfImageType := 2; // TGA_RGB = 2
- tgaHeader.tfWidth := Width;
- tgaHeader.tfHeight := Height;
- tgaHeader.tfBpp := 32;
- //Daten von OpenGL anfordern
- glReadPixels(0,0,Width, Height, GL_BGRA, GL_UNSIGNED_BYTE, @DataBuffer[0]);
- //Datei erstellen
- AssignFile(f, Name);
- Rewrite( f,1 );
- try
- // TGA Kopf in die Datei reinschreiben
- BlockWrite(F, tgaHeader, SizeOf(tgaHeader));
- // Die eigentlichen Bilddaten in die Datei schreiben
- BlockWrite(f, DataBuffer[0], DataSize );
- finally
- CloseFile(f);
- end;
- end;
- procedure TForm1.btnRenderToTgaClick(Sender: TObject);
- begin
- ScreenShot('test.tga');
- end;
- procedure TForm1.btnRenderClick(Sender: TObject);
- begin
- Render;
- end;
- end.