Archived

This topic is now archived and is closed to further replies.

soonks

wglCreateContext fail problem in MFC

Recommended Posts

Hi~ I''m trying to save OpenGL rendering result to memory bitmap using MFC. To do this, I created compatible DC and Bitmap, then attempted to create rendering context. But wglCreateContext() returns 0. Here is the code. Is there anything wrong with my code...? BOOL SetupPixelFormat(HDC hDC) { // default pixel format for a double-buffered, // OpenGL-supporting, hardware-accelerated, // RGBA-mode format. Pass in a pointer to a different // pixel format if you want something else PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR),// size of this pfd 1, // version number PFD_DRAW_TO_BITMAP | // support bitmap PFD_SUPPORT_OPENGL | // support OpenGL PFD_DOUBLEBUFFER, // double buffer PFD_TYPE_RGBA, // RGBA type 24, // 24-bit 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, // accum bits ignored 16, // 16-bit z-buffer 0, // no stencil buffer 0, // no auxiliary buffer PFD_MAIN_PLANE, // main layer 0, // reserved 0, 0, 0 // layer masks ignored }; int pixelformat; if (0 == (pixelformat = ChoosePixelFormat(hDC, &pfd))) { AfxMessageBox("ChoosePixelFormat failed."); return FALSE; } if (FALSE == SetPixelFormat(hDC, pixelformat, &pfd)) { AfxMessageBox("SetPixelFormat failed."); return FALSE; } return TRUE; } if(m_dcMem->GetSafeHdc()) m_dcMem->DeleteDC(); if(m_dcMem->CreateCompatibleDC(pDC) == 0) { AfxMessageBox("Can''t create CompatibleDC!!"); return; } if(m_Bitmap->GetSafeHandle()) m_Bitmap->DeleteObject(); if(m_Bitmap->CreateCompatibleBitmap(pDC, m_width, m_height) == 0) { AfxMessageBox("Can''t create CompatibleBitmap!!"); return; } if(!(m_dcMem->SelectObject(m_Bitmap))) { AfxMessageBox("SelectObject failed.\n"); return; } if (!SetupPixelFormat(m_dcMem->GetSafeHdc())) { AfxMessageBox("SetupPixelFormat failed.\n"); return; } if (!(m_hMemRC = wglCreateContext(m_dcMem->GetSafeHdc()))) { AfxMessageBox("wglCreateContext failed."); return; } //////////////.................... if(m_dcMem->GetSafeHdc()) m_dcMem->DeleteDC(); if(m_dcMem->CreateCompatibleDC(pDC) == 0) { AfxMessageBox("Can''t create CompatibleDC!!"); return; } if(m_Bitmap->GetSafeHandle()) m_Bitmap->DeleteObject(); if(m_Bitmap->CreateCompatibleBitmap(pDC, m_width, m_height) == 0) { AfxMessageBox("Can''t create CompatibleBitmap!!"); return; } if(!(m_dcMem->SelectObject(m_Bitmap))) { AfxMessageBox("SelectObject failed.\n"); return; } if (!SetupPixelFormat(m_dcMem->GetSafeHdc())) { AfxMessageBox("SetupPixelFormat failed.\n"); return; } if (!(m_hMemRC = wglCreateContext(m_dcMem->GetSafeHdc()))) { AfxMessageBox("wglCreateContext failed."); return; } wglMakeCurrent(m_dcMem->GetSafeHdc(), m_hMemRC); // CDC* m_dcMem; // CBitmap* m_Bitmap; // HGLRC m_hMemRC; // Above three members are declared in a header file. Thanks for reading...

Share this post


Link to post
Share on other sites