现在的位置: 首页 > 综合 > 正文

【转载】Nokia上使用OPENGL ES GLFONT类

2012年02月05日 ⁄ 综合 ⁄ 共 2994字 ⁄ 字号 评论关闭

Texture-mapped font for OpenGL ES

Reviewer Approved    http://www.forum.nokia.com/piazza/wiki/images/thumb/3/34/Thumbs_up_icon_sm.jpg/31px-Thumbs_up_icon_sm.jpg

An efficient approach to implement fonts in OpenGL is to
use a single texture-mapped quad for each character. This technique has very
good performance and presents nice results. At this link
 [1], there is more information on this
subject.

glFont

The glFont tool [2] is a well-known Windows application
and
 API to generate textures for fonts, and to
render them using OpenGL. According to its licence, this tool is free to be
used in any program, commercial or non-commercial
 [3]. The original author is Brad Fish
(brad.fish@gmail.com).

Here is the font class converted to the Symbian OS API
(The original author gently allowed the conversion to be published here):

//*******************************************************************
// glfont2.h -- Header for glfont2.cpp
// Copyright (c) 1998-2002 Brad Fish
// See glfont.html for terms of use
// May 14, 2002
//
// Symbian OS port - June 2007
// Luis Valente - lpvalente
(http://wiki.forum.nokia.com/index.php/User:Lpvalente)

//
//*******************************************************************
 
#ifndef GLFONT2_H
#define GLFONT2_H
 
#include <e32base.h>
#include <GLES/gl.h>
 
//_____________________________________________________________________________
//
// Simple class to output text as texture-mapped triangles. Does not
support

// unicode strings. Reference point when drawing: top-left.
//
 
class GLFont
{    
 
public:
 
  
/**
    * Factory-method.
    */

  
static GLFont* NewL (const TDesC & aFilename);
 
 
public:
 
  
/**
    * Destructor.
    */
      
   ~GLFont
();
 
 
public:
 
 
  
/**
    * Retrieves the texture width and
height.
    */

  
void GetTexSize (TInt & aWidth, TInt & aHeight);
 
  
/**
    * Retrieves the character interval.
    */

  
void GetCharInterval (TInt & aStart, TInt & aEnd);
 
  
/**
    * Retrieves the character dimensions.
    */

  
void GetCharSize (TText8 c, TInt & aWidth, TInt
aHeight
);
 
 
  
/**
    * Calculates the dimensions of a
string.
    */

  
void GetStringSize (const TDesC8 & aText, TInt & aWidth, TInt & aHeight);
 
  
/**
    * Renders a string.
    */

  
void DrawString (const TDesC8 & aText, GLfixed
aX, GLfixed aY
); 
 
  
/**
    * Sets required states for the font.
    */

  
void BeginDraw ()
  
{              
    glEnable
(GL_BLEND);
    glBlendFunc
(GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA
);
    glEnable
(GL_TEXTURE_2D);
    glEnableClientState
(GL_TEXTURE_COORD_ARRAY);
  
}
 
  
/**
    * Turns off required states.
    */

  
void EndDraw ()
  
{              
    glDisable
(GL_BLEND);           
    glDisable
(GL_TEXTURE_2D);
    glDisableClientState
(GL_TEXTURE_COORD_ARRAY);
  
}        
 
 
private:
 
   
/**
     * Default constructor.
     */

   GLFont
();
 
  
/**
    * Final part of the two-phase
constructor.
    */

  
void ConstructL (const TDesC & aFilename);
 
  
/**
    * Loads the font file.
    */

  
void LoadFileL (RFs & aFs, const TDesC & aFilename);      
 
  
/**
    * Destroys the font.
    */
      
  
void Destroy ();           
 
 
 
private:  
 
  
// single character
  
struct GLFontChar
  
{
      GLfixed dx, dy
;
      GLfixed tx1, ty1
;
      GLfixed tx2, ty2
;
  
};
 
  
// font header
  
struct GLFontHeader
  
{
      GLuint tex
;
      TInt   texWidth, texHeight
;
      TInt   startChar, endChar
;
      GLFontChar
*chars;
  
};             
 
 
private:
 
   GLFontHeader iHeader
;
};
 
//*******************************************************************
#endif

Here is the class implementation:

抱歉!评论已关闭.