3dEngine.cpp
资源名称:gloop.zip [点击查看]
上传用户:shxiangxiu
上传日期:2007-01-03
资源大小:1101k
文件大小:10k
源码类别:
OpenGL
开发平台:
Visual C++
- /////////////////////////////////////////////////////////////////////////////
- // 3dEngine.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"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //////////////////////////////////////////////////////////////////
- // C3dEngine
- IMPLEMENT_DYNAMIC(C3dEngine, CObject)
- /////////////////////////////////////////////////////////////////////////////
- // C3dEngine construction
- C3dEngine::C3dEngine()
- {
- // Assign Default values to member attributes
- m_hPalette = NULL;
- m_bDisplayErrorMessages = TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dEngine Destructor
- C3dEngine::~C3dEngine()
- {
- // Delete palette if created
- if(m_hPalette)
- DeleteObject(m_hPalette);
- }
- /////////////////////////////////////////////////////////////////////////////
- // C3dEngine Procedures
- BOOL C3dEngine::SetWindowPixelFormat(HDC hDC, DWORD dwFlags)
- {
- int iPixelFormat; // Pixel format
- int iMaxPixelFormatIndex; // Max Pixel format index
- int iNumEntries; // Number of entries in the logical palette were
- // mapped to different entries in the system palette.
- // dwFlags += PFD_DRAW_TO_WINDOW | // Draw to Window (not bitmap) <dwFlags>
- // PFD_SUPPORT_OPENGL | // Support OpenGL calls in window
- // PFD_DOUBLEBUFFER; // Double buffered mode
- PIXELFORMATDESCRIPTOR pfd = {
- sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure <nSize>
- 1, // Version of this structure <nVersion>
- dwFlags, // Pixel format descriptor flags
- PFD_TYPE_RGBA, // RGBA Color mode <iPixelType>
- 24, // Want 24bit color <cColorBits>
- 0,0,0,0,0,0, // Not used to select mode color bits / shift
- 0,0, // Not used to select mode alpha bits / shift
- 0,0,0,0,0, // Not used to select mode accum bits / shift
- 16, // Size of depth buffer <cDepthBits>
- 16, // Size of the Stencel buffer <CStencilBits>
- 0, // Not used to select mode <cAuxBuffers>
- PFD_MAIN_PLANE, // Draw in main plane <iLayerType>
- 0, // MUST BE ZERO <bReserved>
- 0,0,0 }; // Not used to select mode
- // Choose a pixel format that best matches that described in pfd
- iPixelFormat = ChoosePixelFormat(hDC, &pfd);
- if (iPixelFormat==0) // Let's choose a default index.
- {
- DisplayError("C3dEngine::SetWindowPixelFormat");
- return FALSE;
- // iPixelFormat = 1;
- // if (DescribePixelFormat(hDC,
- // iPixelFormat,
- // sizeof(PIXELFORMATDESCRIPTOR),
- // &pfd)==0)
- // return FALSE;
- }
- // Set the pixel format for the device context
- if(!SetPixelFormat(hDC, iPixelFormat, &pfd))
- {
- DisplayError("C3dEngine::SetWindowPixelFormat");
- return FALSE;
- }
- // With the pixel format index, get it's description
- iMaxPixelFormatIndex = DescribePixelFormat(hDC,
- iPixelFormat,
- sizeof(PIXELFORMATDESCRIPTOR),
- &pfd);
- if(!iMaxPixelFormatIndex)
- {
- DisplayError("C3dEngine::SetWindowPixelFormat");
- return FALSE;
- }
- // Does this pixel format require a palette?
- if(pfd.dwFlags & PFD_NEED_PALETTE)
- {
- if(!m_hPalette)
- // Palette not created so create one!
- m_hPalette = CreateNewPalette(hDC);
- // If a palette was just created or previously created, select
- // the palette into our device context
- if(m_hPalette)
- {
- // Selects the palette into the current device context
- SelectPalette(hDC, m_hPalette, FALSE);
- // Map entries from the currently selected palette to
- // the system palette. The return value is the number
- // of palette entries modified.
- iNumEntries = RealizePalette(hDC);
- }
- }
- return TRUE;
- }
- BOOL C3dEngine::EnableRC(HDC hDC, HGLRC hRC, BOOL bEnable)
- {
- if(bEnable)
- {
- // Make the rendering context current
- if(!wglMakeCurrent(hDC, hRC))
- {
- if(m_bDisplayErrorMessages)
- {
- int iPixelFormat;
- PIXELFORMATDESCRIPTOR pfd;
- // Get the current pixel format index
- iPixelFormat = GetPixelFormat(hDC);
- // obtain a detailed description of that pixel format
- DescribePixelFormat(hDC, iPixelFormat,
- sizeof(PIXELFORMATDESCRIPTOR), &pfd);
- DisplayError("C3dEngine::EnableRC");
- }
- return FALSE;
- }
- }
- else
- // Releases the device context that is used by the rendering context
- // to allow other rendering contexts to co-exist.
- wglMakeCurrent(NULL, NULL);
- return TRUE;
- }
- void C3dEngine::DisplayError(LPSTR szTitle)
- {
- LPVOID lpMsgBuf;
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- GetLastError(),
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPTSTR) &lpMsgBuf,
- 0,
- NULL);
- if(m_bDisplayErrorMessages)
- // Display the string.
- MessageBox( NULL, (LPTSTR)lpMsgBuf, szTitle, MB_OK|MB_ICONINFORMATION );
- TRACE("%s: %sn", szTitle, lpMsgBuf);
- // Free the buffer.
- LocalFree( lpMsgBuf );
- }
- /******************************************************************************
- * *
- * HPALETTE CreateNewPalette() *
- * *
- * Parameter: *
- * *
- * HDC - Handle to a Device Context *
- * *
- * Return Value: *
- * *
- * HPALETTE - Returns a handle to a palette or NULL if it *
- * fails. *
- * *
- * Description: *
- * *
- * This function will build a palette with a spectrum of colors. It is *
- * useful when you want to display a number of DIBs each with a different *
- * palette yet still have an a good selection of colors for the DIBs to map *
- * to. *
- * *
- *****************************************************************************/
- HPALETTE C3dEngine::CreateNewPalette(HDC hDC)
- {
- PIXELFORMATDESCRIPTOR pfd; // Pixel Format Descriptor
- LOGPALETTE *pPal; // Pointer to memory for logical palette
- HPALETTE hPal; // Handle to our palette
- int iPixelFormat; // 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
- iPixelFormat = GetPixelFormat(hDC);
- DescribePixelFormat(hDC, iPixelFormat, 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 NULL;
- // 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
- hPal = CreatePalette(pPal);
- // Go ahead and select and realize the palette for this device context
- // SelectPalette(hDC,(HPALETTE)hPalette,FALSE);
- // RealizePalette(hDC);
- // Free the memory used for the logical palette structure
- free(pPal);
- return hPal; // Return the handle to this palette
- }