Difference between revisions of "BGRABitmap and OpenGL"

From Lazarus wiki
Jump to navigationJump to search
m (fix typo)
(8 intermediate revisions by 3 users not shown)
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]] contains 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:
  
<syntaxhighlight>uses  
+
<syntaxhighlight lang="pascal">
 +
uses  
 
   BGRABitmapTypes;
 
   BGRABitmapTypes;
  
Line 23: Line 24:
 
end;</syntaxhighlight>
 
end;</syntaxhighlight>
  
==== Without TBGLVirtualScreen ====
+
==== Using only TOpenGLControl ====
  
Create the component with the following properties:
+
In the tab OpenGL you will find TOpenGLControl. Add it to the form and set the property <tt>AutoResizeViewPort</tt> to <tt>'''true'''</tt>.
<syntaxhighlight> OpenGLControl := TOpenGLControl.Create(Self);
 
  with OpenGLControl do
 
  begin
 
    Align := alClient;
 
    Parent := Self;
 
    OnPaint := @OpenGLControlPaint;
 
    AutoResizeViewport := True;  //ensure correct proportions
 
  end; </syntaxhighlight>
 
  
 
In the Paint event:
 
In the Paint event:
<syntaxhighlight>procedure TForm1.OpenGLControlPaint(Sender: TObject);
+
 
var
+
<syntaxhighlight lang="pascal">
  mousePos: TPoint;
+
uses BGRAOpenGL, BGRABitmapTypes;
 +
 
 +
procedure TForm1.OpenGLControl1Paint(Sender: TObject);
 
begin
 
begin
   BGLViewPort(OpenGLControl.Width, OpenGLControl.Height, BGRAWhite);
+
   BGLViewPort(OpenGLControl1.Width, OpenGLControl1.Height, BGRAWhite);
  
 
   // do you drawing here
 
   // do you drawing here
 
   BGLCanvas.FillRect(10, 10, 100, 100, CSSRed);
 
   BGLCanvas.FillRect(10, 10, 100, 100, CSSRed);
  
   OpenGLControl.SwapBuffers;
+
   OpenGLControl1.SwapBuffers;
end;</syntaxhighlight>
+
end;
 +
</syntaxhighlight>
 +
 
 +
You will get:
 +
 
 +
[[File:bgrabitmap-openglcontrol-example1.png]]
  
 
==== Note on Linux ====
 
==== Note on Linux ====
Line 56: Line 56:
 
   #in some cases, add a link to the library (for 64bits processors)
 
   #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/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
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
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.
 
When recompiling Lazarus, the program may be stuck on the slashscreen. In that case, terminate the process with the system monitor.
Line 65: Line 71:
  
 
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:
<syntaxhighlight>uses  
+
 
 +
<syntaxhighlight lang="pascal">uses  
 
   BGRAOpenGL;
 
   BGRAOpenGL;
  
Line 76: Line 83:
  
 
To free it, simply do:
 
To free it, simply do:
<syntaxhighlight>tex := nil;</syntaxhighlight>
+
 
 +
<syntaxhighlight lang="pascal">tex := nil;</syntaxhighlight>
  
 
=== Creating textures ===
 
=== Creating textures ===
Line 84: Line 92:
 
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.
 
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.
  
<syntaxhighlight>uses  
+
<syntaxhighlight lang="pascal">
 +
uses  
 
   BGRAOpenGL, BGRABitmapTypes;
 
   BGRAOpenGL, BGRABitmapTypes;
  
Line 100: Line 109:
  
 
You will find examples in the directory test/test4lcl_opengl[https://github.com/bgrabitmap/bgrabitmap/tree/master/test/test4lcl_opengl].  
 
You will find examples in the directory test/test4lcl_opengl[https://github.com/bgrabitmap/bgrabitmap/tree/master/test/test4lcl_opengl].  
 +
 +
==== Probability ====
  
 
There is also a demo called probability[https://github.com/bgrabitmap/bgracontest/tree/master/2015/probability] in BGRAContext 2015 repository.
 
There is also a demo called probability[https://github.com/bgrabitmap/bgracontest/tree/master/2015/probability] in BGRAContext 2015 repository.
 +
 +
[[File:probability-screenshot.png]]

Revision as of 14:17, 30 July 2023

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. 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[1]. 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[2].

Probability

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

probability-screenshot.png