
6e60901d7e7ec3c97b1fd23803530782.ppt
- Количество слайдов: 44
Module 03 – Fonty v Open. GL Module 03 Fonty
Module 03 – Fonty v Open. GL Obsah § Zobrazovanie textu § Bitmapové fonty § Outline fonty § 2 D texture fonty § Demonštračný program
Module 03 – Fonty v Open. GL Zobrazovanie textu 1. Niekedy je potrebné zobraziť v grafickej aplikácii aj texty 2. Texty sa používajú napríklad v: 1. menu 2. screensavers 3. dialógy 4. Jednoduché textové efekty 3. Je niekoľko základných techník používaných pri výpise textu pomocou Open. GL knižnice. 4. Mnohé sú závisle od daného operačný systému, na ktorom sa bude aplikácie spúšťať
Module 03 – Fonty v Open. GL Bitmapové Fonty § Bitmapové fonty ponúkajú jednoduchý spôsob na zobrazenie 2 D textu na obrazovku § Informácie o znakoch sú uložené v podobe bitmapy v jednom veľkom bitmapovom obrázku § Nevýhodou je ich kostrbatosť a bez antialiasingu pri zväčšovaní sú neosté § Výhodou je ich jednoduchosť implementácie a rýchlosť vypisovania textu na obrazovku. § Vytvárajú sa pomocou wgl funkcií wgl. Use. Font Bitmaps(), ktoré generujú bitmapy z existujúceho súboru s fontom natiahnutého vo vašom OS.
Module 03 – Fonty v Open. GL Outline Fonty 1. Outline fontsú veľmi podobné bitmapovým fontom 2. Outline fonty definujú znaky vo fonte ako sériu čiara kriviek, ktoré je možné zväčšovať aj zmenšovať bez straty kvality. 3. Pomocou Open. GL je možné hýbať s outline fontovým textom okolo obrazovky v 3 D, priradiť textu hĺbku, a narábať s fontom rovnako, ako z ľubovolným iným 3 D objektom.
Module 03 – Fonty v Open. GL 2 D Texture Fonts § Používanie texúrory na uloženie fontu má viacero výhod (šetrenie pamätovým priestorom, zvýšenie FPS) § Napríklad v tomto module budeme používať iba jednu textúru na definovanie 256 znakov. § Na jeden znak máme vymedzený priestor 16 x 16 pixelov pri štandardnej veľkosti textúry 256 x 256
Module 03 – Fonty v Open. GL Drawing Open. GL bitmap font (1) Postup: 1) 2) 3) 4) 5) 6) 7) 8) 9) include header files for working with input and arguments declare new global variable for base display list for the font set declare new global functions for building, printing and releasing the GL font define font building function Build. Font() define font releasing function Kill. Font() define font printing function gl. Print() load and enable the new font while setting up the Open. GL environment draw text properly release font
Module 03 – Fonty v Open. GL Drawing Open. GL bitmap font (2) 1) include header files for working with input and arguments //----------------------------------------// Header Files //----------------------------------------… #include
Module 03 – Fonty v Open. GL Drawing Open. GL bitmap font (3) 3) declare new global functions for building, printing and releasing font //----------------------------------------// Global Functions //----------------------------------------… GLvoid Build. Font(GLvoid); // Declaration For Build. Font GLvoid gl. Print(const char *fmt, . . . ); // Declaration For gl. Print GLvoid Kill. Font(GLvoid); // Declaration For Kill. Font … 4) define font building function Build. Font() GLvoid Build. Font(GLvoid) { HFONT font; HFONT oldfont; base = gl. Gen. Lists(96); … } // end of Build. Font() // Windows Font ID // Used For Good House Keeping // Storage For 96 Characters
Module 03 – Fonty v Open. GL Drawing Open. GL bitmap font (4) 4) define font building function Build. Font() GLvoid Build. Font(GLvoid) { … font = Create. Font( } … // end of Build. Font() -24, // Height Of Font 0, // Width Of Font 0, // Angle Of Escapement 0, // Orientation Angle FW_BOLD, // Font Weight FALSE, // Italic FALSE, // Underline FALSE, // Strikeout ANSI_CHARSET, // Character Set Identifier OUT_TT_PRECIS, // Output Precision CLIP_DEFAULT_PRECIS, // Clipping Precision ANTIALIASED_QUALITY, // Output Quality FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch "Courier New"); // Font Name
Module 03 – Fonty v Open. GL Drawing Open. GL bitmap font (5) 4) define font building function Build. Font() GLvoid Build. Font(GLvoid) { … oldfont = (HFONT)Select. Object(h. DC, font); // Selects The Font We Want // Builds 96 Characters Starting At Character 32 wgl. Use. Font. Bitmaps(h. DC, 32, 96, base); } Select. Object(h. DC, oldfont); Delete. Object(font); // end of Build. Font() // Selects The Font We Want // Delete The Font
Module 03 – Fonty v Open. GL Drawing Open. GL bitmap font (6) 5) define font releasing function Kill. Font() GLvoid Kill. Font(GLvoid) { gl. Delete. Lists(base, 96); } // end of Kill. Font() // Delete All 96 Characters 6) define font printing function gl. Print() GLvoid gl. Print(const char *fmt, . . . ) { char text[256]; va_list ap; if (fmt == NULL) return; } … // end of gl. Print() // Holds Our String // Pointer To List Of Arguments // If There's No Text // Do Nothing
Module 03 – Fonty v Open. GL Drawing Open. GL bitmap font (7) 6) define font printing function gl. Print() GLvoid gl. Print(const char *fmt, . . . ) { … va_start(ap, fmt); vsprintf(text, fmt, ap); va_end(ap); } // Parses The String For Variables // And Converts Symbols To Actual Numbers // Results Are Stored In Text gl. Push. Attrib(GL_LIST_BIT); // Pushes The Display List Bits gl. List. Base(base - 32); // Sets The Base Character to 32 gl. Call. Lists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text gl. Pop. Attrib(); // Pops The Display List Bits // end of gl. Print()
Module 03 – Fonty v Open. GL Drawing Open. GL bitmap font (8) 7) load and enable the new font while setting up the Open. GL environment int Init. GL(GLvoid) { … Build. Font(); … } // end of Init. GL(Glvoid) // All Setup For Open. GL Goes Here // Build The Font 8) draw text int Draw. GLScene(GLvoid) { … gl. Load. Identity(); // Reset The Current Modelview Matrix gl. Translatef(0. 0 f, -1. 0 f); // Move One Unit Into The Screen gl. Color 3 f(1. 0, 0. 0); // Set Font Color gl. Raster. Pos 2 f(-0. 35, 0); // Set Font Position gl. Print("Module 04 - Open. GL Bitmap Fonts"); // Set text … } // end of Draw. GLScene()
Module 03 – Fonty v Open. GL Drawing Open. GL bitmap font (9) 9) properly release font GLvoid Kill. GLWindow(GLvoid) { … } Kill. Font(); // end of Kill. GLWindow() // release thefont
Module 03 – Fonty v Open. GL Final result Using of bitmap font
Module 03 – Fonty v Open. GL Practice Open: Lab 03 / Bitmap. Font. sln
Module 03 – Fonty v Open. GL Drawing Open. GL outline font (1) We will continue with previous lab (1). Steps to do: 1) include header files for working with input and arguments 2) declare new global variables for text rotation and outline font characters storage info 3) declare new global functions for building, printing and releasing the GL outline font 4) define outline font building function Build. Font() 5) define outline font releasing function Kill. Font() 6) define outline font printing function gl. Print() 7) load and enable the new outline font setting up the Open. GL environment 8) draw text 9) properly release font
Module 03 – Fonty v Open. GL Drawing Open. GL outline font (2) 1) include header files for working with input and arguments //----------------------------------------// Header Files //----------------------------------------… #include
Module 03 – Fonty v Open. GL Drawing Open. GL outline font (3) 3) declare new global functions for building, printing and releasing outline font //----------------------------------------// Global Functions //----------------------------------------… GLvoid Build. Font(GLvoid); // Declaration For Build. Font GLvoid gl. Print(const char *fmt, . . . ); // Declaration For gl. Print GLvoid Kill. Font(GLvoid); // Declaration For Kill. Font … 4) define outline font building function Build. Font() GLvoid Build. Font(GLvoid) { HFONT font; base = gl. Gen. Lists(256); … } // end of Build. Font() // Windows Font ID // Storage For 256 Characters
Module 03 – Fonty v Open. GL Drawing Open. GL outline font (4) 4) define outline font building function Build. Font() GLvoid Build. Font(GLvoid) { … font = Create. Font( } … // end of Build. Font() -24, // Height Of Font 0, // Width Of Font 0, // Angle Of Escapement 0, // Orientation Angle FW_BOLD, // Font Weight FALSE, // Italic FALSE, // Underline FALSE, // Strikeout ANSI_CHARSET, // Character Set Identifier OUT_TT_PRECIS, // Output Precision CLIP_DEFAULT_PRECIS, // Clipping Precision ANTIALIASED_QUALITY, // Output Quality FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch "Courier New"); // Font Name
Module 03 – Fonty v Open. GL Drawing Open. GL outline font (5) 4) define outline font building function Build. Font() GLvoid Build. Font(GLvoid) { … Select. Object(h. DC, font); wgl. Use. Font. Outlines( } // end of Build. Font() // Selects The Font We Created h. DC, // Select The Current DC 0, // Starting Character 255, // Number Of Display Lists To Build base, // Starting Display Lists 0. 0 f, // Deviation From The True Outlines 0. 2 f, // Font Thickness In The Z Direction WGL_FONT_POLYGONS, // Use Polygons, Not Lines gmf); // Address Of Buffer To Recieve Data
Module 03 – Fonty v Open. GL Drawing Open. GL outline font (6) 5) define outline font releasing function Kill. Font() GLvoid Kill. Font(GLvoid) { gl. Delete. Lists(base, 256); } // end of Kill. Font() // Delete All 256 Characters 6) define outline font printing function gl. Print() GLvoid gl. Print(const char *fmt, . . . ) { char text[256]; va_list ap; if (fmt == NULL) return; } … // end of gl. Print() // Holds Our String // Pointer To List Of Arguments // If There's No Text // Do Nothing
Module 03 – Fonty v Open. GL Drawing Open. GL outline font (7) 6) define font printing function gl. Print() GLvoid gl. Print(const char *fmt, . . . ) { … va_start(ap, fmt); vsprintf(text, fmt, ap); va_end(ap); // Parses The String For Variables // And Converts Symbols To Actual Numbers // Results Are Stored In Text for (unsigned int loop=0; loop<(strlen(text)); loop++) // Loop To Find Text Length { length+=gmf[text[loop]]. gmf. Cell. Inc. X; // Increase Length By Each Characters Width } gl. Translatef(-length/2, 0. 0 f); // Center Our Text On The Screen } gl. Push. Attrib(GL_LIST_BIT); // Pushes The Display List Bits gl. List. Base(base); // Sets The Base Character to 0 gl. Call. Lists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text gl. Pop. Attrib(); // Pops The Display List Bits // end of gl. Print()
Module 03 – Fonty v Open. GL Drawing Open. GL outline font (8) 7) load and enable the new font while setting up the Open. GL environment int Init. GL(GLvoid) // All Setup For Open. GL Goes Here { … Build. Font(); // Build The Font … } // end of Init. GL(Glvoid) 8) draw text int Draw. GLScene(GLvoid) { … gl. Load. Identity(); gl. Translatef(0. 0 f, -15. 0 f); … } // end of Draw. GLScene() // Reset The Current Modelview Matrix // Move One Unit Into The Screen
Module 03 – Fonty v Open. GL Drawing Open. GL outline font (9) 8) draw text int Draw. GLScene(GLvoid) { … gl. Rotatef(rot, 1. 0 f, 0. 0 f); // Rotate On The X Axis gl. Rotatef(rot*1. 5 f, 0. 0 f, 1. 0 f, 0. 0 f); // Rotate On The Y Axis gl. Rotatef(rot*1. 4 f, 0. 0 f, 1. 0 f); // Rotate On The Z Axis gl. Color 3 f(1, 0, 0); // Set Outline Font Color gl. Print("Module 04 - Open. GL Outline Fonts"); // Print GL Text To The Screen rot+=0. 25 f; // Increase The Rotation Variable … } // end of Draw. GLScene()
Module 03 – Fonty v Open. GL Drawing Open. GL outline font (10) 9) properly release font GLvoid Kill. GLWindow(GLvoid) { … } Kill. Font(); // end of Kill. GLWindow() // release thefont
Module 03 – Fonty v Open. GL Final result Open. GL outline font
Module 03 – Fonty v Open. GL Practice Open: Lab 03 / Outline. Font. sln
Module 03 – Fonty v Open. GL Drawing 2 D texture Open. GL font (1) Steps to do: 1) include header files for working with math and input operations 2) declare new global variables to point us to our display list, for texture, loop and to move text & for coloring 3) 4) 5) declare new global functions for loading font bmp file, converting it to the texture, building, printing and releasing 2 D texture font load font bmp file load font bitmaps and convert to texture 6) define 2 D texture font building function Build. Font() 7) define 2 D texture font releasing function Kill. Font() 8) define 2 D texture font printing function gl. Print() 9) load and enable the new 2 D texture font setting up the Open. GL environment 10) draw text 11) properly release font
Module 03 – Fonty v Open. GL Drawing 2 D texture Open. GL font (2) 1) include header files for working with math and input operations in init. h //----------------------------------------// Header Files //----------------------------------------… #include
Module 03 – Fonty v Open. GL Drawing 2 D texture Open. GL font (3) 2) declare new global variables to point us to our display list, for texture, loop and to move text & for coloring … GLfloat … cnt 1; cnt 2; // 1 st Counter Used To Move Text & For Coloring // 2 nd Counter Used To Move Text & For Coloring 3) declare new global functions for loading font bmp file, converting it to the texture, building, printing and releasing 2 D texture font //----------------------------------------// Global Functions //----------------------------------------… AUX_RGBImage. Rec *Load. BMP(char *Filename); // Declaration For Load. BMP int Load. GLTextures(); // Declaration For Load. GLTextures GLvoid Build. Font(GLvoid); // Declaration For Build. Font GLvoid Kill. Font(GLvoid); // Declaration For Kill. Font GLvoid gl. Print(GLint x, GLint y, char *string, int set); // Declaration For gl. Print …
Module 03 – Fonty v Open. GL Drawing 2 D texture Open. GL font (4) 4) load font bmp file AUX_RGBImage. Rec *Load. BMP(char *Filename) { FILE *File=NULL; // File Handle if (!Filename) // Make Sure A Filename Was Given { return NULL; // If Not Return NULL } File=fopen(Filename, "r"); // Check To See If The File Exists if (File) // Does The File Exist? { fclose(File); // Close The Handle return aux. DIBImage. Load(Filename); // Load The Bitmap And Return A Pointer } return NULL; // If Load Failed Return NULL } // end of Load. BMP()
Module 03 – Fonty v Open. GL Drawing 2 D texture Open. GL font (5) 5) load font bitmaps and convert to texture int Load. GLTextures() { int Status=FALSE; AUX_RGBImage. Rec *Texture. Image[2]; memset(Texture. Image, 0, sizeof(void *)*2); } // Status Indicator // Create Storage Space For The Textures // Set The Pointer To NULL if (Texture. Image[0]=Load. BMP("Data/Font. bmp")) { Status=TRUE; // Set The Status To TRUE gl. Gen. Textures(1, &texture[0]); // Create One Texture gl. Bind. Texture(GL_TEXTURE_2 D, texture[0]); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); gl. Tex. Parameteri(GL_TEXTURE_2 D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); … // end of Load. GLTextures()
Module 03 – Fonty v Open. GL Drawing 2 D texture Open. GL font (6) 5) load font bitmaps and convert to texture int Load. GLTextures() {. . . gl. Tex. Image 2 D(GL_TEXTURE_2 D, 0, 3, Texture. Image[loop]->size. X, Texture. Image[loop]>size. Y, 0, GL_RGB, GL_UNSIGNED_BYTE, Texture. Image[0]->data); } if (Texture. Image[0]) // If Texture Exists { if (Texture. Image[0]->data) // If Texture Image Exists { free(Texture. Image[0]->data); } free(Texture. Image[0]); } } return Status; // end of Load. GLTextures() // Return The Status
Module 03 – Fonty v Open. GL Drawing 2 D texture Open. GL font (7) 6) defefine 2 D texture font building function Build. Font() GLvoid Build. Font(GLvoid) { float cx; // Holds Our X Character Coord float cy; // Holds Our Y Character Coord base=gl. Gen. Lists(256); // Creating 256 Display Lists gl. Bind. Texture(GL_TEXTURE_2 D, texture[0]); // Select Our Font Texture for (loop=0; loop<256; loop++) // Loop Through All 256 Lists { cx=float(loop%16)/16. 0 f; // X Position Of Current Character cy=float(loop/16)/16. 0 f; // Y Position Of Current Character gl. New. List(base+loop, GL_COMPILE); // Start Building A List … // Loop Until All 256 Are Built } // end of Build. Font()
Module 03 – Fonty v Open. GL Drawing 2 D texture Open. GL font (8) 6) defefine 2 D texture font building function Build. Font() GLvoid Build. Font(GLvoid) { … gl. Begin(GL_QUADS); gl. Tex. Coord 2 f(cx, 1 -cy-0. 0625 f); gl. Vertex 2 i(0, 0); gl. Tex. Coord 2 f(cx+0. 0625 f, 1 -cy-0. 0625 f); gl. Vertex 2 i(16, 0); gl. Tex. Coord 2 f(cx+0. 0625 f, 1 -cy); gl. Vertex 2 i(16, 16); gl. Tex. Coord 2 f(cx, 1 -cy); gl. Vertex 2 i(0, 16); gl. End(); gl. Translated(10, 0, 0); gl. End. List(); } } // end of Build. Font() // Use A Quad For Each Character // Texture Coord (Bottom Left) // Vertex Coord (Bottom Left) // Texture Coord (Bottom Right) // Vertex Coord (Bottom Right) // Texture Coord (Top Right) // Vertex Coord (Top Right) // Texture Coord (Top Left) // Vertex Coord (Top Left) // Done Building Our Quad (Character) // Move To The Right Of The Character // Done Building The Display List // Loop Until All 256 Are Built
Module 03 – Fonty v Open. GL Drawing 2 D texture Open. GL font (9) 7) defefine 2 D texture font releasing function Kill. Font() GLvoid Kill. Font(GLvoid) { gl. Delete. Lists(base, 256); } // end of Kill. Font() 8) // Delete All 256 Display Lists define 2 D texture font printing function gl. Print() GLvoid gl. Print(GLint x, GLint y, char *string, int set) { if (set>1) { set=1; } gl. Bind. Texture(GL_TEXTURE_2 D, texture[0]); gl. Disable(GL_DEPTH_TEST); … } // end of gl. Print() // Select Our Font Texture // Disables Depth Testing
Module 03 – Fonty v Open. GL Drawing 2 D texture Open. GL font (10) 8) define 2 D texture font printing function gl. Print() GLvoid gl. Print(GLint x, GLint y, char *string, int set) { … gl. Matrix. Mode(GL_PROJECTION); gl. Push. Matrix(); gl. Load. Identity(); gl. Ortho(0, 640, 0, 480, -1, 1); gl. Matrix. Mode(GL_MODELVIEW); gl. Push. Matrix(); gl. Load. Identity(); gl. Translated(x, y, 0); gl. List. Base(base-32+(128*set)); gl. Call. Lists(strlen(string), GL_BYTE, string); gl. Matrix. Mode(GL_PROJECTION); gl. Pop. Matrix(); gl. Matrix. Mode(GL_MODELVIEW); gl. Pop. Matrix(); gl. Enable(GL_DEPTH_TEST); } // end of gl. Print() // Select The Projection Matrix // Store The Projection Matrix // Reset The Projection Matrix // Set Up An Ortho Screen // Select The Modelview Matrix // Store The Modelview Matrix // Reset The Modelview Matrix // Position The Text (0, 0 - Bottom Left) // Choose The Font Set (0 or 1) // Write The Text To The Screen // Select The Projection Matrix // Restore The Old Projection Matrix // Select The Modelview Matrix // Restore The Old Projection Matrix // Enables Depth Testing
Module 03 – Fonty v Open. GL Drawing 2 D texture Open. GL font (11) 9) load and enable the new 2 D texture font setting up the Open. GL environment int Init. GL(GLvoid) { if (!Load. GLTextures()) { return FALSE; } } // Jump To Texture Loading Routine // If Texture Didn't Load Return FALSE Build. Font(); // Build The Font gl. Clear. Color(0. 0 f, 0. 0 f); // Clear The Background Color To Black gl. Clear. Depth(1. 0); // Enables Clearing Of The Depth Buffer gl. Depth. Func(GL_LEQUAL); // The Type Of Depth Test To Do gl. Blend. Func(GL_SRC_ALPHA, GL_ONE); // Select The Type Of Blending gl. Shade. Model(GL_SMOOTH); // Enables Smooth Color Shading gl. Enable(GL_TEXTURE_2 D); // Enable 2 D Texture Mapping return TRUE; // Initialization Went OK // end of Init. GL()
Module 03 – Fonty v Open. GL Drawing 2 D texture Open. GL font (12) 10) draw text int Draw. GLScene(GLvoid) { gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Enable(GL_BLEND); // Enable Blending gl. Load. Identity(); // Reset The View // Pulsing Colors Based On Text Position gl. Color 3 f(1. 0 f*float(cos(cnt 1)), 1. 0 f*float(sin(cnt 2)), 1. 0 f-0. 5 f*float(cos(cnt 1+cnt 2))); // Print GL Text To The Screen gl. Print((280+250*cos(cnt 1))), int(235+200*sin(cnt 2)), "Module 04", 0); // Pulsing Colors Based On Text Position gl. Color 3 f(1. 0 f*float(sin(cnt 2)), 1. 0 f-0. 5 f*float(cos(cnt 1+cnt 2)), 1. 0 f*float(cos(cnt 1))); // Print GL Text To The Screen gl. Print((280+230*cos(cnt 2))), int(235+200*sin(cnt 1)), "2 D Texture Font", 1); cnt 1+=0. 01 f; // Increase The First Counter cnt 2+=0. 0081 f; // Increase The Second Counter return TRUE; // Everything Went OK } // end of Draw. GLScene()
Module 03 – Fonty v Open. GL Drawing 2 D texture Open. GL font (13) 11) properly release font GLvoid Kill. GLWindow(GLvoid) { … } Kill. Font(); // end of Kill. GLWindow() // release thefont
Module 03 – Fonty v Open. GL Final result Open. GL 2 D texture font
Module 03 – Fonty v Open. GL Practice Open: Lab 03 / Bitmap. Font 2. sln