Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Module 03 Fonty.

Slides:



Advertisements
Similar presentations
MS® PowerPoint.
Advertisements

Using GLU/GLUT Objects GLU/GLUT provides very simple object primitives glutWireCube glutWireCone gluCylinder glutWireTeapot.
Text David Meredith Aalborg University.
OPENGL Return of the Survival Guide. Buffers (0,0) OpenGL holds the buffers in a coordinate system such that the origin is the lower left corner.
Computer Graphic Creator: Mohsen Asghari Session 2 Fall 2014.
Chapter 3. Drawing primitives in Space Presented by Garrett Yeh.
MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Overview Today: - Make sure everyone is set up with an OpenGL environment - OpenGL basics:
Chapter 12 Interactive Graphics Chih-Kuo Yeh. Direct Manipulation Demo.
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
OpenGL and Projections
OpenGL (II). How to Draw a 3-D object on Screen?
CS 4731: Computer Graphics Lecture 17: Texturing Emmanuel Agu.
3D Rendering with JOGL Introduction to Java OpenGL Graphic Library By Ricardo Veguilla
OpenGL Pixel Operations, Bitmaps, Fonts and Images The Current Raster Position Current raster position: A position in window coordinates where the next.
Introduction to OpenGL Jian Huang This set of slides are extracted from the Interactive OpenGL Programming course given by Dave Shreine, Ed Angel and Vicki.
19/4/ :32 Graphics II Syllabus Selection and Picking Session 1.
3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!
1 Scrolling and Panning CIS 487/587 Bruce R. Maxim UM-Dearborn.
Texture Mapping. To add surface details… World of Warcraft, Blizzard Inc. More polygons (slow and hard to handle small details) Less polygons but with.
Module 04 – Texture mapping, Texture filtering, Lighting and Blending Module 04 Texture mapping, Texture filtering, Lighting and Blending.
Texture Mapping. Scope Buffers Buffers Various of graphics image Various of graphics image Texture mapping Texture mapping.
Texturing A picture is worth a thousand words. Texturing Texturing is the art of doing this with any picture to any model.  (This is Opus the Penguin.
Computer Graphics Ben-Gurion University of the Negev Fall 2012.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
ECSE-4750 Computer Graphics Fall 2004 Prof. Michael Wozny TA. Abhishek Gattani TA. Stephen
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
1. OpenGL/GLU/GLUT  OpenGL v4.0 (latest) is the “core” library that is platform independent  GLUT v3.7 is an auxiliary library that handles window creation,
CG1 Labs Wei Li. Back Face Culling // enable back-face culling glEnable( GL_CULL_FACE ); // orientation of front-facing polygons glFrontFace( GL_CCW );
Computer Graphics Bing-Yu Chen National Taiwan University.
Modeling with OpenGL Practice with OpenGL transformations.
Computer Science Term 1, 2006 Tutorial 2 Assignment 3 – The Virtual World.
CH8 Frame Buffer Object 1. Introduction Name OpenGL :Frame Buffer Object DirectX:Render Target Usage Render to Texture 2.
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
NoufNaief.net TA: Nouf Al-harbi.
Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower.
OpenGL: The Open Graphics Language Introduction By Ricardo Veguilla.
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
Lecture 7 Midterm Review. OpenGL Libraries gl: Basic OpenGL library, e.g. primitives. glu: OpenGL Utility library, a set of functions to create texture.
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
Programming with Visual Studio MFC and OpenGL. Outline Creating a project Adding OpenGL initialization code and libraries Creating a mouse event Drawing.
Files A collection of related data treated as a unit. Two types Text
1 Geometric Transformations-II Modelling Transforms By Dr.Ureerat Suksawatchon.
Viewing and Projection. The topics Interior parameters Projection type Field of view Clipping Frustum… Exterior parameters Camera position Camera orientation.
Texture Mapping What is texture mapping? - a method for adding detail, surface texture (a bitmap or raster image), or color to a computer-generated graphic.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL Based on GL (graphics library) by Silicon Graphics Inc. (SGI) Advantages: Runs on everything, including.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when.
OpenGL LAB III.
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
OpenGL: The Open Graphics Language Technology and Historical Overview By Ricardo Veguilla.
School of Computer Science
Texture Mapping 고려대학교 컴퓨터 그래픽스 연구실 kucg.korea.ac.kr.
Programming with OpenGL Part 2: Complete Programs
9. Drawing Let's Learn Python and Pygame
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
texturing a parametric surface
Assignment 3 Particle System
Geb Thomas Adapted from the OpenGL Programming Guide
Chapter 3 arrays of vertices vertex arrays display lists drawing text
Project 1: Into Space! CG Concepts Needed
Chapter 3 arrays of vertices vertex arrays display lists drawing text
Projection in 3-D Glenn G. Chappell
Introduction to OpenGL
Chapter 3 arrays of vertices vertex arrays display lists drawing text
Programming with OpenGL Part 2: Complete Programs
Introduction : What is OpenGL
Programming Textures Lecture 15 Fri, Sep 28, 2007.
Presentation transcript:

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Module 03 Fonty

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Obsah  Zobrazovanie textu  Bitmapové fonty  Outline fonty  2D texture fonty  Demonštračný program

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Zobrazovanie textu  Niekedy je potrebné zobraziť v grafickej aplikácii aj texty  Texty sa používajú napríklad v: omenu oscreensavers odialógy oJednoduché textové efekty Je niekoľko základných techník používaných pri výpise textu pomocou OpenGL knižnice. Mnohé sú závisle od daného operačný systému, na ktorom sa bude aplikácie spúšťať

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Bitmapové Fonty  Bitmapové fonty ponúkajú jednoduchý spôsob na zobrazenie 2D 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í wglUseFont Bitmaps(), ktoré generujú bitmapy z existujúceho súboru s fontom natiahnutého vo vašom OS.

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Outline Fonty  Outline fontsú veľmi podobné bitmapovým fontom  Outline fonty definujú znaky vo fonte ako sériu čiara kriviek, ktoré je možné zväčšovať aj zmenšovať bez straty kvality.  Pomocou OpenGL je možné hýbať s outline fontovým textom okolo obrazovky v 3D, priradiť textu hĺbku, a narábať s fontom rovnako, ako z ľubovolným iným 3D objektom.

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL 2D 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 256x256

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL bitmap font (1) Postup: 1) include header files for working with input and arguments 2) declare new global variable for base display list for the font set 3) declare new global functions for building, printing and releasing the GL font 4) define font building function BuildFont() 5) define font releasing function KillFont() 6) define font printing function glPrint() 7) load and enable the new font while setting up the OpenGL environment 8) draw text 9) properly release font

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL bitmap font (2) 1) include header files for working with input and arguments // // Header Files // … #include // Header File For Standard Input/Output #include // Header File For Variable Argument Routines … 2) declare new global variables for base display list for the font set // // Global Variables // … GLuintbase;// Base Display List For The Font Set …

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL bitmap font (3) 3) declare new global functions for building, printing and releasing font // // Global Functions // … GLvoid BuildFont(GLvoid);// Declaration For BuildFont GLvoid glPrint(const char *fmt,...);// Declaration For glPrint GLvoid KillFont(GLvoid);// Declaration For KillFont … 4) define font building function BuildFont() GLvoid BuildFont(GLvoid) { HFONTfont;// Windows Font ID HFONToldfont;// Used For Good House Keeping base = glGenLists(96);// Storage For 96 Characters … }// end of BuildFont()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL bitmap font (4) 4) define font building function BuildFont() GLvoid BuildFont(GLvoid) { … font = CreateFont(-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 … }// end of BuildFont()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL bitmap font (5) 4) define font building function BuildFont() GLvoid BuildFont(GLvoid) { … oldfont = (HFONT)SelectObject(hDC, font); // Selects The Font We Want // Builds 96 Characters Starting At Character 32 wglUseFontBitmaps(hDC, 32, 96, base); SelectObject(hDC, oldfont);// Selects The Font We Want DeleteObject(font);// Delete The Font }// end of BuildFont()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL bitmap font (6) 5) define font releasing function KillFont() GLvoid KillFont(GLvoid) { glDeleteLists(base, 96);// Delete All 96 Characters }// end of KillFont() 6) define font printing function glPrint() GLvoid glPrint(const char *fmt,...) { chartext[256];// Holds Our String va_listap;// Pointer To List Of Arguments if (fmt == NULL)// If There's No Text return;// Do Nothing … }// end of glPrint()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL bitmap font (7) 6) define font printing function glPrint() GLvoid glPrint(const char *fmt,...) { … va_start(ap, fmt);// Parses The String For Variables vsprintf(text, fmt, ap);// And Converts Symbols To Actual Numbers va_end(ap);// Results Are Stored In Text glPushAttrib(GL_LIST_BIT);// Pushes The Display List Bits glListBase(base - 32);// Sets The Base Character to 32 glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);// Draws The Display List Text glPopAttrib();// Pops The Display List Bits }// end of glPrint()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL bitmap font (8) 7) load and enable the new font while setting up the OpenGL environment int InitGL(GLvoid)// All Setup For OpenGL Goes Here { … BuildFont();// Build The Font … }// end of InitGL(Glvoid) 8) draw text int DrawGLScene(GLvoid) { … glLoadIdentity();// Reset The Current Modelview Matrix glTranslatef(0.0f,0.0f,-1.0f);// Move One Unit Into The Screen glColor3f(1.0,0.0,0.0);// Set Font Color glRasterPos2f(-0.35, 0);// Set Font Position glPrint("Module04 - OpenGL Bitmap Fonts");// Set text … }// end of DrawGLScene()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL bitmap font (9) 9) properly release font GLvoid KillGLWindow(GLvoid) { … KillFont(); // release thefont }// end of KillGLWindow()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Final result Using of bitmap font

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Practice Open: Lab03 / BitmapFont / BitmapFont.sln

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL 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 BuildFont() 5) define outline font releasing function KillFont() 6) define outline font printing function glPrint() 7) load and enable the new outline font setting up the OpenGL environment 8) draw text 9)properly release font

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL outline font (2) 1) include header files for working with input and arguments // // Header Files // … #include // Header File For Standard Input/Output #include // Header File For Variable Argument Routines … 2) declare new global variables for text rotation and outline font characters storage info // // Global Variables // … GLuint base; // Base Display List For The Font Set GLuintrot;// Used To Rotate The Text GLYPHMETRICSFLOAT gmf[256]; // Storage For Information About Our Outline Font Characters …

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL outline font (3) 3) declare new global functions for building, printing and releasing outline font // // Global Functions // … GLvoid BuildFont(GLvoid);// Declaration For BuildFont GLvoid glPrint(const char *fmt,...);// Declaration For glPrint GLvoid KillFont(GLvoid);// Declaration For KillFont … 4) define outline font building function BuildFont() GLvoid BuildFont(GLvoid) { HFONTfont;// Windows Font ID base = glGenLists(256);// Storage For 256 Characters … }// end of BuildFont()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL outline font (4) 4) define outline font building function BuildFont() GLvoid BuildFont(GLvoid) { … font = CreateFont(-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 … }// end of BuildFont()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL outline font (5) 4) define outline font building function BuildFont() GLvoid BuildFont(GLvoid) { … SelectObject(hDC, font);// Selects The Font We Created wglUseFontOutlines(hDC,// Select The Current DC 0,// Starting Character 255,// Number Of Display Lists To Build base,// Starting Display Lists 0.0f,// Deviation From The True Outlines 0.2f,// Font Thickness In The Z Direction WGL_FONT_POLYGONS,// Use Polygons, Not Lines gmf);// Address Of Buffer To Recieve Data }// end of BuildFont()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL outline font (6) 5) define outline font releasing function KillFont() GLvoid KillFont(GLvoid) { glDeleteLists(base, 256);// Delete All 256 Characters } // end of KillFont() 6) define outline font printing function glPrint() GLvoid glPrint(const char *fmt,...) { chartext[256];// Holds Our String va_listap;// Pointer To List Of Arguments if (fmt == NULL)// If There's No Text return;// Do Nothing … }// end of glPrint()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL outline font (7) 6) define font printing function glPrint() GLvoid glPrint(const char *fmt,...) { … va_start(ap, fmt);// Parses The String For Variables vsprintf(text, fmt, ap);// And Converts Symbols To Actual Numbers va_end(ap);// Results Are Stored In Text for (unsigned int loop=0;loop<(strlen(text));loop++)// Loop To Find Text Length { length+=gmf[text[loop]].gmfCellIncX; // Increase Length By Each Characters Width } glTranslatef(-length/2,0.0f,0.0f);// Center Our Text On The Screen glPushAttrib(GL_LIST_BIT);// Pushes The Display List Bits glListBase(base);// Sets The Base Character to 0 glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);// Draws The Display List Text glPopAttrib();// Pops The Display List Bits }// end of glPrint()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL outline font (8) 7) load and enable the new font while setting up the OpenGL environment int InitGL(GLvoid)// All Setup For OpenGL Goes Here { … BuildFont();// Build The Font … }// end of InitGL(Glvoid) 8) draw text int DrawGLScene(GLvoid) { … glLoadIdentity();// Reset The Current Modelview Matrix glTranslatef(0.0f,0.0f,-15.0f);// Move One Unit Into The Screen … }// end of DrawGLScene()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL outline font (9) 8) draw text int DrawGLScene(GLvoid) { … glRotatef(rot,1.0f,0.0f,0.0f);// Rotate On The X Axis glRotatef(rot*1.5f,0.0f,1.0f,0.0f);// Rotate On The Y Axis glRotatef(rot*1.4f,0.0f,0.0f,1.0f);// Rotate On The Z Axis glColor3f(1,0,0); // Set Outline Font Color glPrint("Module04 - OpenGL Outline Fonts");// Print GL Text To The Screen rot+=0.25f;// Increase The Rotation Variable … } // end of DrawGLScene()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing OpenGL outline font (10) 9) properly release font GLvoid KillGLWindow(GLvoid) { … KillFont(); // release thefont } // end of KillGLWindow()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Final result OpenGL outline font

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Practice Open: Lab03 / OutlineFont / OutlineFont.sln

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing 2D texture OpenGL 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 text texture, loop and to move text & for coloring 3) declare new global functions for loading font bmp file, converting it to the texture, building, printing and releasing 2D texture font 4) load font bmp file 5) load font bitmaps and convert to texture 6) define 2D texture font building function BuildFont() 7) define 2D texture font releasing function KillFont() 8) define 2D texture font printing function glPrint() 9) load and enable the new 2D texture font setting up the OpenGL environment 10) draw text 11) properly release font

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing 2D texture OpenGL font (2) 1) include header files for working with math and input operations in init.h // // Header Files // … #include // Header File For Windows Math Library #include // Header File For Standard Input/Output … 2) declare new global variables to point us to our display list, for text texture, loop and to move text & for coloring // // Global Variables // … GLuintbase;// Base Display List For The Font GLuinttexture[1];// Storage For Font Texture GLuintloop;// Generic Loop Variable …

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing 2D texture OpenGL font (3) 2) declare new global variables to point us to our display list, for text texture, loop and to move text & for coloring … GLfloatcnt1;// 1st Counter Used To Move Text & For Coloring GLfloatcnt2;// 2nd 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 2D texture font // // Global Functions // … AUX_RGBImageRec *LoadBMP(char *Filename);// Declaration For LoadBMP int LoadGLTextures();// Declaration For LoadGLTextures GLvoid BuildFont(GLvoid);// Declaration For BuildFont GLvoid KillFont(GLvoid);// Declaration For KillFont GLvoid glPrint(GLint x, GLint y, char *string, int set);// Declaration For glPrint …

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing 2D texture OpenGL font (4) 4) load font bmp file AUX_RGBImageRec *LoadBMP(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 auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer } return NULL; // If Load Failed Return NULL } // end of LoadBMP()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing 2D texture OpenGL font (5) 5) load font bitmaps and convert to texture int LoadGLTextures() { int Status=FALSE; // Status Indicator AUX_RGBImageRec *TextureImage[2]; // Create Storage Space For The Textures memset(TextureImage,0,sizeof(void *)*2); // Set The Pointer To NULL if (TextureImage[0]=LoadBMP("Data/Font.bmp")) { Status=TRUE; // Set The Status To TRUE glGenTextures(1, &texture[0]); // Create One Texture glBindTexture(GL_TEXTURE_2D, texture[0]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); … }// end of LoadGLTextures()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing 2D texture OpenGL font (6) 5) load font bitmaps and convert to texture int LoadGLTextures() {... glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[loop]->sizeX, TextureImage[loop]- >sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); } if (TextureImage[0])// If Texture Exists { if (TextureImage[0]->data)// If Texture Image Exists { free(TextureImage[0]->data); } free(TextureImage[0]); } return Status; // Return The Status }// end of LoadGLTextures()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing 2D texture OpenGL font (7) 6) defefine 2D texture font building function BuildFont() GLvoid BuildFont(GLvoid) { floatcx;// Holds Our X Character Coord floatcy;// Holds Our Y Character Coord base=glGenLists(256);// Creating 256 Display Lists glBindTexture(GL_TEXTURE_2D, texture[0]);// Select Our Font Texture for (loop=0; loop<256; loop++)// Loop Through All 256 Lists { cx=float(loop%16)/16.0f;// X Position Of Current Character cy=float(loop/16)/16.0f;// Y Position Of Current Character glNewList(base+loop,GL_COMPILE);// Start Building A List …// Loop Until All 256 Are Built }// end of BuildFont()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing 2D texture OpenGL font (8) 6) defefine 2D texture font building function BuildFont() GLvoid BuildFont(GLvoid) { … glBegin(GL_QUADS);// Use A Quad For Each Character glTexCoord2f(cx,1-cy f);// Texture Coord (Bottom Left) glVertex2i(0,0);// Vertex Coord (Bottom Left) glTexCoord2f(cx f,1-cy f);// Texture Coord (Bottom Right) glVertex2i(16,0);// Vertex Coord (Bottom Right) glTexCoord2f(cx f,1-cy);// Texture Coord (Top Right) glVertex2i(16,16);// Vertex Coord (Top Right) glTexCoord2f(cx,1-cy);// Texture Coord (Top Left) glVertex2i(0,16);// Vertex Coord (Top Left) glEnd();// Done Building Our Quad (Character) glTranslated(10,0,0);// Move To The Right Of The Character glEndList();// Done Building The Display List }// Loop Until All 256 Are Built } // end of BuildFont()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing 2D texture OpenGL font (9) 7) defefine 2D texture font releasing function KillFont() GLvoid KillFont(GLvoid) { glDeleteLists(base,256);// Delete All 256 Display Lists } // end of KillFont() 8) define 2D texture font printing function glPrint() GLvoid glPrint(GLint x, GLint y, char *string, int set) { if (set>1) { set=1; } glBindTexture(GL_TEXTURE_2D, texture[0]);// Select Our Font Texture glDisable(GL_DEPTH_TEST);// Disables Depth Testing … }// end of glPrint()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing 2D texture OpenGL font (10) 8) define 2D texture font printing function glPrint() GLvoid glPrint(GLint x, GLint y, char *string, int set) { … glMatrixMode(GL_PROJECTION);// Select The Projection Matrix glPushMatrix();// Store The Projection Matrix glLoadIdentity();// Reset The Projection Matrix glOrtho(0,640,0,480,-1,1);// Set Up An Ortho Screen glMatrixMode(GL_MODELVIEW);// Select The Modelview Matrix glPushMatrix();// Store The Modelview Matrix glLoadIdentity();// Reset The Modelview Matrix glTranslated(x,y,0);// Position The Text (0,0 - Bottom Left) glListBase(base-32+(128*set));// Choose The Font Set (0 or 1) glCallLists(strlen(string),GL_BYTE,string);// Write The Text To The Screen glMatrixMode(GL_PROJECTION);// Select The Projection Matrix glPopMatrix();// Restore The Old Projection Matrix glMatrixMode(GL_MODELVIEW);// Select The Modelview Matrix glPopMatrix();// Restore The Old Projection Matrix glEnable(GL_DEPTH_TEST);// Enables Depth Testing } // end of glPrint()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing 2D texture OpenGL font (11) 9) load and enable the new 2D texture font setting up the OpenGL environment int InitGL(GLvoid) { if (!LoadGLTextures())// Jump To Texture Loading Routine { return FALSE;// If Texture Didn't Load Return FALSE } BuildFont();// Build The Font glClearColor(0.0f, 0.0f, 0.0f, 0.0f);// Clear The Background Color To Black glClearDepth(1.0);// Enables Clearing Of The Depth Buffer glDepthFunc(GL_LEQUAL);// The Type Of Depth Test To Do glBlendFunc(GL_SRC_ALPHA,GL_ONE);// Select The Type Of Blending glShadeModel(GL_SMOOTH);// Enables Smooth Color Shading glEnable(GL_TEXTURE_2D);// Enable 2D Texture Mapping return TRUE;// Initialization Went OK }// end of InitGL()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing 2D texture OpenGL font (12) 10) draw text int DrawGLScene(GLvoid) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_BLEND);// Enable Blending glLoadIdentity();// Reset The View // Pulsing Colors Based On Text Position glColor3f(1.0f*float(cos(cnt1)),1.0f*float(sin(cnt2)),1.0f-0.5f*float(cos(cnt1+cnt2))); // Print GL Text To The Screen glPrint(int(( *cos(cnt1))),int( *sin(cnt2)),"Module04",0); // Pulsing Colors Based On Text Position glColor3f(1.0f*float(sin(cnt2)),1.0f-0.5f*float(cos(cnt1+cnt2)),1.0f*float(cos(cnt1))); // Print GL Text To The Screen glPrint(int(( *cos(cnt2))),int( *sin(cnt1)),"2D Texture Font",1); cnt1+=0.01f;// Increase The First Counter cnt2+=0.0081f;// Increase The Second Counter return TRUE;// Everything Went OK }// end of DrawGLScene()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Drawing 2D texture OpenGL font (13) 11) properly release font GLvoid KillGLWindow(GLvoid) { … KillFont(); // release thefont } // end of KillGLWindow()

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Final result OpenGL 2D texture font

Module 03 –Fonty v OpenGL Module 03 – Fonty v OpenGL Practice Open: Lab03 / BitmapFont2 / BitmapFont2.sln