MyColorPaletteWnd.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:12k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // MyColorPaletteWnd.cpp : implementation file
- //
- // glOOP (OpenGL Object Oriented Programming library)
- // Copyright (c) Craig Fahrnbach 1997, 1998
- //
- // OpenGL is a registered trademark of Silicon Graphics
- //
- //
- // This program is provided for educational and personal use only and
- // is provided without guarantee or warrantee expressed or implied.
- //
- // Commercial use is strickly prohibited without written permission
- // from ImageWare Development.
- //
- // This program is -not- in the public domain.
- //
- /////////////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "glOOP.h"
- #include "MyColorPaletteWnd.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMyColorPaletteWnd
- IMPLEMENT_DYNAMIC(CMyColorPaletteWnd, CMyglWnd)
- CMyColorPaletteWnd::CMyColorPaletteWnd()
- {
- // Initialize our class variables
- m_fRed = 0.0f;
- m_fGrn = 0.0f;
- m_fBlu = 0.0f;
- // Set our camera to Orthographic view and
- // initialize our camera variables
- m_Camera.m_bPerspective = FALSE;
- m_Camera.m_fOrigin[X] = 0.0f;
- m_Camera.m_fOrigin[Y] = 0.0f;
- m_Camera.m_fOrigin[Z] = 220.0f;
- m_Camera.m_fRotation[X] = 30.0f;
- m_Camera.m_fRotation[Y] = 225.0f;
- m_Camera.m_fRotation[Z] = 0.0f;
- m_Camera.m_fNear = 0.0f;
- m_Camera.m_fFar = 300.0f;
- }
- CMyColorPaletteWnd::~CMyColorPaletteWnd()
- {
- }
- BEGIN_MESSAGE_MAP(CMyColorPaletteWnd, CWnd)
- //{{AFX_MSG_MAP(CMyColorPaletteWnd)
- ON_WM_PAINT()
- ON_WM_CREATE()
- ON_WM_LBUTTONDOWN()
- ON_WM_KEYDOWN()
- ON_WM_RBUTTONDOWN()
- ON_WM_VSCROLL()
- ON_WM_HSCROLL()
- ON_WM_DESTROY()
- ON_WM_SIZE()
- ON_WM_QUERYNEWPALETTE()
- ON_WM_PALETTECHANGED()
- ON_WM_ERASEBKGND()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- BOOL CMyColorPaletteWnd::Create(DWORD dwExStyle, LPCTSTR lpszClassName,
- LPCTSTR lpszWindowName, DWORD dwStyle,
- const RECT& rect, CWnd* pParentWnd, UINT nID,
- CCreateContext* pContext)
- {
- if(lpszClassName == NULL)
- // Register a class with its own device context and the 'cross' cursor
- lpszClassName = AfxRegisterWndClass(CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
- ::LoadCursor(NULL, IDC_CROSS));
- return CMyglWnd::Create(dwExStyle, lpszClassName, lpszWindowName,
- dwStyle, rect, pParentWnd, nID, pContext);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyColorPaletteWnd messages forwarded to CMyglWnd base class
- void CMyColorPaletteWnd::OnDestroy()
- {
- // Forward the message to the base class
- CMyglWnd::OnDestroy();
- }
- void CMyColorPaletteWnd::OnSize(UINT nType, int cx, int cy)
- {
- // Forward the message to the base class
- CMyglWnd::OnSize(nType, cx, cy);
- }
- void CMyColorPaletteWnd::OnPaletteChanged(CWnd* pFocusWnd)
- {
- // Forward the message to the base class
- CMyglWnd::OnPaletteChanged(pFocusWnd);
- }
- BOOL CMyColorPaletteWnd::OnQueryNewPalette()
- {
- // Forward the message to the base class
- return CMyglWnd::OnQueryNewPalette();
- }
- BOOL CMyColorPaletteWnd::OnEraseBkgnd(CDC* pDC)
- {
- // Forward the message to the base class
- return CMyglWnd::OnEraseBkgnd(pDC);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyColorPaletteWnd message handlers
- int CMyColorPaletteWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CMyglWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- // Initialize our scroll bar range
- // 0 to 360 degrees
- SetScrollRange(SB_HORZ, 0, 360, TRUE);
- SetScrollRange(SB_VERT, 0, 360, TRUE);
- SetScrollPos(SB_HORZ, (int)(360.0f-m_Camera.m_fRotation[Y]), TRUE );
- SetScrollPos(SB_VERT, (int)m_Camera.m_fRotation[X], TRUE );
- return 0;
- }
- void CMyColorPaletteWnd::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- // Make this view the current OpenGL rendering context...
- if(!the3dEngine.EnableRC(dc, m_hRC, TRUE))
- return;
- // Call OpenGL drawing code
- RenderColorCube();
- // Exchange the front and back buffers
- SwapBuffers(dc);
- // Releases the device context that is used by the rendering context
- // to allow other rendering contexts to co-exist.
- the3dEngine.EnableRC(NULL, NULL, FALSE);
- // Do not call CWnd::OnPaint() for painting messages
- }
- void CMyColorPaletteWnd::OnLButtonDown(UINT nFlags, CPoint point)
- {
- COLORREF PaletteColor;
- SelectPalette(m_pDC->GetSafeHdc(), the3dEngine.GetPalette(), 0); //map palette to our bitmap
- PaletteColor = GetPixel(m_pDC->GetSafeHdc(), point.x, point.y);
- m_fRed = (GLfloat)GetRValue(PaletteColor)/255;
- m_fGrn = (GLfloat)GetGValue(PaletteColor)/255;
- m_fBlu = (GLfloat)GetBValue(PaletteColor)/255;
- // Route message to parent
- CWnd* pParent = GetParent();
- if(pParent)
- pParent->SendMessage(WM_CPW_LBUTTONDOWN,
- (WPARAM)nFlags,
- (LPARAM)point.x<<16 | point.y);
- CWnd::OnLButtonDown(nFlags, point);
- }
- void CMyColorPaletteWnd::OnRButtonDown(UINT nFlags, CPoint point)
- {
- COLORREF PaletteColor;
- SelectPalette(m_pDC->GetSafeHdc(), the3dEngine.GetPalette(), 0); //map palette to our bitmap
- PaletteColor = GetPixel(m_pDC->GetSafeHdc(), point.x, point.y);
- m_fRed = (GLfloat)GetRValue(PaletteColor)/255;
- m_fGrn = (GLfloat)GetGValue(PaletteColor)/255;
- m_fBlu = (GLfloat)GetBValue(PaletteColor)/255;
- // Route message to parent
- CWnd* pParent = GetParent();
- if(pParent)
- pParent->SendMessage(WM_CPW_RBUTTONDOWN,
- (WPARAM)nFlags,
- (LPARAM)point.x<<16 | point.y);
- CWnd::OnRButtonDown(nFlags, point);
- }
- void CMyColorPaletteWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- if(nChar == VK_UP)
- m_Camera.m_fRotation[X]-= 5.0f;
- if(nChar == VK_DOWN)
- m_Camera.m_fRotation[X] += 5.0f;
- if(nChar == VK_LEFT)
- m_Camera.m_fRotation[Y] += 5.0f;
- if(nChar == VK_RIGHT)
- m_Camera.m_fRotation[Y] -= 5.0f;
- m_Camera.m_fRotation[X] = Normal360(m_Camera.m_fRotation[X]);
- m_Camera.m_fRotation[Y] = Normal360(m_Camera.m_fRotation[Y]);
- InvalidateRect(NULL, FALSE);
- CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- GLfloat CMyColorPaletteWnd::Normal360(GLfloat fSource)
- {
- if(fSource < 0.0)
- fSource = 360.0f + fSource;
- if(fSource > 360.0f)
- fSource = fSource - 360.0f;
- return fSource;
- }
- void CMyColorPaletteWnd::RenderColorCube()
- {
- glEnable(GL_DEPTH_TEST);
- glShadeModel(GL_SMOOTH);
- // If we have a palette, enable dithering of color before
- // writing to the color buffer
- if(the3dEngine.GetPalette())
- glEnable(GL_DITHER);
- // Set the background color to black (default)
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- // Clear the window with current clearing color
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Save the current Modelview matrix
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
- // Position our camera
- m_Camera.PositionCamera();
- // Front face
- glBegin(GL_POLYGON);
- // White
- glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)255);
- glVertex3f(50.0f,50.0f,50.0f);
- // Yellow
- glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)0);
- glVertex3f(50.0f,-50.0f,50.0f);
- // Red
- glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)0);
- glVertex3f(-50.0f,-50.0f,50.0f);
- // Magenta
- glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)255);
- glVertex3f(-50.0f,50.0f,50.0f);
- glEnd();
- // Back Face
- glBegin(GL_POLYGON);
- // Cyan
- glColor3f(0.0f, 1.0f, 1.0f);
- glVertex3f(50.0f,50.0f,-50.0f);
- // Green
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(50.0f,-50.0f,-50.0f);
- // Black
- glColor3f(0.0f, 0.0f, 0.0f);
- glVertex3f(-50.0f,-50.0f,-50.0f);
- // Blue
- glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3f(-50.0f,50.0f,-50.0f);
- glEnd();
- // Top Face
- glBegin(GL_POLYGON);
- // Cyan
- glColor3f(0.0f, 1.0f, 1.0f);
- glVertex3f(50.0f,50.0f,-50.0f);
- // White
- glColor3f(1.0f, 1.0f, 1.0f);
- glVertex3f(50.0f,50.0f,50.0f);
- // Magenta
- glColor3f(1.0f, 0.0f, 1.0f);
- glVertex3f(-50.0f,50.0f,50.0f);
- // Blue
- glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3f(-50.0f,50.0f,-50.0f);
- glEnd();
- // Bottom Face
- glBegin(GL_POLYGON);
- // Green
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(50.0f,-50.0f,-50.0f);
- // Yellow
- glColor3f(1.0f, 1.0f, 0.0f);
- glVertex3f(50.0f,-50.0f,50.0f);
- // Red
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3f(-50.0f,-50.0f,50.0f);
- // Black
- glColor3f(0.0f, 0.0f, 0.0f);
- glVertex3f(-50.0f,-50.0f,-50.0f);
- glEnd();
- // Left face
- glBegin(GL_POLYGON);
- // White
- glColor3f(1.0f, 1.0f, 1.0f);
- glVertex3f(50.0f,50.0f,50.0f);
- // Cyan
- glColor3f(0.0f, 1.0f, 1.0f);
- glVertex3f(50.0f,50.0f,-50.0f);
- // Green
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(50.0f,-50.0f,-50.0f);
- // Yellow
- glColor3f(1.0f, 1.0f, 0.0f);
- glVertex3f(50.0f,-50.0f,50.0f);
- glEnd();
- // Right face
- glBegin(GL_POLYGON);
- // Magenta
- glColor3f(1.0f, 0.0f, 1.0f);
- glVertex3f(-50.0f,50.0f,50.0f);
- // Blue
- glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3f(-50.0f,50.0f,-50.0f);
- // Black
- glColor3f(0.0f, 0.0f, 0.0f);
- glVertex3f(-50.0f,-50.0f,-50.0f);
- // Red
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3f(-50.0f,-50.0f,50.0f);
- glEnd();
- // Restore the Modelview matrix
- glPopMatrix();
- // Flush drawing commands
- glFlush();
- }
- void CMyColorPaletteWnd::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- if(nSBCode == SB_LINEUP) // nSBCode = 0
- {
- m_Camera.m_fRotation[X] = Normal360(m_Camera.m_fRotation[X]-6);
- SetScrollPos(SB_VERT, (int)m_Camera.m_fRotation[X], TRUE );
- InvalidateRect(NULL, FALSE);
- }
- if(nSBCode == SB_LINEDOWN) // nSBCode = 1
- {
- m_Camera.m_fRotation[X] = Normal360(m_Camera.m_fRotation[X]+6);
- SetScrollPos(SB_VERT, (int)m_Camera.m_fRotation[X], TRUE );
- InvalidateRect(NULL, FALSE);
- }
- if(nSBCode == SB_PAGEUP) // nSBCode = 2
- {
- m_Camera.m_fRotation[X] = Normal360(m_Camera.m_fRotation[X]-36);
- SetScrollPos(SB_VERT, (int)m_Camera.m_fRotation[X], TRUE );
- InvalidateRect(NULL, FALSE);
- }
- if(nSBCode == SB_PAGEDOWN) // nSBCode = 3
- {
- m_Camera.m_fRotation[X] = Normal360(m_Camera.m_fRotation[X]+36);
- SetScrollPos(SB_VERT, (int)m_Camera.m_fRotation[X], TRUE );
- InvalidateRect(NULL, FALSE);
- }
- if(nSBCode == SB_THUMBPOSITION) // nSBCode = 4
- {
- m_Camera.m_fRotation[X] = Normal360((float)nPos);
- SetScrollPos(SB_VERT, (int)m_Camera.m_fRotation[X], TRUE );
- InvalidateRect(NULL, FALSE);
- }
- if(nSBCode == SB_THUMBTRACK) // nSBCode = 5
- {
- m_Camera.m_fRotation[X] = Normal360((float)nPos);
- InvalidateRect(NULL, FALSE);
- }
- CWnd::OnVScroll(nSBCode, nPos, pScrollBar);
- }
- void CMyColorPaletteWnd::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- if(nSBCode == SB_LINELEFT) // nSBCode = 0
- {
- m_Camera.m_fRotation[Y] = Normal360(m_Camera.m_fRotation[Y]+6);
- SetScrollPos(SB_HORZ, (int)(360.0f-m_Camera.m_fRotation[Y]), TRUE );
- InvalidateRect(NULL, FALSE);
- }
- if(nSBCode == SB_LINERIGHT) // nSBCode = 1
- {
- m_Camera.m_fRotation[Y] = Normal360(m_Camera.m_fRotation[Y]-6);
- SetScrollPos(SB_HORZ, (int)(360.0f-m_Camera.m_fRotation[Y]), TRUE );
- InvalidateRect(NULL, FALSE);
- }
- if(nSBCode == SB_PAGELEFT) // nSBCode = 2
- {
- m_Camera.m_fRotation[Y] = Normal360(m_Camera.m_fRotation[Y]+36);
- SetScrollPos(SB_HORZ, (int)(360.0f-m_Camera.m_fRotation[Y]), TRUE );
- InvalidateRect(NULL, FALSE);
- }
- if(nSBCode == SB_PAGERIGHT) // nSBCode = 3
- {
- m_Camera.m_fRotation[Y] = Normal360(m_Camera.m_fRotation[Y]-36);
- SetScrollPos(SB_HORZ, (int)(360.0f-m_Camera.m_fRotation[Y]), TRUE );
- InvalidateRect(NULL, FALSE);
- }
- if(nSBCode == SB_THUMBPOSITION) // nSBCode = 4
- {
- m_Camera.m_fRotation[Y] = Normal360((float)(360.0f-nPos));
- SetScrollPos(SB_HORZ, (int)(360.0f-m_Camera.m_fRotation[Y]), TRUE );
- InvalidateRect(NULL, FALSE);
- }
- if(nSBCode == SB_THUMBTRACK) // nSBCode = 5
- {
- m_Camera.m_fRotation[Y] = Normal360((float)(360.0f-nPos));
- InvalidateRect(NULL, FALSE);
- }
- CWnd::OnHScroll(nSBCode, nPos, pScrollBar);
- }