Click to See Complete Forum and Search --> : OpenGL print and Print Preview


Roger Allen
September 14th, 1999, 10:56 AM
Hi All,
I am posting again, because my earlier one got cropped some how.

As before my problem is that my OpenGL graph doesn't get plotted on a DIB which I then StretchDIB to the print preview DC and print DC.

Here is some of the code in question. m_hDC is the DC for the window used to display the OpenGL graph.


bool COpenGLCtrl::RenderToDIB(CRect size)
{
if (!m_bBitmapRendered)
{
m_hMemDC = CreateCompatibleDC(m_hDC);
m_hPrintBitmap = CreateDIBSurface(size);
m_hOldPrintBitmap = (HBITMAP)::SelectObject(m_hMemDC, m_hPrintBitmap);
if (!PrepareDIBSurface())
AfxMessageBox("The pixel format of the memory DC is not set properly\nError");
m_hPrinterRC = wglCreateContext(m_hMemDC);
TRACE("RenderToDIB wglMakeCurrent()\n") ;
wglMakeCurrent(m_hMemDC, m_hPrinterRC);
OnPrint(m_hMemDC, size) ;
m_bBitmapRendered = true ;
}
return true ;
}

bool COpenGLCtrl::SetRenderState(bool new_state)
{
bool bOld ;

bOld = m_bBitmapRendered ;
if (!new_state)
{
::SelectObject(m_hMemDC, m_hOldPrintBitmap) ;
::DeleteObject(m_hPrintBitmap) ;
::DeleteDC(m_hMemDC) ;
m_hMemDC = NULL ;
wglDeleteContext(m_hPrinterRC);
m_hPrinterRC = NULL ;
}
m_bBitmapRendered = new_state ;
return bOld ;
}

bool COpenGLCtrl::RenderToDevice(CDC *pDC, CRect size)
{
if (m_bBitmapRendered)
{
HPALETTE old ;
old = SelectPalette(pDC->m_hDC, hPal, FALSE) ;
::StretchDIBits(pDC->m_hDC,
size.left,
size.top,
m_pbi->bmiHeader.biWidth,
m_pbi->bmiHeader.biHeight,
0,
0,
m_pbi->bmiHeader.biWidth,
m_pbi->bmiHeader.biHeight,
m_lpBits,
(BITMAPINFO *)m_pbi,
DIB_RGB_COLORS,
SRCCOPY) ;
SelectPalette(m_hDC, old, FALSE);
}
return true ;
}

bool COpenGLCtrl::GetRenderState()
{
return m_bBitmapRendered ;
}

HBITMAP COpenGLCtrl::CreateDIBSurface(CRect size)
{
HBITMAP hBmRet = NULL;
m_pbi = (BITMAPINFO *)biInfo;

ZeroMemory(m_pbi, sizeof(BITMAPINFO));

if (!m_hDC)
return NULL;

TRACE("CreateDIBSurface Size is %1d by %1d\n", size.Width(), size.Height()) ;
m_pbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_pbi->bmiHeader.biWidth = size.Width();
m_pbi->bmiHeader.biHeight = size.Height();
m_pbi->bmiHeader.biPlanes = 1;
m_pbi->bmiHeader.biBitCount = GetDeviceCaps(m_hDC, PLANES) * GetDeviceCaps(m_hDC, BITSPIXEL);
m_pbi->bmiHeader.biCompression = BI_RGB;
m_pbi->bmiHeader.biSizeImage = WIDTHBYTES((DWORD)m_pbi->bmiHeader.biWidth * m_pbi->bmiHeader.biBitCount) * m_pbi->bmiHeader.biHeight;

hBmRet = CreateDIBSection(m_hDC, m_pbi, DIB_RGB_COLORS, &m_lpBits, NULL, (DWORD)0);

return hBmRet;
}

BOOL COpenGLCtrl::PrepareDIBSurface(void)
{
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW|PFD_DRAW_TO_BITMAP| PFD_SUPPORT_OPENGL | PFD_SUPPORT_GDI,
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
};
BOOL bRet = TRUE;

int nIndex;

pfd.cColorBits = GetDeviceCaps(m_hDC, PLANES) * GetDeviceCaps(m_hDC, BITSPIXEL);
nIndex = ChoosePixelFormat(m_hMemDC, &pfd) ;
if (!nIndex)
bRet = FALSE;

DescribePixelFormat(m_hMemDC, nIndex, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
if (!SetPixelFormat(m_hMemDC, nIndex, &pfd))
bRet = FALSE;

if (bRet && pfd.dwFlags & PFD_NEED_PALETTE)
CreateRGBPalette();

return bRet;
}

//***********************************************************************
// Function: CreateRGBPalette
//
// Purpose: Creates a logical palette
//
// Parameters:
// void
//
// Returns:
// void
//
//**********************************************************************
void COpenGLCtrl::CreateRGBPalette(void)
{
PIXELFORMATDESCRIPTOR pfd;
LOGPALETTE *pPal;
int n, i;
BYTE byRedMask, byGreenMask, byBlueMask;
m_pbi = (BITMAPINFO *)biInfo;

n = GetPixelFormat(m_hMemDC);
DescribePixelFormat(m_hMemDC, n, sizeof(PIXELFORMATDESCRIPTOR), &pfd);

if (pfd.dwFlags & PFD_NEED_PALETTE) {
n = 1 << pfd.cColorBits;

pPal = (PLOGPALETTE)VirtualAlloc(NULL, sizeof(LOGPALETTE) +
n * sizeof(PALETTEENTRY), MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
pPal->palVersion = 0x300;
pPal->palNumEntries = n;

byRedMask = (1<<pfd.cRedBits) -1;
byGreenMask =(1<<pfd.cGreenBits) -1;
byBlueMask = (1<<pfd.cBlueBits) -1;
for (i=0; i<n; i++) {
m_pbi->bmiColors[ i ].rgbRed = pPal->palPalEntry[ i ].peRed =
(((i>>pfd.cRedShift) & byRedMask) * 255) /byRedMask;
m_pbi->bmiColors[ i ].rgbGreen = pPal->palPalEntry[ i ].peGreen =
(((i>>pfd.cGreenShift) & byGreenMask) * 255) /byGreenMask;
m_pbi->bmiColors[ i ].rgbBlue = pPal->palPalEntry[ i ].peBlue =
(((i>>pfd.cBlueShift) & byBlueMask) * 255) /byBlueMask;
m_pbi->bmiColors[ i ].rgbReserved = pPal->palPalEntry[ i ].peFlags = 0;
}
SetDIBColorTable(m_hMemDC, 0, n, m_pbi->bmiColors);
if (hPal) DeleteObject(hPal);
hPal = CreatePalette(pPal);
VirtualFree(pPal, 0, MEM_RELEASE);

hPalOld = SelectPalette(m_hDC, hPal, FALSE);
RealizePalette(m_hDC);
SelectPalette(m_hDC, hPalOld, FALSE);
}
}




I call the plot code as follows :


m_Control.RenderToDIB(size) ;
m_Control.RenderToDevice(pDC, size) ;



Where size is the Coordinates of the plot recangle from which the DIB is created etc.

Is there a standard reason why, when I try and plot the OpenGL call list on the MemDC, which has the DIB seleced into it, that I only get the background erased (to the correct colour), but no OpenGL graphics commands ?

Any help would be very appreciated, I have been trying to solve this one for 2 weeks, and my deadline is tonight!

Thanks for any advice....


Roger Allen aka [CB]RIGamortis, the quake2 player.