GLFont.cpp
Upload User: xuczgm
Upload Date: 2022-04-25
Package Size: 8601k
Code Size: 7k
Category:

Special Effects

Development Platform:

Visual C++

  1. // GLFont.cpp: implementation of the CGLFont class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "GLFont.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char THIS_FILE[]=__FILE__;
  9. #define new DEBUG_NEW
  10. #endif
  11. //////////////////////////////////////////////////////////////////////
  12. //unsigned int Base;
  13. //////////////////////////////////////////////////////////////////////
  14. CGLFont::CGLFont()
  15. {
  16. }
  17. CGLFont::~CGLFont()
  18. {
  19. }
  20. void CGLFont::entext( float x,float y, LPCTSTR str, HFONT hFont,
  21.   float r, float g, float b) //平面字符显示,不支持汉字。
  22. {  HDC hdc = wglGetCurrentDC();        //获取设置场景
  23. SelectObject(hdc, hFont);           //设置字体
  24. unsigned int Base = glGenLists(96); //创建大小为96显示列表,存放字符位图
  25. wglUseFontBitmaps(hdc, 32, 96,Base);//创建位图字体
  26. glDisable(GL_TEXTURE_2D);           //不使用贴图
  27. glDisable(GL_LIGHTING);             //不使用光照
  28. glPushAttrib(GL_LIST_BIT);          //保存显示列表位属性
  29. glColor3f(r,g,b);                   //颜色
  30. glRasterPos2f(x/100, y/100);        //平面定位
  31. glListBase(Base - 32);          //从显示列表开始
  32. glCallLists(strlen(str), GL_UNSIGNED_BYTE, str); //绘制显示列表
  33. glPopAttrib();                      //恢复显示列表属性
  34. glEnable(GL_LIGHTING);              //使用贴图
  35. glEnable(GL_TEXTURE_2D);            //使用光照
  36. glDeleteLists(Base, 96);            //删除显示列表
  37. }
  38. //////////////////////////////////////////////////////////////////
  39. void CGLFont::c3dtext(LPCTSTR str,HFONT hFont,float z)// 3D文字显示,支持汉字。
  40. { glDisable(GL_TEXTURE_2D);     // 关闭贴图
  41. glDisable(GL_LIGHTING);       // 关闭光照
  42. Printfc3d("立体汉字",hFont,z);// 调用3D文字显示
  43. glEnable(GL_LIGHTING);        // 使用光照
  44. glEnable(GL_TEXTURE_2D);      // 使用贴图
  45. }
  46. void CGLFont::Printfc3d(CString strText,HFONT hFont,float z)
  47. { HDC hdc = wglGetCurrentDC(); //设备场景
  48. HFONT hOldFont=(HFONT)::SelectObject(hdc,hFont);//将字体选入场景
  49. UCHAR * pChar=(UCHAR*)strText.GetBuffer(strText.GetLength());//定义字符串长度
  50. int   nListNum;                                 //显示列表
  51. DWORD dwChar;                                   //字符指针
  52. GLYPHMETRICSFLOAT pgmf[1];                      //轮廓字体字符集的信息
  53. glPushMatrix();                                 //压入堆栈
  54. for(int i = 0; i < strText.GetLength(); i++)
  55. { if(IsDBCSLeadByte((BYTE)pChar[i]))            //是否双字节(汉字)
  56. { dwChar=(DWORD)((pChar[i]<<8)|pChar[i+1]); //取当前字符,双字节转换
  57.   i++;
  58. }
  59.   else dwChar = pChar[i];                      //取当前字符
  60.   nListNum = glGenLists(1);                     //创建列表
  61.   wglUseFontOutlines( hdc, //拥有字体的HDC
  62.   dwChar, //转化为显示列表的第一个字符
  63.   1, //转化为显示列表的字符数
  64.   nListNum, //显示列表的开始
  65.   0.0f, 
  66.   z, //Z轴负方向的厚度
  67.   WGL_FONT_POLYGONS, //绘制字体方式
  68.   pgmf //指向存放信息的数组,为count个
  69. );
  70.   glCallList(nListNum);                         //绘制显示列表
  71.   glDeleteLists(nListNum, 1);                   //删除列表
  72. }
  73. glPopMatrix();                                  //弹出堆栈
  74. strText.ReleaseBuffer();                        //清除字符串
  75. ::SelectObject(hdc,hOldFont);                   //恢复字体
  76. }
  77. ////////////////////////////////////////////////////////////////////////
  78. void CGLFont:: settext (float x,float y,CString str,HFONT Font,float r,float g,float b)
  79.   //平面字符显示,汉字完全解决方案。
  80. {   glLoadIdentity();
  81. glPushAttrib(GL_CURRENT_BIT);//保存现有颜色属性
  82. glDisable(GL_TEXTURE_2D);                       //关闭贴图
  83. glDisable(GL_LIGHTING);                         //关闭光照
  84. glColor3f(r,g,b);                               //颜色
  85.   glTranslatef(-(420-x)/800,(260-y)/600,-1.0f); //定位
  86.   Printftext (0,0, str,Font);                   //OpenGL平面汉字
  87. glEnable(GL_LIGHTING);                          //光照有效
  88. glEnable(GL_TEXTURE_2D);                        //贴图有效
  89. glPopAttrib();//恢复前一属性
  90. }
  91. void CGLFont:: Printftext (int x, int y, LPCTSTR lpszText,HFONT hFont)
  92. { CBitmap bitmap; //设备相关位图变量
  93.   BITMAP bm;                                        //位图结构变量
  94.   SIZE size;                                        //位图尺寸
  95.   HDC MDC = ::CreateCompatibleDC(0); //暂存设备场景
  96.   SelectObject(MDC,hFont); //选择新字体
  97.   ::GetTextExtentPoint32(MDC,lpszText,strlen(lpszText),&size);//获取字符位图大小
  98.   bitmap.CreateBitmap(size.cx, size.cy, 1, 1, NULL);//创建与MDC相关单色位图
  99.   HBITMAP oldBmp=(HBITMAP)SelectObject(MDC,bitmap); //字符位图与MDC关连
  100.   SetBkColor  (MDC, RGB(0,     0,   0)); //底色,黑色
  101.   SetTextColor(MDC, RGB(255, 255, 255)); //字色,白色
  102.   TextOut(MDC, 0, 0, lpszText, strlen(lpszText)); //输出文字到暂存MDC
  103.   bitmap.GetBitmap(&bm); //获得相关位图数据结构
  104.   size.cx = (bm.bmWidth + 31) & (~31); //边缘对齐
  105.   int bufsize =size.cy * size.cx; //图形数据长度
  106.   struct {  BITMAPINFOHEADER bih;
  107. RGBQUAD col[2];
  108.    }bic;                                      //定义单色位图结构
  109.   BITMAPINFO *binf = (BITMAPINFO *)&bic;            //获取位图结构信息
  110.   binf->bmiHeader.biSize     = sizeof(binf->bmiHeader);//
  111.   binf->bmiHeader.biWidth    = bm.bmWidth; //图形宽
  112.   binf->bmiHeader.biHeight   = bm.bmHeight; //图形高
  113.   binf->bmiHeader.biPlanes   = 1;                   //
  114.   binf->bmiHeader.biBitCount = 1;                   //单色
  115.   binf->bmiHeader.biCompression = BI_RGB;           //颜色方式
  116.   binf->bmiHeader.biSizeImage   = bufsize;          //图形数据长度
  117.   UCHAR* Bits = new UCHAR[bufsize]; //定义图形数据块变量
  118.   ::GetDIBits(MDC,bitmap,0,bm.bmHeight,Bits,binf,DIB_RGB_COLORS); 
  119.                                                     //取设备无关数据到Bits
  120.   glPixelStorei(GL_UNPACK_ALIGNMENT ,1); //控制像素存储
  121.   glRasterPos2i(x,y);                               //平面定位
  122.   glBitmap(size.cx,size.cy,0,0,0,0,Bits);           //平面位图显示
  123.   delete Bits;                                      //删除Bits
  124.   SelectObject(MDC, oldBmp);                        //恢复位图特性
  125.   ::DeleteDC(MDC);                                  //删除设备场景
  126. }
  127. //////////////////////////////////////////////////////////////////////////
  128. BOOL CGLFont::loadbmp(CString cc)
  129. {//图片汉字演示
  130. HANDLE m_Buf= NULL;
  131. LPVOID lpmem = NULL;
  132. CFile file;
  133. if( !file.Open( cc,CFile::modeRead)) return NULL;
  134. BITMAPFILEHEADER fileinfo;
  135. file.Read(&fileinfo,sizeof(fileinfo));
  136. if(fileinfo.bfType != (('M'<<8)+'B')) return NULL;
  137. UINT m_Off=fileinfo.bfOffBits; 
  138. UINT length = file.GetLength() - sizeof(BITMAPFILEHEADER);
  139. if(m_Buf != NULL) 
  140. {GlobalFree(m_Buf);m_Buf = NULL;}
  141. m_Buf = GlobalAlloc(GMEM_MOVEABLE|GMEM_DISCARDABLE,length);
  142. lpmem = GlobalLock(m_Buf);//图形数据指针lpmem
  143. if(length != file.ReadHuge(lpmem,length))
  144. { GlobalUnlock(m_Buf);
  145. GlobalFree(m_Buf);
  146. return NULL;
  147. }
  148. UCHAR* tmp = new UCHAR[length];
  149. memcpy(tmp,lpmem,length);
  150. LPBITMAPINFOHEADER bm=(BITMAPINFOHEADER *)tmp;
  151. bm->biSize = sizeof(BITMAPINFOHEADER);
  152. int w=bm->biWidth;  //图形高
  153. int h=bm->biHeight;//图形宽
  154. //////////////////////////////////////////////
  155. glDisable(GL_TEXTURE_2D);//glEnable
  156. glDisable(GL_LIGHTING);
  157. glColor3f(255,255,255);
  158. glPixelStorei(GL_UNPACK_ALIGNMENT,4); //控制像素存储
  159. glRasterPos2i(0,0);
  160. glBitmap(w,h,0,0,0,0,tmp+6*2);
  161. glEnable(GL_LIGHTING);
  162. glEnable(GL_TEXTURE_2D);
  163. delete tmp;
  164. GlobalUnlock(m_Buf);GlobalFree(m_Buf);
  165. return TRUE;
  166. }