3dPageTerrainColor.cpp
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:11k
源码类别:

OpenGL

开发平台:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // 3dPageTerrainColor.cpp : implementation file
  3. //
  4. // glOOP (OpenGL Object Oriented Programming library)
  5. // Copyright (c) Craig Fahrnbach 1997, 1998
  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. // This program is -not- in the public domain.
  17. //
  18. /////////////////////////////////////////////////////////////////////////////
  19. #include "stdafx.h"
  20. #include "glOOP.h"
  21. #include "MyColorPaletteWnd.h"
  22. #include "EditColorDlg.h"
  23. #include "3dObjectDialog.h"
  24. #ifdef _DEBUG
  25. #undef THIS_FILE
  26. static char BASED_CODE THIS_FILE[] = __FILE__;
  27. #endif
  28. //////////////////////////////////////////////////////////////////
  29. // C3dPageTerrainColor
  30. IMPLEMENT_DYNCREATE(C3dPageTerrainColor, CPropertyPage)
  31. /////////////////////////////////////////////////////////////////////////////
  32. // C3dPageTerrainColor dialog construction
  33. C3dPageTerrainColor::C3dPageTerrainColor()
  34. :CPropertyPage(C3dPageTerrainColor::IDD)
  35. {
  36. m_pWorld  = NULL;
  37. m_pObject = NULL;
  38. m_pWndTerrainPreview = new CMyTerrainWnd;
  39. //{{AFX_DATA_INIT(C3dPageTerrainColor)
  40. m_iColorSelect = 0;
  41. m_iSolidColor = -1;
  42. m_bReflect = FALSE;
  43. //}}AFX_DATA_INIT
  44. }
  45. /////////////////////////////////////////////////////////////////////////////
  46. // C3dPageTerrainColor Destructor
  47. C3dPageTerrainColor::~C3dPageTerrainColor()
  48. {
  49. if(m_pWndTerrainPreview)
  50. delete m_pWndTerrainPreview;
  51. }
  52. void C3dPageTerrainColor::DoDataExchange(CDataExchange* pDX)
  53. {
  54. CPropertyPage::DoDataExchange(pDX);
  55. //{{AFX_DATA_MAP(C3dPageTerrainColor)
  56. DDX_Radio(pDX, IDC_RADIO_SNOW, m_iColorSelect);
  57. DDX_Radio(pDX, IDC_RADIO_COLOR_SOLID, m_iSolidColor);
  58. DDX_Check(pDX, IDC_CHECK_REFLECT, m_bReflect);
  59. //}}AFX_DATA_MAP
  60. }
  61. BEGIN_MESSAGE_MAP(C3dPageTerrainColor, CPropertyPage)
  62. //{{AFX_MSG_MAP(C3dPageTerrainColor)
  63. ON_BN_CLICKED(IDC_BUTTON_COLOR_WND, OnButtonColorWnd)
  64. ON_WM_DRAWITEM()
  65. ON_BN_CLICKED(IDC_RADIO_COLOR_GRADIENT, OnRadioColorGradient)
  66. ON_BN_CLICKED(IDC_RADIO_COLOR_SOLID, OnRadioColorSolid)
  67. ON_BN_CLICKED(IDC_RADIO_GRASS, OnRadioGrass)
  68. ON_BN_CLICKED(IDC_RADIO_ROCK, OnRadioRock)
  69. ON_BN_CLICKED(IDC_RADIO_SNOW, OnRadioSnow)
  70. ON_BN_CLICKED(IDC_RADIO_WATER, OnRadioWater)
  71. ON_BN_CLICKED(IDC_CHECK_REFLECT, OnCheckReflect)
  72. //}}AFX_MSG_MAP
  73. END_MESSAGE_MAP()
  74. /////////////////////////////////////////////////////////////////////////////
  75. // C3dPageTerrainColor operations
  76. void C3dPageTerrainColor::PaintButtonWnd(CWnd* pWnd, C3dColor* pColor) 
  77. {
  78. CRect rect; // window rect
  79. CDC* pDC; // pointer to a device context
  80. // Get the size of the gradient window
  81. pWnd->GetWindowRect(&rect);
  82. // Get a pointer to the windows device context
  83. pDC = pWnd->GetDC();
  84. if(pDC)
  85. pDC->FillSolidRect(0,
  86.    0,
  87.    rect.right-rect.left,
  88.    rect.bottom-rect.top,
  89.    COLORREF RGB(pColor->m_fColor[0]*255,
  90. pColor->m_fColor[1]*255,
  91. pColor->m_fColor[2]*255));
  92. ReleaseDC(pDC);
  93. }
  94. /////////////////////////////////////////////////////////////////////////////
  95. // C3dPageTerrainColor message handlers
  96. BOOL C3dPageTerrainColor::OnInitDialog() 
  97. {
  98. // let the base class do the default work
  99. CPropertyPage::OnInitDialog();
  100. if(!m_pWorld)
  101. return FALSE;
  102. // Create the owner drawn button color window
  103. CRect rect;
  104. GetDlgItem(IDC_BUTTON_COLOR_WND)->GetWindowRect(&rect);
  105. ScreenToClient(&rect);
  106. m_wndButtonColor.Create(NULL, // lpszClassName
  107. NULL, // lpszWindowName
  108. WS_CHILD | WS_VISIBLE | // dwStyle
  109. WS_DLGFRAME,
  110. rect, // rect
  111. this, // CWnd* pParentWnd
  112. IDC_BUTTON_COLOR_WND, // UINT  nID
  113. 0); // pContext
  114. GetDlgItem(IDC_TERRAIN_PREVIEW_WND)->GetWindowRect(&rect);
  115. ScreenToClient(&rect);
  116. m_pWndTerrainPreview->Create(NULL, // dwExtStyle
  117. NULL, // lpszClassName
  118. NULL, // lpszWindowName
  119. WS_CHILD | WS_VISIBLE | // dwStyle
  120. WS_DLGFRAME,
  121. rect, // rect
  122. this, // CWnd* pParentWnd
  123. IDC_TERRAIN_PREVIEW_WND,// UINT  nID
  124. 0); // pContext
  125. if(!m_pWndTerrainPreview->m_pTerrainObject)
  126. {
  127. AfxMessageBox("Not enough memory to create preview terrain.", MB_OK);
  128. return FALSE;
  129. }
  130. if(m_pObject->m_bSolidColor)
  131. {
  132. m_iSolidColor = 0;
  133. // Set the data window enable/disable states
  134. GetDlgItem(IDC_RADIO_SNOW)->EnableWindow(FALSE);
  135. GetDlgItem(IDC_RADIO_ROCK)->EnableWindow(FALSE);
  136. GetDlgItem(IDC_RADIO_GRASS)->EnableWindow(FALSE);
  137. GetDlgItem(IDC_RADIO_WATER)->EnableWindow(FALSE);
  138. GetDlgItem(IDC_CHECK_REFLECT)->EnableWindow(FALSE);
  139. }
  140. else
  141. m_iSolidColor = 1;
  142. m_bReflect = m_pObject->m_bReflect;
  143. // Initialize our preview window Terrain object
  144. m_pObject->CopyParameters(m_pWndTerrainPreview->m_pTerrainObject);
  145. m_pWndTerrainPreview->m_pTerrainObject->m_bBuildLists = TRUE;
  146. m_pWndTerrainPreview->m_pTerrainObject->InitTerrain();
  147. // Dialog box is being initialized (FALSE)
  148. // or data is being retrieved (TRUE).
  149. UpdateData(FALSE);
  150. return TRUE;  // return TRUE unless you set the focus to a control
  151.               // EXCEPTION: OCX Property Pages should return FALSE
  152. }
  153. void C3dPageTerrainColor::OnOK() 
  154. {
  155. // Dialog box is being initialized (FALSE)
  156. // or data is being retrieved (TRUE).
  157. UpdateData(TRUE);
  158. // Copy the parameters from the preview window to our terrain object
  159. m_pWndTerrainPreview->m_pTerrainObject->CopyParameters(m_pObject);
  160. // Force the object to rebuild its' display list
  161. m_pObject->m_bBuildLists = TRUE;
  162. // Rebuild our objects vertices and color table
  163. m_pObject->InitTerrain();
  164. CPropertyPage::OnOK();
  165. }
  166. void C3dPageTerrainColor::OnButtonColorWnd() 
  167. {
  168. C3dColor SelColor;
  169. // Dialog box is being initialized (FALSE)
  170. // or data is being retrieved (TRUE).
  171. UpdateData(TRUE);
  172. C3dObjectTerrain* pTerrain = m_pWndTerrainPreview->m_pTerrainObject;
  173. if(m_pWorld && pTerrain) 
  174. {
  175. if(!pTerrain->m_bSolidColor)
  176. {
  177. if(m_iColorSelect == 0)
  178. SelColor.SetColor4fv(&pTerrain->m_ColorSnow);
  179. else if(m_iColorSelect == 1)
  180. SelColor.SetColor4fv(&pTerrain->m_ColorRock);
  181. else if(m_iColorSelect == 2)
  182. SelColor.SetColor4fv(&pTerrain->m_ColorGrass);
  183. else
  184. SelColor.SetColor4fv(&pTerrain->m_ColorWater);
  185. }
  186. else
  187. SelColor.SetColor4fv(&pTerrain->m_Color);
  188. // Create the dialog
  189. CEditColorDlg colorDlg(&m_pWorld->m_ColorList, &SelColor, this);
  190. // Invoke the modal dialog box and return the
  191. // dialog-box result when done.
  192. if(colorDlg.DoModal() == IDOK)
  193. {
  194. if(!pTerrain->m_bSolidColor)
  195. {
  196. // Save any changes the user may have made
  197. if(m_iColorSelect == 0)
  198. pTerrain->m_ColorSnow.SetColor4fv(&colorDlg.m_Color);
  199. else if(m_iColorSelect == 1)
  200. pTerrain->m_ColorRock.SetColor4fv(&colorDlg.m_Color);
  201. else if(m_iColorSelect == 2)
  202. pTerrain->m_ColorGrass.SetColor4fv(&colorDlg.m_Color);
  203. else
  204. pTerrain->m_ColorWater.SetColor4fv(&colorDlg.m_Color);
  205. }
  206. else
  207. pTerrain->m_Color.SetColor4fv(&colorDlg.m_Color);
  208. // Repaint the color button
  209. PaintButtonWnd(&m_wndButtonColor, &colorDlg.m_Color);
  210. // Reinitialize our preview terrain data and update the window
  211. pTerrain->m_bBuildLists = TRUE;
  212. pTerrain->InitTerrain();
  213. m_pWndTerrainPreview->RedrawWindow();
  214. }
  215. }
  216. }
  217. void C3dPageTerrainColor::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
  218. {
  219. CPropertyPage::OnDrawItem(nIDCtl, lpDrawItemStruct);
  220. C3dObjectTerrain* pTerrain = m_pWndTerrainPreview->m_pTerrainObject;
  221. if(nIDCtl == IDC_BUTTON_COLOR_WND && m_wndButtonColor.m_hWnd)
  222. {
  223. if(!pTerrain->m_bSolidColor)
  224. {
  225. if(m_iColorSelect == 0)
  226. PaintButtonWnd(&m_wndButtonColor, &pTerrain->m_ColorSnow);
  227. else if(m_iColorSelect == 1)
  228. PaintButtonWnd(&m_wndButtonColor, &pTerrain->m_ColorRock);
  229. else if(m_iColorSelect == 2)
  230. PaintButtonWnd(&m_wndButtonColor, &pTerrain->m_ColorGrass);
  231. else
  232. PaintButtonWnd(&m_wndButtonColor, &pTerrain->m_ColorWater);
  233. }
  234. else
  235. PaintButtonWnd(&m_wndButtonColor, &pTerrain->m_Color);
  236. }
  237. }
  238. void C3dPageTerrainColor::OnRadioColorGradient() 
  239. {
  240. C3dObjectTerrain* pTerrain = m_pWndTerrainPreview->m_pTerrainObject;
  241. // Set the data window enable/disable states
  242. GetDlgItem(IDC_RADIO_SNOW)->EnableWindow(TRUE);
  243. GetDlgItem(IDC_RADIO_ROCK)->EnableWindow(TRUE);
  244. GetDlgItem(IDC_RADIO_GRASS)->EnableWindow(TRUE);
  245. GetDlgItem(IDC_RADIO_WATER)->EnableWindow(TRUE);
  246. GetDlgItem(IDC_CHECK_REFLECT)->EnableWindow(TRUE);
  247. // Repaint the color button
  248. if(m_iColorSelect == 0)
  249. PaintButtonWnd(&m_wndButtonColor, &pTerrain->m_ColorSnow);
  250. else if(m_iColorSelect == 1)
  251. PaintButtonWnd(&m_wndButtonColor, &pTerrain->m_ColorRock);
  252. else if(m_iColorSelect == 2)
  253. PaintButtonWnd(&m_wndButtonColor, &pTerrain->m_ColorGrass);
  254. else
  255. PaintButtonWnd(&m_wndButtonColor, &pTerrain->m_ColorWater);
  256. // Re-initialize our terrain data and update the preview window
  257. pTerrain->m_bSolidColor = FALSE;
  258. pTerrain->m_bBuildLists = TRUE;
  259. pTerrain->InitTerrain();
  260. m_pWndTerrainPreview->RedrawWindow();
  261. }
  262. void C3dPageTerrainColor::OnRadioColorSolid() 
  263. {
  264. C3dObjectTerrain* pTerrain = m_pWndTerrainPreview->m_pTerrainObject;
  265. PaintButtonWnd(&m_wndButtonColor, &pTerrain->m_Color);
  266. // Set the data window enable/disable states
  267. GetDlgItem(IDC_RADIO_SNOW)->EnableWindow(FALSE);
  268. GetDlgItem(IDC_RADIO_ROCK)->EnableWindow(FALSE);
  269. GetDlgItem(IDC_RADIO_GRASS)->EnableWindow(FALSE);
  270. GetDlgItem(IDC_RADIO_WATER)->EnableWindow(FALSE);
  271. GetDlgItem(IDC_CHECK_REFLECT)->EnableWindow(FALSE);
  272. // Re-initialize our terrain data and update the preview window
  273. pTerrain->m_bSolidColor = TRUE;
  274. pTerrain->m_bBuildLists = TRUE;
  275. pTerrain->InitTerrain();
  276. m_pWndTerrainPreview->RedrawWindow();
  277. }
  278. void C3dPageTerrainColor::OnRadioGrass() 
  279. {
  280. C3dObjectTerrain* pTerrain = m_pWndTerrainPreview->m_pTerrainObject;
  281. PaintButtonWnd(&m_wndButtonColor, &pTerrain->m_ColorGrass);
  282. }
  283. void C3dPageTerrainColor::OnRadioRock() 
  284. {
  285. C3dObjectTerrain* pTerrain = m_pWndTerrainPreview->m_pTerrainObject;
  286. PaintButtonWnd(&m_wndButtonColor, &pTerrain->m_ColorRock);
  287. }
  288. void C3dPageTerrainColor::OnRadioSnow() 
  289. {
  290. C3dObjectTerrain* pTerrain = m_pWndTerrainPreview->m_pTerrainObject;
  291. PaintButtonWnd(&m_wndButtonColor, &pTerrain->m_ColorSnow);
  292. }
  293. void C3dPageTerrainColor::OnRadioWater() 
  294. {
  295. C3dObjectTerrain* pTerrain = m_pWndTerrainPreview->m_pTerrainObject;
  296. PaintButtonWnd(&m_wndButtonColor, &pTerrain->m_ColorWater);
  297. }
  298. void C3dPageTerrainColor::OnCheckReflect() 
  299. {
  300. C3dObjectTerrain* pTerrain = m_pWndTerrainPreview->m_pTerrainObject;
  301. pTerrain->m_bReflect = !pTerrain->m_bReflect;
  302. // Re-initialize our terrain data and redraw our
  303. // preview window.
  304. pTerrain->m_bBuildLists = TRUE;
  305. pTerrain->InitTerrain();
  306. m_pWndTerrainPreview->RedrawWindow();
  307. }