Advertisement

OpenGL in a MFCDialog + Thread = Problem

Started by September 18, 2003 08:15 AM
5 comments, last by Jason_Frost 21 years, 5 months ago
Hi, i try to draw in my Gl window of my Dialogbox throught a theard, but the window will not refresht. Ok I know it must be a problem with the DC and RC but i haven´t found a solution. Knows anyone a solution for this problem? Here my code: BOOL DialogAnalog::OnInitDialog() { CDialog::OnInitDialog(); m_Interrupt = 0; m_intOpenGlvalbuffer = 50; pclStatic = (CStatic *)GetDlgItem(IDC_OPENGLWIN); pclGlView = new CGlView(pclStatic); // pclGlView->SetPixelformat(m_hDC); pclGlView->DrawGLScene(); return true; } void DialogAnalog::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(▭); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } CPaintDC dc(this); // device context for painting HDC m_hDC; m_hDC = ::GetDC(this->m_hWnd); RECT rect; GetClientRect(▭); int iWidth = -(rect.right - rect.left); int iHeight = rect.top - rect.bottom; //pclGlView->SwitchContext(this->m_hWnd); pclGlView->OnCreate(); pclGlView->ReSizeGLScene(iWidth, iHeight); pclGlView->InitGL(); pclGlView->DrawGLScene(); } void DialogAnalog::OnStart() { m_Interrupt = 0; m_bWantExit = false; m_pThread = AfxBeginThread(Output, this); } UINT DialogAnalog::Output(LPVOID pParam){ DialogAnalog *pDialogAnalog = (DialogAnalog*) pParam; for (int i = 0; i < pDialogAnalog->mv_data.size() && pDialogAnalog->m_Interrupt != 1; ++i){ pDialogAnalog->GotoPosition(i); pDialogAnalog->FillFields(i); pDialogAnalog->ThreadGlInit(); pDialogAnalog->UpdateTime(0,i); //pDialogAnalog->UpdateGlWindow(); } return 0; } //OGL Class CGlView::CGlView(CWnd *pclWnd) { m_pclWnd = pclWnd; m_hWnd = pclWnd->m_hWnd; m_hDC = ::GetDC(m_hWnd); m_floatSimZoom = 1; m_floatSimRes = 20 ; m_intBufferSize = 50; globjectcoordqueue.resize(m_intBufferSize); } CGlView::~CGlView() { } BEGIN_MESSAGE_MAP(CGlView, CWnd) //{{AFX_MSG_MAP(CGlView) // ON_WM_DESTROY() // ON_WM_ERASEBKGND() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CGlView message handlers int CGlView::OnCreate() { m_hDC = ::GetDC(this->m_hWnd); if(!SetPixelformat(m_hDC)) { ::MessageBox(::GetFocus(),"SetPixelformat Failed!","Error",MB_OK); return -1; } m_hglRC = wglCreateContext(m_hDC); int i= wglMakeCurrent(m_hDC,m_hglRC); InitGL(); return 0; }
Wow, that''s a good one... I haven''t started messing with threads yet at all.

Have you tried www.experts-exchange.com or www.codeguru.com? Those sites are more MFC-related that OpenGL-related, but if you can replicate the problem with like a CDC instead of an OpenGL context they should be able to help you with it - especially codeguru.com, they''ve been very helpful to me.

If you get an answer to this, I''d like to know it too!

Love means nothing to a tennis player

My nothing-to-write-home-about OpenGL webpage. (please pardon the popups!)

Advertisement
I''ve tried rendering in a thread before and managed (finally) to get it it to work. What you have to know is that your rendering code must be done in the same thread that created the render context. If not then you get an unrefreshed screen with gl functions that return GL_ERRORs. I tried looking at your code, but MFC makes my head spin but I think this might be your problem.

Check and see what thread you''re actually creating the render context in and what thread you''re trying to render in. There is a "threads" debug window in the Debug->Threads menu in VC6 (which I assume you''re using) so if you break on the wglCreateContext and look at the threads debug window you should be able to get the thread id, then break on the drawing code and compare thread ids.

Hope that helps
cool i will try it ;o) ...... is there a chance to take a look in your code? My Mailadress ist jason_frost@web.de ...... thx.

....

Ok i reinit in the thread the RC and DC ...... but it still does not work ;o( any Ideas?
[edited by - Jason_Frost on September 19, 2003 3:32:08 AM]

[edited by - Jason_Frost on September 19, 2003 4:27:59 AM]
Mmmm. Don''t exctly know where my old code is but I''ve been wanting to get a render thread going again (been using idle time lately) so I''ll look for it, and try and rewrite it if I can''t find it and get you a copy - probably only sometime this weekend though (at work now)
thx a lot ;o)
Advertisement
I have managed to get a render thread working and have sent you the code to the email address you gave.

Hope it helps

This topic is closed to new replies.

Advertisement