Sign in Join
Wiki copytemporary

BGRABitmap and OpenGL: what changed

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

Title: BGRABitmap and OpenGL

OldNewLine
1+[[BGRABitmap]] allows to draw with [[OpenGL]] and so to benefit from hardware acceleration.
2+
3+### OpenGL surface
4+
5+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.
6+
7+The installation of the package on Linux can be problematic. See below the Linux note before installing the package.
8+
9+#### TBGLVirtualScreen
10+
11+The 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.
12+
13+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:
14+
15+``` pascal
16+uses
17+ BGRABitmapTypes;
18+
19+procedure TForm1.BGLVirtualScreen1Redraw(Sender: TObject; BGLContext: TBGLContext);
20+begin
21+ BGLContext.Canvas.FillRect(10, 10, 100, 100, CSSRed);
22+end;
23+```
24+
25+#### Using only TOpenGLControl
26+
27+In the tab OpenGL you will find TOpenGLControl. Add it to the form and set the property `AutoResizeViewPort` to **`true`**.
28+
29+In the Paint event:
30+
31+``` pascal
32+uses BGRAOpenGL, BGRABitmapTypes;
33+
34+procedure TForm1.OpenGLControl1Paint(Sender: TObject);
35+begin
36+ BGLViewPort(OpenGLControl1.Width, OpenGLControl1.Height, BGRAWhite);
37+
38+ // do you drawing here
39+ BGLCanvas.FillRect(10, 10, 100, 100, CSSRed);
40+
41+ OpenGLControl1.SwapBuffers;
42+end;
43+```
44+
45+You will get:
46+
47+<figure>
48+<img src="bgrabitmap-openglcontrol-example1.png" title="bgrabitmap-openglcontrol-example1.png" />
49+<figcaption>bgrabitmap-openglcontrol-example1.png</figcaption>
50+</figure>
51+
52+#### Note on Linux
53+
54+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:
55+
56+``` bash
57+ #install library
58+ sudo apt-get install libgl-dev
59+ #in some cases, add a link to the library (for 64bits processors)
60+ sudo ln -s /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1 /usr/lib/libGL.so
61+ sudo ln -s /usr/lib/x86_64-linux-gnu/libglib-2.0.so /usr/lib/libglib-2.0.so
62+ sudo ln -s /usr/lib/x86_64-linux-gnu/llibgthread-2.0.so /usr/lib/libgthread-2.0.so
63+ sudo ln -s /usr/lib/x86_64-linux-gnu/libgmodule-2.0.so /usr/lib/libgmodule-2.0.so
64+ sudo ln -s /usr/lib/x86_64-linux-gnu/libgobject-2.0.so /usr/lib/libgobject-2.0.so
65+```
66+
67+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.
68+
69+When recompiling Lazarus, the program may be stuck on the slashscreen. In that case, terminate the process with the system monitor.
70+
71+### Textures
72+
73+[[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.
74+
75+Textures are stored in a IBGLTexture variable. You can load a texture directly from a file:
76+
77+``` pascal
78+uses
79+ BGRAOpenGL;
80+
81+var
82+ tex: IBGLTexture;
83+
84+begin
85+ tex := BGLTexture(path);
86+ ...
87+```
88+
89+To free it, simply do:
90+
91+``` pascal
92+tex := nil;
93+```
94+
95+### Creating textures
96+
97+Instead 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.
98+
99+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.
100+
101+``` pascal
102+uses
103+ BGRAOpenGL, BGRABitmapTypes;
104+
105+var
106+ bmp: TBGLBitmap;
107+ tex: IBGLTexture;
108+
109+begin
110+ bmp := TBGLBitmap.Create(path);
111+ bmp.Rectangle(0, 0, bmp.Width, bmp.Height, CSSRed, dmSet);
112+ tex := bmp.MakeTextureAndFree;
113+ ...
114+```
115+
116+### Examples
117+
118+You will find examples in the directory test/test4lcl_opengl[1](https://github.com/bgrabitmap/bgrabitmap/tree/master/test/test4lcl_opengl).
119+
120+#### Probability
121+
122+There is also a demo called probability[2](https://github.com/bgrabitmap/bgracontest/tree/master/2015/probability) in BGRAContext 2015 repository.
123+
124+<figure>
125+<img src="probability-screenshot.png" title="probability-screenshot.png" />
126+<figcaption>probability-screenshot.png</figcaption>
127+</figure>
128+
129+---
130+*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