Sign in to follow this  

OpenGL OpenGL in CAD app [Solved - see how]

Recommended Posts

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]

Share this post

Link to post
Share on other sites
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' ) 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

if message process_message
else render

is not suitable for simulations and or CAD.

Share this post

Link to post
Share on other sites
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?

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
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

// ctor
// TODO: add construction code here
m_pCDC = NULL;
m_glHRC = NULL;
// ...

// create the context
BOOL GLView::CreateContext(void){
if(m_glHRC!=NULL) throw 1;
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){
// 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 style
return CView::PreCreateWindow(cs);

// on create
int GLView::OnCreate(LPCREATESTRUCT lpCreateStruct){
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;

// GL
if(!CreateContext())return -1;

// ...

return 0;

void GLView::OnDestroy(){

BOOL GLView::OnEraseBkgnd(CDC* pDC){
return TRUE; // no flickering

// base draw
void GLView::OnDraw(CDC* pDC){
// GL stuff here
::SwapBuffers(::wglGetCurrentDC()); // swap

Share this post

Link to post
Share on other sites
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?

Share this post

Link to post
Share on other sites
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){
return ::wglMakeCurrent(m_pCDC->GetSafeHdc(),m_glHRC)==TRUE;
else return true;

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
I think the reason you're getting flooded with WM_PAINT messages is because you haven't called ValidateRect to mark the window as drawn. You have to override WM_ERASEBKGND anyway to get rid of flickering and I can't think of any good reason off the top of my head why ERASEBKGND can't be used to do painting in.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
  • Popular Now