PDA

View Full Version : MFC OpenGL Invalidate Flickering



AeroSujan
09-14-2011, 09:19 AM
Dear Friends
I am wirting an application for drawing a rectangle on the screen using opengl and mfc. So I implemented the OnPaint function, OnDraw function is also giving the same problem. It's drawing fine. I want to implement pan, zoom, and rotate functionalities. Now everytime the mouse moves I am calling Invalidate() or otherwise I can call Invalidate() in the ::OnDraw function. Pan, zoom and rotate everything is happening but its heavily flickering. I have serached a lot and tried all means like return 1 in the OnEraseBackground() functio for WM_ERASEBACKGROUND etc. But I am getting still flickering . Please help getting rid of this problem. Check the code snippet below.



void CRevolutionProjView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CRect RectAff;
GetClientRect(RectAff);
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ) ;
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
glPushMatrix( ) ;
glTranslatef(trans[0], trans[1], trans[2]);
glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
drawcube();
glPopMatrix( ) ;
glFinish( ) ;
glFlush();
SwapBuffers(hDC);
Invalidate(false);
// Do not call CView::OnPaint() for painting messages
}

void CRevolutionProjView::OnMouseMove(UINT nFlags, CPoint point)
{
newP = point;
// TODO: Add your message handler code here and/or call default
int dx = oldP.x - newP.x;
int dy = newP.y - oldP.y;
switch(STATE)
{
case PAN:
{
trans[0] -= dx/100.0f;
trans[1] -= dy/100.0f;
// Invalidate();
}
break;
case ZOOM:
{
trans[2] -= (dx+dy) / 100.0f;
// Invalidate();
}
break;
case ROTATE:
{
rot[0] += (dy * 180.0f) / 500.0f;
rot[1] -= (dx * 180.0f) / 500.0f;
#define clamp(x) x = x > 360.0f ? x-360.0f : x < -360.0f ? x+=360.0f : x
clamp(rot[0]);
clamp(rot[1]);
// Invalidate();

}
break;
}

oldP = newP;

CView::OnMouseMove(nFlags, point);
}

Aleksandar
09-14-2011, 11:19 AM
Now everytime the mouse moves I am calling Invalidate() or otherwise I can call Invalidate() in the ::OnDraw function. Pan, zoom and rotate everything is happening but its heavily flickering.
Remove Invalidate() from OnDraw() immediately!!!


I have serached a lot and tried all means like return 1 in the OnEraseBackground() functio for WM_ERASEBACKGROUND etc.
OnEraseBackground() should return TRUE. Otherwise before each drawing a window is erased with the background color, which is usually white. That creates flicker for sure if OnEraseBackground() is not handled properly.

Take a look at comments I've inserted into your code.


void CRevolutionProjView::OnPaint()
{
CPaintDC dc(this);
CRect RectAff;
GetClientRect(RectAff);
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ) ;
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
glPushMatrix( ); //???
glLoadIdentity(); // PROBABLY MISSING!
glTranslatef(trans[0], trans[1], trans[2]);
glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
drawcube();
glPopMatrix( ); //???
glFinish( ); // REMOVE!
glFlush(); // REMOVE!
SwapBuffers(hDC);
Invalidate(false); // REMOVE!!!!!!!!
}

AeroSujan
09-15-2011, 12:36 AM
Hi Aleksandar I'll check it in the evening with your suggestions and let u know. I may need your further help. I have to resolve this issue I am stuck. Thanks Sujan

AeroSujan
09-15-2011, 08:07 AM
Hi Aleksandar If i remove glFlush() its not painting anything.

check the OnEraseBackground()

BOOL CRevolutionProjView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
//InvalidateRect(NULL,FALSE);
return true;
//return CView::OnEraseBkgnd(pDC);
}

I am just returning true.

It's still flickering dear. Please help me. Thanks Sujan

V-man
09-15-2011, 09:23 AM
You removed glFlush and it doesn't paint?
Sounds like you have a single buffer context instead of double buffer. Check your parameters.
http://msdn.microsoft.com/en-us/library/dd318284%28VS.85%29.aspx

You need to setup PFD_DOUBLEBUFFER

AeroSujan
09-15-2011, 09:58 AM
Hi V-man can u give me ur mail id. I'll send u the sln file. VC++ project. U please compile and check. It'll be of great help. PLease giveme ur mail id. Thanks Sujan

Aleksandar
09-15-2011, 12:16 PM
I've just made a simple GL MFC project. You can download it from the following link: Simple MFC GL Project (https://sites.google.com/site/opengltutorialsbyaks/events/simpleoldglmfcproject/GL.ZIP) and take a look at it. Arrow keys rotate the cube. And... there is no flicker. :)

AeroSujan
09-16-2011, 04:22 AM
Thanks Aleksandar I am going to download and look into your simple MFC GL Project in a few hours. Thanks a lot for your help. I'll come back once I execute this. Thanks Sujan

AeroSujan
09-17-2011, 04:37 AM
Hi Aleksandar I am able to execute ur program. Its excellent job.. Thanks a lot....I am so happy its working excellent. I am actually revolving a profile and creating surface. So if I find any difficulty in creating the surface I'll post message here again. Please keep in touch with this thread. Thanks a lot sujan

ZbuffeR
09-17-2011, 09:38 AM
If you have problems having nothing to do with the title of this topic, you should create a new topic.