# VC++(MFC) and OpenGL

**URL:** <https://community.khronos.org/t/vc-mfc-and-opengl/27613>\
**Category:** OpenGL: Basic Coding\
**Created:** [June 4, 2002, 7:57am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613 "2002-06-04T07:57:00Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![aus79er](https://community.khronos.org/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@aus79er](https://community.khronos.org/u/aus79er)\
**Post date:** [June 4, 2002, 7:57am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/1 "2002-06-04T07:57:00Z")

</div>

Hello,  
I have written a program to draw a cube using MFC and OpenGL.  
I have also written few functions to rotate the cube on mouse movement using Leftbutton down etc…  
Everything is working fine .  
But the only problem seems to be that every time I move the mouse over the view,the whole view is redrawn again as a result there is some sort of flickering effect.  
I am appending the code…please tell me what I am doing wrong…and how can i rectify it…

void CSim\_4\_26\_02View::OnLButtonDown(UINT nFlags, CPoint point)  
{  
// TODO: Add your message handler code here and/or call default  
m\_lcapture=true;  
m\_pt=point;

```
SetCapture();
//CView::OnLButtonDown(nFlags, point);

```

}

void CSim\_4\_26\_02View::OnMouseMove(UINT nFlags, CPoint point)  
{  
// TODO: Add your message handler code here and/or call default  
if(m\_lcapture)  
{  
m\_dz = float(point.y - m\_pt.y);  
m\_dx = float(point.x - m\_pt.x);

```
m_angy += m_dz;
m_angx += m_dx;

}
if(m_rcapture)
{
	m_tz = float(point.y - m_pt1.y)/40.0f;
m_tx = float(point.x - m_pt1.x)/40.0f;

m_inx+=m_tx;
m_iny+=m_tz;

m_inz += (m_dx + m_dz)/2.f;

}
Invalidate(FALSE);
//CallView();

```

// CView::OnMouseMove(nFlags, point);

}

void CSim\_4\_26\_02View::OnLButtonUp(UINT nFlags, CPoint point)  
{  
// TODO: Add your message handler code here and/or call default  
m\_lcapture=false;  
//CView::OnLButtonUp(nFlags, point);  
ReleaseCapture();  
}

void CSim\_4\_26\_02View::OnRButtonDown(UINT nFlags, CPoint point)  
{  
// TODO: Add your message handler code here and/or call default  
m\_pt1=point;  
m\_rcapture=true;  
ReleaseCapture();  
//CView::OnRButtonDown(nFlags, point);  
}

void CSim\_4\_26\_02View::OnRButtonUp(UINT nFlags, CPoint point)  
{  
// TODO: Add your message handler code here and/or call default  
m\_rcapture=false;  
// CView::OnRButtonUp(nFlags, point);  
}

int CSim\_4\_26\_02View::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{  
if (CView::OnCreate(lpCreateStruct) == -1)  
return -1;

```
// TODO: Add your specialized creation code here
PIXELFORMATDESCRIPTOR pfd =
{
	sizeof ( PIXELFORMATDESCRIPTOR ), 
	1,
	PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL,
	PFD_TYPE_RGBA,
	24,
	0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 
	32,
	0, 0,
	PFD_MAIN_PLANE,
	0,
	0, 0, 0
} ;
m_d = new CClientDC ( this ) ;

int pixformat = ChoosePixelFormat ( m_d -&gt; m_hDC, &pfd ) ;
SetPixelFormat ( m_d -&gt; m_hDC, pixformat, &pfd ) ;

DescribePixelFormat ( m_d -&gt; m_hDC, pixformat, sizeof ( pfd ), &pfd ) ;

m_hGRC = wglCreateContext ( m_d -&gt; m_hDC ) ;
wglMakeCurrent ( m_d -&gt; m_hDC, m_hGRC ) ;

glShadeModel(GL_SMOOTH);

glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);

```

return 1;  
}

Bascially I want smooth movement of the cube(on mouse move)…

Thanks,  
aus

---

<div class="post-metadata">

**Author:** ![chicho](https://community.khronos.org/letter_avatar_proxy/v4/letter/c/ecc23a/32.png) [@chicho](https://community.khronos.org/u/chicho)\
**Post date:** [June 4, 2002, 9:19pm UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/2 "2002-06-04T21:19:00Z")

</div>

You must to use a doublebuffered window. You can do it setting in pdfpixelformatdescriptor structure:

pfd.dwFlags = PFD\_DRAW\_TO\_WINDOW |PFD\_SUPPORT\_OPENGL | PFD\_DOUBLEBUFFER;

Also, when you go to draw you must to call:

SwapBuffers (hDC);

---

<div class="post-metadata">

**Author:** ![mikael\_aronsson](https://community.khronos.org/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mikael\_aronsson](https://community.khronos.org/u/mikael_aronsson)\
**Post date:** [June 5, 2002, 2:20am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/3 "2002-06-05T02:20:00Z")

</div>

Hi !

If that does not help, then you have the background erasing enabled, overload the OnEraseBkgnd() (I think that’s the name on it) and return, hmmm I think it is TRUE to stop Windows from erasing the background every time, this can cause lot’s of flickering.

Mikael

---

<div class="post-metadata">

**Author:** ![Miguel\_dup1](https://community.khronos.org/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@Miguel\_dup1](https://community.khronos.org/u/Miguel_dup1)\
**Post date:** [June 5, 2002, 10:39am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/4 "2002-06-05T10:39:00Z")

</div>

Actually the OnEraseBkgnd() funciton avoids the flickery background, and the returning value does ot matter… ![](http://www.opengl.org/discussion_boards/ubb/smile.gif)

So, if you are using MFC you add

afx\_msg BOOL OnEraseBkgnd(CDC\* pDC);

to your list of message functions… The implementation of it simply resturns true or false; it does not matter for our OGL purposes…

BOOL view::OnEraseBkgnd(CDC\* pDC)  
{  
return true;  
}

If you are using Win32, you simply add

case WM\_ERASEBKGND:  
{  
break;  
}

Good luck…

> Originally posted by mikael\_aronsson:  
> [b]Hi !

> If that does not help, then you have the background erasing enabled, overload the OnEraseBkgnd() (I think that’s the name on it) and return, hmmm I think it is TRUE to stop Windows from erasing the background every time, this can cause lot’s of flickering.

> Mikael[/b]

---

<div class="post-metadata">

**Author:** ![aus79er](https://community.khronos.org/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@aus79er](https://community.khronos.org/u/aus79er)\
**Post date:** [June 5, 2002, 1:39pm UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/5 "2002-06-05T13:39:00Z")

</div>

Thanks for your replies.  
I have now written the code for BOOL view::OnEraseBkgnd(CDC\* pDC)  
{  
return true;  
}

I have also tried what chicho told me  
PIXELFORMATDESCRIPTOR pfd = // Structure used to describe the format  
{  
sizeof(PIXELFORMATDESCRIPTOR),  
1, // Version  
PFD\_DRAW\_TO\_WINDOW | // Supports GDI  
PFD\_SUPPORT\_OPENGL | // Supports OpenGL  
PFD\_DOUBLEBUFFER, // Use double buffering (more efficient drawing)  
PFD\_TYPE\_RGBA, // No pallettes  
24, // Number of color planes  
// in each color buffer  
24, 0, // for Red-component  
24, 0, // for Green-component  
24, 0, // for Blue-component  
24, 0, // for Alpha-component  
0, // Number of planes  
// of Accumulation buffer  
0, // for Red-component  
0, // for Green-component  
0, // for Blue-component  
0, // for Alpha-component  
32, // Depth of Z-buffer  
0, // Depth of Stencil-buffer  
0, // Depth of Auxiliary-buffer  
0, // Now is ignored  
0, // Number of planes  
0, // Now is ignored  
0, // Color of transparent mask  
0 // Now is ignored  
};  
I have tried using this code in int COGView::OnCreate(LPCREATESTRUCT lpCreateStruct) but now the window is not erased at all as a result I don’t see any object…Do I need to add something else to my code also…

Thanks  
aus

---

<div class="post-metadata">

**Author:** ![aus79er](https://community.khronos.org/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@aus79er](https://community.khronos.org/u/aus79er)\
**Post date:** [June 5, 2002, 1:47pm UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/6 "2002-06-05T13:47:00Z")

</div>

Hi  
It seems there is a problem whenever i add  
PFD\_DOUBLEBUFFER to my code.  
When i add this statement I cannot see the object infact window is not earsed at all …means the view remains the same like blank or some thing else.  
When i remove this line ,I can see the cube …but there is flickering effect…

aus

---

<div class="post-metadata">

**Author:** ![Deiussum](https://community.khronos.org/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@Deiussum](https://community.khronos.org/u/Deiussum)\
**Post date:** [June 6, 2002, 4:08am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/7 "2002-06-06T04:08:00Z")

</div>

Are you using SwapBuffers after you draw your scene?

---

<div class="post-metadata">

**Author:** ![aus79er](https://community.khronos.org/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@aus79er](https://community.khronos.org/u/aus79er)\
**Post date:** [June 6, 2002, 6:44am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/8 "2002-06-06T06:44:00Z")

</div>

Hi,  
Yes I am using Swapbuffer after I have drawn the scene…  
void CSim\_4\_26\_02View::OnDraw(CDC\* pDC)  
{  
CSim\_4\_26\_02Doc\* pDoc = GetDocument();  
ASSERT\_VALID(pDoc);  
// TODO: add draw code for native data here

```
 	draw3dobject();
	
	SwapBuffers(m_hdc);

```

}

Thanks,  
aus

---

<div class="post-metadata">

**Author:** ![Deiussum](https://community.khronos.org/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@Deiussum](https://community.khronos.org/u/Deiussum)\
**Post date:** [June 6, 2002, 9:09am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/9 "2002-06-06T09:09:00Z")

</div>

Instead of using m\_hdc, (did you store that, I don’t seem to rmember that being on the basic CView class), maybe you should use the HDC that you are given in the CDC\*

SwapBuffers(pDC-\>GetSafeHdc());

Edit: Changed GetSafeDC to the correct function GetSafeHdc(). ![](http://www.opengl.org/discussion_boards/ubb/smile.gif)

[This message has been edited by Deiussum (edited 06-06-2002).]

---

<div class="post-metadata">

**Author:** ![aus79er](https://community.khronos.org/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@aus79er](https://community.khronos.org/u/aus79er)\
**Post date:** [June 6, 2002, 9:21am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/10 "2002-06-06T09:21:00Z")

</div>

Hi  
I changed it to  
SwapBuffers(pDC-\>GetSafeHdc());  
But still no change…  
My view is not getting cleared at all…

Is there anything else I need to do…?

Thanks,  
aus

---

<div class="post-metadata">

**Author:** ![Deiussum](https://community.khronos.org/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@Deiussum](https://community.khronos.org/u/Deiussum)\
**Post date:** [June 6, 2002, 11:00am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/11 "2002-06-06T11:00:00Z")

</div>

I’m not quite sure I understand… you say it’s not getting cleared, but you also say it remains blank, which to me suggests nothing drawing.

Anyway, some other things to check.

1. Are you clearing both the color buffer bit and the depth buffer bit.
2. Did you set the view to have a class with the CS\_OWNDC style?

(To do this, I do something like so in PreCreateWindow…)

> ```auto
> 
> ```

> cs.lpszClass = AfxRegisterWndClass(CS\_HREDRAW|CS\_VREDRAW|CS\_OWNDC,  
> ::LoadCursor(NULL, IDC\_ARROW), HBRUSH(COLOR\_WINDOW+1), NULL);

> ```auto
> 
> ```

---

<div class="post-metadata">

**Author:** ![aus79er](https://community.khronos.org/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@aus79er](https://community.khronos.org/u/aus79er)\
**Post date:** [June 6, 2002, 1:03pm UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/12 "2002-06-06T13:03:00Z")

</div>

What i want to say is that ,Before putting doublebuffer statement I can see the cube…but after putting that statement in setpixelformat function i don’t see any thing.  
Infact when i move my application my view is filled with the previous view as if it hasn’t been erased.  
I hope now you can understand what I am trying to say…

This what I have in precreate window  
BOOL CSim\_4\_26\_02View::PreCreateWindow(CREATESTRUCT& cs)  
{  
// TODO: Modify the Window class or styles here by modifying  
// the CREATESTRUCT cs  
cs.style |= WS\_CLIPSIBLINGS | WS\_CLIPCHILDREN;

```
return CView::PreCreateWindow(cs);

```

}

also I am using  
glClear ( GL\_COLOR\_BUFFER\_BIT|GL\_DEPTH\_BUFFER\_BIT ) ;

I think , there is somewhere a small mistake in my code…don’t know where… ![](http://www.opengl.org/discussion_boards/ubb/smile.gif)(I mean I can see the cube but then there is flickering)

Thanks for your help  
aus

---

<div class="post-metadata">

**Author:** ![Deiussum](https://community.khronos.org/letter_avatar_proxy/v4/letter/d/c89c15/32.png) [@Deiussum](https://community.khronos.org/u/Deiussum)\
**Post date:** [June 7, 2002, 4:15am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/13 "2002-06-07T04:15:00Z")

</div>

Try adding the line of code I gave you above to your PreCreateWindow. I’ve had weird effects at times when the CS\_OWNDC wasn’t set on the CView-derived class.

Since you say it works when you don’t enabled double buffering, it really sounds like the SwapBuffer isn’t working right. From your code, it should be, though. Maybe just a weird anomaly from not having CS\_OWNDC specified, though…

---

<div class="post-metadata">

**Author:** ![mikael\_aronsson](https://community.khronos.org/letter_avatar_proxy/v4/letter/m/5daacb/32.png) [@mikael\_aronsson](https://community.khronos.org/u/mikael_aronsson)\
**Post date:** [June 7, 2002, 5:05am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/14 "2002-06-07T05:05:00Z")

</div>

Hi !

Actually the return value from OnEraseBkgnd is very important, if don’t return TRUE, then the Win32 API will not mark it as erased, so it will be erased anyway later on, so please do return TRUE.

Mikael

---

<div class="post-metadata">

**Author:** ![aus79er](https://community.khronos.org/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@aus79er](https://community.khronos.org/u/aus79er)\
**Post date:** [June 7, 2002, 9:26am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/15 "2002-06-07T09:26:00Z")

</div>

Thanks for you help,  
I did manage to find out the problem…  
I have OnDraw as well as OnPaint event in my code.  
I had written swapbuffer in ondraw …but not in onpaint…  
Now i have added it to onpaint …and its working fine…

Once again thanks for all your help  
aus

---

<div class="post-metadata">

**Author:** ![J\_P](https://community.khronos.org/letter_avatar_proxy/v4/letter/j/bbce88/32.png) [@J\_P](https://community.khronos.org/u/J_P)\
**Post date:** [June 8, 2002, 6:14am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/16 "2002-06-08T06:14:00Z")

</div>

Why do you have both? Typically MFC codes use one or the other. There are subtle differences. The ondraw is usually used with the MFC Doc/View architecture. I would think you should have only one, either onpaint or ondraw unless you have an indepth understanding of MFC and have ruled out the possibility of further conflict, this could lead to more problems.

See  
[www.mfcogl.com](http://www.mfcogl.com)

---

<div class="post-metadata">

**Author:** ![aus79er](https://community.khronos.org/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@aus79er](https://community.khronos.org/u/aus79er)\
**Post date:** [June 8, 2002, 8:50am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/17 "2002-06-08T08:50:00Z")

</div>

Thanks,  
I am still learning MFC…  
So can anybody clearly explain to me what is the differ in OnPaint and OnDraw and …which should be used and when?

Aus

---

<div class="post-metadata">

**Author:** ![J\_P](https://community.khronos.org/letter_avatar_proxy/v4/letter/j/bbce88/32.png) [@J\_P](https://community.khronos.org/u/J_P)\
**Post date:** [June 9, 2002, 2:52am UTC](https://community.khronos.org/t/vc-mfc-and-opengl/27613/18 "2002-06-09T02:52:00Z")

</div>

Have you tried [www.mfcogl.com?](http://www.mfcogl.com?) If that doesn’t satisfy you, you could do some reading in the compiler help, MSDN and some books. Check the references given at mfcogl.

Without going to a lot of work, I think a safe bet would be to use what the App Wizard creates for you or if you create your own MFC program use OnPaint.
