发信人: 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]


