I have this really strange and frustrating problem. I am writing a game with MFC and openGL and tried using multi-threading to control the timing of the physics engine and the rendering, but the rendering thread always only created a black screen (even though the same code worked in OnPaint(). To try to solve the problem, I made a really stripped down program to try to reproduce the problem, but instead have found a problem that I simply cannot explain. Here is the pertinent code:
In "OnInit"
[code]
pDC = GetDC();
g_hDC = ::GetDC(this->GetSafeHwnd());//pDC->GetSafeHdc();
int nPixelFormat;
PIXELFORMATDESCRIPTOR pfd = {
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,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0};
nPixelFormat = ChoosePixelFormat(g_hDC, &pfd);
SetPixelFormat(g_hDC, nPixelFormat, &pfd);
hRC = wglCreateContext(g_hDC);
wglMakeCurrent(g_hDC, hRC);
[/code]
In "OnSize"
[code]
CRect clientRect;
GetClientRect(clientRect);
glViewport(0, 0, clientRect.Width(), clientRect.Height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)clientRect.Width() / (GLfloat) clientRect.Height(), 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
[/code]
In "OnPaint"
[code]
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -1.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_LINES);
glVertex3f(-5.0f, 0.0f, 0.0f);
glVertex3f(5.0f, 0.0f, 0.0f);
glEnd();
SwapBuffers(g_hDC);
Invalidate(FALSE);
[/code]
This is so simple it is ridiculous. Running as shown above, I see a horizontal white line as expected. But, if I change the glTranslatef call in "OnPaint" to glTranslatef(0.0f, 0.0f, -2.0f) or anything smaller than a -1 shift in the z-direction the line disappears. What gives? This is really stumping me...so any help would be greatly appreciated.