BGRABitmap and OpenGL

From Free Pascal wiki
Jump to navigationJump to search

English (en) français (fr)

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 control TBGLVirtualScreen is here to make it easy for you. Note that it relies on LazOpenGLContext so you will need to install this package anyway.

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

TBGLVirtualScreen

The package BGLControls in the archive of BGRABitmap contains the component TBGLVirtualScreen that supplies an easy-to-use OpenGL surface via TBGLContext class. 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;

Using only TOpenGLControl

In the tab OpenGL you will find TOpenGLControl. Add it to the form and set the property AutoResizeViewPort to true.

In the Paint event:

uses BGRAOpenGL, BGRABitmapTypes;

procedure TForm1.OpenGLControl1Paint(Sender: TObject);
begin
  BGLViewPort(OpenGLControl1.Width, OpenGLControl1.Height, BGRAWhite);

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

  OpenGLControl1.SwapBuffers;
end;

You will get:

bgrabitmap-openglcontrol-example1.png

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
  sudo ln -s /usr/lib/x86_64-linux-gnu/libglib-2.0.so /usr/lib/libglib-2.0.so
  sudo ln -s /usr/lib/x86_64-linux-gnu/llibgthread-2.0.so /usr/lib/libgthread-2.0.so
  sudo ln -s /usr/lib/x86_64-linux-gnu/libgmodule-2.0.so /usr/lib/libgmodule-2.0.so
  sudo ln -s /usr/lib/x86_64-linux-gnu/libgobject-2.0.so /usr/lib/libgobject-2.0.so

After checking the package to install, clicking Yes directly may not work. Instead you can go to Tools and configure Lazarus build. Check the option to do a full clean compilation. From there compile the IDE.

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:

uses 
  BGRAOpenGL;

var 
  tex: IBGLTexture;

begin
  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.

uses 
  BGRAOpenGL, BGRABitmapTypes;

var 
  bmp: TBGLBitmap; 
  tex: IBGLTexture;

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

Examples

You will find examples in the directory test/test4lcl_opengl[1].

Probability

There is also a demo called probability[2] in BGRAContext 2015 repository.

probability-screenshot.png