织梦CMS - 轻松建站从此开始!

罗索

opengl es 显示中文 (利用TextOut)

落鹤生 发布于 2012-03-12 10:20 点击:次 
利用TextOut速度快,但是感觉有点毛刺不舒服。如果不要求极致就可以使用这种方法,否则使用freetype
TAG:

利用TextOut速度快,但是感觉有点毛刺不舒服。如果不要求极致就可以使用这种方法,否则使用freetype

 #include <Windows.h> 
#include <GdiPlus.h> 
#include <GLES/glutes.h> 
#include "gl/gl.h" 
#include "PVRShell.h" 
#include "PVRShellAPI.h"

#pragma warning(disable:4244
using namespace Gdiplus; 
#pragma comment(lib,"gdiplus.lib"

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)    
{    
    UINT num = 0;                     // number of image encoders    
    UINT size = 0;                   // size of the image encoder array in bytes    
    ImageCodecInfo* pImageCodecInfo = NULL;    
    GetImageEncodersSize(&num, &size);    
    if(size == 0)    
        return -1;     //   Failure    
 
    pImageCodecInfo = (ImageCodecInfo*)(malloc(size));    
    if(pImageCodecInfo == NULL)    
        return -1;     //   Failure    
 
    GetImageEncoders(num, size, pImageCodecInfo);    
    for(UINT j = 0; j < num; ++j)    
    {    
        if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )    
        {    
            *pClsid = pImageCodecInfo[j].Clsid;    
            free(pImageCodecInfo);    
            return j;     //   Success    
        }            
    }    
    free(pImageCodecInfo);    
    return -1;     //   Failure    

 
UINT32* GL_TEXTURE_DATA = NULL; 
UINT32 GL_TEXTURE_W = 0
UINT32 GL_TEXTURE_H = 0
 
void MyLoadTexture(wchar_t* textureFile) 

    GdiplusStartupInput gdiplusStartupInput; 
    ULONG_PTR gdiplusToken; 
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); 
 
    CLSID             encoderClsid; 
    EncoderParameters encoderParameters; 
    ULONG             transformation; 
    UINT              width; 
    UINT              height; 
    Status            stat; 
 
    // Get a JPEG image from the disk. 
    Image* image = new Image(textureFile); 
 
    Bitmap* pbitMap = (Bitmap*) image; 
    UINT w= pbitMap->GetWidth(); 
    UINT h = pbitMap->GetHeight(); 
    //    UINT size = image->GetPaletteSize(); 
    //    printf("The size of the palette is %d bytes.\n", size); 
    //    ColorPalette* palette = (ColorPalette*)malloc(size); 
 
    //    image->GetPalette(palette, size); 
    Rect rect(0,0,w,h); 
    BitmapData* pBitmapData = new BitmapData; 
    pbitMap->LockBits(&rect,ImageLockModeRead,PixelFormat32bppARGB,pBitmapData); 
    int offset = pBitmapData->Stride - w *4 ; 
    BYTE* pByte = (BYTE*)pBitmapData->Scan0; 
    GL_TEXTURE_DATA = new UINT32[h*w]; 
    int iCount = 0
    for (int i = 0; i < h; i++) 
    { 
        for (int j = 0;j < w; j++) 
        { 
            GL_TEXTURE_DATA[iCount++] =  (pByte[3]<<24) + (pByte[0]<<16) + (pByte[1]<<8) + (pByte[2]);; 
            //memcpy(GL_TEXTURE_DATA+iCount,pByte,4); 
            //iCount++; 
            pByte += 4
        } 
        pByte += offset; 
    } 
    GL_TEXTURE_W = w; 
    GL_TEXTURE_H = h; 
    pbitMap->UnlockBits(pBitmapData); 
    delete image; 
    delete pBitmapData; 
    GdiplusShutdown(gdiplusToken); 
 
    //LoadBitmap(,"c:\\img\\5.bmp"); 

 
 
SIZE size; 
HDC hdc; 
HDC hMemDC; 
 
bool CreateText(HWND hWnd,HFONT hFont ,LPCTSTR lpszText, unsigned int &tex) 

    hdc = ::GetDC(hWnd);; 
    HFONT hOldFont = (HFONT)SelectObject(hdc,hFont); 
    int nTexWidth,nTexHeight; 
    HBITMAP _hBmp = NULL; 
 
#if 0   // 这种方法得到的位图尺寸变形很大????          
    ::GetTextExtentPoint32(hdc, lpszText, len, &size); 
    HDC hMemDC = ::CreateCompatibleDC(hdc); 
    nTexWidth = size.cx; 
    nTexHeight = size.cy; 
    BITMAPINFO bmi; 
    ZeroMemory(&bmi.bmiHeader, sizeof(BITMAPINFOHEADER)); 
    bmi.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER); 
    bmi.bmiHeader.biWidth        = nTexWidth; 
    bmi.bmiHeader.biHeight        = nTexHeight; 
    bmi.bmiHeader.biPlanes        = 1
    bmi.bmiHeader.biBitCount    = 32
    bmi.bmiHeader.biCompression = BI_RGB; 
    //(这里需要定义一个指针指向位图的数据: 
    BYTE *        _pBmpBits;            // 位图的数据指针) 
    _hBmp = CreateDIBSection( hMemDC, &bmi, DIB_RGB_COLORS, 
        (void **) &pBmpBits, NULL, 0 ); 
    if ( NULL == _hBmp || NULL == pBmpBits) 
    { 
        return false
    } 
    // 将hBmp和hFont加入到hDc 
    SelectObject( hMemDC, _hBmp ); 
    SetBkColor(hMemDC, RGB(000)); 
    SetTextColor(hMemDC, RGB(255255255)); 
    SetBkMode(hMemDC, OPAQUE);         
    TextOut(hMemDC, 00, lpszText, len);                
    int nBitBytes = bmi.bmiHeader.biBitCount / 8
    for (int index = 0; index < nTexHeight*nTexWidth*nBitBytes; index+=nBitBytes) 
    { 
        pBmpBits[index+3] = (pBmpBits[index] > 0) ? 0xff : 0x00
    } 
