BGRABitmap and OpenGL

From Lazarus wiki
Revision as of 10:43, 21 May 2018 by Circular (talk | contribs) (Created page with "BGRABitmap allows to draw with OpenGL and so to benefit from hardware acceleration. === OpenGL surface === You will need an OpenGL surface. The package LazOpenGLContext in t...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

BGRABitmap allows to draw with OpenGL and so to benefit from hardware acceleration.

OpenGL surface

You will need an OpenGL surface. The package LazOpenGLContext in the directory components\opengl of lazarus supplies this surface. It is necessary to add some code to handle it properly.

The installation of the package on Linux can be problematic. See below before installing the package.

TBGLVirtualScreen

The package BGLControls in the archive of BGRABitmap conains the component TBGLVirtualScreen that supplies an easy-to-use OpenGL surface. It is recommended to install it as well.

It appears in the component toolbar, in the OpenGL tab. You just need to put it on the form and to redefine the Redraw event. For example to draw a red rectangle:

uses BGRABitmapTypes;

procedure TForm1.BGLVirtualScreen1Redraw(Sender: TObject;
  BGLContext: TBGLContext);
begin
  BGLContext.Canvas.FillRect(10,10,100,100, CSSRed);
end;

Without TBGLVirtualScreen

Create the component with the following properties:

  OpenGLControl := TOpenGLControl.Create(Self);
  with OpenGLControl do
  begin
    Align := alClient;
    Parent := Self;
    OnPaint := @OpenGLControlPaint;
    //If you dont do it,you will have some problems
    AutoResizeViewport := True;
  end;

In the Paint event:

procedure TForm1.OpenGLControlPaint(Sender: TObject);
var
  mousePos: TPoint;
begin
  BGLViewPort(OpenGLControl.Width, OpenGLControl.Height, BGRAWhite);

  //do you drawing here
  BGLCanvas.FillRect(10,10,100,100, CSSRed);

  OpenGLControl.SwapBuffers;
end;

Note on Linux

On Linux, it is possible that the OpenGL library be missing, which prevents the final compilation linking step. To solve the problem, do the following:Pour résoudre le problème, effectuez:

 #install library
 sudo apt-get install libgl-dev 
 #in some cases, add a link to the library (for 64bits processors)
 sudo ln -s /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1 /usr/lib/libGL.so

When recompiling Lazarus, the program may be stuck on the slashscreen. In that case, terminate the process with the system monitor.

Textures

OpenGL uses its own memory to store images. Moreover it is necessary to be in the right OpenGL context. So load images within the OnPaint event or use LoadTextures and UnloadTextures events of TBGLVirtualScreen. You can also use the fonction UseContext.

Textures are stored in a IBGLTexture variable. You can load a texture directly from a file:

var tex: IBGLTexture;

tex := BGLTexture(path);

To free it, simply do:

tex := nil;

Creating textures

Instead of using TBGRABitmap class, use TBGLBitmap class of BGRAOpenGL unit. It is similar in all respect except that it has a Texture property that can be used with OpenGL functions.

Finally, if you have finished your drawing and will not modify it anymore, you can free the TBGLBitmap object and retrieve the texture with MakeTextureAndFree function.

var bmp: TBGLBitmap; tex: IBGLTexture;

  bmp := TBGLBitmap.Create(path);
  bmp.Rectangle(0,0,bmp.Width,bmp.Height,CSSRed,dmSet);
  tex:=bmp.MakeTextureAndFree;