help with OpenGL and MFC, please..

I can’t set up an OpenGL window properly. I have a doc/view application, and here’s what I do:
onPreCreate->I add WS_CLIPSIBLINGS and WS_CLIPCHILDREN
onCreate-> get a dc, choose and set pixel format, create and activate gl context;
onSize->setup OpenGL, enable GL_DEPTH_TEST,ecc
onDraw->clear buffers,draw the scene and call swapBuffers.
The problems are: first, the background color is always a light blue, doesn’t matter how I call glClearColor.
Second: when depth test is enabled I only get a light blue window and nothing is drawn.
Please, help. Quite desperate.

MFC stinx…

hello,

you setup OpenGL and depth test every OnSize message - do you need to do that ?? you’ll only need to do that once, probably in InitialUpdate or something, and only setting up the viewport and perspective in OnSize,since this is the only thing that changes in differnet window sizes. i dont know why you got a lightblue screen, maybe check your OnDraw code for that. And override EraseBackground(),only return TRUE,so Windows won’t erase your screen (OpenGL does it)

Could you post your init code so that we can spot the bug ?
What do you mean by initializing OpenGL in your “OnSize” ??? The proper init should be in the OnCreate member function…

Post your code so that we can help you !

Regards.

Eric

P.S. : To Igor, MFC does not “stinx”. It is a bit tricky to have OpenGL in MFC but when you managed to do it, MFC saves a lot of time !

here’s the source for the view class only.(everything else is unchanged). It’s a simple sdi doc/view project.
BOOL CGlScacchi3DView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS);

return CView::PreCreateWindow(cs);

}

/////////////////////////////////////////////////////////////////////////////
// CGlScacchi3DView drawing

void CGlScacchi3DView::OnDraw(CDC* pDC)
{
CGlScacchi3DDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

BOOL bResult = wglMakeCurrent (pDC->m_hDC, m_hGLContext);
if (!bResult){
	TRACE("wglMakeCurrent Failed %x

", GetLastError() ) ;
}

glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT | | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,20.0,0.0,0.0,0.0,0.0,1.0,0.0);

glDisable(GL_LIGHTING);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(10.0f,0.0f,0.0f);
glVertex3f(0.0f,10.0f,0.0f);
glEnd();

glFinish();
SwapBuffers(pDC->m_hDC);

// TODO: add draw code for native data here

}

/////////////////////////////////////////////////////////////////////////////
// CGlScacchi3DView diagnostics

#ifdef _DEBUG
void CGlScacchi3DView::AssertValid() const
{
CView::AssertValid();
}

void CGlScacchi3DView: ump(CDumpContext& dc) const
{
CView: ump(dc);
}

CGlScacchi3DDoc* CGlScacchi3DView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGlScacchi3DDoc)));
return (CGlScacchi3DDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CGlScacchi3DView message handlers

int CGlScacchi3DView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here

CClientDC cDC(this);

if (SetWindowPixelFormat(cDC.m_hDC)==FALSE)
	return -1;
if (CreateViewGLContext(cDC.m_hDC)==FALSE)
	return -1;

return 0;

}

BOOL CGlScacchi3DView::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;

memset(&pixelDesc, 0, sizeof(PIXELFORMATDESCRIPTOR)) ;

pixelDesc.nSize		= sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion	= 1;

pixelDesc.dwFlags	=	PFD_DRAW_TO_WINDOW | 
						PFD_SUPPORT_OPENGL | 
						PFD_DOUBLEBUFFER;  

pixelDesc.iPixelType		= PFD_TYPE_RGBA;
pixelDesc.cColorBits		= 32;
pixelDesc.cDepthBits		= 16;
pixelDesc.iLayerType		= PFD_MAIN_PLANE;

/*pixelDesc.cRedBits		= 8;
pixelDesc.cRedShift		= 0;
pixelDesc.cGreenBits		= 8;
pixelDesc.cGreenShift		= 0;
pixelDesc.cBlueBits		= 8;
pixelDesc.cBlueShift		= 0;
pixelDesc.cAlphaBits		= 0;
pixelDesc.cAlphaShift		= 0;
pixelDesc.cAccumBits		= 0;	
pixelDesc.cAccumRedBits		= 0;
pixelDesc.cAccumGreenBits	= 0;
pixelDesc.cAccumBlueBits	= 0;
pixelDesc.cAccumAlphaBits	= 0;
pixelDesc.cStencilBits		= 0;
pixelDesc.cAuxBuffers		= 0;
pixelDesc.bReserved		= 0;
pixelDesc.dwLayerMask		= 0;
pixelDesc.dwVisibleMask		= 0;
pixelDesc.dwDamageMask		= 0;*/

m_GLPixelIndex = ChoosePixelFormat( hDC, &pixelDesc);
if (m_GLPixelIndex==0) // Let's choose a default index.
{
	m_GLPixelIndex = 1;	
	if (DescribePixelFormat(hDC, 
					m_GLPixelIndex, 
					sizeof(PIXELFORMATDESCRIPTOR), 
					&pixelDesc)==0)
	{
		return FALSE;
	}
}

if (SetPixelFormat( hDC, 
			  m_GLPixelIndex, 
			  &pixelDesc)==FALSE)
{
	return FALSE;
}

return TRUE;

}

BOOL CGlScacchi3DView::CreateViewGLContext(HDC hDC)
{
m_hGLContext = wglCreateContext(hDC);
if (m_hGLContext == NULL)
{
return FALSE;
}

if (wglMakeCurrent(hDC, m_hGLContext)==FALSE)
{
	return FALSE;
}

return TRUE;

}

void CGlScacchi3DView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);

GLdouble vertex0[]={-5.0,0.0,-5.0};
GLdouble vertex1[]={0.0,0.0,5.0};
GLdouble vertex2[]={5.0,0.0,-5.0};

GLsizei width, height;
GLdouble aspect;

width = cx;
height = cy;

if (cy==0)
	aspect = (GLdouble)width;
else
	aspect = (GLdouble)width/(GLdouble)height;

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, aspect, 1.0, 500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glDrawBuffer(GL_BACK);
//glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepth(1.0f);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);


// TODO: Add your message handler code here

}

BOOL CGlScacchi3DView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
//return CView::OnEraseBkgnd(pDC);
}

Thanx alot!

Try :

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

The operators | | and | are not interchangeable (only the second one is bit-wise and that is what you want !).

Regards.

Eric

How stupid I am! anyway, you made me a happy man. I spent 3 days trying to figure out what was wrong. This forum is such a valuable help… Wow. I’ve coded an OpenGL chess game with glut, but I need MFC support for user interface. Well, it’s now time to work.