ModelMagic3D.cpp
上传用户:cding2008
上传日期:2007-01-03
资源大小:1812k
文件大小:11k
源码类别:

OpenGL

开发平台:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // ModelMagic3D.cpp : Defines the class behaviors for the application.
  3. //
  4. // ModelMagic 3D and 'glOOP' (OpenGL Object Oriented Programming library)
  5. // Copyright (c) Craig Fahrnbach 1997, 1999
  6. //
  7. // OpenGL is a registered trademark of Silicon Graphics
  8. //
  9. //
  10. // This program is provided for educational and personal use only and
  11. // is provided without guarantee or warrantee expressed or implied.
  12. //
  13. // Commercial use is strickly prohibited without written permission
  14. // from ImageWare Development.
  15. //
  16. /////////////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. #include "memory.h"
  19. #include "ModelMagic3D.h"
  20. #include "MyTime.h"
  21. #include "ChildFrm.h"
  22. #include <afxinet.h>
  23. #ifdef _DEBUG
  24. #define new DEBUG_NEW
  25. #undef THIS_FILE
  26. static char THIS_FILE[] = __FILE__;
  27. #endif
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CMyglApp
  30. BEGIN_MESSAGE_MAP(CMyglApp, CWinApp)
  31. //{{AFX_MSG_MAP(CMyglApp)
  32. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  33. ON_COMMAND(ID_HELP_WEBSITE, OnHelpWebsite)
  34. ON_COMMAND(ID_HELP_SENDMAIL, OnHelpSendmail)
  35. ON_COMMAND(ID_ORDER_FORM, OnOrderForm)
  36. ON_COMMAND(ID_ORDER_ONLINE, OnOrderOnline)
  37. //}}AFX_MSG_MAP
  38. // Standard file based document commands
  39. ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  40. ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  41. // Standard print setup command
  42. ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  43. END_MESSAGE_MAP()
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CMyglApp construction
  46. CMyglApp::CMyglApp()
  47. {
  48. // Initialize our local variables
  49. m_bConfirmObjectDeletion = TRUE;
  50. m_bRegistered = TRUE;
  51. m_iDays = 0;
  52. }
  53. CMyglApp::~CMyglApp()
  54. {
  55. }
  56. /////////////////////////////////////////////////////////////////////////////
  57. // The one and only CMyglApp object
  58. CMyglApp theApp;
  59. /////////////////////////////////////////////////////////////////////////////
  60. // Global variables
  61. //
  62. GLfloat g_fMouseZMult    = 0.01f; // Mouse 'Z'-Axis multiplier
  63. GLfloat g_fMouseRotateMult = 0.5f; // Mouse 'Rotation' multiplier 
  64. GLfloat g_fMouseScaleMult  = 0.01f; // Mouse 'Scale' multiplier
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CMyglApp initialization
  67. BOOL CMyglApp::InitInstance()
  68. {
  69. // CG: The following block was added by the Splash Screen component. { CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash); }
  70. // Standard initialization
  71. // If you are not using these features and wish to reduce the size
  72. //  of your final executable, you should remove from the following
  73. //  the specific initialization routines you do not need.
  74. #ifdef _AFXDLL
  75. Enable3dControls(); // Call this when using MFC in a shared DLL
  76. #else
  77. Enable3dControlsStatic(); // Call this when linking to MFC statically
  78. #endif
  79. // Determine the Registration status
  80. // GetRegStatus();
  81. LoadStdProfileSettings();  // Load standard INI file options (including MRU)
  82. // Register the application's document templates.  Document templates
  83. //  serve as the connection between documents, frame windows and views.
  84. m_pDocTemplate = new CMultiDocTemplate(
  85. IDR_MYGLVIEW,
  86. RUNTIME_CLASS(CMyglDoc),
  87. RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  88. RUNTIME_CLASS(CMyglView));
  89. // Add the template
  90. AddDocTemplate(m_pDocTemplate);
  91.  
  92. // create main MDI Frame window
  93. CMainFrame* pMainFrame = new CMainFrame;
  94. if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  95. return FALSE;
  96. m_pMainWnd = pMainFrame;
  97. // Parse command line for standard shell commands, DDE, file open
  98. CCommandLineInfo cmdInfo;
  99. ParseCommandLine(cmdInfo);
  100. // Dispatch commands specified on the command line
  101. if (!ProcessShellCommand(cmdInfo))
  102. return FALSE;
  103. // The main window has been created.  If we have not
  104. // restored the window state, lets show and update it.
  105. if(!pMainFrame->m_bWndInitialized) {
  106. pMainFrame->ShowWindow(m_nCmdShow);
  107. pMainFrame->UpdateWindow();
  108. }
  109. return TRUE;
  110. }
  111. /////////////////////////////////////////////////////////////////////////////
  112. // CAboutDlg dialog used for App About
  113. class CAboutDlg : public CDialog
  114. {
  115. public:
  116. CAboutDlg();
  117. // Dialog Data
  118. //{{AFX_DATA(CAboutDlg)
  119. enum { IDD = IDD_ABOUTBOX };
  120. //}}AFX_DATA
  121. // ClassWizard generated virtual function overrides
  122. //{{AFX_VIRTUAL(CAboutDlg)
  123. protected:
  124. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  125. //}}AFX_VIRTUAL
  126. // Implementation
  127. protected:
  128. //{{AFX_MSG(CAboutDlg)
  129. virtual BOOL OnInitDialog();
  130. afx_msg void OnButtonWebsite();
  131. //}}AFX_MSG
  132. DECLARE_MESSAGE_MAP()
  133. };
  134. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  135. {
  136. //{{AFX_DATA_INIT(CAboutDlg)
  137. //}}AFX_DATA_INIT
  138. }
  139. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  140. {
  141. CDialog::DoDataExchange(pDX);
  142. //{{AFX_DATA_MAP(CAboutDlg)
  143. //}}AFX_DATA_MAP
  144. }
  145. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  146. //{{AFX_MSG_MAP(CAboutDlg)
  147. ON_BN_CLICKED(IDC_BUTTON_WEBSITE, OnButtonWebsite)
  148. //}}AFX_MSG_MAP
  149. END_MESSAGE_MAP()
  150. // App command to run the dialog
  151. void CMyglApp::OnAppAbout()
  152. {
  153. CAboutDlg aboutDlg;
  154. aboutDlg.DoModal();
  155. }
  156. BOOL CAboutDlg::OnInitDialog() 
  157. {
  158. CDialog::OnInitDialog();
  159. // TODO: Add extra initialization here
  160. return TRUE;  // return TRUE unless you set the focus to a control
  161.               // EXCEPTION: OCX Property Pages should return FALSE
  162. }
  163. void CAboutDlg::OnButtonWebsite() 
  164. {
  165. // Get a pointer to our application
  166. CMyglApp* pMyApp = (CMyglApp*)AfxGetApp();
  167. ASSERT(pMyApp);
  168. pMyApp->OnHelpWebsite();
  169. //assumes URL names have been initialized
  170. // CInternetSession session("My Session");
  171. // CStdioFile* pFile = NULL;
  172. //use a URL and display a Web page
  173. // pFile = session.OpenURL("http://members.home.net/craignan/index.htm");
  174. // delete pFile;
  175. // session.Close();
  176. }
  177. /////////////////////////////////////////////////////////////////////////////
  178. // CMyglApp commands
  179. BOOL CMyglApp::OnIdle(LONG lCount) 
  180. {
  181. static BOOL bAnimating = FALSE;
  182. static BOOL bResetRequired = FALSE;
  183. static int iFrames;
  184. static double dLastStatusBarUpdate;
  185. double dElapsedTime;
  186. BOOL bMore = CWinApp::OnIdle(lCount);
  187. // Get a pointer to our MainFrame and determine if we need
  188. // to Animate our OpenGL window...
  189. CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
  190. ASSERT(pFrame);
  191. if(pFrame->m_pActiveView) {
  192. // Get a pointer to our CView derived class
  193. CMyglView* pView = (CMyglView*)pFrame->m_pActiveView;
  194. ASSERT_VALID(pView);
  195. if(pFrame->m_pActiveWorld->m_bAnimate)
  196. {
  197. bResetRequired = TRUE;
  198. if(!bAnimating)
  199. {
  200. MyTimeBegin(); // Record the high performance counter
  201. bAnimating = TRUE;
  202. iFrames = 0;
  203. }
  204. // Get the elapsed time since we started animating.
  205. dElapsedTime = MyTimeElapsed();
  206. // Get a pointer to our document and save the time index
  207. CMyglDoc* pDoc = (CMyglDoc*)pFrame->m_pActiveView->GetDocument();
  208. ASSERT(pDoc);
  209. pDoc->m_dTime = dElapsedTime;
  210. // Force a repaint of the window
  211. pFrame->m_pActiveView->InvalidateRect(NULL, FALSE);
  212. // Increment the number of frames
  213. iFrames++;
  214. // Get the time elapsed since we last updated the 
  215. // status bar.
  216. double dTime = dElapsedTime-dLastStatusBarUpdate;
  217. if(dTime > 2.0)
  218. {
  219. dLastStatusBarUpdate = dElapsedTime;
  220. // Calculate the number of frames per second
  221. float fps = (float)(iFrames/dTime);
  222. iFrames = 0; // reset frames
  223. char buf[80];
  224. sprintf(buf, "Animating at %2.2f fpsn", fps);
  225. CStatusBar* pStatus = &pFrame->m_wndStatusBar;
  226. pStatus->SetPaneText(0, buf);
  227. }
  228. return TRUE;
  229. }
  230. else
  231. {
  232. bAnimating = FALSE;
  233. dLastStatusBarUpdate = 0.0;
  234. if(bResetRequired)
  235. {
  236. // Reset the world Animation variables
  237. pFrame->m_pActiveWorld->ResetAnimation(&pView->m_Camera);
  238. bResetRequired = FALSE;
  239. // Force a repaint of the window
  240. pFrame->m_pActiveView->InvalidateRect(NULL, FALSE);
  241. }
  242. }
  243. }
  244. return bMore;
  245. }
  246. BOOL CMyglApp::PreTranslateMessage(MSG* pMsg)
  247. {
  248. // CG: The following lines were added by the Splash Screen component. if (CSplashWnd::PreTranslateAppMessage(pMsg)) return TRUE; return CWinApp::PreTranslateMessage(pMsg);
  249. }
  250. int CMyglApp::ExitInstance() 
  251. {
  252. return CWinApp::ExitInstance();
  253. }
  254. /////////////////////////////////////////////////////////////////////////////
  255. // CMyglApp procedures/Operations
  256. void CMyglApp::GetRegStatus() 
  257. {
  258. CString szWinDir, szRegFile;
  259. CFile cfile;
  260. CFileException fe;
  261. char pBuf[100];
  262. UINT nBytesWrite;
  263. DWORD dw = 0;
  264. struct tm *newtime;
  265. long ltime;
  266. // Get the current time
  267. time( &ltime );
  268. // Obtain coordinated universal time:
  269. newtime = gmtime( &ltime );
  270. // Get the windows directory
  271. GetWindowsDirectory(szWinDir.GetBuffer(MAX_PATH), // address of buffer
  272. MAX_PATH); // size of directory buffer 
  273. szRegFile.Format("%s\%s", szWinDir, "iwdreg.dat"); // Format the name of the file
  274. // Open the file
  275. if(!cfile.Open( szRegFile, CFile::modeCreate | CFile::modeNoTruncate |
  276. CFile::modeReadWrite, &fe))
  277. {
  278. // Error!  Display message to the user..
  279. CString buffer;
  280. buffer.Format("Could not open Regristration status file!n");
  281. AfxMessageBox(buffer, MB_OK, 0);
  282. return;
  283. }
  284. // Read the data
  285. if(cfile.Read( pBuf, 100 ))
  286. {
  287. int iYear, iDays;
  288. sscanf( pBuf, "%ld", &dw);
  289. iYear = dw & 0x000000FF;
  290. iDays = (dw & 0x0000FF00) >> 8;
  291. m_bRegistered = (dw & 0x10000000) >> 28;
  292. m_iDays = ((newtime->tm_year-iYear)*365)+
  293.    (newtime->tm_yday-iDays);
  294. }
  295. else
  296. {
  297. // File does not exist, or contains no data, so save the data
  298. dw = newtime->tm_year | (newtime->tm_yday << 8);
  299. nBytesWrite = sprintf(pBuf, "%ld", dw);
  300. cfile.Write( pBuf, nBytesWrite );
  301. }
  302. }
  303. void CMyglApp::OnHelpWebsite() 
  304. {
  305. DisplayWebSite();
  306. }
  307. void CMyglApp::DisplayWebSite()
  308. {
  309. ShellExecute( NULL, "open", "www.imagewaredev.com", 0,0,1);
  310. }
  311. void CMyglApp::OnHelpSendmail() 
  312. {
  313. SendEmail();
  314. }
  315. void CMyglApp::SendEmail()
  316. {
  317. ShellExecute( NULL, "open", "mailto:support@imagewaredev.com", 0,0,1);
  318. }
  319. void CMyglApp::OnOrderForm() 
  320. {
  321. AfxGetApp()->WinHelp(0x10000 + ID_ORDER_FORM);
  322. }
  323. void CMyglApp::OnOrderOnline() 
  324. {
  325. ShellExecute( NULL, "open", "www.imagewaredev.com", 0,0,1);
  326. }
  327. void CMyglApp::SetPrinterDeviceDefaults(HGLOBAL hDevNames, HGLOBAL hDevMode)
  328. {
  329. // Lock the handles to the structures to get pointers
  330. LPDEVNAMES pDevNames = (LPDEVNAMES)::GlobalLock(hDevNames);
  331. LPDEVMODE  pDevMode  = (LPDEVMODE)::GlobalLock(hDevMode);
  332. // Free the old printer information if it exists
  333. if (m_hDevNames != NULL)
  334. ::GlobalFree(m_hDevNames);
  335. if (m_hDevMode != NULL)
  336. ::GlobalFree(m_hDevMode);
  337. // Allocate space for the new printer info structures
  338. m_hDevNames = ::GlobalAlloc(GPTR, ::GlobalSize(hDevNames));
  339. m_hDevMode = ::GlobalAlloc(GPTR, ::GlobalSize(hDevMode));
  340. // Lock the new handles
  341. LPDEVNAMES m_pDevNames = (LPDEVNAMES)::GlobalLock(m_hDevNames);
  342. LPDEVMODE  m_pDevMode  = (LPDEVMODE)::GlobalLock(m_hDevMode);
  343. // Copy the printer information into the document
  344. memcpy(m_pDevNames, pDevNames,
  345. (size_t)::GlobalSize(hDevNames));
  346. memcpy(m_pDevMode, pDevMode,
  347. (size_t)::GlobalSize(hDevMode));
  348. // Unlock the handles
  349. ::GlobalUnlock(hDevNames);
  350. ::GlobalUnlock(hDevMode);
  351. ::GlobalUnlock(m_hDevNames);
  352. ::GlobalUnlock(m_hDevMode);
  353. }