CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2004
    Posts
    5

    weird opengl/mfc problem

    Hi all,
    I tried to create an MFC OpenGl control, similar to the one in this article: http://steinsoft.net/index.php?site=.../opengl_dialog. It seems to work and draw fine in general, unless I try to use a timer in the dialog that contains the OpenGl control. Then my timer never gets triggered. If I add a line that says CPaintDC dc(this) to the beginning of the OnPaint() function of the control, then the timer gets triggered, but then the rendering doesn't work. Does anyone know what the cause of this could be?? I've seen other tutorials that created the gl context during each OnPaint call, but that seems a little excessive. Is that a better way?

    Thanks,
    akgreene

  2. #2
    Join Date
    Nov 2004
    Posts
    199

    Re: weird opengl/mfc problem

    I don't know how is your code, but must be:

    //initializations:

    int CYourGLWindow:OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    if (CWnd::OnCreate(lpCreateStruct) == -1)
    return -1;

    m_hDC = ::GetDC(this->m_hWnd); //get DC from window

    SetPixelFormat(m_hDC, m_nPixelFormat, NULL); //set pfd ...

    m_hRC = wglCreateContext(m_hDC); ///create rendering context

    ... bla bla bla... OGL initializations.....

    //now init the timer

    SetTimer(IDC_TIMER, 100, NULL);

    }


    //this is Timer func:

    void CGLWindow::OnTimer(UINT nIDEvent)
    {
    InvalidateRect(NULL); //you must invalidate Rect of your glWindow
    CWnd::OnTimer(nIDEvent);
    }


    //OnPaint func:

    void CYourGLWWindow::OnPaint()
    {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //draw rotating cube for example.....
    // static float fX = 0.0f; etc...

    SwapBuffers(m_hDC);

    ValidateRect(NULL);
    }

    Bye
    Last edited by Elementer; June 27th, 2005 at 06:03 AM.

  3. #3
    Join Date
    Aug 2004
    Posts
    5

    Re: weird opengl/mfc problem

    My OpenGL initialization code is very similar. However, the problem is that the Timer and Paint functions are in different classes. The timer is meant to be running in the Dialog that contains my opengl control. E.g.:

    Code:
    MyDialog::OnTimerStartButton() {
         SetTimer(0, 100, NULL);
    }
    
    MyDialog::OnTimer(){
    // ... some sort of work done here
    }
    
    MyOpenGLControl::OnPaint() {
    // Draw something here
    }
    For some reason, the painting works if I don't use a CPaintDC, but then the timer doesn't work. If I do use a CPaintDC, then the timer works, but the rendering doesn't.

  4. #4
    Join Date
    Aug 2004
    Posts
    5

    Re: weird opengl/mfc problem

    Actually, given your advice I just solved the problem. I left out a pesky "ValidateRect" at the end of my paint function. Everything seems to be working now. Thanks!

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: weird opengl/mfc problem

    [ Moved thread ]

  6. #6
    Join Date
    Nov 2004
    Posts
    199

    Re: weird opengl/mfc problem

    You're welcome


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured