简体   繁体   English

MFC和OpenGL CView,C ++,在GLEW之后初始化OpenGL

[英]MFC and OpenGL CView, C++, initialising OpenGL after GLEW

I'm relatively (read: very) new to MFC using C++ - I usually just worry about OpenGL. 我相对(读:非常)熟悉使用C ++的MFC-我通常只担心OpenGL。

From what I know writing C++ Win32 with OGL, I need to initialise GLEW before I initialise OpenGL and create window, which requires a dummy window to be used. 据我所知,用OGL编写C ++ Win32,我需要在初始化OpenGL和创建窗口之前初始化GLEW,这需要使用一个虚拟窗口。 Cool, dunnit. 酷,唐宁。

In MFC I've derived a CView class, which hold a member OpenGL class. 在MFC中,我派生了一个CView类,其中包含一个成员OpenGL类。

I've successfully initialised GLEW using a temporary window in OnPreCreateWindow, I've also successfully initialised OpenGL, but alas, with the wrong HWND / HDC.. indeed debugging tells me the HDC held as a member in my OpenGL class is not the one I get in OnDraw from the CDC ? 我已经使用OnPreCreateWindow中的一个临时窗口成功地初始化了GLEW,我也已经成功地初始化了OpenGL,但是可惜,使用了错误的HWND / HDC。.实际上调试告诉我,作为我的OpenGL类成员的HDC不是一个我从CDC进入OnDraw吗?

Wheres my correct HDC for OpenGL? 我正确的OpenGL HDC在哪里? - I need some MFC help! -我需要一些MFC帮助! Will I need to update this DC from time to time? 我是否需要不时更新此DC?

It seems I have a choice of OnInitialUpdate, OnPreCreateWindow, and OnDraw(CDC*) to initialise/update OpenGL with the correct DC. 看来我可以选择OnInitialUpdate,OnPreCreateWindow和OnDraw(CDC *)来使用正确的DC初始化/更新OpenGL。

void CJesseView::OnInitialUpdate()
{
    CView::OnInitialUpdate();
    HDC hDC = GetDC()->m_hDC;
}

This works and initialises GLEW - should I do it elsewhere? 这可以工作并初始化GLEW-我应该在其他地方这样做吗?

BOOL CJesseView::PreCreateWindow(CREATESTRUCT& cs)
{
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs

    /* modify the style of the view / window */
    cs.dwExStyle |= CS_OWNDC;

    /*  OpenGL Set up
        1. Create a faux window
    */
    CString m_ClassName("JesseGLView");

    HWND hwnd = CreateWindowEx(WS_EX_APPWINDOW, m_ClassName, m_ClassName, 
                    WS_POPUP, 0, 0, 640, 480, NULL, NULL, cs.hInstance, NULL);

    ::ShowWindow(hwnd, SW_HIDE);    // Don't show the window.

    /* OpenGL Set up    
       2. Initialise Glews extension Library to give us access to OpenGL 4.1 Core
    */
    m_OpenGL = new OpenGL();

    if( ! m_OpenGL->PreInjectGLEW( hwnd ) )
        MessageBox(_T("No OpenGL Niceties - Couldn't initialise GLEW!"), _T("CJesseView"));

    ::DestroyWindow( hwnd );    //Destroy Window now that the extensions are loaded
    hwnd = NULL;

    //..
    return CView::PreCreateWindow(cs);
}

void CJesseView::OnDraw(CDC* pCDC)
{
    CJesseDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);

    // TODO: add draw code for native data here

    BeginScene(0.4, 0.4, 0.8, 1.0);
    // Draw something!
    EndScene();

    //..
    ::SwapBuffers( pCDC->GetSafeHdc() );
}

int CJesseView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CView::OnCreate(lpCreateStruct) == -1)
        return -1;

    // TODO:  Add your specialized creation code here
    m_OpenGL->Create( AfxGetApp()->GetMainWnd()->GetSafeHwnd(), 
                true, 800, 600, 0.1f, 60000.0f ); // VSYNC, 0.1, 60k for near and far

    return 0;
}

And finally my OpenGL::Create() function, well the errors got to be to do with the HWND I pass in sic how I get the HDC.. 最后是我的OpenGL :: Create()函数,该错误与我在获取HDC时传递的HWND有关。

bool OpenGL::InitOpenGL( HWND hWnd, int sW, int sH, float nearP, float farP, bool vsync )
{
    bool bOk = false;
    bool error = false;

    m_Context = GetDC( hWnd );

    //do some PIXELFORMATDESCRIPTOR and context attrib setup

    bOk = bOk && (m_Renderer = wglCreateContextAttribsARB(m_Context, 0, actualContextAttribs));

    if( ! bOk )
        return false;

    if(! wglMakeCurrent(m_Context, m_Renderer) ) {    
        wglDeleteContext(m_Renderer);
        return false;
    }

    //... other stuff
}

I need to initialise GLEW before I initialise OpenGL 在初始化OpenGL之前,我需要初始化GLEW

No. GLEW needs an existing OpenGL context being made current to properly initialize. 否。GLEW需要使现有的OpenGL上下文成为当前的上下文才能正确初始化。 This is also clearly stated in the GLEW manual. 这在GLEW手册中也有明确说明。 So go ahead and Read The Fine Manual (RTFM), please. 因此,请继续阅读《精细手册》(RTFM)。

Now for creating an advanced OpenGL context GLEW is of little help. 现在,对于创建高级OpenGL上下文,GLEW几乎没有帮助。 You need to create a proxy OpenGL context first which you use to get the function pointers to the extension wgl… functions. 您首先需要创建一个代理OpenGL上下文,该上下文用于获取指向扩展wgl…函数的函数指针。 Don't bother with the MFC here. 在这里不要操心MFC。 Use the plain Win32 functions for that. 为此,请使用普通的Win32函数。 It's explained very well here: http://www.opengl.org/wiki/Creating_an_OpenGL_Context_(WGL)#Proper_Context_Creation 在这里对此进行了很好的解释: http : //www.opengl.org/wiki/Creating_an_OpenGL_Context_(WGL)#Proper_Context_Creation

Once you got your proper OpenGL context you can use GLEW. 获得正确的OpenGL上下文后,即可使用GLEW。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM