Is it possible to have openGL in 2 windows? as in 2 different windows (lets say the first is 640x480 and the other is 1024x768) rendering different things (lets say one window i
I've done multiple OpenGL windows in an MFC application before. Here's a class you might find useful: since there can only be one current render context in the UI thread at a time, I wrote a class wrapper to make managing it easier.
SaveRestoreRC.h
// this class helps to manage multiple RCs using an RAII design pattern
class CSaveRestoreRC
{
public:
HDC oldDC;
HGLRC oldRC;
CSaveRestoreRC(HDC hDC, HGLRC hRC);
~CSaveRestoreRC(void);
};
SaveRestoreRC.cpp:
CSaveRestoreRC::CSaveRestoreRC(HDC hDC, HGLRC hRC)
{
ASSERT( hDC );
ASSERT( hRC );
oldDC = wglGetCurrentDC();
oldRC = wglGetCurrentContext();
BOOL result = wglMakeCurrent( hDC, hRC );
ASSERT( result );
}
CSaveRestoreRC::~CSaveRestoreRC(void)
{
if( !oldRC )
return;
ASSERT( oldDC );
BOOL result = wglMakeCurrent( oldDC, oldRC );
ASSERT( result );
}
Now derive a class from CWnd and add these member variables:
class COpenGLControl : public CWnd
{
// used to interface OpenGL with Windows
HDC hdc;
HGLRC hrc;
// ...
int COpenGLControl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// Get device context only once.
hdc = GetDC()->m_hDC;
// ... ChoosePixelFormat, SetPixelFormat, etc. here.
// Create the OpenGL Rendering Context.
hrc = wglCreateContext(hdc);
ASSERT( hrc );
Then in every member function where you call ANY OpenGL commands, use CSaveRestoreRC so that your current render context doesn't get screwed up.
void COpenGLControl::UpdateCamera()
{
CSaveRestoreRC c(hdc, hrc);
// Map the OpenGL device coordinates.
glViewport(0, 0, renderingWindow.Width(), renderingWindow.Height());
// Do your other OpenGL stuff here
// ...
// the CSaveRestoreRC destructor will automatically put the correct render context back,
// even if you call other functions. Of course, put another CSaveRestoreRC there too.
}