Sign in Join
Wiki copytemporary

BGRABitmap and OpenGL: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11[[BGRABitmap]] allows to draw with [[OpenGL]] and so to benefit from hardware acceleration.
22
33### OpenGL surface
44
55You 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.
66
77The installation of the package on Linux can be problematic. See below the Linux note before installing the package.
88
99#### TBGLVirtualScreen
1010
1111The package BGLControls in the archive of [[BGRABitmap]] contains the component TBGLVirtualScreen that supplies an easy-to-use OpenGL surface via [TBGLContext](https://bgrabitmap.github.io/doc/BGRAOpenGL.TBGLContext.html) class. It is recommended to install it as well.
1212
1313It 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:
1414
1515``` pascal
1616uses
1717 BGRABitmapTypes;
1818
1919procedure TForm1.BGLVirtualScreen1Redraw(Sender: TObject; BGLContext: TBGLContext);
2020begin
2121 BGLContext.Canvas.FillRect(10, 10, 100, 100, CSSRed);
2222end;
2323```
2424
2525#### Using only TOpenGLControl
2626
2727In the tab OpenGL you will find TOpenGLControl. Add it to the form and set the property `AutoResizeViewPort` to **`true`**.
2828
2929In the Paint event:
3030
3131``` pascal
3232uses BGRAOpenGL, BGRABitmapTypes;
3333
3434procedure TForm1.OpenGLControl1Paint(Sender: TObject);
3535begin
3636 BGLViewPort(OpenGLControl1.Width, OpenGLControl1.Height, BGRAWhite);
3737
3838 // do you drawing here
3939 BGLCanvas.FillRect(10, 10, 100, 100, CSSRed);
4040
4141 OpenGLControl1.SwapBuffers;
4242end;
4343```
4444
4545You will get:
4646
47-<figure>
48-<img src="bgrabitmap-openglcontrol-example1.png" title="bgrabitmap-openglcontrol-example1.png" />
49-<figcaption>bgrabitmap-openglcontrol-example1.png</figcaption>
50-</figure>
47+![](/wiki-images/4/41/Bgrabitmap-openglcontrol-example1.png)
5148
5249#### Note on Linux
5350
5451On 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:
5552
5653``` bash
5754 #install library
5855 sudo apt-get install libgl-dev
5956 #in some cases, add a link to the library (for 64bits processors)
6057 sudo ln -s /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1 /usr/lib/libGL.so
6158 sudo ln -s /usr/lib/x86_64-linux-gnu/libglib-2.0.so /usr/lib/libglib-2.0.so
6259 sudo ln -s /usr/lib/x86_64-linux-gnu/llibgthread-2.0.so /usr/lib/libgthread-2.0.so
6360 sudo ln -s /usr/lib/x86_64-linux-gnu/libgmodule-2.0.so /usr/lib/libgmodule-2.0.so
6461 sudo ln -s /usr/lib/x86_64-linux-gnu/libgobject-2.0.so /usr/lib/libgobject-2.0.so
6562```
6663
6764After 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.
6865
6966When recompiling Lazarus, the program may be stuck on the slashscreen. In that case, terminate the process with the system monitor.
7067
7168### Textures
7269
7370[[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.
7471
7572Textures are stored in a IBGLTexture variable. You can load a texture directly from a file:
7673
7774``` pascal
7875uses
7976 BGRAOpenGL;
8077
8178var
8279 tex: IBGLTexture;
8380
8481begin
8582 tex := BGLTexture(path);
8683 ...
8784```
8885
8986To free it, simply do:
9087
9188``` pascal
9289tex := nil;
9390```
9491
9592### Creating textures
9693
9794Instead of using TBGRABitmap class, use [TBGLBitmap class](https://bgrabitmap.github.io/doc/BGRAOpenGL.TBGLBitmap.html) of [BGRAOpenGL unit](https://bgrabitmap.github.io/doc/BGRAOpenGL.html). It is similar in all respect except that it has a Texture property that can be used with OpenGL functions.
9895
9996Finally, 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.
10097
10198``` pascal
10299uses
103100 BGRAOpenGL, BGRABitmapTypes;
104101
105102var
106103 bmp: TBGLBitmap;
107104 tex: IBGLTexture;
108105
109106begin
110107 bmp := TBGLBitmap.Create(path);
111108 bmp.Rectangle(0, 0, bmp.Width, bmp.Height, CSSRed, dmSet);
112109 tex := bmp.MakeTextureAndFree;
113110 ...
114111```
115112
116113### Examples
117114
118115You will find examples in the directory test/test4lcl_opengl[1](https://github.com/bgrabitmap/bgrabitmap/tree/master/test/test4lcl_opengl).
119116
120117#### Probability
121118
122119There is also a demo called probability[2](https://github.com/bgrabitmap/bgracontest/tree/master/2015/probability) in BGRAContext 2015 repository.
123120
124-<figure>
125-<img src="probability-screenshot.png" title="probability-screenshot.png" />
126-<figcaption>probability-screenshot.png</figcaption>
127-</figure>
121+![](/wiki-images/6/6d/Probability-screenshot.png)
128122
129123---
130124*From the Free Pascal wiki, page "BGRABitmap and OpenGL", as it stood on 2024-04-03 after a change by Circular, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history