Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef OFFSCREENGL_H
Toshihiro Shimizu 890ddd
#define OFFSCREENGL_H
Rozhuk Ivan ac51ab
#if defined(LINUX) || defined(FREEBSD) || defined(MACOSX)
Toshihiro Shimizu 890ddd
#include "tofflinegl.h"
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class OffScreenGL {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  OffScreenGL(int width, int height, int bpp, int stencilBpp = 0) {
Shinya Kitaoka 120a6e
// -----------------------------------------------------------------------------------
Shinya Kitaoka 120a6e
// //
Shinya Kitaoka 120a6e
// creo il contesto openGL //
Shinya Kitaoka 120a6e
// -----------------------------------------------------------------------------------
Shinya Kitaoka 120a6e
// //
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
    initBITMAPINFO(m_info, width, height, bpp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_offData = 0;  // a pointer to buffer
Shinya Kitaoka 120a6e
    m_offHdc  =     // open an offscreen device
Shinya Kitaoka 120a6e
        GetDC(NULL);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_offDIB =  // and a bitmap image
Shinya Kitaoka 120a6e
        CreateDIBSection(m_offHdc, &m_info, DIB_RGB_COLORS, &m_offData, NULL,
Shinya Kitaoka 120a6e
                         0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    assert(m_offDIB);
Shinya Kitaoka 120a6e
    assert(m_offData);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_offDC = CreateCompatibleDC(m_offHdc);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_oldobj =  // select BIB to write
Shinya Kitaoka 120a6e
        SelectObject(m_offDC, m_offDIB);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    static PIXELFORMATDESCRIPTOR pfd = {
Shinya Kitaoka 120a6e
        sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
Shinya Kitaoka 120a6e
        1,                              // version number
Shinya Kitaoka 120a6e
        0 | (false ? (0) : (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI)) |
Shinya Kitaoka 120a6e
            PFD_SUPPORT_OPENGL,  // support OpenGL
Shinya Kitaoka 120a6e
        PFD_TYPE_RGBA,           // RGBA type
Shinya Kitaoka 120a6e
        bpp,                     // 32-bit color depth
Shinya Kitaoka 120a6e
        0,
Shinya Kitaoka 120a6e
        0, 0, 0, 0, 0,   // color bits ignored
Shinya Kitaoka 120a6e
        bpp >> 2,        // alpha buffer
Shinya Kitaoka 120a6e
        0,               // shift bit ignored
Shinya Kitaoka 120a6e
        0,               // no accumulation buffer
Shinya Kitaoka 120a6e
        0, 0, 0, 0,      // accum bits ignored
Shinya Kitaoka 120a6e
        0,               // 32-bit z-buffer
Shinya Kitaoka 120a6e
        stencilBpp,      // no stencil buffer
Shinya Kitaoka 120a6e
        0,               // no auxiliary buffer
Shinya Kitaoka 120a6e
        PFD_MAIN_PLANE,  // main layer
Shinya Kitaoka 120a6e
        0,               // reserved
Shinya Kitaoka 120a6e
        0, 0, 0          // layer masks ignored
Shinya Kitaoka 120a6e
    };
Shinya Kitaoka 120a6e
    // std::cout << "bit depth = " << v << std::endl;
Shinya Kitaoka 120a6e
    // get the best available match of pixel format for the device context
Shinya Kitaoka 120a6e
    int iPixelFormat = ChoosePixelFormat(m_offDC, &pfd);
Shinya Kitaoka 120a6e
    assert(iPixelFormat != 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // make that the pixel format of the device context
Shinya Kitaoka 120a6e
    int ret  = SetPixelFormat(m_offDC, iPixelFormat, &pfd);
Shinya Kitaoka 120a6e
    DWORD dw = GetLastError();
Shinya Kitaoka 120a6e
    assert(ret == TRUE);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // make a valid context for OpenGL rendering
Shinya Kitaoka 120a6e
    m_hglRC = wglCreateContext(m_offDC);
Shinya Kitaoka 120a6e
    assert(m_hglRC);
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 120a6e
          ret = wglMakeCurrent( m_offDC, m_hglRC );
Shinya Kitaoka 120a6e
          assert(ret == TRUE);
Toshihiro Shimizu 890ddd
    */
Toshihiro Shimizu 890ddd
#else
Rozhuk Ivan ac51ab
#if defined(LINUX) || defined(FREEBSD) || defined(MACOSX)
Shinya Kitaoka 120a6e
    m_offlineGL = new TOfflineGL(TDimension(width, height));
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ~OffScreenGL() {
Shinya Kitaoka 120a6e
// -----------------------------------------------------------------------------------
Shinya Kitaoka 120a6e
// //
Shinya Kitaoka 120a6e
// cancello il contesto openGL //
Shinya Kitaoka 120a6e
// -----------------------------------------------------------------------------------
Shinya Kitaoka 120a6e
// //
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
    wglDeleteContext(m_hglRC);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    // release object
Shinya Kitaoka 120a6e
    SelectObject(m_offDC, m_oldobj);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    DeleteObject(m_offDC);
Shinya Kitaoka 120a6e
    DeleteObject(m_offDIB);
Shinya Kitaoka 120a6e
    DeleteObject(m_offHdc);
Toshihiro Shimizu 890ddd
#else
Rozhuk Ivan ac51ab
#if defined(LINUX) || defined(FREEBSD) || defined(MACOSX)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    delete m_offlineGL;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
// init a BITMAPINFO structure
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
  void initBITMAPINFO(BITMAPINFO &info, int width, int height, int bpp) {
Shinya Kitaoka 120a6e
    memset(&info, 0, sizeof(BITMAPINFOHEADER));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    info.bmiHeader.biSize          = sizeof(BITMAPINFOHEADER);
Shinya Kitaoka 120a6e
    info.bmiHeader.biWidth         = width;
Shinya Kitaoka 120a6e
    info.bmiHeader.biHeight        = height;
Shinya Kitaoka 120a6e
    info.bmiHeader.biPlanes        = 1;
Shinya Kitaoka 120a6e
    info.bmiHeader.biBitCount      = bpp;
Shinya Kitaoka 120a6e
    info.bmiHeader.biCompression   = BI_RGB;
Shinya Kitaoka 120a6e
    info.bmiHeader.biSizeImage     = 0;
Shinya Kitaoka 120a6e
    info.bmiHeader.biXPelsPerMeter = 1000;
Shinya Kitaoka 120a6e
    info.bmiHeader.biYPelsPerMeter = 1000;
Shinya Kitaoka 120a6e
    info.bmiHeader.biClrUsed       = 0;
Shinya Kitaoka 120a6e
    info.bmiHeader.biClrImportant  = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void *m_offData;
Shinya Kitaoka 120a6e
  HDC m_offHdc;
Shinya Kitaoka 120a6e
  HBITMAP m_offDIB;
Shinya Kitaoka 120a6e
  HDC m_offDC;
Shinya Kitaoka 120a6e
  HGDIOBJ m_oldobj;
Shinya Kitaoka 120a6e
  HGLRC m_hglRC;
Shinya Kitaoka 120a6e
  BITMAPINFO m_info;
Toshihiro Shimizu 890ddd
#else
Rozhuk Ivan ac51ab
#if defined(LINUX) || defined(FREEBSD) || defined(MACOSX)
Shinya Kitaoka 120a6e
  TOfflineGL *m_offlineGL;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif