OpenGL in CAD app [Solved - see how]

Started by
9 comments, last by ZQJ 18 years, 7 months ago
Hi there. I'm developing for my computer science degree project a CAD sofware which involves cloth simulation. As I always did, I will use Opengl, but I've encountered some problems. Basically, my app has a window, and an opengl viewport in the main window. I'm using ATL/WTL, so this viewport is, in fact, a class derived from CAxDialogImpl. I'm doing all the redrawing stuff (opengl rendering) on WM_PAINT message. The 2 problems are: 1. As I've learned opengl from NeHe's site, I'm trying to use a big modified part of it's base source code. I've eliminated all the window creation stuff (because i'm not making a new window, I'm using one that already exists) and just capture the DC, set the pixel format, setup opengl, resize and draw. The problem is my window receives WM_PAINT message continuously. I've ran another NeHe's application and the effect is the same. The cpu is used 97-98% all the time. That might be of some use in a game, but not in a CAD application. If I run 3dS Max, when idle, it uses 0% cpu, but it has 4 openglviewports. How can I unload so much work from the cpu, and why my window receives so many WM_PAINT messages? 2. I've specified PFD_DOUBLEBUFFER in the pixel format, I still use SwapBuffers (because it draws nothing without it) and the viewport performs well when moving the window, but it's flickering to death when resizing the window, which i know from my experience that it's not happening to a normal window. But if we look again to 3dsmax, it doesn't flicker at all. So why is that happening? thanks alot [Edited by - meeshoo on September 7, 2005 2:13:40 AM]
Advertisement
I use MFC for my project (similar to your).

The window receives a PAINT message only when it is resized or overlapped.
To disable the flickering I had to override the EraseBackground() method (see 'removed' )...in fact you use GL to clear the background and you dont need the white background proposed by Windows (it is the cause).

I dont know why you use NeHe base code for your app if it is not a demo or a game [smile] ...set up GL for MFC is even simpler ( the window is already created and you have not to deal with fullscreen stuff).

The (classic) WinMain loop proposed by NeHe

loop{    if message process_message    else render}


is not suitable for simulations and or CAD.
If you are interested in MFC I can post you the code since it is very very short.
well, i think the erase bck is my problem too. yes i use opengl to clear it, so i dont' need it anymore. i use as glClearColor the black color, and when it flickers it alternates with the control bck color (gray in my case). I'm not using NeHe like that. i've modified it all. I'm not having that loop you are talking about, and i'm not recreating the window. I just used the init opengl part, with the pixel descriptor stuff, and the resizing stuff. I've removed the fullscreen part too. now i'm at work and don't have the code with me, but there is no problem. I'm redrawing the window from my WTL dialog class, where i've added an OnPaint message handler to my message map. The problem is that this OnPaint is called continuously. I've tested this by inserted a breakpoint and a counter in the method. if i comment out the MESSAGE_HANDLER(WM_PAINT, OnPaint) stuff from my message map, the cpu is at 0%, and if i dont't it stays at 98%. do you know why it keeps sending WM_PAINT?
yes, some code may be usefull(i know mfc as well, but now i've learned WTL and i'm very enthuziast about it). especially the message handling code. thanks
I post the code

In GLView declaration (I dont report) you have two member variables

CDC* m_pCDC; // client device context (no more used)
HGLRC m_glHRC; // GL render context

and you have to override (via wizard)

afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnDestroy();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);

virtual void OnDraw(CDC* pDC);
BOOL PreCreateWindow(CREATESTRUCT& cs);

Now the implementation

this is the most important part
// ctorGLView::GLView(){	// TODO: add construction code here	m_pCDC	= NULL;        m_glHRC	= NULL;        // ...}// create the contextBOOL GLView::CreateContext(void){	try{	   if(m_glHRC!=NULL) throw 1;           PIXELFORMATDESCRIPTOR pfdPFD={sizeof(PIXELFORMATDESCRIPTOR),1,                                             PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,			               PFD_TYPE_RGBA,32,0,0,0,0,0,0,0,0,0,0,0,0,0,32,0,0,PFD_MAIN_PLANE,                             0,0,0,0};	   int iFormat;	   m_pCDC = new CClientDC(this);	   if(!m_pCDC) throw 2;	   if(!(iFormat=ChoosePixelFormat(m_pCDC->GetSafeHdc(),&pfdPFD)))throw 3;	   if(!SetPixelFormat(m_pCDC->GetSafeHdc(),iFormat,&pfdPFD))throw 4;	   if(!(m_glHRC=wglCreateContext(m_pCDC->GetSafeHdc())))throw 5;	        }catch(int i){	   this->ReleaseContext();	   // removed (here I write some info about the error code i=1,...,5)	   return FALSE;	}	return TRUE;}void GLView::ReleaseContext(void){	HGLRC hrc = ::wglGetCurrentContext();	if(hrc==m_glHRC)wglMakeCurrent(NULL,  NULL);	if (m_glHRC!=NULL)::wglDeleteContext(m_glHRC);	m_glHRC = NULL;	if(m_pCDC) delete(m_pCDC);	m_pCDC = NULL;}


Other methods...

// adjust the styleBOOL GLView::PreCreateWindow(CREATESTRUCT& cs){	cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;	return CView::PreCreateWindow(cs);}// on createint GLView::OnCreate(LPCREATESTRUCT lpCreateStruct){	if (CView::OnCreate(lpCreateStruct) == -1)		return -1;	// GL	if(!CreateContext())return -1;	// ...	return 0;}void GLView::OnDestroy(){	CView::OnDestroy();	this->ReleaseContext();}BOOL GLView::OnEraseBkgnd(CDC* pDC){	return TRUE; // no flickering}// base drawvoid GLView::OnDraw(CDC* pDC){   MakeCurrent();     //   GL stuff here   ::SwapBuffers(::wglGetCurrentDC());  // swap}

thanks alot. The initialization and destruction stuff is pretty similar to mine. It looks like the background erasing problem is the only one i've got till now. Maybe this erasing problem gerenrates the flooding WM_PAINT message. I still have a question though: what does the MakeCurrent() function call from OnDraw?
Quote:Original post by meeshoo
I still have a question though: what does the MakeCurrent() function call from OnDraw?


You are right! I forgot to post the method because it is in the declaration

It's simply a wrapping of wglMakeCurrent
bool MakeCurrent(void){	if(m_glHRC!=::wglGetCurrentContext())             return  ::wglMakeCurrent(m_pCDC->GetSafeHdc(),m_glHRC)==TRUE;	else return true;}
oh, i got it now. thanks alot for helping me. when i'll get home i'll try to correct the problem and hope it will work. thanks again.
i've finally got it working. i still don't know why there are so many WM_PAINT messages, but i've done the following (maybe it will help some of you who are using ATL/WTL):

1. I've added WM_ERASEBKGND message handler.
2. I've moved the drawing code from OnPaint to OnEraseBackground.
3. I've removed the WM_PAINT message handler.
4. In WM_SIZE message handler (OnSize) i send a WM_ERASEBKGND to the window, so it redraws. The reason for the last action is that when resizing, the WM_ERASEBKGND is used only a few times, so redrawing is not happening real time.

By doing this I resolved the flickering problem and the cpu load pb. now the cpu stays at 0% when the app is idle.

thank you guys for help


This topic is closed to new replies.

Advertisement