[摘要]// Format Must Support OpenGL PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA,...
// Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
m_DMsaved.dmBitsPerPel, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
if ( !( m_hDC = ::GetDC ( m_hWnd ) ) ) { // Did We Get A Device Context?
KillGLWindow (); // Reset The Display
TRACE ( "Can't Create A GL Device Context." );
return FALSE;
}
if ( !( PixelFormat = ChoosePixelFormat ( m_hDC, &pfd ) ) ) { // Did Windows Find A Matching Pixel Format?
KillGLWindow (); // Reset The Display
TRACE ( "Can't Find A Suitable PixelFormat." );
return FALSE;
}
if ( !SetPixelFormat ( m_hDC, PixelFormat, &pfd ) ){ // Are We Able To Set The Pixel Format?
KillGLWindow (); // Reset The Display
TRACE ( "Can't Set The PixelFormat." );
return FALSE;
}
if ( !( m_hRC = wglCreateContext ( m_hDC ) ) ) { // Are We Able To Get A Rendering Context?
KillGLWindow (); // Reset The Display
TRACE( " Can't Create A GL Rendering Context." );
return FALSE;
}
if ( !wglMakeCurrent ( m_hDC, m_hRC ) ) { // Try To Activate The Rendering Context
KillGLWindow (); // Reset The Display
TRACE ( "Can't Activate The GL Rendering Context." );
return FALSE;
}
if ( !InitGL () ) { // Initialize Our Newly Created GL Window
KillGLWindow (); // Reset The Display
TRACE ( "Initialization Failed." );
return FALSE;
}
m_bInit = TRUE;
return 0;
}
void COpenGL::KillGLWindow()
{
if (m_bFullScreen) // Are We In Fullscreen Mode?
{
if (!ChangeDisplaySettings(NULL,CDS_TEST)) { // if the shortcut doesn't work
ChangeDisplaySettings(NULL,CDS_RESET); // Do it anyway (to get the values out of the registry)
ChangeDisplaySettings(&m_DMsaved,CDS_RESET); // change it to the saved settings
} else {
ChangeDisplaySettings(NULL,CDS_RESET);
}
ShowCursor(TRUE); // Show Mouse Pointer
}
if ( m_hRC ) { // Do We Have A Rendering Context?
if ( !wglMakeCurrent ( NULL, NULL ) ) { // Are We Able To Release The DC And RC Contexts?
TRACE ( "Release Of DC And RC Failed." );
}
if ( !wglDeleteContext ( m_hRC ) ) { // Are We Able To Delete The RC?
TRACE ( "Release Rendering Context Failed." );
}
m_hRC = NULL; // Set RC To NULL
}
if ( m_hDC && !::ReleaseDC ( m_hWnd, m_hDC ) ) { // Are We Able To Release The DC
TRACE ( "Release Device Context Failed." );
m_hDC = NULL; // Set DC To NULL
}
if ( m_hWnd && !::DestroyWindow ( m_hWnd ) ) { // Are We Able To Destroy The Window?
TRACE( "Could Not Release m_hWnd." );
m_hWnd = NULL; // Set m_hWnd To NULL
}
}
int COpenGL::InitGL()
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
return TRUE; // Initialization Went OK
}
void COpenGL::RenderGLScene()
{
if(!m_bInit) return;
glClear(GL_COLOR_BUFFER_BIT
关键词:制作一个基于MFC对话框的OpenGL类