OpenGL with multiple Views in an MDI App (MFC)

Hello,

I have made an MDI application using the Visual C++ App Wizard and I have set up my View class to display a simple scene.

When i run the app the first frame+view is shown as per usual - this works fine. But when i select Window->New Window, A new window is displayed but it is blank.

here is the code for my on paint method:

void CBaseOpenGLMDIView::OnPaint()
{
CPaintDC dc(this);
CBaseOpenGLMDIDoc* pDoc = GetDocument();
wglMakeCurrent(m_pCDC->GetSafeHdc(), m_hGLContext);
pDoc->RenderScene();
SwapBuffers(m_pCDC->GetSafeHdc());
wglMakeCurrent(NULL,NULL);
}

I dont understand why, when i create a new instance of the view class by selecting Window->New Window, the new window is blank!?

i know this is more a MFC implementation question but i couldnt find anywhere else to ask.

thanks,
kev.

Do you use SetPixelFormat in the OnCreate member of your CView-derived class ? If not, you should !

Seriously, I believe this is where your problem lies : I suppose you have created a Rendering Context for the document itself. Now, each time another view is opened, you need to set its pixel format.

Here is a little schematic :

If first view
{
SetPixelFormat
CreateContext
}
else
{
SetPixelFormat
}

I am pretty sure that is why you have this blank screen ! Come back if it does not work !

Regards.

Eric

I have already done that

i set the pixel format and create the opengl RC…

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

m_pCDC= new CClientDC(this);

if (SetWindowPixelFormat(m_pCDC->GetSafeHdc())==FALSE)       
	return 0;

if (CreateViewGLContext(m_pCDC->GetSafeHdc())==FALSE)
	return 0;
return 0;

}

Well, do you have one RC per view or one RC per document ?!?

You should post the whole code that concerns your initializations… I have done a lot of MDI apps using OpenGL so I should be able to spot the fault.

Best regards.

Eric

Ok - here goes.
These are all the initialisations.

All members belong to the view - the document has nothing apart from the RenderScence() method which draws a a sphere.

BOOL CBaseOpenGLMDIView::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
cs.lpszClass = AfxRegisterWndClass(CS_OWNDC | CS_HREDRAW | CS_VREDRAW);

return CView::PreCreateWindow(cs);

}
BOOL CBaseOpenGLMDIView::CreateViewGLContext(HDC hDC)
{
m_hGLContext = wglCreateContext(hDC);

if (m_hGLContext == NULL)    
{
    return FALSE;    
}   

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

return TRUE;

}

BOOL CBaseOpenGLMDIView::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER |
PFD_STEREO_DONTCARE;

pixelDesc.iPixelType  = PFD_TYPE_RGBA;
pixelDesc.cColorBits  = 32;   
pixelDesc.cRedBits    = 8;
pixelDesc.cRedShift   = 16;   
pixelDesc.cGreenBits  = 8;
pixelDesc.cGreenShift = 8;  
pixelDesc.cBlueBits   = 8;
pixelDesc.cBlueShift  = 0;    
pixelDesc.cAlphaBits  = 0;
pixelDesc.cAlphaShift = 0;   
pixelDesc.cAccumBits  = 64;    
pixelDesc.cAccumRedBits    = 16;    
pixelDesc.cAccumGreenBits  = 16;
pixelDesc.cAccumBlueBits   = 16;  
pixelDesc.cAccumAlphaBits  = 0;
pixelDesc.cDepthBits       = 32;  
pixelDesc.cStencilBits     = 8;
pixelDesc.cAuxBuffers    = 0;
pixelDesc.iLayerType     = PFD_MAIN_PLANE;
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;

}

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

m_pCDC= new CClientDC(this);

if (SetWindowPixelFormat(m_pCDC->GetSafeHdc())==FALSE)       
	return 0;

if (CreateViewGLContext(m_pCDC->GetSafeHdc())==FALSE)
	return 0;
return 0;

}

void CBaseOpenGLMDIView::OnSize(UINT nType, int cx, int cy)
{

CView::OnSize(nType, cx, cy);

GLsizei w,h;
GLdouble aspect;

w = cx;
h = cy;

if (cy==0)
	aspect = (GLdouble)w;
else
	aspect = (GLdouble)w/(GLdouble)h;

wglMakeCurrent(m_pCDC->GetSafeHdc(), m_hGLContext);

glClearColor(1.0,1.0,1.0,1.0);

glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20, aspect, 1, m_viewDepth);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glDrawBuffer(GL_BACK);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);

wglMakeCurrent(NULL,NULL);

}

void CBaseOpenGLMDIView::OnPaint()
{
CPaintDC dc(this);
CBaseOpenGLMDIDoc* pDoc = GetDocument();

wglMakeCurrent(m_pCDC->GetSafeHdc(), m_hGLContext);

pDoc->RenderScene();
SwapBuffers(m_pCDC->GetSafeHdc());

wglMakeCurrent(NULL,NULL);

}

thanks,
kev.

Crappy microsofts crappy extensions!!!

Ok - i have fixed the problem - in fact there was no problem at all. It was the first time i tried to get opengl working with in an MDI environmnent - i figured what i did would work but for some reson it didnt.

I got a copy of CGLEnabledView - which i new worked. I tried my example using this class and it still didnt work!!??

The problem - it turned out - was that i was using a auxSolidSphere for my test scene.
i changed it to a glut solid sphere and everything worked just fine!! multiple views 'n all.

why is there this somewhat obscure problem with the glaux32 library???

Eric: thanks for your help - i guess i should have used a more stable library - i never normaly use glaux anyway its just i had ‘lost’ the glut dll’s and i could be bothered to get them again.

kev.

Don’t worry kev : I am happy to help when I can (I must say that I was trying to spot any problem in your code and could not find anything : I was starting to wonder if I knew MFC/OpenGL that well !).

Do not hesitate to contact me for any other problem (I think I met a great number of possible problems when trying to have MFC and OpenGL altogether !).

Regards.

Eric

P.S. : I am glad you found a solution to your problem !