Click to See Complete Forum and Search --> : weird opengl/mfc problem


akgreene
November 23rd, 2004, 01:09 PM
Hi all,
I tried to create an MFC OpenGl control, similar to the one in this article: http://steinsoft.net/index.php?site=Programming/Tutorials/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

Elementer
November 23rd, 2004, 02:48 PM
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

akgreene
November 23rd, 2004, 02:59 PM
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.:


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.

akgreene
November 23rd, 2004, 03:22 PM
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!

Andreas Masur
November 23rd, 2004, 03:42 PM
[ Moved thread ]

Elementer
November 23rd, 2004, 07:02 PM
You're welcome ;)

:)