I am trying to render text in OpenGL, this is how I do it:
read pixels to bitmap using glReadPixels and SetDIBits;
draw text on bitmap using canvas;
draw pixels to main frame buffer using GetDIBits and glDrawPixels.
This is what I get when I render Sample text (81x21).
The bitmap.
This is what I get when I render Sample text. (84x21) (with dot at the end).
It works. It always works when resulting text's width is power of two! Strange...
This is the code.
procedure TMainForm.RenderBtnClick(Sender: TObject);
var
DC, RC: HDC;
BMP: TBitmap;
Pixels: Pointer;
X, Y, W, H: Integer;
Header: PBitmapInfo;
Result, Error: Integer;
Str: String;
begin
// Initialize OpenGL
if InitOpenGL = False then
Application.Terminate;
DC := GetDC(Handle);
RC := CreateRenderingContext(DC,
[OpDoubleBuffered],
32,
24,
0,
0,
0,
0);
ActivateRenderingContext(DC, RC);
Caption :=
'OpenGL version: ' + glGetString(GL_VERSION) + ' | ' +
'vendor: ' + glGetString(GL_VENDOR) + ' | ' +
'renderer: ' + glGetString(GL_RENDERER);
// Setup OpenGL
glClearColor(0.27, 0.4, 0.7, 0.0); // Light blue
glViewport(0, 0, ClientWidth, ClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glOrtho(0, ClientWidth, 0, ClientHeight, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
glClear(GL_COLOR_BUFFER_BIT);
BMP := TBitmap.Create;
BMP.PixelFormat := pf24bit;
BMP.Canvas.Font.Name := 'Segoe UI';
BMP.Canvas.Font.Size := 12;
BMP.Canvas.Font.Color := clWhite;
BMP.Canvas.Brush.Style := bsClear;
Str := Edit.Text;
W := BMP.Canvas.TextWidth(Str);
H := BMP.Canvas.TextHeight(Str);
X := (ClientWidth - W) div 2;
Y := (ClientHeight - H) div 2;
BMP.Width := W;
BMP.Height := H;
GetMem(Pixels, W * H * 3);
GetMem(Header, SizeOf(TBitmapInfoHeader));
with Header^.bmiHeader do
begin
biSize := SizeOf(TBitmapInfoHeader);
biWidth := W;
biHeight := H;
biCompression := BI_RGB;
biPlanes := 1;
biBitCount := 24;
biSizeImage := W * H * 3;
end;
glReadPixels(X, Y, W, H, GL_BGR, GL_UNSIGNED_BYTE, Pixels);
Result := SetDIBits(BMP.Canvas.Handle, BMP.Handle, 0, H, Pixels,
TBitmapInfo(Header^), DIB_RGB_COLORS);
if Result = 0 then
begin
Error := GetLastError;
raise Exception.Create('"SetDIBits" error ' + IntToStr(Error) + ': ' + SysErrorMessage(Error));
end;
BMP.Canvas.TextOut(0, 0, Str);
BMP.SaveToFile('C:/TextOut.bmp'); // for debugging purposes of course
Result := GetDIBits(BMP.Canvas.Handle, BMP.Handle, 0, H, Pixels, TBitmapInfo(Header^), DIB_RGB_COLORS);
if Result = 0 then
begin
Error := GetLastError;
raise Exception.Create('"GetDIBits" error ' + IntToStr(Error) + ': ' + SysErrorMessage(Error));
end;
glRasterPos2i(X, Y);
glDrawPixels(W, H, GL_BGR, GL_UNSIGNED_BYTE, Pixels);
SwapBuffers(DC);
// Free memory
DeactivateRenderingContext;
wglDeleteContext(RC);
ReleaseDC(Handle, DC);
FreeMem(Header);
FreeMem(Pixels);
BMP.Free;
end;
I double checked the code with glGetError - no errors. I've seen many reports of odd behaviour with SetDIBits and its derivatives.
Some claim that the weirdness has to do with Delphi memory management,
though I have my doubts. Any ideas what I can try next?
Edit: it works if I use alpha.
You need to take the alignment into account. By default, GL expects a 4 byte alignment for each image row. Asy you use 3 bytes per pixel, can be anything, depending on the width. Have a look at glPixelStore() to change the alignmet. Especially useful should be setting GL_PACK_ALIGNMENT (for reading pixels from the GL) and GL_UNPACK_ALIGNMENT (for sending pixels to the GL) to 1 for your use case.
Related
I draw several textures on my canvas and on one of them i would like to apply a TGaussianBlurEffect. Problem is that TGaussianBlurEffect will apply to all the container in with it is placed (so all the canvas) where me i want to apply it only to one texture.
i found this function in delphi
procedure TFilterEffect.ProcessTexture(const Visual: TTexture; const Context: TContext3D);
var
Ver: TVertexBuffer;
Ind: TIndexBuffer;
Mat: TTextureMaterial;
begin
if Assigned(FFilter) then
begin
FFilter.ValuesAsTexture['Input'] := Visual;
FFilter.ApplyWithoutCopytoOutput;
if Assigned(Context) then
if Context.BeginScene then
try
Ver := TVertexBuffer.Create([TVertexFormat.Vertex, TVertexFormat.TexCoord0], 4);
Ver.Vertices[0] := Point3D(Context.PixelToPixelPolygonOffset.X,
Context.PixelToPixelPolygonOffset.Y, 0);
Ver.TexCoord0[0] := PointF(0.0, 0.0);
Ver.Vertices[1] := Point3D(Context.PixelToPixelPolygonOffset.X + Visual.Width,
Context.PixelToPixelPolygonOffset.Y, 0);
Ver.TexCoord0[1] := PointF(Visual.Width / TFilterManager.FilterTexture.Width, 0.0);
Ver.Vertices[2] := Point3D(Context.PixelToPixelPolygonOffset.X + Visual.Width,
Context.PixelToPixelPolygonOffset.Y + Visual.Height, 0);
Ver.TexCoord0[2] := PointF(Visual.Width / TFilterManager.FilterTexture.Width,
Visual.Height / TFilterManager.FilterTexture.Height);
Ver.Vertices[3] := Point3D(Context.PixelToPixelPolygonOffset.X,
Context.PixelToPixelPolygonOffset.Y + Visual.Height, 0);
Ver.TexCoord0[3] := PointF(0.0, Visual.Height / TFilterManager.FilterTexture.Height);
Ind := TIndexBuffer.Create(6);
Ind[0] := 0;
Ind[1] := 1;
Ind[2] := 3;
Ind[3] := 3;
Ind[4] := 1;
Ind[5] := 2;
Mat := TTextureMaterial.Create;
Mat.Texture := TFilterManager.FilterTexture;
Context.Clear(0);
Context.SetContextState(TContextState.cs2DScene);
Context.SetContextState(TContextState.csZWriteOff);
Context.SetContextState(TContextState.csZTestOff);
Context.SetMatrix(TMatrix3D.Identity);
Context.DrawTriangles(Ver, Ind, Mat, 1);
Mat.Free;
Ind.Free;
Ver.Free;
finally
Context.EndScene;
end;
end;
end;
but i don't understand it's purpose and how to use it :(
I want to draw a curved right arrow at TCanvas as shape in Microsoft Word.
Does anybody know an working method?
Simple method to draw complex figures. If you need antialiasing, use GDIPlus or other advanced graphic means.
procedure DrawCurveArrow(ACanvas: TCanvas; AColor: TColor;
X0, Y0, Size: Integer);
const
Magic = 0.552; // constant to approximate circular arc with Bezier curve
var
Pt: array of TPoint;
Flags: array of Byte;
R, RMag: Integer;
begin
SetLength(Pt, 18);
SetLength(Flags, 18);
R := 5 * Size div 16;
RMag := Round(R * Magic);
Pt[0] := Point(X0 + 1, Y0); // to thicken tail a bit
Flags[0] := PT_MOVETO;
Pt[1] := Point(X0 + 1, Y0 - RMag);
Flags[1] := PT_BEZIERTO;
Pt[2] := Point(X0 + R - RMag, Y0 - R);
Flags[2] := PT_BEZIERTO;
Pt[3] := Point(X0 + R, Y0 - R);
Flags[3] := PT_BEZIERTO;
Pt[4] := Point(X0 + R + RMag, Y0 - R);
Flags[4] := PT_BEZIERTO;
Pt[5] := Point(X0 + 2 * R, Y0 - RMag);
Flags[5] := PT_BEZIERTO;
Pt[6] := Point(X0 + 2 * R, Y0);
Flags[6] := PT_BEZIERTO;
Pt[7] := Point(X0 + Size div 2, Y0);
Flags[7] := PT_LINETO;
Pt[8] := Point(X0 + Size * 3 div 4, Y0 + Size div 4);
Flags[8] := PT_LINETO;
Pt[9] := Point(X0 + Size, Y0);
Flags[9] := PT_LINETO;
Pt[10] := Point(X0 + 7 * Size div 8, Y0);
Flags[10] := PT_LINETO;
R := 7 * Size div 16;
RMag := Round(R * Magic);
Pt[11] := Point(X0 + 2 * R, Y0 - RMag);
Flags[11] := PT_BEZIERTO;
Pt[12] := Point(X0 + R + RMag, Y0 - R);
Flags[12] := PT_BEZIERTO;
Pt[13] := Point(X0 + R, Y0 - R);
Flags[13] := PT_BEZIERTO;
Pt[14] := Point(X0 + R - RMag, Y0 - R);
Flags[14] := PT_BEZIERTO;
Pt[15] := Point(X0, Y0 - RMag);
Flags[15] := PT_BEZIERTO;
Pt[16] := Point(X0, Y0);
Flags[16] := PT_BEZIERTO;
Pt[17] := Point(X0 + 1, Y0);
Flags[17] := PT_LINETO or PT_CLOSEFIGURE;
BeginPath(ACanvas.Handle);
PolyDraw(ACanvas.Handle, Pt[0], Flags[0], Length(Pt));
EndPath(ACanvas.Handle);
ACanvas.Brush.Color := AColor;
FillPath(ACanvas.Handle);
end;
begin
DrawCurveArrow(Canvas, clBlue, 100, 200, 300);
Well, it seems that MBo was quicker then me and his solution is better then my. But I'll put my answer anyway. Note that it counts with white background (MBo's solution is background-independent).
procedure draw_arrow(canvas: TCanvas; x, y, size: Integer; color: TColor);
begin
with canvas do
begin
Pen.Style:=psClear;
Brush.Style:=bsSolid;
Brush.Color:=color;
Ellipse(x+1, y, x+size+1, y+size);
Brush.Color:=clWhite;
Ellipse(x, y+size div 6, x+Round(size/1.5), y+Round(size/1.2));
Rectangle(x, y+size div 2, x+size+1, y+size);
Brush.Color:=color;
Polygon([Point(x+size div 2, y+size div 2), Point(x+size div 2+Round(size/1.5), y+size div 2), Point(x+size-size div 6, y+Round(size/1.2))]);
end;
end;
I'm trying to create a function to create a TBitmap.
This bitmap will be a Glyph with Transparent background, and it will be only a character of the Wingdings font.
After this, I will use this glyph to assign to a TBitBtn (button).
This is my current code:
function CreateTransparent(aChar: Char; aFontSize, aWidth, aHeight: Integer; aColor: TColor): TBitmap;
function _GPColor(Col: TColor): TGPColor;
begin
Result := ColorRefToARGB(ColorToRGB(Col));
end;
var
f: TGPFont;
r, rTx: TGPRectF;
b: TGPSolidBrush;
c: TGPGraphics;
tx: TGPStringFormat;
bt: TGPBitmap;
h: HBITMAP;
bk, fg: Cardinal;
s: string;
attr: TGPImageAttributes;
begin
s := aChar;
fg := _GPColor(aColor);
bt := TGPBitmap.Create(abs(aWidth), aHeight, PixelFormat32bppARGB);
try
c := TGPGraphics.Create(bt);
f := TGPFont.Create('Wingdings', aFontSize, FontStyleRegular, UnitPixel);
b := TGPSolidBrush.Create( MakeColor(0, 0, 0, 0) );
tx := TGPStringFormat.Create;
try
// configura o device
tx.SetLineAlignment(StringAlignmentCenter);
r.X := 0;
r.Y := 0;
r.Width := 2000;
r.Height := aHeight;
c.MeasureString(WideString(s), -1, f, r, rTx);
if (aWidth < 0) and (rTx.Width > Abs(aWidth)) then
begin
c.Free;
bt.Free;
aWidth := Ceil(rTx.Width);
bt := TGPBitmap.Create(aWidth, aHeight, PixelFormat32bppARGB);
c := TGPGraphics.Create(bt);
end;
c.SetTextRenderingHint(TextRenderingHintAntiAlias);
// inicializa as variáveis
r.X := 0;
r.Y := 0;
r.Width := bt.GetWidth;
r.Height := bt.GetHeight;
// escreve o texto
b.SetColor(fg);
c.DrawString(WideString(s), -1, f, r, tx, b);
finally
f.Free;
b.Free;
tx.Free;
c.Free;
end;
Result := TBitmap.Create;
if bt.GetHBITMAP(0, h)= ok then
TBitmap(Result).Handle := h;
finally
bt.Free;
end;
end;
Usage:
myGlyph := CreateTransparent('N', 14, 16, 16, clGray);
The problem:
The resulting bitmap isn't transparent, the background becomes black!
Can someone tell me what I need to do to "fill" the background as transparent?
According to what I understood , you want the background of bitmap to be transparent ?
If so , you need to use alpha channel bitmap ..
By default the background color is black so you only need to set the property AlphaFormat of your bitmap to afDefined :
...
Result := TBitmap.Create;
Result.AlphaFormat := afDefined;
if bt.GetHBITMAP(0, h) = ok then
TBitmap(Result).Handle := h;
...
And this is the result :
I'm trying to create a mask window with "loading" text, to alert the user about a busy state of my application.
For this, I first created a single form with:
BorderStyle = bsNone
Color = clBlack
AlphaBlend = True
AlphaBlendValue = 180;
As the second step, I want to create another Form, but this one will has dynamic content.
I need to create a transparent bitmap with some status text and use the UpdateLayeredWindow to draw the window as the text.
Take a look and my desired result:
Remember: the text will be different in some cases, like:
Recalculating
Loading resources
Loading report
That's the reason what I need a dynamic bitmap generation.
QUESTION
How can I create a transparent bitmap with text and use it on a form with UpdateLayeredWindow?
I'm trying this, but without success ( to try the code put a Button5 and Label2 on a form):
procedure Inc(var p: pointer);
begin
p := Pointer(Integer(p) + 1);
end;
var
s: string;
frm: TForm;
f: HFont;
tx: HDC;
bmp, old: HBITMAP;
rc: TRect;
h: BITMAPINFOHEADER;
pvBits: Pointer;
t: tagBITMAPINFO;
x,y: integer;
a, r, g, b: byte;
sz: TSize;
p: tpoint;
BlendFunction: TBlendFunction;
begin
tx := CreateCompatibleDC(0);
s := label2.Caption;
f := SelectObject(tx, label2.Font.Handle);
fillchar(rc, SizeOf(rc), 0);
DrawText(tx, PChar(s), length(s), rc, DT_CALCRECT);
fillchar(h, SizeOf(h), 0);
pvBits := nil;
h.biSize := SizeOf(h);
h.biWidth := rc.Right - rc.Left;
h.biHeight := rc.Bottom - rc.Top;
h.biPlanes := 1;
h.biBitCount := 32;
h.biCompression := BI_RGB;
FillChar(t, SizeOf(t), 0);
t.bmiHeader := h;
bmp := CreateDIBSection(tx, t, 0, pvBits, 0, 0);
old := SelectObject(tx, bmp);
if old > 0 then
begin
SetTextColor(tx, $00FFFFFF);
SetBkColor(tx, $00000000);
SetBkMode(tx, TRANSPARENT);
DrawText(tx, PChar(s), length(s), rc, DT_NOCLIP);
r := GetRValue($FF);
g := GetGValue($FF);
b := GetBValue($FF);
for x := 0 to h.biWidth-1 do
for y := 0 to h.biHeight-1 do
begin
a := Byte(pvBits^);
Inc(pvBits);
Byte(pvBits^) := (b * a) shr 8;
Inc(pvBits);
Byte(pvBits^) := (g * a) shr 8;
Inc(pvBits);
Byte(pvBits^) := (r * a) shr 8;
Inc(pvBits);
Byte(pvBits^) := a;
end;
SelectObject(tx, old);
end;
SelectObject(tx, f);
deleteDC(tx);
sz.cx := h.biWidth;
sz.cy := h.biHeight;
p := Point(0,0);
BlendFunction.BlendOp := AC_SRC_OVER;
BlendFunction.BlendFlags := 0;
BlendFunction.SourceConstantAlpha := 255;
BlendFunction.AlphaFormat := AC_SRC_ALPHA;
frm := TForm.CreateNew(self);
frm.BorderStyle := bsNone;
frm.Position := poOwnerFormCenter;
frm.Show;
UpdateLayeredWindow(frm.Handle, 0, nil, #sz, bmp, #p, 0, #BlendFunction, ULW_ALPHA);
end;
Assuming you can use Windows API functions from Delphi (since you've tagged winapi), one easy way is:
CreateDIBSection() to create a 32 bit bitmap
FillRect() to fill the background, DrawText() to draw the text
Fix up the alpha
Use that bitmap with UpdateLayeredWindow()
Fixing up the alpha is done by directly modifying the bitmap bits that you get back from CreateDIBSection(). Note that UpdateLayeredWindow() requires pre-multiplied alpha, so you have to multiple the RGB components by the alpha value in advance.
The following code is C but hopefully will give you the idea:
LPVOID pBits; // bits from CreateDIBSection
const int width, height; // size of bitmap
const int alpha; // level of transparency
RGBQUAD* pPtr = (RGBQUAD*)pBits;
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x, ++pPtr)
{
pPtr->rgbBlue = (alpha * pPtr->rgbBlue) / 255;
pPtr->rgbGreen = (alpha * pPtr->rgbGreen) / 255;
pPtr->rgbRed = (alpha * pPtr->rgbRed) / 255;
pPtr->rgbReserved = alpha;
}
}
I use Toolbar2000 component. It shows button's hint below correct position with system scale > 100%. So, I need to set HintPos manually. I have Mouse.CursorPos. But hint should be displayed below mouse cursor image.
How to get mouse cursor dimensions?
You should ask Windows for System Metrics - see http://msdn.microsoft.com/en-us/library/windows/desktop/ms724385.aspx
However if user installed something like Stardock CursorFX those values would not match what the user really sees and what behavior he expects from programs.
That seems to be one of Win32 API limitations, that the value cannot be changed apart of few relatively small standard values from old approved set.
You can create an Icon, use GetCursor to set the handle, additional information can be retrieved with GetIconInfo. This will even work if userdefined cursors are shown, which might have nearly any size.
var
ico: TIcon;
IcoInfo: TIconInfo;
begin
ico := TIcon.Create;
try
ico.Handle := GetCursor;
try
GetIconInfo(ico.Handle, IcoInfo);
Caption := Format('Width %d, Height %d HotSpotX %d, HotSpotY %d',
[ico.Width, ico.Height, IcoInfo.xHotspot, IcoInfo.yHotspot]);
finally
ico.ReleaseHandle;
end;
finally
ico.Free;
end;
end;
// Just as example for an very unusual cursor
procedure TForm1.Button1Click(Sender: TObject);
var
IconInfo: TIconInfo;
AndMask, Bmp: TBitmap;
w, h: Integer;
begin
w := Screen.Width * 2;
h := Screen.Height * 2;
// Creation And Mask
AndMask := TBitmap.Create;
AndMask.Monochrome := True;
AndMask.Height := h;
AndMask.Width := w;
// Draw on And Mask
AndMask.Canvas.Brush.Color := clWhite;
AndMask.Canvas.FillRect(AndMask.Canvas.ClipRect);
AndMask.Canvas.Pen.Color := clwhite;
AndMask.Canvas.Pen.Width := 5;
AndMask.Canvas.MoveTo(w div 2, 0);
AndMask.Canvas.LineTo(w div 2, h);
AndMask.Canvas.MoveTo(0, h div 2);
AndMask.Canvas.LineTo(w, h div 2);
{Create the "XOr" mask}
Bmp := TBitmap.Create;
Bmp.Width := w;
Bmp.Height := h;
{Draw on the "XOr" mask}
Bmp.Canvas.Brush.Color := clblack;
Bmp.Canvas.FillRect(Rect(0, 0, w, h));
Bmp.Canvas.Pen.Color := clwhite;
Bmp.Canvas.Pen.Width := 5;
Bmp.Canvas.MoveTo(w div 2, 0);
Bmp.Canvas.LineTo(w div 2, h);
Bmp.Canvas.MoveTo(0, h div 2);
Bmp.Canvas.LineTo(w, h div 2);
IconInfo.fIcon := true;
IconInfo.xHotspot := w div 2;
IconInfo.yHotspot := h div 2;
IconInfo.hbmMask := AndMask.Handle;
IconInfo.hbmColor := Bmp.Handle;
Screen.Cursors[1]:= CreateIconIndirect(IconInfo);
Screen.Cursor:=1;
end;
This is Windows 7 issue and there is no proper solution. GetSystemMetrics(SM_CYCURSOR) returns size of cursor image with background. And it seems this value is much more incorrect with system scale >100%. Delphi XE2 shows a hint on incorrect position too. But it's interesting to note that Explorer shows a hint on the correct position.