// WinVSXWView.cpp : implementation of the CWinVSXWView class // #include "stdafx.h" #include "WinVSXW.h" #include "WinVSXWDoc.h" #include "WinVSXWView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CWinVSXWView IMPLEMENT_DYNCREATE(CWinVSXWView, CView) BEGIN_MESSAGE_MAP(CWinVSXWView, CView) //{{AFX_MSG_MAP(CWinVSXWView) ON_WM_CREATE() ON_WM_SIZE() ON_WM_DESTROY() ON_WM_ERASEBKGND() ON_WM_QUERYNEWPALETTE() ON_WM_PALETTECHANGED() //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CWinVSXWView construction/destruction CWinVSXWView::CWinVSXWView() { } CWinVSXWView::~CWinVSXWView() { } BOOL CWinVSXWView::PreCreateWindow(CREATESTRUCT& cs) { cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS | CS_OWNDC); return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CWinVSXWView drawing void CWinVSXWView::OnDraw(CDC* pDC) { // Make the rendering context current wglMakeCurrent(m_hDC,m_hRC); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Call our external OpenGL code DrawFace(); // Flush drawing commands glFlush(); // Swap our scene to the front SwapBuffers(m_hDC); // Allow other rendering contexts to co-exist wglMakeCurrent(m_hDC,NULL); } ///////////////////////////////////////////////////////////////////////////// // CWinVSXWView printing BOOL CWinVSXWView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CWinVSXWView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { } void CWinVSXWView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { } ///////////////////////////////////////////////////////////////////////////// // CWinVSXWView diagnostics #ifdef _DEBUG void CWinVSXWView::AssertValid() const { CView::AssertValid(); } void CWinVSXWView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CWinVSXWDoc* CWinVSXWView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CWinVSXWDoc))); return (CWinVSXWDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CWinVSXWView message handlers int CWinVSXWView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; int nPixelFormat; // Pixel format index m_hDC = ::GetDC(m_hWnd); // Get the Device context static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure 1, // Version of this structure PFD_DRAW_TO_WINDOW | // Draw to Window (not to bitmap) PFD_SUPPORT_OPENGL | // Support OpenGL calls in window PFD_DOUBLEBUFFER, // Double buffered mode PFD_TYPE_RGBA, // RGBA Color mode 24, // Want 24bit color 0,0,0,0,0,0, // Not used to select mode 0,0, // Not used to select mode 0,0,0,0,0, // Not used to select mode 32, // Size of depth buffer 0, // Not used to select mode 0, // Not used to select mode PFD_MAIN_PLANE, // Draw in main plane 0, // Not used to select mode 0,0,0 }; // Not used to select mode // Choose a pixel format that best matches that described in pfd nPixelFormat = ChoosePixelFormat(m_hDC, &pfd); // Set the pixel format for the device context VERIFY(SetPixelFormat(m_hDC, nPixelFormat, &pfd)); // Create the rendering context m_hRC = wglCreateContext(m_hDC); // Make the rendering context current, perform initialization, then // deselect it VERIFY(wglMakeCurrent(m_hDC,m_hRC)); glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(20, 1, 4, 12); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0, 0, 8, 0, 0, 0, 0, 1, 0.0); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); wglMakeCurrent(NULL,NULL); // Create the palette if needed InitializePalette(); return 0; } void CWinVSXWView::OnSize(UINT nType, int cx, int cy) { CView::OnSize(nType, cx, cy); // TODO: Add your message handler code here VERIFY(wglMakeCurrent(m_hDC,m_hRC)); // Set Viewport to window dimensions glViewport(0, 0, cx, cy); VERIFY(wglMakeCurrent(NULL,NULL)); } void CWinVSXWView::OnDestroy() { wglDeleteContext(m_hRC); ::ReleaseDC(m_hWnd,m_hDC); CView::OnDestroy(); } // Initializes the CPalette object void CWinVSXWView::InitializePalette(void) { PIXELFORMATDESCRIPTOR pfd; // Pixel Format Descriptor LOGPALETTE *pPal; // Pointer to memory for logical palette int nPixelFormat; // Pixel format index int nColors; // Number of entries in palette int i; // Counting variable BYTE RedRange,GreenRange,BlueRange; // Range for each color entry (7,7,and 3) // Get the pixel format index and retrieve the pixel format description nPixelFormat = GetPixelFormat(m_hDC); DescribePixelFormat(m_hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd); // Does this pixel format require a palette? If not, do not create a // palette and just return NULL if(!(pfd.dwFlags & PFD_NEED_PALETTE)) return; // Number of entries in palette. 8 bits yeilds 256 entries nColors = 1 << pfd.cColorBits; // Allocate space for a logical palette structure plus all the palette entries pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) +nColors*sizeof(PALETTEENTRY)); // Fill in palette header pPal->palVersion = 0x300; // Windows 3.0 pPal->palNumEntries = nColors; // table size // Build mask of all 1's. This creates a number represented by having // the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and // pfd.cBlueBits. RedRange = (1 << pfd.cRedBits) -1; GreenRange = (1 << pfd.cGreenBits) - 1; BlueRange = (1 << pfd.cBlueBits) -1; // Loop through all the palette entries for(i = 0; i < nColors; i++) { // Fill in the 8-bit equivalents for each component pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange; pPal->palPalEntry[i].peRed = (unsigned char)( (double) pPal->palPalEntry[i].peRed * 255.0 / RedRange); pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange; pPal->palPalEntry[i].peGreen = (unsigned char)( (double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange); pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange; pPal->palPalEntry[i].peBlue = (unsigned char)( (double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange); pPal->palPalEntry[i].peFlags = (unsigned char) NULL; } // Create the palette m_GLPalette.CreatePalette(pPal); // Go ahead and select and realize the palette for this device context SelectPalette(m_hDC,(HPALETTE)m_GLPalette,FALSE); RealizePalette(m_hDC); // Free the memory used for the logical palette structure free(pPal); } BOOL CWinVSXWView::OnQueryNewPalette() { // If the palette was created. if((HPALETTE)m_GLPalette) { int nRet; // Selects the palette into the current device context SelectPalette(m_hDC, (HPALETTE)m_GLPalette, FALSE); // Map entries from the currently selected palette to // the system palette. The return value is the number // of palette entries modified. nRet = RealizePalette(m_hDC); // Repaint, forces remap of palette in current window InvalidateRect(NULL,FALSE); return nRet; } return CView::OnQueryNewPalette(); } void CWinVSXWView::OnPaletteChanged(CWnd* pFocusWnd) { if(((HPALETTE)m_GLPalette != NULL) && (pFocusWnd != this)) { // Select the palette into the device context SelectPalette(m_hDC,(HPALETTE)m_GLPalette,FALSE); // Map entries to system palette RealizePalette(m_hDC); // Remap the current colors to the newly realized palette UpdateColors(m_hDC); return; } CView::OnPaletteChanged(pFocusWnd); } BOOL CWinVSXWView::OnEraseBkgnd(CDC* pDC) { return FALSE; } void CWinVSXWView::DrawFace() { glBegin(GL_POLYGON); glVertex3f(-0.5, -0.5, -0.5); glVertex3f( 0.5, -0.5, -0.5); glVertex3f( 0.5, 0.5, -0.5); glVertex3f(-0.5, 0.5, -0.5); glEnd(); }