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

OpenGL

开发平台:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // MyColorPaletteWnd.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. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CMyColorPaletteWnd
  29. IMPLEMENT_DYNAMIC(CMyColorPaletteWnd, CMyglWnd)
  30. CMyColorPaletteWnd::CMyColorPaletteWnd()
  31. {
  32. // Initialize our class variables
  33. m_fRed = 0.0f;
  34. m_fGrn = 0.0f;
  35. m_fBlu = 0.0f;
  36. // Set our camera to Orthographic view and
  37. // initialize our camera variables
  38. m_Camera.m_bPerspective = FALSE;
  39. m_Camera.m_fOrigin[X] =   0.0f;
  40. m_Camera.m_fOrigin[Y] =   0.0f;
  41. m_Camera.m_fOrigin[Z] = 220.0f;
  42. m_Camera.m_fRotation[X] =  30.0f;
  43. m_Camera.m_fRotation[Y] = 225.0f;
  44. m_Camera.m_fRotation[Z] =   0.0f;
  45. m_Camera.m_fNear =   0.0f;
  46. m_Camera.m_fFar  = 300.0f;
  47. }
  48. CMyColorPaletteWnd::~CMyColorPaletteWnd()
  49. {
  50. }
  51. BEGIN_MESSAGE_MAP(CMyColorPaletteWnd, CWnd)
  52. //{{AFX_MSG_MAP(CMyColorPaletteWnd)
  53. ON_WM_PAINT()
  54. ON_WM_CREATE()
  55. ON_WM_LBUTTONDOWN()
  56. ON_WM_KEYDOWN()
  57. ON_WM_RBUTTONDOWN()
  58. ON_WM_VSCROLL()
  59. ON_WM_HSCROLL()
  60. ON_WM_DESTROY()
  61. ON_WM_SIZE()
  62. ON_WM_QUERYNEWPALETTE()
  63. ON_WM_PALETTECHANGED()
  64. ON_WM_ERASEBKGND()
  65. //}}AFX_MSG_MAP
  66. END_MESSAGE_MAP()
  67. BOOL CMyColorPaletteWnd::Create(DWORD dwExStyle, LPCTSTR lpszClassName,
  68. LPCTSTR lpszWindowName, DWORD dwStyle,
  69. const RECT& rect, CWnd* pParentWnd, UINT nID,
  70. CCreateContext* pContext) 
  71. {
  72. if(lpszClassName == NULL)
  73. // Register a class with its own device context and the 'cross' cursor
  74. lpszClassName = AfxRegisterWndClass(CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
  75. ::LoadCursor(NULL, IDC_CROSS));
  76. return CMyglWnd::Create(dwExStyle, lpszClassName, lpszWindowName,
  77. dwStyle, rect, pParentWnd, nID, pContext);
  78. }
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CMyColorPaletteWnd messages forwarded to CMyglWnd base class
  81. void CMyColorPaletteWnd::OnDestroy() 
  82. {
  83. // Forward the message to the base class
  84. CMyglWnd::OnDestroy();
  85. }
  86. void CMyColorPaletteWnd::OnSize(UINT nType, int cx, int cy) 
  87. {
  88. // Forward the message to the base class
  89. CMyglWnd::OnSize(nType, cx, cy);
  90. }
  91. void CMyColorPaletteWnd::OnPaletteChanged(CWnd* pFocusWnd) 
  92. {
  93. // Forward the message to the base class
  94. CMyglWnd::OnPaletteChanged(pFocusWnd);
  95. }
  96. BOOL CMyColorPaletteWnd::OnQueryNewPalette() 
  97. {
  98. // Forward the message to the base class
  99. return CMyglWnd::OnQueryNewPalette();
  100. }
  101. BOOL CMyColorPaletteWnd::OnEraseBkgnd(CDC* pDC) 
  102. {
  103. // Forward the message to the base class
  104. return CMyglWnd::OnEraseBkgnd(pDC);
  105. }
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CMyColorPaletteWnd message handlers
  108. int CMyColorPaletteWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  109. {
  110. if (CMyglWnd::OnCreate(lpCreateStruct) == -1)
  111. return -1;
  112. // Initialize our scroll bar range
  113. // 0 to 360 degrees
  114. SetScrollRange(SB_HORZ, 0, 360, TRUE);
  115. SetScrollRange(SB_VERT, 0, 360, TRUE);
  116. SetScrollPos(SB_HORZ, (int)(360.0f-m_Camera.m_fRotation[Y]), TRUE );
  117. SetScrollPos(SB_VERT, (int)m_Camera.m_fRotation[X], TRUE );
  118. return 0;
  119. }
  120. void CMyColorPaletteWnd::OnPaint() 
  121. {
  122. CPaintDC dc(this); // device context for painting
  123. // Make this view the current OpenGL rendering context...
  124. if(!the3dEngine.EnableRC(dc, m_hRC, TRUE))
  125. return;
  126. // Call OpenGL drawing code
  127. RenderColorCube();
  128. // Exchange the front and back buffers
  129. SwapBuffers(dc);
  130. // Releases the device context that is used by the rendering context
  131. // to allow other rendering contexts to co-exist.
  132. the3dEngine.EnableRC(NULL, NULL, FALSE);
  133. // Do not call CWnd::OnPaint() for painting messages
  134. }
  135. void CMyColorPaletteWnd::OnLButtonDown(UINT nFlags, CPoint point) 
  136. {
  137. COLORREF PaletteColor;
  138. SelectPalette(m_pDC->GetSafeHdc(), the3dEngine.GetPalette(), 0); //map palette to our bitmap
  139. PaletteColor = GetPixel(m_pDC->GetSafeHdc(), point.x, point.y);
  140. m_fRed = (GLfloat)GetRValue(PaletteColor)/255;
  141. m_fGrn = (GLfloat)GetGValue(PaletteColor)/255;
  142. m_fBlu = (GLfloat)GetBValue(PaletteColor)/255;
  143. // Route message to parent
  144. CWnd* pParent = GetParent();
  145. if(pParent)
  146. pParent->SendMessage(WM_CPW_LBUTTONDOWN,
  147. (WPARAM)nFlags,
  148. (LPARAM)point.x<<16 | point.y);
  149. CWnd::OnLButtonDown(nFlags, point);
  150. }
  151. void CMyColorPaletteWnd::OnRButtonDown(UINT nFlags, CPoint point) 
  152. {
  153. COLORREF PaletteColor;
  154. SelectPalette(m_pDC->GetSafeHdc(), the3dEngine.GetPalette(), 0); //map palette to our bitmap
  155. PaletteColor = GetPixel(m_pDC->GetSafeHdc(), point.x, point.y);
  156. m_fRed = (GLfloat)GetRValue(PaletteColor)/255;
  157. m_fGrn = (GLfloat)GetGValue(PaletteColor)/255;
  158. m_fBlu = (GLfloat)GetBValue(PaletteColor)/255;
  159. // Route message to parent
  160. CWnd* pParent = GetParent();
  161. if(pParent)
  162. pParent->SendMessage(WM_CPW_RBUTTONDOWN,
  163. (WPARAM)nFlags,
  164. (LPARAM)point.x<<16 | point.y);
  165. CWnd::OnRButtonDown(nFlags, point);
  166. }
  167. void CMyColorPaletteWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  168. {
  169. if(nChar == VK_UP)
  170. m_Camera.m_fRotation[X]-= 5.0f;
  171. if(nChar == VK_DOWN)
  172. m_Camera.m_fRotation[X] += 5.0f;
  173. if(nChar == VK_LEFT)
  174. m_Camera.m_fRotation[Y] += 5.0f;
  175. if(nChar == VK_RIGHT)
  176. m_Camera.m_fRotation[Y] -= 5.0f;
  177. m_Camera.m_fRotation[X] = Normal360(m_Camera.m_fRotation[X]);
  178. m_Camera.m_fRotation[Y] = Normal360(m_Camera.m_fRotation[Y]);
  179. InvalidateRect(NULL, FALSE);
  180. CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
  181. }
  182. GLfloat CMyColorPaletteWnd::Normal360(GLfloat fSource)
  183. {
  184. if(fSource < 0.0)
  185. fSource = 360.0f + fSource;
  186. if(fSource > 360.0f)
  187. fSource = fSource - 360.0f;
  188. return fSource;
  189. }
  190. void CMyColorPaletteWnd::RenderColorCube()
  191. {
  192. glEnable(GL_DEPTH_TEST);
  193. glShadeModel(GL_SMOOTH);
  194. // If we have a palette, enable dithering of color before
  195. // writing to the color buffer
  196. if(the3dEngine.GetPalette())
  197. glEnable(GL_DITHER);
  198. // Set the background color to black (default)
  199. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  200. // Clear the window with current clearing color
  201. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  202. // Save the current Modelview matrix
  203. glMatrixMode(GL_MODELVIEW);
  204. glPushMatrix();
  205. // Position our camera
  206. m_Camera.PositionCamera();
  207. // Front face
  208. glBegin(GL_POLYGON);
  209. // White
  210. glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)255);
  211. glVertex3f(50.0f,50.0f,50.0f);
  212. // Yellow
  213. glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)0);
  214. glVertex3f(50.0f,-50.0f,50.0f);
  215. // Red
  216. glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)0);
  217. glVertex3f(-50.0f,-50.0f,50.0f);
  218. // Magenta
  219. glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)255);
  220. glVertex3f(-50.0f,50.0f,50.0f);
  221. glEnd();
  222. // Back Face
  223. glBegin(GL_POLYGON);
  224. // Cyan
  225. glColor3f(0.0f, 1.0f, 1.0f);
  226. glVertex3f(50.0f,50.0f,-50.0f);
  227. // Green
  228. glColor3f(0.0f, 1.0f, 0.0f);
  229. glVertex3f(50.0f,-50.0f,-50.0f);
  230. // Black
  231. glColor3f(0.0f, 0.0f, 0.0f);
  232. glVertex3f(-50.0f,-50.0f,-50.0f);
  233. // Blue
  234. glColor3f(0.0f, 0.0f, 1.0f);
  235. glVertex3f(-50.0f,50.0f,-50.0f);
  236. glEnd();
  237. // Top Face
  238. glBegin(GL_POLYGON);
  239. // Cyan
  240. glColor3f(0.0f, 1.0f, 1.0f);
  241. glVertex3f(50.0f,50.0f,-50.0f);
  242. // White
  243. glColor3f(1.0f, 1.0f, 1.0f);
  244. glVertex3f(50.0f,50.0f,50.0f);
  245. // Magenta
  246. glColor3f(1.0f, 0.0f, 1.0f);
  247. glVertex3f(-50.0f,50.0f,50.0f);
  248. // Blue
  249. glColor3f(0.0f, 0.0f, 1.0f);
  250. glVertex3f(-50.0f,50.0f,-50.0f);
  251. glEnd();
  252. // Bottom Face
  253. glBegin(GL_POLYGON);
  254. // Green
  255. glColor3f(0.0f, 1.0f, 0.0f);
  256. glVertex3f(50.0f,-50.0f,-50.0f);
  257. // Yellow
  258. glColor3f(1.0f, 1.0f, 0.0f);
  259. glVertex3f(50.0f,-50.0f,50.0f);
  260. // Red
  261. glColor3f(1.0f, 0.0f, 0.0f);
  262. glVertex3f(-50.0f,-50.0f,50.0f);
  263. // Black
  264. glColor3f(0.0f, 0.0f, 0.0f);
  265. glVertex3f(-50.0f,-50.0f,-50.0f);
  266. glEnd();
  267. // Left face
  268. glBegin(GL_POLYGON);
  269. // White
  270. glColor3f(1.0f, 1.0f, 1.0f);
  271. glVertex3f(50.0f,50.0f,50.0f);
  272. // Cyan
  273. glColor3f(0.0f, 1.0f, 1.0f);
  274. glVertex3f(50.0f,50.0f,-50.0f);
  275. // Green
  276. glColor3f(0.0f, 1.0f, 0.0f);
  277. glVertex3f(50.0f,-50.0f,-50.0f);
  278. // Yellow
  279. glColor3f(1.0f, 1.0f, 0.0f);
  280. glVertex3f(50.0f,-50.0f,50.0f);
  281. glEnd();
  282. // Right face
  283. glBegin(GL_POLYGON);
  284. // Magenta
  285. glColor3f(1.0f, 0.0f, 1.0f);
  286. glVertex3f(-50.0f,50.0f,50.0f);
  287. // Blue
  288. glColor3f(0.0f, 0.0f, 1.0f);
  289. glVertex3f(-50.0f,50.0f,-50.0f);
  290. // Black
  291. glColor3f(0.0f, 0.0f, 0.0f);
  292. glVertex3f(-50.0f,-50.0f,-50.0f);
  293. // Red
  294. glColor3f(1.0f, 0.0f, 0.0f);
  295. glVertex3f(-50.0f,-50.0f,50.0f);
  296. glEnd();
  297. // Restore the Modelview matrix
  298. glPopMatrix();
  299. // Flush drawing commands
  300. glFlush();
  301. }
  302. void CMyColorPaletteWnd::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
  303. {
  304. if(nSBCode == SB_LINEUP) // nSBCode = 0
  305. {
  306. m_Camera.m_fRotation[X] = Normal360(m_Camera.m_fRotation[X]-6);
  307. SetScrollPos(SB_VERT, (int)m_Camera.m_fRotation[X], TRUE );
  308. InvalidateRect(NULL, FALSE);
  309. }
  310. if(nSBCode == SB_LINEDOWN) // nSBCode = 1
  311. {
  312. m_Camera.m_fRotation[X] = Normal360(m_Camera.m_fRotation[X]+6);
  313. SetScrollPos(SB_VERT, (int)m_Camera.m_fRotation[X], TRUE );
  314. InvalidateRect(NULL, FALSE);
  315. }
  316. if(nSBCode == SB_PAGEUP) // nSBCode = 2
  317. {
  318. m_Camera.m_fRotation[X] = Normal360(m_Camera.m_fRotation[X]-36);
  319. SetScrollPos(SB_VERT, (int)m_Camera.m_fRotation[X], TRUE );
  320. InvalidateRect(NULL, FALSE);
  321. }
  322. if(nSBCode == SB_PAGEDOWN) // nSBCode = 3
  323. {
  324. m_Camera.m_fRotation[X] = Normal360(m_Camera.m_fRotation[X]+36);
  325. SetScrollPos(SB_VERT, (int)m_Camera.m_fRotation[X], TRUE );
  326. InvalidateRect(NULL, FALSE);
  327. }
  328. if(nSBCode == SB_THUMBPOSITION) // nSBCode = 4
  329. {
  330. m_Camera.m_fRotation[X] = Normal360((float)nPos);
  331. SetScrollPos(SB_VERT, (int)m_Camera.m_fRotation[X], TRUE );
  332. InvalidateRect(NULL, FALSE);
  333. }
  334. if(nSBCode == SB_THUMBTRACK) // nSBCode = 5
  335. {
  336. m_Camera.m_fRotation[X] = Normal360((float)nPos);
  337. InvalidateRect(NULL, FALSE);
  338. }
  339. CWnd::OnVScroll(nSBCode, nPos, pScrollBar);
  340. }
  341. void CMyColorPaletteWnd::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
  342. {
  343. if(nSBCode == SB_LINELEFT) // nSBCode = 0
  344. {
  345. m_Camera.m_fRotation[Y] = Normal360(m_Camera.m_fRotation[Y]+6);
  346. SetScrollPos(SB_HORZ, (int)(360.0f-m_Camera.m_fRotation[Y]), TRUE );
  347. InvalidateRect(NULL, FALSE);
  348. }
  349. if(nSBCode == SB_LINERIGHT) // nSBCode = 1
  350. {
  351. m_Camera.m_fRotation[Y] = Normal360(m_Camera.m_fRotation[Y]-6);
  352. SetScrollPos(SB_HORZ, (int)(360.0f-m_Camera.m_fRotation[Y]), TRUE );
  353. InvalidateRect(NULL, FALSE);
  354. }
  355. if(nSBCode == SB_PAGELEFT) // nSBCode = 2
  356. {
  357. m_Camera.m_fRotation[Y] = Normal360(m_Camera.m_fRotation[Y]+36);
  358. SetScrollPos(SB_HORZ, (int)(360.0f-m_Camera.m_fRotation[Y]), TRUE );
  359. InvalidateRect(NULL, FALSE);
  360. }
  361. if(nSBCode == SB_PAGERIGHT) // nSBCode = 3
  362. {
  363. m_Camera.m_fRotation[Y] = Normal360(m_Camera.m_fRotation[Y]-36);
  364. SetScrollPos(SB_HORZ, (int)(360.0f-m_Camera.m_fRotation[Y]), TRUE );
  365. InvalidateRect(NULL, FALSE);
  366. }
  367. if(nSBCode == SB_THUMBPOSITION) // nSBCode = 4
  368. {
  369. m_Camera.m_fRotation[Y] = Normal360((float)(360.0f-nPos));
  370. SetScrollPos(SB_HORZ, (int)(360.0f-m_Camera.m_fRotation[Y]), TRUE );
  371. InvalidateRect(NULL, FALSE);
  372. }
  373. if(nSBCode == SB_THUMBTRACK) // nSBCode = 5
  374. {
  375. m_Camera.m_fRotation[Y] = Normal360((float)(360.0f-nPos));
  376. InvalidateRect(NULL, FALSE);
  377. }
  378. CWnd::OnHScroll(nSBCode, nPos, pScrollBar);
  379. }