I was having some issues using a rendering worker thread in my application and created a stripped down application to troubleshoot, but stumbled onto a problem that is probably so obvious I am just completely overlooking it. The pertinent code is as follows:
BOOL GameDlg::OnInitDialog()
{
CDialog::OnInitDialog();
pDC = GetDC();
g_hDC = ::GetDC(this->GetSafeHwnd());
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);
return TRUE; // return TRUE unless you set the focus to a control
}
void GameDlg::OnSize(UINT nType, int cx, int cy)
{
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();
}
void GameDlg::OnPaint()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_LINES);
glVertex3f(-.5f, 0.0f, 0.0f);
glVertex3f(.5f, 0.0f, 0.0f);
glEnd();
SwapBuffers(g_hDC);
Invalidate(FALSE);
}
The key problem is that the camera is not where I expect, the glTranslatef() calls in OnPaint() do not do what I expect, or the viewing volume is incorrect. As is, the program displays nothing but a black screen. This is unexpected considering my call to glTranslate() before rendering that should move the line away 5 units, leaving me with a relatively short line but easily visible horizontal line. Even stranger, when I change the glTranslate call to glTranslatef(0.0f, 0.0f, -1.0f) I see a horizontal line centered vertically on the screen that is exactly half the width of the screen (it is as if x = -1 and x = 1 are at the left and right sides of the screen. But, if I change it to glTranslatef(0.0f, 0.0f, -2.0f) or any greater translation along the negative z-axis, the line disappears completely. I don't understand because I think I have set up my viewing volume to show a distance up to 1000 from the view, but it is acting like my viewing volume is only 1 unit deep. What is up with that?