#else   // 这种算法得到的图象是经过字符反走样后的结果,显示效果佳(推荐)      
    UCHAR *pBmpBits = NULL; 
    ::GetTextExtentPoint32(hdc, lpszText, strlen(lpszText), &size); 
    HBITMAP hbitmap = CreateBitmap(size.cx, size.cy,1,32, NULL); 
    if(!hbitmap) 
        return false
    hMemDC = ::CreateCompatibleDC(hdc); 
    if(hMemDC) 
    { 
        HBITMAP hPrevBmp = (HBITMAP)SelectObject(hMemDC,hbitmap); 
        HFONT hPrevFont = (HFONT)SelectObject(hMemDC, hFont); 
        SetBkColor(hMemDC, RGB(255255255)); 
        ::SetTextColor(hMemDC, RGB(000)); 
        SetBkMode(hMemDC, OPAQUE); 
        TextOut(hMemDC, 00, lpszText, strlen(lpszText)); 
        // copy GDI bitmap to DIB 
        BITMAP bm; 
        SelectObject(hdc,hbitmap);  
        GetObject(hbitmap, sizeof(bm), &bm); 
        size.cx = bm.bmWidth;    /*(bm.bmWidth + 31) & (~31); */                
        size.cy = bm.bmHeight; 
        nTexWidth = size.cx; 
        nTexHeight = size.cy; 
        int bufsize = size.cy * /*(((bm.bmWidth + 31) & (~31)) /8)*/size.cx *4
        pBmpBits = new UCHAR[bufsize]; 
        memset(pBmpBits, 0sizeof(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 = 32
        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); 
        int nBitBytes = binf->bmiHeader.biBitCount / 8
        for (int index = 0; index < nTexHeight*nTexWidth*nBitBytes; index+=nBitBytes) 
        { 
            pBmpBits[index+3] = (pBmpBits[index] > 0) ? 0xff : 0x00
        } 
    }             
#endif        
    glGenTextures(1, &tex);         
    glBindTexture(GL_TEXTURE_2D, tex); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, nTexWidth, nTexHeight, 0

, GL_RGBA, GL_UNSIGNED_BYTE,pBmpBits);//_pBits); 
 
    SelectObject(hMemDC,hbitmap); 
    /*    DeleteDC(hMemDC); 
    if (!_hBmp)    DeleteObject(_hBmp);        
    if ( pBmpBits) delete pBmpBits; 
    */ 
    return true

 
class CLesson2 : public PVRShell 

public
    virtual bool InitApplication(); 
    virtual bool InitView(); 
    virtual bool ReleaseView(); 
    virtual bool QuitApplication(); 
    virtual bool RenderScene(); 
}; 
 
bool CLesson2::InitApplication() 

    return true

 
bool CLesson2::QuitApplication() 

 
 
    return true

 
bool CLesson2::InitView() 

    glEnable(GL_TEXTURE_2D); 
 
    LOGFONT lf; 
    lf.lfHeight = 80*4
    lf.lfWidth = 0
    lf.lfEscapement = 0
    lf.lfOrientation = 0
    lf.lfWeight = 80*4
    lf.lfItalic = 0
    lf.lfUnderline = 0
    lf.lfStrikeOut = 0
    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,"隶书"); 
    // 创建字体 
    HFONT hFont = CreateFontIndirect(&lf); 
 
    HWND hWnd = GetHwnd(); 
    unsigned int tex; 
    CreateText(hWnd,hFont,"1好人1aaA!!",tex); 
 
    return true

 
bool CLesson2::ReleaseView() 

    return true

 
PVRShell * NewDemo(void

    return new CLesson2(); 

 
bool CLesson2::RenderScene() 
{     
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
 
 
    glClearColor(1,0,0,0); 
 
 
    float verts[] = { 
        -0.8,0.8,0
        0.8,0.8,0
        -0.8,-0.8,0
        0.8,-0.8,0 
    }; 
 
    float norm[] = { 
        0,0
        1,0
        0,-1
        1,-1 
    }; 
 
    glEnable(GL_ALPHA_TEST); 
    glAlphaFunc(GL_NOTEQUAL, 1); 
    //glColor4f(1,0,0,0); 
    glEnableClientState(GL_VERTEX_ARRAY); 
    glEnableClientState(GL_TEXTURE_COORD_ARRAY); 
    glVertexPointer(3, GL_FLOAT, 3 * sizeof(float), verts); 
    glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(float), norm); 
    glDrawArrays(GL_TRIANGLE_STRIP, 04); 
    glDisableClientState(GL_VERTEX_ARRAY); 
    glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
 
    return true

(cr0_3)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201203/15807.html]
本文出处:百度博客 作者:cr0_3 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容