Difference between revisions of "BGRABitmap and OpenGL"

From Lazarus wiki
Jump to navigationJump to search
m (wiki)
m (wiki)
Line 1: Line 1:
 
{{BGRABitmap and OpenGL}}
 
{{BGRABitmap and OpenGL}}
  
[[BGRABitmap]] allows to draw with OpenGL and so to benefit from hardware acceleration.
+
[[BGRABitmap]] allows to draw with [[OpenGL]] and so to benefit from hardware acceleration.
  
 
=== OpenGL surface ===
 
=== OpenGL surface ===
Line 11: Line 11:
 
==== TBGLVirtualScreen ====
 
==== 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.
+
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:
 
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:
Line 50: Line 50:
 
==== Note on Linux ====
 
==== 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:
+
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:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
   #install library
 
   #install library
Line 62: Line 62:
 
=== Textures ===
 
=== 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.
+
[[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:
 
Textures are stored in a IBGLTexture variable. You can load a texture directly from a file:

Revision as of 09:58, 24 May 2018

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 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;
    AutoResizeViewport := True;  //ensure correct proportions
  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;

Examples

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

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