From: Michael H. <mh...@us...> - 2000-12-15 18:36:33
|
Update of /cvsroot/pythianproject/Prototypes/GLCanvas In directory slayer.i.sourceforge.net:/tmp/cvs-serv31590/GLCanvas Modified Files: glCanvas.pas glcanvas.htm Log Message: added multi-line edit -mike Index: glCanvas.pas =================================================================== RCS file: /cvsroot/pythianproject/Prototypes/GLCanvas/glCanvas.pas,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -r1.13 -r1.14 *** glCanvas.pas 2000/12/13 22:34:12 1.13 --- glCanvas.pas 2000/12/15 18:36:29 1.14 *************** *** 50,53 **** --- 50,56 ---- New- TRGB, ColorToRGB + Line drawing + Font structuring + Underline Notes- *************** *** 92,96 **** TTexBMPData = record DisplayList :integer; - //TexIDs :array[1..GLC_MAXTEXIDS] of Cardinal; Textures :array[1..GLC_MAXTEXIDS] of TTexture; cellsWidth, cellsHeight :integer; --- 95,98 ---- *************** *** 98,136 **** end; - { TGLCanvasFontData = record - Name, FileName:string; - FontType :integer; - end; - - TArrayOfGLCanvasFontData = array[1..GLC_MAXFONTS] of TGLCanvasFontData; } - - {const - GLC_DEFAULT_FONT_DATA :TArrayOfGLCanvasFontData = ( - (Name: 'Arial'; - FileName: 'arial1.glf'; - FontType: GLCANVAS_TEXT_GLF; - ), - (Name: 'Courier New'; - FileName: 'courier1.glf'; - FontType: GLCANVAS_TEXT_GLF; - ), - (Name: 'Courier New'; - FileName: 'CourierNew Grid.bmp'; - FontType: GLCANVAS_TEXT_QUADTEXT; - ), - (Name: 'Arial'; - FileName: 'Arial Grid.bmp'; - FontType: GLCANVAS_TEXT_QUADTEXT; - ), - (Name: 'VinerHand ITC'; - FileName: 'VinerHand ITC Grid.bmp'; - FontType: GLCANVAS_TEXT_QUADTEXT; - ) - ); } - - - type - TRGB = record r,g,b:byte; --- 100,104 ---- *************** *** 255,258 **** --- 223,227 ---- procedure SetColor(const Value: TColor); procedure LinesOnChange(Sender:TObject); // be sure to call if you override this event handler + function StringWidth(s:string):integer; // returns string width class procedure RegisterFont(name,filename:string;widths:TQuadTextWidthsArray); overload ; *************** *** 293,296 **** --- 262,267 ---- procedure DrawBitmap(X,Y:integer; bmp:TGLBitmap); virtual ; procedure DrawBitmapSubRect(X,Y:integer; SubRect:TRect; bmp:TGLBitmap); virtual ; + procedure TileBitmap(where:TRect; bmp:TGLBitmap); virtual ; + procedure TileBitmapSubRect(where,subRect:TRect; bmp:TGLBitmap); virtual ; // other *************** *** 307,310 **** --- 278,282 ---- // shape routines here - will standardise on the american spelling of colo(u)r procedure Rectangle(Left, Top, Right, Bottom:integer); virtual ; + procedure Line(X1,Y1,X2,Y2 :integer); virtual ; function ColorToRGB(c:TColor):TRGB; *************** *** 579,599 **** procedure TGLCanvas.Rectangle(Left, Top, Right, Bottom: integer); begin - (* glColor4f(FFillR,FFillG,FFillB,FFillAlpha); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity; - - //glScalef(2.0 / Width, -2.0 / Height, 1.0); - //glTranslatef(-(Width/2),-(Height/2),0); - glScalef(2.0 / Width, 2.0 / Height, 1.0); - glTranslatef(-(Width/2),(Height/2),0); - glScalef(1.0,-1.0,1.0); - - glBegin(GL_QUADS); - glVertex2i(Left,Top); - glVertex2i(Left,Bottom); - glVertex2i(Right,Bottom); - glVertex2i(Right,Top); - glEnd; *) - glDisable(GL_TEXTURE_2D); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); --- 551,554 ---- *************** *** 604,608 **** glScalef(2.0 / Width, 2.0 / Height, 1.0); glTranslatef(-(Width/2),(Height/2),0); - //glScalef(1.0,-1.0,1.0); if Solid then --- 559,562 ---- *************** *** 689,692 **** --- 643,685 ---- end; + procedure TGLCanvas.TileBitmap(where: TRect; bmp: TGLBitmap); + begin + TileBitmapSubRect(where,Rect(0,0,bmp.Width,bmp.Height),bmp); + end; + + + procedure TGLCanvas.TileBitmapSubRect(where, subRect: TRect; + bmp: TGLBitmap); + var + w,h,tilex,tiley:integer; + cr:TRect; + begin + // tile the texture + cr := FClipRect; + SetClipping(where); + w := subRect.Right-subRect.Left-1; + h := subRect.Bottom-subRect.Top-1; + for tilex := 0 to ((where.Right-where.Left) div w) do + for tiley := 0 to ((where.bottom-where.top) div h) do + DrawBitmapSubRect(where.Left+(tilex*w),where.Top+(tiley*h),subRect,bmp); + SetClipping(cr); // restore clipping + end; + + procedure TGLCanvas.Line(X1, Y1, X2, Y2: integer); + begin + glDisable(GL_TEXTURE_2D); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glColor4f(CurrentRed,CurrentGreen,CurrentBlue,FFillAlpha); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity; + glScalef(2.0 / Width, 2.0 / Height, 1.0); + glTranslatef(-(Width/2),(Height/2),0); + + glBegin(GL_LINES); + glVertex2i(X1, -Y1); + glVertex2i(X2, -Y2); + glEnd; + end; + { TGLText } *************** *** 1119,1122 **** --- 1112,1120 ---- o.Free; end; + end; + + function TGLText.StringWidth(s: string): integer; + begin + result := qtGetStringWidth(QT,s); end; Index: glcanvas.htm =================================================================== RCS file: /cvsroot/pythianproject/Prototypes/GLCanvas/glcanvas.htm,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -r1.2 -r1.3 *** glcanvas.htm 2000/12/09 19:52:04 1.2 --- glcanvas.htm 2000/12/15 18:36:30 1.3 *************** *** 11,14 **** --- 11,16 ---- + + Hearn</a><br> <a href="mailto:d_...@sy...">Darryl Long</a><br> *************** *** 18,21 **** --- 20,24 ---- Change Log:<br> <br> + - 14th December 2000: Changed to reflect new font registration system<br> - 4th December 2000: Add info about InitMatrix (doh!)<br> <hr align="Left" width="100%" size="2"><br> *************** *** 24,39 **** --- 27,50 ---- + + drawing 2D graphics onto an OpenGL canvas much easier than it would otherwise + + be. The algorithms used in the canvas have been designed for speed and ease + + of use, not necessarily simplicity. This is why some operations with it + + may seem a strange way of doing things. Anyway, let's go.<br> <br> *************** *** 52,69 **** DrawText().<br> <br> <u>Drawing Images</u><br> <br> ! Images in OpenGL are not directly supported unless you use the glDrawPixels() ! ! command which directly copies pixel data from system memory to the pixel ! ! buffer. This would be ideal but unfortunately this is a <b>very</b>slow - operation, and I mean slow. Drawing a 640x480 image in this way on my machine - takes almost half a second :(<br> - Nevertheless, the Canvas supports this method for when performance is not --- 63,80 ---- + + DrawText().<br> <br> <u>Drawing Images</u><br> <br> ! Images in OpenGL are not directly supported unless you use the glDrawPixels() ! command which directly copies pixel data from system memory to the pixel ! buffer. This would be ideal but unfortunately this is a <b>very</b> ! slow operation, and I mean slow. Drawing a 640x480 image in this way on ! my machine takes almost half a second :(<br> ! Nevertheless, the Canvas supports this method for when performance is not *************** *** 75,102 **** --- 86,127 ---- + + things that results in much better framerates (ie. about 120fps on my machine + + for a 640x480 image :) This system breaks the image you want to draw into + + multiple textures and then uses polygons to display them. Because these + + images are hardware accellerated things move along much better. Why multiple + + textures? Well, most hardware cards have a limit of 256x256 pixels for textures + + due to the internals of their engines. So the canvas breaks an image into + + multiple textures when the image is loaded.<br> <br> *************** *** 105,116 **** --- 130,147 ---- + + by objects, in this case the TGLBitmap class is used. To use a bitmap it + + must be loaded into one of these objects, which can then be passed to the + + DrawBitmap() method of the GLCanvas. Here's an example:<br> <br> *************** *** 130,144 **** --- 161,187 ---- <br> Note that before any GLCanvas methods can be called you must call InitMatrix, + this initializes the co-ordinate systems. This example would display "logo.png"at + location 50,50 from the top left of the window. As you can see, there is + nothing to it. However, we can do more than this! The GLBitmap class supports + transparency using a transparent colour: if we set the transparent colour + to black then any black pixels in the picture will be see-through, meaning + you can draw non-rectangular bitmaps. This is done by setting the <code>UseTransparency</code>property + to true and setting the TransparentColor property to the colour you want + (it defaults to black). Because the GLCanvas is based partly on the FastDIB + library you must specify the colour as RGB data, not a Delphi colour constant. + Although in some places you can use constants like clBlack or clAqua in + this instance that's not allowed. You create a colour for this property + using the FRGB function:<br> <br> *************** *** 147,150 **** --- 190,195 ---- Picture.TransparentColor := FRGB(0,0,255); // blue is our transparent + + colour<br> Picture.LoadFromFile("logo.png");</code><br> *************** *** 152,155 **** --- 197,202 ---- Notice that you <i>must</i>set the transparency properties before loading + + the file. If you want you can use direct drawing by using a different constructor:<br> <br> *************** *** 158,161 **** --- 205,210 ---- However, this isn't really supported very well - for instance transparency + + doesn't work with this method. Also, it's slow so it's best to avoid this.<br> <br> *************** *** 163,184 **** <br> Again, OpenGL has no direct support for drawing text. There are many, many - different ways of drawing text (for more information on this subject check - - out NeHe's excellent <a href="http://nehe.gamedev.net/opengl/">tutorial pages</a>) and the Canvas - - offers you two which should combine the best of both worlds - bitmapped - text which looks nice at small sizes, and vector text which can be resized ! to any area needed without losing resolution. Vector fonts are drawn using - the <a href="http://romka.demonews.com">GLF library</a>written by Romka, who is a seriously - cool guy. You can get more fonts from his website.<br> - <br> - Bitmapped fonts are drawn using my own system that uses a 256x256 bitmap - - with letters arranged in a grid formation. Textured polygons are drawn that use this and this means <i>fast fast fast!</i><br> --- 212,230 ---- <br> Again, OpenGL has no direct support for drawing text. There are many, many + different ways of drawing text (for more information on this subject + check out NeHe's excellent <a href="http://nehe.gamedev.net/opengl/">tutorial pages</a>) and + the Canvas offers you two which should combine the best of both worlds - + bitmapped text which looks nice at small sizes, and vector text which + can be resized to any area needed without losing resolution. Vector fonts + are drawn using the <a href="http://romka.demonews.com">GLF library </a>written by Romka, + who is a seriously cool guy. You can get more fonts from his website.<br> + <br> + Bitmapped fonts are drawn using my own system that uses a 256x256 bitmap ! with letters arranged in a grid formation. Textured polygons are drawn that use this and this means <i>fast fast fast!</i><br> *************** *** 186,203 **** --- 232,265 ---- This also means that fonts are very easy to make, although it does take some + + time. You can use the included "20x20grid.bmp" file to help you create new + + fonts. To use the text facility you can use the TGLText object. The reason + + that text is represented by objects too is for performance reasons, when + + you use an object something called pre-caching becomes available which stores + + the commands for drawing the text in the hardware accellerator itself, meaning + + - yep, you've guessed it, faster execution! Of course, if this isn't important + + to you it's possible to use the DrawString() command for simplicity but + + it's really designed to use an object. Here's a simple example of it:<br> <br> *************** *** 216,225 **** --- 278,295 ---- As you can see, this is quite easy, but you can do more :) Text objects can + + have multiple lines (accessed through the Lines property), and of course + + this can be used to load text files. The demo program shows this in action. + + This uses textured quads to draw bitmapped text. To use the GLF vector based + + text:<br> <br> *************** *** 227,236 **** <br> begin<br> ! Text2 := TGLText.Create("Hello World","Arial",GLCANVAS_TEXT_GLF,GLC_DEFAULT_FONT_DATA);<br> // here we have used the other overloaded constructor to select GLF text. you can ignore<br> // the last parameter, it selects a font data array, the default one will do for now.<br> Text2.Size := 20;<br> --- 297,310 ---- <br> begin<br> ! Text2 := TGLText.Create("Hello World","Arial",GLCANVAS_TEXT_GLF);<br> // here we have used the other overloaded constructor to select GLF text. + + you can ignore<br> // the last parameter, it selects a font data array, the default one will + + do for now.<br> Text2.Size := 20;<br> *************** *** 238,245 **** end;</code><br> <br> ! If you want to change the directory fonts are loaded from (the default is ! for the current directory) you can set the FontsDirectory variable in the ! unit. This is a string that is appended to the start of the font filenames ! before they are loaded, and therefore they <b>must</b>have a / at the end. For instance:<br> <br> --- 312,319 ---- end;</code><br> <br> ! If you want to change the directory fonts are loaded from (the default is ! for the current directory) you can set the FontsDirectory variable in the ! unit. This is a string that is appended to the start of the font filenames ! before they are loaded, and therefore they <b>must</b>have a / at the end. For instance:<br> <br> *************** *** 248,261 **** <br> How you add new fonts depends on the system you use. If you're drawing vector - text you can simply download more GLF fonts from Romkas website but I'm ! ! not sure how you can make your own. Then you add the entry for it to the ! ! GLC_DEFAULT_FONT_DATA array as shown below. For bitmap text it's more complex ! ! (i'm afraid the canvas only comes with Arial and Courier New) but everything ! ! can be done using Paint Shop Pro or a similar program.<br> <br> To create a new bitmapped font:<br> --- 322,330 ---- <br> How you add new fonts depends on the system you use. If you're drawing vector text you can simply download more GLF fonts from Romkas website but I'm ! not sure how you can make your own. Then you register the font as shown ! below. For bitmap text it's more complex (i'm afraid the canvas only ! comes with Arial and Courier New) but everything can be done using Paint ! Shop Pro or a similar program.<br> <br> To create a new bitmapped font:<br> *************** *** 264,295 **** <li>Paste the "20x20 Grid.bmp" file over the top. This will show you where to place characters. If you want you can place the grid over the fonts that come with the canvas to see how it's done.</li> <li>For each character place the letter (in white) in each square aligned to the left of each grid square.</li> <li>Once this is done for every character (well, every character that is in the font set, you can see them in the other font grids) save it and change ! the GLCanvas.pas file in the following way:</li> ! <li>Add a new entry to the GLC_DEFAULT_FONT_DATA array. The fields are ! fairly self-explanatory, just make sure you set FontType to be GLCANVAS_TEXT_QUADTEXT.</li> <li>You also need to add a widths array to the QuadTextUnit.pas file. This array specifies the width of each character and is how the system support - variable width fonts. See the code for examples of how to do this.</li> - <li>Finally, you need to add an entry to the MatchFontWidths method of ! the TGLText class. This just returns the array given a font name.</li> <li>That's it! I know it's long winded, some time I may automate it but for now that's the way to do it. If you want to add some sort of exotic character not already in the font set add it to the array and set the rest of the widths in the other arrays to 0.</li> </ol> --- 333,385 ---- <li>Paste the "20x20 Grid.bmp" file over the top. This will show you where + + to place characters. If you want you can place the grid over the fonts that + + come with the canvas to see how it's done.</li> <li>For each character place the letter (in white) in each square aligned + + to the left of each grid square.</li> <li>Once this is done for every character (well, every character that is + + in the font set, you can see them in the other font grids) save it and change ! ! the GLCanvas.pas file in the following way:</li> <li>You also need to add a widths array to the QuadTextUnit.pas file. This + + array specifies the width of each character and is how the system support ! ! variable width fonts. See the code for examples of how to do this.</li> ! <li>Then you add an entry to the initialization section of the GLCanvas ! unit like this:<br> ! <br> ! TGLText.RegisterFont('Arial','Arial Grid.bmp',ARIAL_WIDTHS);<br> ! <br> ! where you specify the widths array you created above.<br> ! </li> <li>That's it! I know it's long winded, some time I may automate it but + + for now that's the way to do it. If you want to add some sort of exotic + + character not already in the font set add it to the array and set the rest + + of the widths in the other arrays to 0.</li> </ol> *************** *** 299,302 **** --- 389,394 ---- You can draw rectangles using the Rectangle() method. This takes 4 coordinates, + + X1, X2, Y1 and Y2. It draws a rectangle based on the:<br> <br> *************** *** 307,311 **** --- 399,407 ---- properties. If solid is true then the rectangle will be filled with the colour + + and at the opacity specified with FillAlpha. If solid is false then the + + outline is all that is drawn.<br> |