Sign in Join
Wiki copytemporary

BGRABitmap tutorial 10: what changed

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

OldNewLine
11This tutorial shows how to use texture mapping. A sample project can be found in [[BGRABitmap AggPas]].
22
33### Create a new project
44
55Create a new project and add a reference to [[BGRABitmap]], the same way as in [[BGRABitmap tutorial|the first tutorial]].
66
77### Using no particular mapping
88
99Let's see what happens if we draw a polygon with a texture using default mapping:
1010
1111``` pascal
1212procedure TForm1.FormPaint(Sender: TObject);
1313var image: TBGRABitmap;
1414 tex: TBGRABitmap;
1515begin
1616 //black background
1717 image := TBGRABitmap.Create(ClientWidth,ClientHeight, BGRABlack );
1818
1919 tex:= TBGRABitmap.Create('image.png'); //load a bitmap
2020 image.FillPolyAntialias( [PointF(110,10), PointF(250,10), PointF(350,160), PointF(10,160)], tex);
2121 tex.Free;
2222
2323 image.Draw(Canvas,0,0,True); //draw on the screen
2424 image.free;
2525end;
2626```
2727
2828### Run the program
2929
3030You should obtain something like this:
3131
32-<figure>
33-<img src="BGRATutorial10a.png" title="BGRATutorial10a.png" />
34-<figcaption>BGRATutorial10a.png</figcaption>
35-</figure>
32+![](/wiki-images/7/7e/BGRATutorial10a.png)
3633
3734As you can see the image is not deformed.
3835
3936### Affine transformation
4037
4138We can apply an affine transformation like this:
4239
4340``` pascal
4441uses BGRABitmap, BGRABitmapTypes, BGRATransform;
4542
4643procedure TForm1.PaintImage;
4744var image: TBGRABitmap;
4845 tex: TBGRABitmap;
4946 affine: TBGRAAffineBitmapTransform;
5047
5148begin
5249 //black background
5350 image := TBGRABitmap.Create(ClientWidth,ClientHeight, BGRABlack );
5451
5552 tex:= TBGRABitmap.Create('image.png'); //load a bitmap
5653
5754 //create a rotation of 45°
5855 affine := TBGRAAffineBitmapTransform.Create(tex,True);
5956 affine.RotateDeg(45);
6057
6158 //use this transformation as parameter instead of tex
6259 image.FillPolyAntialias( [PointF(110,10), PointF(250,10), PointF(350,160), PointF(10,160)], affine);
6360
6461 affine.Free;
6562 tex.Free;
6663
6764 image.Draw(Canvas,0,0,True); //draw on the screen
6865 image.free;
6966end;
7067```
7168
7269### Run the program
7370
7471You should obtain a rotated picture in the polygon:
7572
76-<figure>
77-<img src="BGRATutorial10b.png" title="BGRATutorial10b.png" />
78-<figcaption>BGRATutorial10b.png</figcaption>
79-</figure>
73+![](/wiki-images/2/2a/BGRATutorial10b.png)
8074
8175### Texture mapping
8276
8377Now, if we want the texture to be aligned with the polygon border, we can use texture mapping.
8478
8579#### Linear mapping
8680
8781Linear mapping stretched the image linearly along the borders. To do this:
8882
8983``` pascal
9084procedure TForm1.PaintImage;
9185var image: TBGRABitmap;
9286 tex: TBGRABitmap;
9387begin
9488 image := TBGRABitmap.Create(ClientWidth,ClientHeight, BGRABlack );
9589
9690 tex:= TBGRABitmap.Create('image.png');
9791 image.FillPolyLinearMapping( [PointF(110,10), PointF(250,10), PointF(350,160), PointF(10,160)], tex,
9892 [PointF(0,0), PointF(tex.width-1,0), PointF(tex.Width-1,tex.Height-1), PointF(0,tex.Height-1)], true);
9993 tex.Free;
10094
10195 image.Draw(Canvas,0,0,True);
10296 image.free;
10397end;
10498```
10599
106100To do the mapping, we use FillPolyLinearMapping. Some new parameters appear. Texture coordinates define, for each point of the polygon, the location in the texture. Interpolation option is used for best quality.
107101
108102#### Run the program
109103
110104Now the texture is deformed according to the polygonal shape.
111105
112-<figure>
113-<img src="BGRATutorial10c.png" title="BGRATutorial10c.png" />
114-<figcaption>BGRATutorial10c.png</figcaption>
115-</figure>
106+![](/wiki-images/f/f3/BGRATutorial10c.png)
116107
117108#### Perspective mapping
118109
119110The perspective mapping allow to change the depth of each point.
120111
121112``` pascal
122113procedure TForm1.PaintImage;
123114var image: TBGRABitmap;
124115 tex: TBGRABitmap;
125116begin
126117 image := TBGRABitmap.Create(ClientWidth,ClientHeight, BGRABlack );
127118
128119 tex:= TBGRABitmap.Create('image.png');
129120 image.FillPolyPerspectiveMapping( [PointF(110,10), PointF(250,10), PointF(350,160), PointF(10,160)],
130121 [75, 75, 50, 50],
131122 tex, [PointF(0,0), PointF(tex.width-1,0), PointF(tex.Width-1,tex.Height-1), PointF(0,tex.Height-1)], true);
132123 tex.Free;
133124
134125 image.Draw(Canvas,0,0,True);
135126 image.free;
136127end;
137128```
138129
139130Here the depth is 75 for the top of the polygon and 50 for the bottom of the polygon. It means that the bottom is closer to the observer, as if it were horizontal, like a floor.
140131
141132#### Run the program
142133
143134Now it seems that it is a 3D polygon:
144135
145-<figure>
146-<img src="BGRATutorial10d.png" title="BGRATutorial10d.png" />
147-<figcaption>BGRATutorial10d.png</figcaption>
148-</figure>
136+![](/wiki-images/6/67/BGRATutorial10d.png)
149137
150138### Conclusion
151139
152140Using these techniques, it is possible to deform an image, like in LazPaint tool "grid deformation", or to render 3D objects with textures, like in tests 19-21 of testbgrafunc (also in LazPaint archive).
153141
154142[[BGRABitmap tutorial 9|Previous tutorial (phong shading)]] [[BGRABitmap tutorial 11|Next tutorial (combining transformations)]]
155143
156144---
157145*From the Free Pascal wiki, page "BGRABitmap tutorial 10", as it stood on 2023-05-20 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).*
158146Category: BGRABitmap
159147Category: Graphics

Back to history