使用OPENGL显示中文的类

    使用OPENGL显示中文的类

//GLfont.cpp: implementation of the GLfont class.
//
//

#include "GLfont.h"
#include "gl/gl.h"

//
// Construction/Destruction
//

GLfont::GLfont()
{
 
}

GLfont::~GLfont()
{
 if(m_hFont) DeleteObject(m_hFont); 
}

//
//
//

void GLfont::CreateFont(char *facename, int height, int weight, bool italic,bool underline,bool strikeout)
{
 LOGFONT lf;

 
 lf.lfHeight = height;
 lf.lfWidth = 0;       
 lf.lfEscapement = 0;  
 lf.lfOrientation = 0; 
 lf.lfWeight = weight; 
 lf.lfItalic = italic; 
 lf.lfUnderline = underline; 
 lf.lfStrikeOut = strikeout;
 lf.lfCharSet = DEFAULT_CHARSET;
 lf.lfOutPrecision = OUT_TT_PRECIS; 
 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS; 
 lf.lfQuality = PROOF_QUALITY; 
 lf.lfPitchAndFamily = VARIABLE_PITCH | TMPF_TRUETYPE | FF_MODERN; 
 strcpy(lf.lfFaceName,facename); 
 
 // 创建字体
 m_hFont = CreateFontIndirect(&lf);
 
}

void GLfont::ShowText(int x, int y, LPCTSTR lpszText)
{
 // 保存原投影矩阵,将投影矩阵设为平行投影
 glMatrixMode( GL_PROJECTION );
 glPushMatrix();
  glLoadIdentity();
  glOrtho( 0, 640, 0, 480, -1, 1 );
  // 保存原模型变换矩阵,平移至( x, y )
  glMatrixMode( GL_MODELVIEW );
  glPushMatrix();
   glLoadIdentity();
   RECT rect;
   GetClientRect(GetActiveWindow(),&rect);
   glTranslatef(x, float(rect.bottom-rect.top-35-y), 0 );
 //
 //
 //
 HBITMAP hbitmap;
 BITMAP bm;
 SIZE size;
 UCHAR* pBmpBits;
 HFONT hOldFont;
 HDC hdc = wglGetCurrentDC();
 
 
 hOldFont = (HFONT)SelectObject(hdc, m_hFont);
 ::GetTextExtentPoint32(hdc, lpszText, strlen(lpszText), &size);
 hbitmap = CreateBitmap(size.cx, size.cy,1, 1, NULL);
 
 HDC hMemDC = ::CreateCompatibleDC(hdc);
 if(hMemDC)
 {
  HBITMAP hPrevBmp = (HBITMAP)SelectObject(hMemDC,hbitmap);
  HFONT hPrevFont = (HFONT)SelectObject(hMemDC, m_hFont);
  
  SetBkColor(hMemDC, RGB(0, 0, 0));
  SetTextColor(hMemDC, RGB(255, 255, 255));
  SetBkMode(hMemDC, OPAQUE);
  TextOut(hMemDC, 0, 0, lpszText, strlen(lpszText));
  
  // copy GDI bitmap to DIB
  SelectObject(hdc,hbitmap);
  GetObject(hbitmap, sizeof(bm), &bm);
 
 size.cx = (bm.bmWidth + 31) & (~31);
  size.cy = bm.bmHeight;
  int bufsize = size.cy * (((bm.bmWidth + 31) & (~31)) /8);
  pBmpBits = new UCHAR[bufsize];
  memset(pBmpBits, 0, sizeof(UCHAR)*bufsize);
  
  struct {
   BITMAPINFOHEADER bih;
   RGBQUAD col[2];
  }bic;
  BITMAPINFO *binf = (BITMAPINFO *)&bic;
 
  binf->bmiHeader.biSize = sizeof(binf->bmiHeader);
  binf->bmiHeader.biWidth = bm.bmWidth;
  binf->bmiHeader.biHeight = bm.bmHeight;
  binf->bmiHeader.biPlanes = 1;
  binf->bmiHeader.biBitCount = 1;
  binf->bmiHeader.biCompression = BI_RGB;
  binf->bmiHeader.biSizeImage = bufsize;
  binf->bmiHeader.biXPelsPerMeter = 1;
  binf->bmiHeader.biYPelsPerMeter = 1;
  binf->bmiHeader.biClrUsed = 0;
  binf->bmiHeader.biClrImportant = 0;
  
  ::GetDIBits(hdc, hbitmap, 0, bm.bmHeight, pBmpBits, binf,DIB_RGB_COLORS);
   
  SelectObject(hMemDC, hPrevBmp);
 }
 ::DeleteDC(hMemDC);
 
 // delete font from DC
 SelectObject(hdc, hOldFont);
 // display text
 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 glRasterPos2i(1,20);
 glBitmap(size.cx, size.cy, 0.0, 2.0, size.cx+2.0f, 0.0, pBmpBits);
 delete pBmpBits;
 // 恢复投影矩阵和模型变换矩阵
   glMatrixMode( GL_PROJECTION );
   glPopMatrix();
  glMatrixMode( GL_MODELVIEW );
  glPopMatrix();
}
 


 

// GLfont.h: interface for the CMiscGL class.
//
//

#if !defined(AFX_MISCGL_H__3CF8DCE9_7A54_4519_AA65_5B375C048664__INCLUDED_)
#define AFX_MISCGL_H__3CF8DCE9_7A54_4519_AA65_5B375C048664__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <windows.h>
class GLfont
{
 HFONT m_hFont;
public:
 GLfont();
 virtual ~GLfont();

 void CreateFont(char *facename, int height, int weight, bool italic,bool underline,bool strikeout);
 void ShowText(int x, int y, LPCTSTR lpszText);

};

#endif // !defined(AFX_MISCGL_H__3CF8DCE9_7A54_4519_AA65_5B375C048664__INCLUDED_)

 


 

示例:

GLfont font;

{
font.CreateFont("黑体",30,FW_BOLD,TRUE,TRUE,FALSE);//CreateFont("宋体",20,FW_DONTCARE,FALSE,FALSE,FALSE);
//.....
}

//
//
//

{
font.ShowText(100,100,"在OPENGL中显示中文");
//.......

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值