2010-02-27 88 views
2

我是OpenGL中的新成員。wglCreateContext()每次在OpenGL和Visual C++中返回NULL

我想在Windows窗體中使用OpenGL進行思考。 如果我使用Win32應用程序與WinMain方法,應用程序工作。 在WinMain方法中,我使用CreateWindow()函數填充HWND函數並將WinMain參數提供給CreateWindows

但我想從Windows窗體得到句柄我不能得到這個。每次 wglCreateContext(hdc)回報NULL there是一個例子是我拿

public: 
    COpenGL(System::Windows::Forms::Form^parentForm, GLsizei iWidth, GLsizei iHeight) 
    { 
    CreateParams^ cp = gcnew CreateParams; 

    // Set the position on the form 
    cp->X = 0; 
    cp->Y = 0; 
    cp->Height = iHeight; 
    cp->Width = iWidth; 

    // Specify the form as the parent. 
    cp->Parent = parentForm->Handle; 

    // Create as a child of the specified parent and make OpenGL compliant (no clipping) 
    cp->Style = WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; 

    // Create the actual window 
    this->CreateHandle(cp); 

    m_hDC = GetDC((HWND)this->Handle.ToPointer()); 

    if(m_hDC) 
    { 
    MySetPixelFormat(m_hDC); 
    ReSizeGLScene(iWidth, iHeight); 
    InitGL(); 
    } 

    rtri = 0.0f; 
    rquad = 0.0f; 
    } 


GLint MySetPixelFormat(HDC hdc) 
    { 

    static PIXELFORMATDESCRIPTOR pfd=  
    { 
    sizeof(PIXELFORMATDESCRIPTOR), 
    1,   
    PFD_DRAW_TO_WINDOW |  
    PFD_SUPPORT_OPENGL |  
    PFD_DOUBLEBUFFER,  
    PFD_TYPE_RGBA,  
    16,   
    0, 0, 0, 0, 0, 0,  
    0,   
    0,   
    0,   
    0, 0, 0, 0,   
    16,   
    0,   
    0,   
    PFD_MAIN_PLANE,  
    0,   
    0, 0, 0   
    }; 

    GLint iPixelFormat; 

    // get the device context's best, available pixel format match 
    if((iPixelFormat = ChoosePixelFormat(hdc, &pfd)) == 0) 
    { 
    MessageBox::Show("ChoosePixelFormat Failed"); 
    return 0; 
    } 


    // make that match the device context's current pixel format 
    if(SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE) 
    { 
    MessageBox::Show("SetPixelFormat Failed"); 
    return 0; 
    } 

    if((m_hglrc = wglCreateContext(hdc)) == NULL) 
    { 
    MessageBox::Show("wglCreateContext Failed"); 
    return 0; 
    } 

    if((wglMakeCurrent(hdc, m_hglrc)) == NULL) 
    { 
    MessageBox::Show("wglMakeCurrent Failed"); 
    return 0; 
    } 


    return 1; 
    } 

有無我能解決這個問題?

回答

1

你可以檢查一下glGetLastError的值,主要是因爲你選擇了錯誤或不兼容的像素格式,你可以試試其他格式,然後你的窗口類應該被標記爲CS_OWNDC並且設置DoubleBuffering爲false。

4

這裏,在構造變化:

m_hDC = GetDC((HWND)this->Handle.ToPointer()); 
if(m_hDC) 
{ 
wglMakeCurrent(m_hDC, NULL); 
MySetPixelFormat(m_hDC); 
ReSizeGLScene(iWidth, iHeight); 
InitGL(); 
} 

必須調用wglMakeCurrent後m_hDC已設置。我回復了這個例子的第一篇文章。這裏Creating an OpenGL view on a Windows Form

解決我的問題:)