大浪淘沙 给我一片天空。。。。
▓   .   *  __       *       ☆
   ./  \~~~▓~~~~\| ◆         .
   / # \ ══════\.◆      *  *
 ..▎[] ▎ 田  田▎ | ┃◆   .
   ▎   ▎          ▎'| '▎ @       .
   ■■■■■■■■■■〓▄▃▂▁
« 上一篇: [转帖]总结:m文件转化为c/c++语言文件,VC编译 下一篇: 注册码大全 »
船长 @ 2004-09-08 10:02

发信人: lanhaitao (蓝天-大海-波涛-你和我), 信区: VR_3D       
标  题: OpenGL中汉字显示方法例程
发信站: BBS 水木清华站 (Fri Feb 11 14:06:04 2000)

//编译好马上、就可以显示!

#include <windows.h>
#include <GL/gl.h>
#include <GL/glaux.h>
#pragma warning(disable:4244)
void myinit(void);
void makeStringFont(char *);
void printString(void);
void CALLBACK myReshape(GLsizei w, GLsizei h);
void CALLBACK display(void);
#define CHN_TEXT_LIST 1
void getBitmap(BITMAP *bmp, LONG width, LONG height)
{
   FillMemory(bmp, sizeof(*bmp), 0);
   bmp->bmWidth = width;
   bmp->bmHeight = height;
   bmp->bmWidthBytes = ((width + 7) / 8 + 1) & (~1);
   bmp->bmPlanes = 1;
   bmp->bmBitsPixel = 1;
   bmp->bmBits = GlobalAlloc(GMEM_FIXED, bmp->bmWidthBytes*height);
}
unsigned char *getGLmonoBits(HDC hDC, HBITMAP hBmp, int* size)
{
   BITMAP bi;
   unsigned char   *bits;
   struct {
       BITMAPINFOHEADER bih;
       RGBQUAD col[2];
   }bic;
   BITMAPINFO *binf = (BITMAPINFO *)&bic;
   GetObject(hBmp, sizeof(bi), &bi);
   *size = bi.bmHeight*(((bi.bmWidth + 31) & (~31)) / 8);
   bits = (unsigned char *)GlobalAlloc(GPTR, *size);
   binf->bmiHeader.biSize = sizeof(binf->bmiHeader);
   binf->bmiHeader.biWidth = bi.bmWidth;
   binf->bmiHeader.biHeight = bi.bmHeight;
   binf->bmiHeader.biPlanes = 1;
   binf->bmiHeader.biBitCount = 1;
   binf->bmiHeader.biCompression = BI_RGB;
   binf->bmiHeader.biSizeImage = *size;
   binf->bmiHeader.biXPelsPerMeter = 1;
   binf->bmiHeader.biYPelsPerMeter = 1;
   binf->bmiHeader.biClrUsed = 0;
   binf->bmiHeader.biClrImportant = 0;
   GetDIBits(hDC, hBmp, 0, bi.bmHeight, bits, binf, DIB_RGB_COLORS);
   return bits;
}
unsigned char *createStringBitmapFont(HDC hDC, HFONT hFont, char *str, PSIZE
L size)
{
   BITMAP  bmp;
   HBITMAP hbmp;
   unsigned char    *pbm;
   int     len = lstrlen(str);
   HFONT   hFontOld = SelectObject(hDC, hFont);
   GetTextExtentPoint32(hDC, str, len, size);
   getBitmap(&bmp, size->cx, size->cy);
   hbmp = CreateBitmapIndirect(&bmp);
   GlobalFree(bmp.bmBits);
   if(hbmp){
       HDC hMemDC = CreateCompatibleDC(hDC);
       if(hMemDC){
           HBITMAP hPrevBmp = SelectObject(hMemDC, hbmp);
           HFONT   hPrevFont;
           int     size0;
           BITMAP  bi;
           SetBkColor(hMemDC, RGB(0,0,0));
           SetTextColor(hMemDC, RGB(255,255,255));
           SetBkMode(hMemDC, OPAQUE);
           hPrevFont = SelectObject(hMemDC, hFont);
           TextOut(hMemDC, 0, 0, str, len);
           GetObject(hbmp, sizeof(bi), &bi);
           pbm =getGLmonoBits(hMemDC, hbmp, &size0);
           size->cx = ((bi.bmWidth+31)&(~31));
           size->cy = bi.bmHeight;
           SelectObject(hMemDC, hPrevFont);
           SelectObject(hMemDC, hPrevBmp);
           DeleteDC(hMemDC);
       }
       else{
           DeleteObject(hbmp);
       }
   }
   SelectObject(hDC, hFontOld);
   return pbm;
}
void makeStringFont(char *str)
{
   SIZE    size;
   LOGFONT lf;
   HFONT   hFont;
   unsigned char *lpBitmap;
   HDC     hDC = GetDC(GetFocus());
   FillMemory(&lf, sizeof(lf), 0);
   lf.lfHeight = 20* GetDeviceCaps(hDC, LOGPIXELSY) / 70;
   lf.lfCharSet = GB2312_CHARSET;
   lf.lfWeight = 1;
   lf.lfOutPrecision = OUT_STROKE_PRECIS;
   lf.lfClipPrecision = CLIP_CHARACTER_PRECIS;
   lstrcpy(lf.lfFaceName, "");
   hFont = CreateFontIndirect(&lf);
   lpBitmap = createStringBitmapFont(hDC, hFont, str, &size);
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   glBitmap(size.cx, size.cy, 0.0, 2.0, size.cx+2, 0.0, lpBitmap);
   GlobalFree(lpBitmap);
}
void CALLBACK display(void)
{
   glClear(GL_COLOR_BUFFER_BIT);
   glRasterPos2i(50, 150);
makeStringFont("这就是汉字!");
glRasterPos2i(50, 100);
makeStringFont("你不识汉字?");
glRasterPos2i(50, 50);
makeStringFont("靠!!我服了你!");
glFlush();
}
void CALLBACK myReshape(GLsizei w, GLsizei h)
{
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho (0.0, w, 0.0, h, -1.0, 1.0);
   glMatrixMode(GL_MODELVIEW);
}
int APIENTRY WinMain(HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR     lpCmdLine,
                    int       nCmdShow)
{
   auxInitDisplayMode (AUX_SINGLE | AUX_RGB);
   auxInitPosition (0, 0, 400,200);
   auxInitWindow ("Chinese Text");
 glShadeModel (GL_FLAT);
   auxReshapeFunc (myReshape);
   auxMainLoop(display);
   return(0);
}

--
※ 来源:·BBS 水木清华站 smth.org·[FROM: 202.38.252.11]


评论 / 个人网页 / 扔小纸条
*昵称

已经注册过? 请登录

Email
网址
*评论
 


 
日 历
个 人 简 介
· 姓名:船长
· 学历:博士
· 院校:中科院
· 给我留言
网志文件夹
· 所有网志
· vc++编程
· Linux
· OpenGL
· MatLab
· Protel
· 单片机开发
· 课题相关
· 虚拟现实
· 电脑应用
· 奇文欣赏
· 乱七八糟
· 我的开发日志
· 数据手套
· 未分类
搜 索
友 情 链 接
· 歪酷博客
· 管理我的Blog
· 山城棒棒儿的MATLAB世界
· 西陆社区
· 中国数学建模
· 精诚电子设计
· SimWe---仿真论坛
· 研学论坛
· 中国软件-技术频道
· 我的U盘
· Azure个人博客OpenGL、CG Language、游戏构架...

订阅 RSS

0181046

狗狗评测:PageRank

歪酷博客