OpenGL + MFC + glTranslated = black screen

Im trying to make a MFC + OpenGL application but when I’m using glTranslated(,,*) with values bigger than 1.0 then nothing gets drawn… Could you help me? Maybe I forgot some important initializations. Here is the initialization code and drawing code:

int MySetPixelFormat(HDC hdc)
{

PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};

int iPixelFormat;

// get the device context’s best, available pixel format match
if((iPixelFormat = ChoosePixelFormat(hdc, &pfd)) == 0)
{
MessageBox(NULL, “ChoosePixelFormat Failed”, NULL, MB_OK);
return 0;
}

// make that match the device context’s current pixel format
if(SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE)
{
MessageBox(NULL, “SetPixelFormat Failed”, NULL, MB_OK);
return 0;
}

return 1;
}
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}
glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,1000.0f);
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}

int COpenGL::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
MySetPixelFormat(::GetDC(m_hWnd));
HDC hdc = ::GetDC(m_hWnd);
HGLRC hglrc;

if (hglrc = wglCreateContext(hdc))
{
// try to make it the thread’s current rendering context
if(wglMakeCurrent(hdc, hglrc))
{

// TODO: Add your specialized creation code here
ReSizeGLScene(500, 300);
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LESS); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glEnable(GL_TEXTURE_2D); /* Enable 2D Texture Mapping */

}
}

wglMakeCurrent(NULL, NULL) ;
::ReleaseDC (m_hWnd, hdc) ;
wglDeleteContext(hglrc);
return 0;
}

void COpenGL::OnPaint()
{
CPaintDC dc(this); // device context for painting

// TODO: Add your message handler code here
HDC hdc = ::GetDC(m_hWnd);
HGLRC hglrc;

// TODO: Add your message handler code here
glClearColor(0,0,0,0);
glColor3f(1, 1, 1);

if (hglrc = wglCreateContext(hdc))
{
// try to make it the thread’s current rendering context
if(wglMakeCurrent(hdc, hglrc))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();// Reset The Current Modelview Matrix
glTranslated(0.0,0.0,-2.00000);
glBegin(GL_TRIANGLES);
glVertex3f(.5, 0, 0);
glVertex3f(0, .5, 0);
glVertex3f(-.5, 0, 0);
glEnd();
SwapBuffers(hdc);
}
}

wglMakeCurrent(NULL, NULL) ;
::ReleaseDC (m_hWnd, hdc) ;
wglDeleteContext(hglrc);
}

thank you

You’re calling wglCreateContext in OnPaint. for any number of reasons, you really shouldn’t be doing that. You created the RC in your OnCreate method; just keep it around in the CWnd class and set it to be current in OnPaint. Obviously, you shouldn’t delete the rendering context.

Because you don’t have an RC bound at all times, your ReSizeGLScene will only do something if there is an RC bound. Therefore, it can only be called fron OnPaint or OnCreate; not from OnSize. In case you’re processing WM_SIZE messages.

In any case, outside of this, I think your main problem is that you’re looking the wrong way. You translate 2.0 in the -z axis. If I recall (I don’t remember, as I always use gluLookAt to set the camera’s direction), GL’s basic viewing coordinate system looks down the -z axis. Which would mean, of course, that you are now rendering behind the camera.

[This message has been edited by Korval (edited 12-04-2002).]