MyglWnd.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:6k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // MyglWnd.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
- /////////////////////////////////////////////////////////////////////////////
- // CMyglWnd
- IMPLEMENT_DYNAMIC(CMyglWnd, CWnd)
- CMyglWnd::CMyglWnd()
- {
- // Initialize our class variables
- m_pDC = NULL;
- m_hRC = NULL;
- // 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;
- }
- CMyglWnd::~CMyglWnd()
- {
- }
- BEGIN_MESSAGE_MAP(CMyglWnd, CWnd)
- //{{AFX_MSG_MAP(CMyglWnd)
- ON_WM_ERASEBKGND()
- ON_WM_SIZE()
- ON_WM_CREATE()
- ON_WM_PALETTECHANGED()
- ON_WM_QUERYNEWPALETTE()
- ON_WM_DESTROY()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- BOOL CMyglWnd::Create(DWORD dwExStyle, LPCTSTR lpszClassName,
- LPCTSTR lpszWindowName, DWORD dwStyle,
- const RECT& rect, CWnd* pParentWnd, UINT nID,
- CCreateContext* pContext)
- {
- return CreateEx(dwExStyle, // Extended Style
- lpszClassName, // Class Name
- lpszWindowName, // Window Name
- dwStyle, // Style
- rect.left, // x position
- rect.top, // y position
- rect.right - rect.left, // window width
- rect.bottom - rect.top, // window height
- pParentWnd->GetSafeHwnd(), // hwndParent
- NULL, // hMenu
- NULL); // lpCreateParams
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyglWnd message handlers
- BOOL CMyglWnd::PreCreateWindow(CREATESTRUCT& cs)
- {
- // OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS and must not
- // include CS_PARENTDC for the class style. Refer to SetPixelFormat
- // documentation in the "Comments" section for further information.
- cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
- return CWnd::PreCreateWindow(cs);
- }
- int CMyglWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- // Get the Device context to our window
- m_pDC = new CClientDC (this);
- if (m_pDC == NULL)
- return (-1);
- // Set our window pixel format for openGL rendering
- if(!the3dEngine.SetWindowPixelFormat(m_pDC->GetSafeHdc(),
- PFD_DRAW_TO_WINDOW | // Draw to Window (not bitmap)
- PFD_SUPPORT_OPENGL | // Support OpenGL calls in window
- PFD_DOUBLEBUFFER)) // Double buffered mode
- return -1;
- // Create the rendering context
- m_hRC = wglCreateContext(m_pDC->GetSafeHdc());
- if (!m_hRC)
- return -1;
- // Enable the window
- EnableWindow(TRUE);
- return 0;
- }
- void CMyglWnd::OnDestroy()
- {
- // Clean up rendering context stuff
- if(m_pDC)
- delete m_pDC;
- // Clean up rendering context stuff
- if(m_hRC)
- wglDeleteContext(m_hRC);
- CWnd::OnDestroy();
- }
- BOOL CMyglWnd::OnEraseBkgnd(CDC* pDC)
- {
- // Override to keep the background from being erased
- // everytime the window is repainted
- return FALSE;
- // return CWnd::OnEraseBkgnd(pDC);
- }
- void CMyglWnd::OnSize(UINT nType, int cx, int cy)
- {
- CWnd::OnSize(nType, cx, cy);
- // Ensure that we have valid window size parameters
- if(!cx && !cy)
- return;
- // Make this view the current OpenGL rendering context...
- if(!the3dEngine.EnableRC(m_pDC->GetSafeHdc(), m_hRC, TRUE))
- return;
- // Reset our camera view
- m_Camera.ResetView(cx, cy);
- // Releases the device context that is used by the rendering context
- // to allow other rendering contexts to co-exist.
- the3dEngine.EnableRC(NULL, NULL, FALSE);
- // Repaint, forces remap of palette in current window
- InvalidateRect(NULL, FALSE);
- }
- void CMyglWnd::OnPaletteChanged(CWnd* pFocusWnd)
- {
- HPALETTE hPaletteOld; // Handle to the old palette
- int iNumEntries; // Number of entries in the logical palette were
- // mapped to different entries in the system palette.
- if((the3dEngine.GetPalette() != NULL) && (pFocusWnd != this))
- {
- // Select the palette into the device context
- hPaletteOld = SelectPalette(m_pDC->GetSafeHdc(), the3dEngine.GetPalette(), FALSE);
- // Map entries from the currently selected palette to
- // the system palette. The return value is the number
- // of palette entries modified.
- iNumEntries = RealizePalette(m_pDC->GetSafeHdc());
- // Remap the current colors to the newly realized palette
- UpdateColors(m_pDC->GetSafeHdc());
- return;
- }
- CWnd::OnPaletteChanged(pFocusWnd);
- }
- BOOL CMyglWnd::OnQueryNewPalette()
- {
- HPALETTE hPaletteOld; // Handle to the old palette
- int iNumEntries; // Number of entries in the logical palette were
- // mapped to different entries in the system palette.
- // If the palette was created.
- if(the3dEngine.GetPalette())
- {
- // Selects the palette into the current device context
- hPaletteOld = SelectPalette(m_pDC->GetSafeHdc(), the3dEngine.GetPalette(), FALSE);
- // Map entries from the currently selected palette to
- // the system palette. The return value is the number
- // of palette entries modified.
- iNumEntries = RealizePalette(m_pDC->GetSafeHdc());
- // Repaint, forces remap of palette in current window
- InvalidateRect(NULL, FALSE);
- return iNumEntries;
- }
- return CWnd::OnQueryNewPalette();
- }