home *** CD-ROM | disk | FTP | other *** search
/ Enter 2003 July / EnterCD 7_2003.iso / Ekstra / GL Force 2001 3.0 / GLFont.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-02-07  |  7.9 KB  |  293 lines

  1. /*
  2. Authors: David Nishimoto
  3. Website: http://www.listensoftware.com
  4. Email: davepamn@relia.net
  5. Program: Force
  6. */
  7.  
  8. #include "stdafx.h"
  9. #include "GLFont.h"
  10.  
  11. #ifdef _DEBUG
  12. #undef THIS_FILE
  13. static char THIS_FILE[]=__FILE__;
  14. #define new DEBUG_NEW
  15. #endif
  16.  
  17. //////////////////////////////////////////////////////////////////////
  18. // Construction/Destruction
  19. //////////////////////////////////////////////////////////////////////
  20.  
  21. CGLFont::CGLFont()
  22. {
  23.     m_FontType = GL_FONT_SOLID;
  24.     m_uiListID = 0;
  25.     m_dThick = 0.1f;
  26.     m_dXOffset = 0.0f;     
  27.     m_dYOffset = 0.0f;
  28.     m_dZOffset = 0.0f;
  29.     m_dXScale = 1.0f;
  30.     m_dYScale = 1.0f;
  31.     m_dZScale = 1.0f;
  32.     m_dXRotate = 0.0f;
  33.     m_dYRotate = 0.0f;
  34.     m_dZRotate = 0.0f;     
  35.  
  36.     m_dMEmission[0] = 0.1f;
  37.     m_dMEmission[1] = 0.1f;
  38.     m_dMEmission[2] = 0.9f;
  39.     m_dMEmission[3] = 1.0f;
  40.  
  41.     m_dMSpecular[0] = 0.9f;
  42.     m_dMSpecular[1] = 0.1f;
  43.     m_dMSpecular[2] = 0.1f;
  44.     m_dMSpecular[3] = 1.0f;
  45.  
  46.     m_dMAmbient[0] = 0.1f;
  47.     m_dMAmbient[1] = 0.9f;
  48.     m_dMAmbient[2] = 0.1f;
  49.     m_dMAmbient[3] = 1.0f;
  50.  
  51.     m_dMDiffuse[0] = 0.8f;
  52.     m_dMDiffuse[1] = 0.8f;
  53.     m_dMDiffuse[2] = 0.8f;
  54.     m_dMDiffuse[3] = 1.0f;
  55.  
  56.     m_dMShininess = 100.0f;
  57. }
  58.  
  59. CGLFont::~CGLFont()
  60. {
  61.     if(m_uiListID != 0)
  62.         glDeleteLists(m_uiListID, FONTLIST);
  63. }
  64.  
  65.  
  66. //////////////////////////////////////////////////////////////////////
  67. // Set the material emission of the font 
  68. //////////////////////////////////////////////////////////////////////
  69. void CGLFont::SetEmission(float dMEmission[4])
  70. {
  71.     m_dMEmission[0] = dMEmission[0];
  72.     m_dMEmission[1] = dMEmission[1];
  73.     m_dMEmission[2] = dMEmission[2];
  74.     m_dMEmission[3] = dMEmission[3];
  75. }
  76.  
  77.  
  78. //////////////////////////////////////////////////////////////////////
  79. // Get the material emission of the font 
  80. //////////////////////////////////////////////////////////////////////
  81. void CGLFont::GetEmission(float dMEmission[4])
  82. {
  83.     dMEmission[0] = m_dMEmission[0];
  84.     dMEmission[1] = m_dMEmission[1];
  85.     dMEmission[2] = m_dMEmission[2];
  86.     dMEmission[3] = m_dMEmission[3];
  87. }
  88.  
  89.  
  90. //////////////////////////////////////////////////////////////////////
  91. // Set the material specular of the font 
  92. //////////////////////////////////////////////////////////////////////
  93. void CGLFont::SetSpecular(float dMSpecular[4])
  94. {
  95.     m_dMSpecular[0] = dMSpecular[0];
  96.     m_dMSpecular[1] = dMSpecular[1];
  97.     m_dMSpecular[2] = dMSpecular[2];
  98.     m_dMSpecular[3] = dMSpecular[3];
  99. }
  100.  
  101.  
  102. //////////////////////////////////////////////////////////////////////
  103. // Get the material specular of the font 
  104. //////////////////////////////////////////////////////////////////////
  105. void CGLFont::GetSpecular(float dMSpecular[4])
  106. {
  107.     dMSpecular[0] = m_dMSpecular[0];
  108.     dMSpecular[1] = m_dMSpecular[1];
  109.     dMSpecular[2] = m_dMSpecular[2];
  110.     dMSpecular[3] = m_dMSpecular[3];
  111. }
  112.  
  113.     
  114. //////////////////////////////////////////////////////////////////////
  115. // Set the material ambient of the font 
  116. //////////////////////////////////////////////////////////////////////
  117. void CGLFont::SetAmbient(float dMAmbient[4]) 
  118. {
  119.     m_dMAmbient[0] = dMAmbient[0];
  120.     m_dMAmbient[1] = dMAmbient[1];
  121.     m_dMAmbient[2] = dMAmbient[2];
  122.     m_dMAmbient[3] = dMAmbient[3];
  123. }
  124.  
  125.  
  126. //////////////////////////////////////////////////////////////////////
  127. // Get the material ambient of the font 
  128. //////////////////////////////////////////////////////////////////////
  129. void CGLFont::GetAmbient(float dMAmbient[4]) 
  130. {
  131.     dMAmbient[0] = m_dMAmbient[0];
  132.     dMAmbient[1] = m_dMAmbient[1];
  133.     dMAmbient[2] = m_dMAmbient[2];
  134.     dMAmbient[3] = m_dMAmbient[3];
  135. }
  136.  
  137.  
  138. //////////////////////////////////////////////////////////////////////
  139. // Set the material diffuse of the font 
  140. //////////////////////////////////////////////////////////////////////
  141. void CGLFont::SetDiffuse(float dMDiffuse[4])
  142. {
  143.     m_dMDiffuse[0] = dMDiffuse[0];
  144.     m_dMDiffuse[1] = dMDiffuse[1];
  145.     m_dMDiffuse[2] = dMDiffuse[2];
  146.     m_dMDiffuse[3] = dMDiffuse[3];
  147. }
  148.  
  149.  
  150. //////////////////////////////////////////////////////////////////////
  151. // Get the material diffuse of the font 
  152. //////////////////////////////////////////////////////////////////////
  153. void CGLFont::GetDiffuse(float dMDiffuse[4])
  154. {
  155.     dMDiffuse[0] = m_dMDiffuse[0];
  156.     dMDiffuse[1] = m_dMDiffuse[1];
  157.     dMDiffuse[2] = m_dMDiffuse[2];
  158.     dMDiffuse[3] = m_dMDiffuse[3];
  159. }
  160.  
  161.     
  162. //////////////////////////////////////////////////////////////////////
  163. // Set the material shininess of the font 
  164. //////////////////////////////////////////////////////////////////////
  165. void CGLFont::SetShininess(float dMShininess)  
  166. {
  167.     m_dMShininess = dMShininess;
  168. }
  169.  
  170.  
  171. //////////////////////////////////////////////////////////////////////
  172. // Get the material shininess of the font 
  173. //////////////////////////////////////////////////////////////////////
  174. void CGLFont::GetShininess(float* dMShininess)
  175. {
  176.     *dMShininess = m_dMShininess;
  177. }
  178.  
  179. //////////////////////////////////////////////////////////////////////
  180. // Create the font to display the text 
  181. //////////////////////////////////////////////////////////////////////
  182. BOOL CGLFont::CreateFont(CDC* pDrawDC, char* fontname)
  183. {
  184.     m_uiListID = glGenLists(FONTLIST);
  185.  
  186.     if(pDrawDC == NULL || m_uiListID == 0)
  187.     {
  188.         return FALSE;
  189.     }
  190.  
  191.     //Create the font to display
  192.     CFont newfont;
  193.     CFont* oldfont;
  194.     BOOL  bresult;
  195.     GLYPHMETRICSFLOAT gmf[FONTLIST];
  196.  
  197.     if(fontname != NULL)
  198.     {    
  199.         bresult = newfont.CreateFont(-12, 0, 0, 0, FW_NORMAL, 0, 0, 0, ANSI_CHARSET, 
  200.                OUT_TT_ONLY_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  201.                FF_DONTCARE|DEFAULT_PITCH, fontname);
  202.  
  203.         //if new font failed to be created
  204.         if(!bresult)
  205.         {
  206.             return FALSE;
  207.         }
  208.  
  209.         oldfont = pDrawDC->SelectObject(&newfont);
  210.     }
  211.     else
  212.     {
  213.         oldfont = (CFont*)pDrawDC->SelectStockObject(SYSTEM_FONT);
  214.     }
  215.  
  216.     if(m_FontType == GL_FONT_SOLID)
  217.     {
  218.         bresult = wglUseFontOutlines(pDrawDC->m_hDC, 0, FONTLIST, m_uiListID, 
  219.             0.0f, (float)m_dThick, WGL_FONT_POLYGONS, gmf);    
  220.     }
  221.     else if(m_FontType == GL_FONT_LINE)
  222.     {
  223.         bresult = wglUseFontOutlines(pDrawDC->m_hDC, 0, FONTLIST, m_uiListID, 
  224.             0.0f, (float)m_dThick, WGL_FONT_LINES, gmf);    
  225.     }
  226.     
  227.     pDrawDC->SelectObject(oldfont);
  228.     newfont.DeleteObject();
  229.     
  230.     if(!bresult)
  231.     {
  232.         return FALSE;
  233.     }
  234.  
  235.     return TRUE;
  236. }
  237.  
  238.  
  239. //////////////////////////////////////////////////////////////////////
  240. // display the text string
  241. //////////////////////////////////////////////////////////////////////
  242. void CGLFont::GLDrawText()
  243. {
  244.     if (m_uiListID == 0 || m_strText.IsEmpty() ||
  245.         m_dXScale == 0.0 || m_dYScale == 0.0 ||
  246.         m_dZScale == 0.0)
  247.     {
  248.         return;
  249.     }
  250.  
  251.     GLsizei size = m_strText.GetLength();
  252.     
  253.     glPushMatrix();
  254.     //set material mission
  255.     glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, m_dMEmission);
  256.     //set material specular
  257.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, m_dMSpecular);
  258.     //set material ambient
  259.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, m_dMAmbient);
  260.     //set material diffuse
  261.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, m_dMDiffuse);
  262.     //set material shininess
  263.     glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, m_dMShininess);
  264.  
  265.     //Translate 
  266.     glTranslated(m_dXOffset, m_dYOffset, m_dZOffset);
  267.     //Scale
  268.     glScaled(m_dXScale, m_dYScale, m_dZScale); 
  269.      //Rotate around X-axis
  270.     glRotated(m_dXRotate, 1.0, 0.0, 0.0);
  271.      //Rotate around Y-axis
  272.     glRotated(m_dYRotate, 0.0, 1.0, 0.0);
  273.      //Rotate around Z-axis
  274.     glRotated(m_dZRotate, 0.0, 0.0, 1.0);
  275.     
  276.     //display the letter
  277.     glListBase(m_uiListID);
  278.     glCallLists(size, GL_UNSIGNED_BYTE, (const GLvoid*)m_strText.GetBuffer(size)); 
  279.  
  280.      //restore the original angle around Z-axis
  281.     glRotated(-1.0f * m_dZRotate, 0.0, 0.0, 1.0);
  282.      //restore the original angle around Y-axis
  283.     glRotated(-1.0f * m_dYRotate, 0.0, 1.0, 0.0);
  284.      //restore the original angle around X-axis
  285.     glRotated(-1.0f * m_dXRotate, 1.0, 0.0, 0.0);
  286.      //restore the original scale
  287.     glScaled(1.0/m_dXScale, 1.0/m_dYScale, 1.0/m_dZScale); 
  288.     //restore the original position
  289.     glTranslated(-m_dXOffset, -m_dYOffset, -m_dZOffset);
  290.  
  291.     glPopMatrix();
  292.  
  293. }