|
-
November 23rd, 2004, 02:09 PM
#1
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
-
November 23rd, 2004, 03:48 PM
#2
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.
-
November 23rd, 2004, 03:59 PM
#3
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.
-
November 23rd, 2004, 04:22 PM
#4
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!
-
November 23rd, 2004, 04:42 PM
#5
Re: weird opengl/mfc problem
-
November 23rd, 2004, 08:02 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|