home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / book / Chap06 / MATLIGHT / MATLIGHTDOC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-06  |  4.8 KB  |  208 lines

  1. // matlightDoc.cpp : implementation of the CMatlightDoc class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "matlight.h"
  6.  
  7. #include "matlightDoc.h"
  8. #include "matlightView.h"
  9.  
  10. #include "lighteditor.h"
  11. #include "mateditor.h"
  12.  
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CMatlightDoc
  21.  
  22. IMPLEMENT_DYNCREATE(CMatlightDoc, CDocument)
  23.  
  24. BEGIN_MESSAGE_MAP(CMatlightDoc, CDocument)
  25.     //{{AFX_MSG_MAP(CMatlightDoc)
  26.     ON_COMMAND(ID_EDIT_LIGHT, OnEditLight)
  27.     ON_COMMAND(ID_EDIT_MATERIAL, OnEditMaterial)
  28.     //}}AFX_MSG_MAP
  29. END_MESSAGE_MAP()
  30.  
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CMatlightDoc construction/destruction
  33.  
  34. CMatlightDoc::CMatlightDoc()
  35.     {
  36.     // TODO: add one-time construction code here
  37.     nAlphaList = 1000;
  38.     }
  39.  
  40. // Destructor for Document
  41. CMatlightDoc::~CMatlightDoc()
  42.     {
  43.     // Delete display lists
  44.     if(glIsList(nAlphaList))
  45.         glDeleteLists(nAlphaList,128);
  46.     }
  47.  
  48. BOOL CMatlightDoc::OnNewDocument()
  49.     {
  50.     if (!CDocument::OnNewDocument())
  51.         return FALSE;
  52.  
  53.     // Reinitialize Lighting Properties
  54.     fLight0Pos[0] = -100.0f;
  55.     fLight0Pos[1] = 200.0f;
  56.     fLight0Pos[2] = 200.0f;
  57.     fLight0Pos[3] = 1.0f;
  58.  
  59.     fLight0Ambient[0] = 0.6f;
  60.     fLight0Ambient[1] = 0.6f;
  61.     fLight0Ambient[2] = 0.6f;
  62.     fLight0Ambient[3] = 1.0f;
  63.  
  64.     fLight0Diffuse[0] = 0.6f;
  65.     fLight0Diffuse[1] = 0.6f;
  66.     fLight0Diffuse[2] = 0.6f;
  67.     fLight0Diffuse[3] = 1.0f;
  68.  
  69.     fLight0Specular[0] = 1.0f;
  70.     fLight0Specular[1] = 1.0f;
  71.     fLight0Specular[2] = 1.0f;
  72.     fLight0Specular[3] = 1.0f;
  73.  
  74.     // Reinitialize Materials Properties
  75.     // Materials Ambient reflectivity
  76.     fMatAmbient[0] = 0.0f;
  77.     fMatAmbient[1] = 0.0f;
  78.     fMatAmbient[2] = 0.6f;
  79.     fMatAmbient[3] = 1.0f;
  80.     
  81.     // Materials Diffuse reflectivity
  82.     fMatDiffuse[0] = 0.0f;
  83.     fMatDiffuse[1] = 0.0f;
  84.     fMatDiffuse[2] = 0.9f;
  85.     fMatDiffuse[3] = 1.0f;
  86.     
  87.     // Materials Specular reflectivity
  88.     fMatSpecular[0] = 1.0f;    
  89.     fMatSpecular[1] = 1.0f;    
  90.     fMatSpecular[2] = 1.0f;    
  91.     fMatSpecular[3] = 1.0f;    
  92.  
  93.     // Materials Shininess
  94.     fShine = 100.0f;                
  95.  
  96.     SetTitle("OpenGL SuperBible");
  97.  
  98.     return TRUE;
  99.     }
  100.  
  101. /////////////////////////////////////////////////////////////////////////////
  102. // CMatlightDoc serialization
  103.  
  104. void CMatlightDoc::Serialize(CArchive& ar)
  105. {
  106.     if (ar.IsStoring())
  107.     {
  108.         // TODO: add storing code here
  109.     }
  110.     else
  111.     {
  112.         // TODO: add loading code here
  113.     }
  114. }
  115.  
  116. /////////////////////////////////////////////////////////////////////////////
  117. // CMatlightDoc diagnostics
  118.  
  119. #ifdef _DEBUG
  120. void CMatlightDoc::AssertValid() const
  121. {
  122.     CDocument::AssertValid();
  123. }
  124.  
  125. void CMatlightDoc::Dump(CDumpContext& dc) const
  126. {
  127.     CDocument::Dump(dc);
  128. }
  129. #endif //_DEBUG
  130.  
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CMatlightDoc commands
  133.  
  134.  
  135. // Setup and initialize the OpenGL environment
  136. void CMatlightDoc::GLSetupRC(HDC hDC)
  137.     {
  138.     HFONT hFont;                    // Font handle
  139.     static GLYPHMETRICSFLOAT agmf[255];    // Throw away
  140.     LOGFONT logfont;                // Logical Font structure
  141.  
  142.     // Set Background to black
  143.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  144.  
  145.     glEnable(GL_DEPTH_TEST);    // Hidden surface removal
  146.     glFrontFace(GL_CCW);        // Counter clock-wise polygons face out
  147.     glCullFace(GL_BACK);
  148.     glEnable(GL_CULL_FACE);    
  149.     glEnable(GL_NORMALIZE);
  150.     glEnable(GL_AUTO_NORMAL);    
  151.     
  152.     
  153.     // Create display list for Alphabet (ABC)
  154.     // Start by creating the font
  155.     logfont.lfHeight = -10;
  156.     logfont.lfWidth = 0;
  157.     logfont.lfEscapement = 0;
  158.     logfont.lfOrientation = 0;
  159.     logfont.lfWeight = FW_BOLD;
  160.     logfont.lfItalic = FALSE;
  161.     logfont.lfUnderline = FALSE;
  162.     logfont.lfStrikeOut = FALSE;
  163.     logfont.lfCharSet = ANSI_CHARSET;
  164.     logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
  165.     logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  166.     logfont.lfQuality = DEFAULT_QUALITY;
  167.     logfont.lfPitchAndFamily = DEFAULT_PITCH;
  168.     strcpy(logfont.lfFaceName,"Arial");
  169.     
  170.     hFont = CreateFontIndirect(&logfont);
  171.     SelectObject (hDC, hFont); 
  172.  
  173.     // Create the display list
  174.     nAlphaList = glGenLists(128);
  175.  
  176.     // Just enough for ABC
  177.     wglUseFontOutlines(hDC, 65, 3, nAlphaList, 0.0f, 0.2f, 
  178.                 WGL_FONT_POLYGONS, agmf);
  179.  
  180.     DeleteObject(hFont);
  181.     }
  182.  
  183.  
  184. void CMatlightDoc::OnEditLight() 
  185.     {
  186.     CLightEditor lightEdObj("Lighting Properties",NULL);
  187.     
  188.     lightEdObj.m_pDocumentPointer = this;
  189.     
  190.     lightEdObj.m_pAmbientPage->SetValues(fLight0Ambient);
  191.     if(lightEdObj.DoModal() == IDOK)
  192.         lightEdObj.Update();
  193.     }
  194.  
  195.  
  196. void CMatlightDoc::OnEditMaterial() 
  197.     {
  198.     CMatEditor matEdObj("Material Properties",NULL);
  199.     
  200.     matEdObj.m_pDocumentPointer = this;
  201.  
  202.     // Need to do ambient page first because, ambient sheet InitDialog
  203.     // gets called too early
  204.     matEdObj.m_pAmbientPage->SetValues(fMatAmbient);
  205.     if(matEdObj.DoModal() == IDOK)
  206.         matEdObj.Update();
  207.     }
  208.