您的位置:首页 > 运维架构

opengl学习笔记(二)---中文显示

2013-04-21 12:40 495 查看
此方法中用到了glut,freeglut, freetype等库

1.显示英文字符

Opengl 显示英文字符相对做的处理要少一点,可以用glut封装的函数在屏幕上显示英文字符;

//! Draw a bitmap string;
static void bitmapString(void* fontId, std::string strText)
{
for (unsigned int i = 0; i < (size_t)strText.size(); ++i)
{
glutBitmapCharacter(fontId, strText.at(i));
}

std::string strLog = "Draw:" + strText;
cout << strLog << endl;
}


void display()
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2f(0, 0);
glViewport(0,0,glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
bitmapString(GLUT_BITMAP_HELVETICA_18, "opengl draw bitmap string test.");


int main()

{
.....
.....
glutDisplayFunc(display);
.....
return 0;
}

2.中文字符

用开源库freetype,有效管理加载*.ttf字体资源
代码:
//Created 2013/04/21;

//! C++;
#include <windows.h>
#include <stdio.h>
#include <stdexcept>
#include <string>
#include <iostream>

//! opengl;
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glut.h>

//! freetype;
#include <ft2build.h>
#include FT_FREETYPE_H
#include <freetype/freetype.h>
#include <freetype/ftglyph.h>

using namespace std;

namespace TE
{
class xFreeTypeLib
{
public:
struct xCharTexture
{
GLuint  m_texID;
wchar_t m_chaID;
int     m_Width;
int     m_Height;

int     m_adv_x;
int     m_adv_y;
int     m_delta_x;
int     m_delta_y;

xCharTexture()
{
m_texID  = 0;
m_chaID  = 0;
m_Width  = 0;
m_Height = 0;
}
};
xCharTexture g_TexID[65536];

FT_Library m_FT2Lib;
FT_Face    m_FT_Face;

int   m_w;
int   m_h;
public:
xFreeTypeLib();
void load(const char* font_file , int _w , int _h);
GLuint loadChar(wchar_t ch);

xCharTexture* getTextChar(wchar_t ch);

};
class TEFont
{
public:
TEFont(void);
~TEFont(void);

xFreeTypeLib m_FreeTypeLib;

static TEFont* sharedFont(void);

void drawText(wchar_t* _strText,int x , int y, int maxW , int h);
};

}


#include "../TE/TEApplication.h"
#include "../TE/TEGlutWindow.h"
#include "../TE/TEFont.h"

class TEFontTestWin : public TEGlutWindow
{
public:
TEFontTestWin(){}
~TEFontTestWin(){}
virtual void onInit()
{
glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
glEnable ( GL_COLOR_MATERIAL );
glColorMaterial ( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );

TEFont::sharedFont()->m_FreeTypeLib.load("C:\\WINDOWS\\Fonts\\simfang.ttf",20,20);

glDisable ( GL_CULL_FACE );

}
virtual void onRender()
{
glClearColor(0.0f , 0.0f , 0.0f , 0.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glEnable ( GL_TEXTURE_2D );

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glColor3f(1,1,1);
glViewport(0,0,glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
glOrtho(0,glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT),0,-100,200);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 30 ,0 , 0 ,10 , 0.0f , 1.0f , 0.0f);

wchar_t strText[] = L"FreeType库是一个完全免费(开源)的、高质量的且可移植的字体引擎,它提供统一的接口来访问多种字体格式文件,包括TrueType, OpenType";
TEFont::sharedFont()->drawText(strText,50,50,900,25);
glutSwapBuffers();
}
};
int main(void)
{
TEApplication* pApp = new TEApplication();
TEFontTestWin* pWin = new TEFontTestWin();
pApp->run();

delete pWin;
delete pApp;
return 0;
}


运行结果:



3.参考链接

1.freetype2 中文显示

2.FreeType Fonts in OpenGL
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: