Rendering problem

Hello guys,

I’m using OpenGL with MFC to draw 2d scenes. I wnat to draw 1000 lines, rectangles, polygons and ellipses per second. The problem is it takes 100% cpu time. Even if I try to draw just a single line every 200 ms, it takes 100% cpu. What could be the problem ? I’m giving the code for OnDraw() function below:

void OnDraw()
{
CRect r;
GetClientRect(&r);

// Set our palette
if ( m_hPalette != NULL )
{
SelectPalette(pDC->m_hDC, m_hPalette, FALSE);
RealizePalette(pDC->m_hDC);
} // if

// Draw the OpenGL scene
wglMakeCurrent(pDC->m_hDC, m_hRC);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

for (int i = 0; i < 1000; i++)
{
   glPushMatrix();

   glBegin(GL_LINE_LOOP);
   glColor3f(1,0,0);
   glVertex2d(0,0);
   glColor3f(0,1,0);
   glVertex2d(r .right,0);
   glColor3f(0,0,1);
   glVertex2d(r .right,r .bottom);
   glColor3f(1,1,0);
   glVertex2d(0,r .bottom);
   glEnd();

   glColor3f(1,0,0);
   glRectd(100,100, 200, 200);
   glRectd(100,440, 200, 480);

   glPopMatrix();
}

SwapBuffers(pDC->m_hDC);
wglMakeCurrent(pDC->m_hDC, NULL);

}

You can insert a Sleep(ms) in your onDraw() function.
Sleep(ms) enables Windows to switch to another thread and at the same time, lets your your thread wait for ms millisecs.

I noticed that you are using glPush/glPop’s in your loop, you don’t need them since you aren’t transforming the scene. I.e glTranslate, glScale or glRotate.

I haven’t had time to run a test to check this, but you don’t need to call wglMakeCurrent every time you draw a frame. At least I don’t, and I do full-screen and windowed rendering with pretty good framerates with fairly complex scenes.

Try taking it out of the rendering code and place it in you initialization code and see what happens.