home *** CD-ROM | disk | FTP | other *** search
/ 3D Games (Spidla) / 3dhry2.iso / Cube Drop 2001 1.0 / GLFont.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2003-03-17  |  7.9 KB  |  292 lines

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