| #pragma once |
| |
| #ifndef OFFSCREENGL_H |
| #define OFFSCREENGL_H |
| #if defined(LINUX) || defined(FREEBSD) || defined(MACOSX) |
| #include "tofflinegl.h" |
| #endif |
| |
| class OffScreenGL { |
| public: |
| OffScreenGL(int width, int height, int bpp, int stencilBpp = 0) { |
| |
| |
| |
| |
| |
| #ifdef _WIN32 |
| initBITMAPINFO(m_info, width, height, bpp); |
| |
| m_offData = 0; |
| m_offHdc = |
| GetDC(NULL); |
| |
| m_offDIB = |
| CreateDIBSection(m_offHdc, &m_info, DIB_RGB_COLORS, &m_offData, NULL, |
| 0); |
| |
| assert(m_offDIB); |
| assert(m_offData); |
| |
| m_offDC = CreateCompatibleDC(m_offHdc); |
| |
| m_oldobj = |
| SelectObject(m_offDC, m_offDIB); |
| |
| static PIXELFORMATDESCRIPTOR pfd = { |
| sizeof(PIXELFORMATDESCRIPTOR), |
| 1, |
| 0 | (false ? (0) : (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI)) | |
| PFD_SUPPORT_OPENGL, |
| PFD_TYPE_RGBA, |
| bpp, |
| 0, |
| 0, 0, 0, 0, 0, |
| bpp >> 2, |
| 0, |
| 0, |
| 0, 0, 0, 0, |
| 0, |
| stencilBpp, |
| 0, |
| PFD_MAIN_PLANE, |
| 0, |
| 0, 0, 0 |
| }; |
| |
| |
| int iPixelFormat = ChoosePixelFormat(m_offDC, &pfd); |
| assert(iPixelFormat != 0); |
| |
| |
| int ret = SetPixelFormat(m_offDC, iPixelFormat, &pfd); |
| DWORD dw = GetLastError(); |
| assert(ret == TRUE); |
| |
| |
| m_hglRC = wglCreateContext(m_offDC); |
| assert(m_hglRC); |
| |
| |
| |
| |
| #else |
| #if defined(LINUX) || defined(FREEBSD) || defined(MACOSX) |
| m_offlineGL = new TOfflineGL(TDimension(width, height)); |
| #endif |
| #endif |
| } |
| |
| ~OffScreenGL() { |
| |
| |
| |
| |
| |
| #ifdef _WIN32 |
| wglDeleteContext(m_hglRC); |
| |
| |
| SelectObject(m_offDC, m_oldobj); |
| |
| DeleteObject(m_offDC); |
| DeleteObject(m_offDIB); |
| DeleteObject(m_offHdc); |
| #else |
| #if defined(LINUX) || defined(FREEBSD) || defined(MACOSX) |
| |
| delete m_offlineGL; |
| #endif |
| #endif |
| } |
| |
| public: |
| |
| #ifdef _WIN32 |
| void initBITMAPINFO(BITMAPINFO &info, int width, int height, int bpp) { |
| memset(&info, 0, sizeof(BITMAPINFOHEADER)); |
| |
| info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
| info.bmiHeader.biWidth = width; |
| info.bmiHeader.biHeight = height; |
| info.bmiHeader.biPlanes = 1; |
| info.bmiHeader.biBitCount = bpp; |
| info.bmiHeader.biCompression = BI_RGB; |
| info.bmiHeader.biSizeImage = 0; |
| info.bmiHeader.biXPelsPerMeter = 1000; |
| info.bmiHeader.biYPelsPerMeter = 1000; |
| info.bmiHeader.biClrUsed = 0; |
| info.bmiHeader.biClrImportant = 0; |
| } |
| |
| void *m_offData; |
| HDC m_offHdc; |
| HBITMAP m_offDIB; |
| HDC m_offDC; |
| HGDIOBJ m_oldobj; |
| HGLRC m_hglRC; |
| BITMAPINFO m_info; |
| #else |
| #if defined(LINUX) || defined(FREEBSD) || defined(MACOSX) |
| TOfflineGL *m_offlineGL; |
| #endif |
| #endif |
| }; |
| |
| #endif |