2010-02-27 5 views
2

OpenGL에서 새로운 기능입니다.OpenGL 및 Visual C++에서 wglCreateContext()가 항상 NULL을 반환합니다.

Windows Forms에서 OpenGL을 사용하여 내가 추측하고 싶습니다. WinMain 메서드를 사용하여 Win32 응용 프로그램을 사용하는 경우 응용 프로그램이 작동합니다. WinMain 메서드에서 HWNDCreateWindow() 함수로 채우고 WinMain 매개 변수를 CreateWindows으로 지정합니다.

하지만 Windows Form에서 Handle을 얻고 싶습니다. 매번 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(); 
} 

m_hDC 있습니다가 설치 된 후 당신은 wglMakeCurrent를 호출해야합니다. 나는이 예제의 첫 번째 기사에 답한다. 여기 Creating an OpenGL view on a Windows Form

그게 내 문제를 해결 :)

관련 문제