资源简介

OpenGL使用Window API绘制矢量字体(非常简单)

资源截图

代码片段和文件信息

#include “Font.h“

bool CFontPrinter::makeChar(wchar_t wChar)
{
HDC hdc = CreateCompatibleDC(wglGetCurrentDC());

if(!hdc)
return false;

HBITMAP hbitmap = CreateCompatibleBitmap(hdc 1 1);
HBITMAP hbitmapOld = (HBITMAP)Selectobject(hdc hbitmap);

if((DWORD)hbitmapOld == GDI_ERROR)
return false;

CFontData* fontData = new CFontData();

glGenTextures(1 &fontData->m_TextureID);
glBindTexture(GL_TEXTURE_2D fontData->m_TextureID);
glTexParameteri(GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR);
glTexEnvi(GL_TEXTURE_ENV GL_TEXTURE_ENV_MODE GL_MODULATE);

glPixelStorei(GL_UNPACK_SWAP_BYTES GL_FALSE);
glPixelStorei(GL_UNPACK_LSB_FIRST GL_FALSE);
glPixelStorei(GL_UNPACK_ROW_LENGTH 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS 0);
glPixelStorei(GL_UNPACK_ALIGNMENT 1);

int iTexWidth = m_FontSize;
int iTexHeight = m_FontSize;
int iLevel = 0;

if (!m_Font)
{
// 创建文字
m_Font = CreateFontW(-m_FontSize 0 0 0 500 false false false
DEFAULT_CHARSET OUT_TT_ONLY_PRECIS CLIP_DEFAULT_PRECIS
DEFAULT_QUALITY DEFAULT_PITCH | (m_FontName?FF_DONTCARE:FF_SWISS) m_FontName);
}
if(!m_Font)
return false;

HFONT hfontOld = (HFONT)Selectobject(hdc m_Font);
if((DWORD)hfontOld == GDI_ERROR)
return false;

GLYPHMETRICS gm={0};
MAT2 const matrix22_identity={{01}{00}{00}{01}};

DWORD dwBuffSize = GetGlyphOutlineW(hdc wChar GGO_GRAY8_BITMAP &gm 0 NULL &matrix22_identity);

if((signed)dwBuffSize == -1)
{
return false;
}

if(GetGlyphOutlineW(hdc wChar GGO_METRICS &gm 0 NULL &matrix22_identity)==GDI_ERROR)
{
return false;
}

BYTE *pBuff = new BYTE[dwBuffSize];
BYTE *pSwapBuff=new BYTE[dwBuffSize];
memset(pBuff 0xff dwBuffSize);
memset(pSwapBuff 0xff dwBuffSize);

if(GetGlyphOutlineW(hdc wChar GGO_GRAY8_BITMAP &gm dwBuffSize pBuff &matrix22_identity) == GDI_ERROR)
{
delete[] pBuff;
return false;
}

if(gm.gmBlackBoxY == 0)
{
printf(“black box Y zero!!!\n“);
return false;
}

// 从 Gray64 转换到 Gray256
unsigned int const uiRowSize = dwBuffSize / gm.gmBlackBoxY;
BYTE* pPtr;
BYTE* pSPtr;
for(unsigned int nY = 0; nY < gm.gmBlackBoxY; nY++)
{
pPtr = pBuff + uiRowSize * nY;
pSPtr = pSwapBuff + gm.gmBlackBoxX * nY;
for (unsigned int nX = 0; nX < gm.gmBlackBoxX; nX++)
{
if (*pPtr == 0)
{
*pSPtr = 0;
}
else if (*pPtr == 0x40)
{
*pSPtr = 0xff;
}
else
{
*pSPtr = *pPtr << 2;
}

pPtr++;
pSPtr++;
}
}

// 加载纹理
glTexImage2D(GL_TEXTURE_2D iLevel GL_LUMINANCE8 gm.gmBlackBoxX gm.gmBlackBoxY 0 GL_LUMINANCE GL_UNSIGNED_BYTE pSwapBuff);

// 记录文字信息
fontData->m_FontWidth = (float)gm.gmCellIncX / (float)m_FontSize;
fontData->m_Width = (float)gm.gmBlackBoxX / (float)m_FontSize;
fontData->m_Height = (float)g

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       6126  2014-08-21 13:59  FontDemo\FontDemo\Debug\BuildLog.htm

     文件       4676  2014-08-21 13:51  FontDemo\FontDemo\Font.cpp

     文件       1083  2014-08-21 13:14  FontDemo\FontDemo\Font.h

     文件       7354  2014-08-21 14:09  FontDemo\FontDemo\FontDemo.cpp

     文件       3602  2014-08-21 14:00  FontDemo\FontDemo\FontDemo.vcproj

     文件       1415  2014-08-21 17:09  FontDemo\FontDemo\FontDemo.vcproj.TianYu-PC.TianYu.user

     文件       6202  2014-08-21 16:45  FontDemo\FontDemo\Release\BuildLog.htm

     文件        890  2014-08-20 13:37  FontDemo\FontDemo.sln

    ..A..H.     23552  2014-08-21 17:09  FontDemo\FontDemo.suo

     目录          0  2014-08-21 16:43  FontDemo\FontDemo\Debug

     目录          0  2014-08-21 17:08  FontDemo\FontDemo\Release

     目录          0  2014-08-21 17:08  FontDemo\Debug

     目录          0  2014-08-21 14:09  FontDemo\FontDemo

     目录          0  2014-08-21 17:08  FontDemo\Release

     目录          0  2014-08-21 17:09  FontDemo

----------- ---------  ---------- -----  ----

                54900                    15


评论

共有 条评论