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

OpenGL

开发平台:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // 3dPageTerrainTexture.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 "3dObjectDialog.h"
  22. #include "EditTextureDlg.h"
  23. #ifdef _DEBUG
  24. #undef THIS_FILE
  25. static char BASED_CODE THIS_FILE[] = __FILE__;
  26. #endif
  27. //////////////////////////////////////////////////////////////////
  28. // C3dPageTerrainTexture definitions
  29. #define TEXTURE_SNOW 0
  30. #define TEXTURE_ROCK 1
  31. #define TEXTURE_GRASS 2
  32. #define TEXTURE_WATER 3
  33. #define MAPTEXTURETOGRID 0
  34. #define MAPTEXTURETOSUBGRID 1
  35. //////////////////////////////////////////////////////////////////
  36. // C3dPageTerrainTexture
  37. IMPLEMENT_DYNCREATE(C3dPageTerrainTexture, CPropertyPage)
  38. /////////////////////////////////////////////////////////////////////////////
  39. // C3dPageTerrainTexture dialog construction
  40. C3dPageTerrainTexture::C3dPageTerrainTexture()
  41. : CPropertyPage(C3dPageTerrainTexture::IDD)
  42. {
  43. //{{AFX_DATA_INIT(C3dPageTerrainTexture)
  44. m_szTextureFileName = _T("<None>");
  45. m_bMapSubGridTextures = FALSE;
  46. m_iApplyTextureToGrid = -1;
  47. m_iSubTextureType = TEXTURE_SNOW;
  48. //}}AFX_DATA_INIT
  49. }
  50. /////////////////////////////////////////////////////////////////////////////
  51. // C3dPageTerrainTexture Destructor
  52. C3dPageTerrainTexture::~C3dPageTerrainTexture()
  53. {
  54. }
  55. void C3dPageTerrainTexture::DoDataExchange(CDataExchange* pDX)
  56. {
  57. CPropertyPage::DoDataExchange(pDX);
  58. //{{AFX_DATA_MAP(C3dPageTerrainTexture)
  59. DDX_Text(pDX, IDC_IMAGE_NAME, m_szTextureFileName);
  60. DDX_Check(pDX, IDC_CHECK_SUBGRIDTEXTURES, m_bMapSubGridTextures);
  61. DDX_Radio(pDX, IDC_RADIO_APPLYTOGRID, m_iApplyTextureToGrid);
  62. DDX_Radio(pDX, IDC_RADIO_SNOW, m_iSubTextureType);
  63. //}}AFX_DATA_MAP
  64. }
  65. BEGIN_MESSAGE_MAP(C3dPageTerrainTexture, CPropertyPage)
  66. //{{AFX_MSG_MAP(C3dPageTerrainTexture)
  67. ON_BN_CLICKED(IDC_TEXTURE_OPEN, OnTextureOpen)
  68. ON_BN_CLICKED(IDC_TEXTURE_EDIT, OnTextureEdit)
  69. ON_BN_CLICKED(IDC_TEXTURE_REMOVE, OnTextureRemove)
  70. ON_BN_CLICKED(IDC_CHECK_SUBGRIDTEXTURES, OnCheckSubgridtextures)
  71. ON_BN_CLICKED(IDC_RADIO_APPLYTOGRID, OnRadioApplytogrid)
  72. ON_BN_CLICKED(IDC_RADIO_APPLYTOSUBGRID, OnRadioApplytosubgrid)
  73. ON_BN_CLICKED(IDC_RADIO_SNOW, OnRadioSnow)
  74. ON_BN_CLICKED(IDC_RADIO_ROCK, OnRadioRock)
  75. ON_BN_CLICKED(IDC_RADIO_GRASS, OnRadioGrass)
  76. ON_BN_CLICKED(IDC_RADIO_WATER, OnRadioWater)
  77. //}}AFX_MSG_MAP
  78. END_MESSAGE_MAP()
  79. /////////////////////////////////////////////////////////////////////////////
  80. // C3dPageTerrainTexture message handlers
  81. BOOL C3dPageTerrainTexture::OnInitDialog() 
  82. {
  83. // let the base class do the default work
  84. CPropertyPage::OnInitDialog();
  85. // Ensure that we have a vaild pointer
  86. if(!m_pObject)
  87. return TRUE;
  88. // Determine which texture names, if any, to show in the edit box
  89. if(m_pObject->m_bApplySubGridTextures && m_pObject->m_bMapTexturesToSubGrid)
  90. {
  91. if(m_iSubTextureType == TEXTURE_SNOW)
  92. {
  93. if(m_pObject->m_pTextureSnow)
  94. {
  95. if(m_pObject->m_pTextureSnow->m_szFileName.GetLength())
  96. m_szTextureFileName = m_pObject->m_pTextureSnow->m_szFileName;
  97. }
  98. }
  99. if(m_iSubTextureType == TEXTURE_ROCK)
  100. {
  101. if(m_pObject->m_pTextureRock)
  102. {
  103. if(m_pObject->m_pTextureRock->m_szFileName.GetLength())
  104. m_szTextureFileName = m_pObject->m_pTextureRock->m_szFileName;
  105. }
  106. }
  107. if(m_iSubTextureType == TEXTURE_GRASS)
  108. {
  109. if(m_pObject->m_pTextureGrass)
  110. {
  111. if(m_pObject->m_pTextureGrass->m_szFileName.GetLength())
  112. m_szTextureFileName = m_pObject->m_pTextureGrass->m_szFileName;
  113. }
  114. }
  115. if(m_iSubTextureType == TEXTURE_WATER)
  116. {
  117. if(m_pObject->m_pTextureWater)
  118. {
  119. if(m_pObject->m_pTextureWater->m_szFileName.GetLength())
  120. m_szTextureFileName = m_pObject->m_pTextureWater->m_szFileName;
  121. }
  122. }
  123. }
  124. else
  125. {
  126. if(m_pObject->m_pTexture)
  127. {
  128. if(m_pObject->m_pTexture->m_szFileName.GetLength())
  129. m_szTextureFileName = m_pObject->m_pTexture->m_szFileName;
  130. }
  131. }
  132. m_bMapSubGridTextures = m_pObject->m_bMapTexturesToSubGrid;
  133. m_iApplyTextureToGrid = m_pObject->m_bApplySubGridTextures;
  134. if(!m_iApplyTextureToGrid)
  135. {
  136. // Set the data window enable/disable states
  137. GetDlgItem(IDC_CHECK_SUBGRIDTEXTURES)->EnableWindow(FALSE);
  138. GetDlgItem(IDC_RADIO_SNOW)->EnableWindow(FALSE);
  139. GetDlgItem(IDC_RADIO_ROCK)->EnableWindow(FALSE);
  140. GetDlgItem(IDC_RADIO_GRASS)->EnableWindow(FALSE);
  141. GetDlgItem(IDC_RADIO_WATER)->EnableWindow(FALSE);
  142. }
  143. // Dialog box is being initialized (FALSE)
  144. // or data is being retrieved (TRUE).
  145. UpdateData(FALSE);
  146. return TRUE;  // return TRUE unless you set the focus to a control
  147.               // EXCEPTION: OCX Property Pages should return FALSE
  148. }
  149. void C3dPageTerrainTexture::OnTextureOpen() 
  150. {
  151. // Dialog box is being initialized (FALSE)
  152. // or data is being retrieved (TRUE).
  153. UpdateData(TRUE);
  154. CFileDialog fileDlg(TRUE, NULL, NULL);
  155. fileDlg.m_ofn.lpstrFilter = "Texture Files (*.bmp)*.bmpDIB Files (*.dib)*.dibAVI Files (*.avi)*.aviAll Files (*.*)*.*";
  156. fileDlg.m_ofn.lpstrTitle = "Open Texture Map";
  157. if(!m_pObject)
  158. return;
  159. int retn = fileDlg.DoModal();
  160. if(retn == IDOK) {
  161. CString szFile = fileDlg.GetFileName();
  162. CString szPath = fileDlg.GetPathName();
  163. CString szFileExt = fileDlg.GetFileExt();
  164. if(szFileExt.Compare("bmp") == 0 ||
  165.    szFileExt.Compare("dib") == 0 ||
  166.    szFileExt.Compare("avi") == 0)
  167. {
  168. if(m_pObject->m_bApplySubGridTextures && m_pObject->m_bMapTexturesToSubGrid)
  169. {
  170. if(m_iSubTextureType == TEXTURE_SNOW)
  171. {
  172. if(!m_pObject->AddSubTexture(szPath.GetBuffer(128), &m_pObject->m_pTextureSnow))
  173. {
  174. AfxMessageBox("Could not create Texture map!", MB_OK, NULL);
  175. return;
  176. }
  177. }
  178. if(m_iSubTextureType == TEXTURE_ROCK)
  179. {
  180. if(!m_pObject->AddSubTexture(szPath.GetBuffer(128), &m_pObject->m_pTextureRock))
  181. {
  182. AfxMessageBox("Could not create Texture map!", MB_OK, NULL);
  183. return;
  184. }
  185. }
  186. if(m_iSubTextureType == TEXTURE_GRASS)
  187. {
  188. if(!m_pObject->AddSubTexture(szPath.GetBuffer(128), &m_pObject->m_pTextureGrass))
  189. {
  190. AfxMessageBox("Could not create Texture map!", MB_OK, NULL);
  191. return;
  192. }
  193. }
  194. if(m_iSubTextureType == TEXTURE_WATER)
  195. {
  196. if(!m_pObject->AddSubTexture(szPath.GetBuffer(128), &m_pObject->m_pTextureWater))
  197. {
  198. AfxMessageBox("Could not create Texture map!", MB_OK, NULL);
  199. return;
  200. }
  201. }
  202. }
  203. else
  204. {
  205. if(!m_pObject->AddTexture(szPath.GetBuffer(128)))
  206. {
  207. AfxMessageBox("Could not create Texture map!", MB_OK, NULL);
  208. return;
  209. }
  210. }
  211. // Force a rebuild of the objects display lists
  212. m_pObject->m_bBuildLists = TRUE;
  213. m_szTextureFileName = szPath;
  214. }
  215. else
  216. AfxMessageBox("Unsupported File Extension!  Select files with either 'bmp', 'dib' or 'avi' extensions.", MB_OK, NULL);
  217. }
  218. // Dialog box is being initialized (FALSE)
  219. // or data is being retrieved (TRUE).
  220. UpdateData(FALSE);
  221. }
  222. void C3dPageTerrainTexture::OnTextureEdit() 
  223. {
  224. CTexture* pTexture = NULL;
  225. // Dialog box is being initialized (FALSE)
  226. // or data is being retrieved (TRUE).
  227. UpdateData(TRUE);
  228. if(m_pObject->m_bApplySubGridTextures && m_pObject->m_bMapTexturesToSubGrid)
  229. {
  230. if(m_iSubTextureType == TEXTURE_SNOW)
  231. {
  232. if(m_pObject->m_pTextureSnow)
  233. pTexture = m_pObject->m_pTextureSnow;
  234. }
  235. if(m_iSubTextureType == TEXTURE_ROCK)
  236. {
  237. if(m_pObject->m_pTextureRock)
  238. pTexture = m_pObject->m_pTextureRock;
  239. }
  240. if(m_iSubTextureType == TEXTURE_GRASS)
  241. {
  242. if(m_pObject->m_pTextureGrass)
  243. pTexture = m_pObject->m_pTextureGrass;
  244. }
  245. if(m_iSubTextureType == TEXTURE_WATER)
  246. {
  247. if(m_pObject->m_pTextureWater)
  248. pTexture = m_pObject->m_pTextureWater;
  249. }
  250. }
  251. else
  252. {
  253. if(m_pObject->m_pTexture)
  254. pTexture = m_pObject->m_pTexture;
  255. }
  256. if(pTexture)
  257. {
  258. ASSERT(pTexture);
  259. CEditTextureDlg textureDlg(pTexture, CWnd::GetActiveWindow());
  260. textureDlg.DoModal();
  261. // Force a reload of the texture map
  262. pTexture->m_bApplyImage = TRUE;
  263. }
  264. else
  265. AfxMessageBox("There are no textures to edit!", MB_OK, 0);
  266. }
  267. void C3dPageTerrainTexture::OnTextureRemove() 
  268. {
  269. // Dialog box is being initialized (FALSE)
  270. // or data is being retrieved (TRUE).
  271. UpdateData(TRUE);
  272. if(m_pObject)
  273. {
  274. // Delete the objects texture map
  275. if(m_pObject->m_bApplySubGridTextures && m_pObject->m_bMapTexturesToSubGrid)
  276. {
  277. if(m_iSubTextureType == TEXTURE_SNOW)
  278. {
  279. if(m_pObject->m_pTextureSnow)
  280. {
  281. m_pObject->DeleteSubTexture(m_pObject->m_pTextureSnow);
  282. m_pObject->m_pTextureSnow = NULL;
  283. }
  284. }
  285. if(m_iSubTextureType == TEXTURE_ROCK)
  286. {
  287. if(m_pObject->m_pTextureRock)
  288. {
  289. m_pObject->DeleteSubTexture(m_pObject->m_pTextureRock);
  290. m_pObject->m_pTextureRock = NULL;
  291. }
  292. }
  293. if(m_iSubTextureType == TEXTURE_GRASS)
  294. {
  295. if(m_pObject->m_pTextureGrass)
  296. {
  297. m_pObject->DeleteSubTexture(m_pObject->m_pTextureGrass);
  298. m_pObject->m_pTextureGrass = NULL;
  299. }
  300. }
  301. if(m_iSubTextureType == TEXTURE_WATER)
  302. {
  303. if(m_pObject->m_pTextureWater)
  304. {
  305. m_pObject->DeleteSubTexture(m_pObject->m_pTextureWater);
  306. m_pObject->m_pTextureWater = NULL;
  307. }
  308. }
  309. }
  310. else
  311. {
  312. if(m_pObject->m_pTexture)
  313. {
  314. m_pObject->DeleteTexture();
  315. m_pObject->m_pTexture = NULL;
  316. }
  317. }
  318. m_szTextureFileName = _T("<None>");
  319. // Dialog box is being initialized (FALSE)
  320. // or data is being retrieved (TRUE).
  321. UpdateData(FALSE);
  322. }
  323. }
  324. void C3dPageTerrainTexture::OnCheckSubgridtextures() 
  325. {
  326. m_pObject->m_bMapTexturesToSubGrid = !m_pObject->m_bMapTexturesToSubGrid;
  327. if(m_pObject->m_bMapTexturesToSubGrid)
  328. {
  329. GetDlgItem(IDC_RADIO_SNOW)->EnableWindow(TRUE);
  330. GetDlgItem(IDC_RADIO_ROCK)->EnableWindow(TRUE);
  331. GetDlgItem(IDC_RADIO_GRASS)->EnableWindow(TRUE);
  332. GetDlgItem(IDC_RADIO_WATER)->EnableWindow(TRUE);
  333. }
  334. else
  335. {
  336. GetDlgItem(IDC_RADIO_SNOW)->EnableWindow(FALSE);
  337. GetDlgItem(IDC_RADIO_ROCK)->EnableWindow(FALSE);
  338. GetDlgItem(IDC_RADIO_GRASS)->EnableWindow(FALSE);
  339. GetDlgItem(IDC_RADIO_WATER)->EnableWindow(FALSE);
  340. }
  341. }
  342. void C3dPageTerrainTexture::OnRadioApplytogrid() 
  343. {
  344. m_pObject->m_bApplySubGridTextures = FALSE;
  345. // Set the data window enable/disable states
  346. GetDlgItem(IDC_CHECK_SUBGRIDTEXTURES)->EnableWindow(FALSE);
  347. m_pObject->m_bMapTexturesToSubGrid = FALSE;
  348. }
  349. void C3dPageTerrainTexture::OnRadioApplytosubgrid() 
  350. {
  351. m_pObject->m_bApplySubGridTextures = TRUE;
  352. // Set the data window enable/disable states
  353. // Disabled for now..
  354. // GetDlgItem(IDC_CHECK_SUBGRIDTEXTURES)->EnableWindow(TRUE);
  355. }
  356. void C3dPageTerrainTexture::OnOK() 
  357. {
  358. // Dialog box is being initialized (FALSE)
  359. // or data is being retrieved (TRUE).
  360. UpdateData(TRUE);
  361. // Force the object to rebuild its' display list
  362. m_pObject->m_bBuildLists = TRUE;
  363. CPropertyPage::OnOK();
  364. }
  365. void C3dPageTerrainTexture::OnRadioSnow() 
  366. {
  367. // Dialog box is being initialized (FALSE)
  368. // or data is being retrieved (TRUE).
  369. UpdateData(TRUE);
  370. if(m_pObject->m_pTextureSnow)
  371. m_szTextureFileName = m_pObject->m_pTextureSnow->m_szFileName;
  372. else
  373. m_szTextureFileName = _T("<None>");
  374. // Dialog box is being initialized (FALSE)
  375. // or data is being retrieved (TRUE).
  376. UpdateData(FALSE);
  377. }
  378. void C3dPageTerrainTexture::OnRadioRock() 
  379. {
  380. // Dialog box is being initialized (FALSE)
  381. // or data is being retrieved (TRUE).
  382. UpdateData(TRUE);
  383. if(m_pObject->m_pTextureRock)
  384. m_szTextureFileName = m_pObject->m_pTextureRock->m_szFileName;
  385. else
  386. m_szTextureFileName = _T("<None>");
  387. // Dialog box is being initialized (FALSE)
  388. // or data is being retrieved (TRUE).
  389. UpdateData(FALSE);
  390. }
  391. void C3dPageTerrainTexture::OnRadioGrass() 
  392. {
  393. // Dialog box is being initialized (FALSE)
  394. // or data is being retrieved (TRUE).
  395. UpdateData(TRUE);
  396. if(m_pObject->m_pTextureGrass)
  397. m_szTextureFileName = m_pObject->m_pTextureGrass->m_szFileName;
  398. else
  399. m_szTextureFileName = _T("<None>");
  400. // Dialog box is being initialized (FALSE)
  401. // or data is being retrieved (TRUE).
  402. UpdateData(FALSE);
  403. }
  404. void C3dPageTerrainTexture::OnRadioWater() 
  405. {
  406. // Dialog box is being initialized (FALSE)
  407. // or data is being retrieved (TRUE).
  408. UpdateData(TRUE);
  409. if(m_pObject->m_pTextureWater)
  410. m_szTextureFileName = m_pObject->m_pTextureWater->m_szFileName;
  411. else
  412. m_szTextureFileName = _T("<None>");
  413. // Dialog box is being initialized (FALSE)
  414. // or data is being retrieved (TRUE).
  415. UpdateData(FALSE);
  416. }