src/gl/OffscreenContextWGL.cc

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    
    /*
    
    Create an OpenGL context without creating an OpenGL Window. for Windows.
    
    For more info:
    
       http://www.nullterminator.net/opengl32.html by Blaine Hodge 
       http://msdn.microsoft.com/en-us/library/ee418815(v=vs.85).aspx
       http://www.cprogramming.com/tutorial/wgl_wiggle_functions.html by RoD
        ( which includes robot.cc by Steven Billington )
       http://blogs.msdn.com/b/oldnewthing/archive/2006/12/04/1205831.aspx by Tom
    */
    
    #undef NOGDI
    #include <windows.h>
    #include <vector>
    
    #include "OffscreenContext.h"
    #include "printutils.h"
    #include "imageutils.h"
    #include "system-gl.h"
    #include "fbo.h"
    
    #include <GL/gl.h> // must be included after glew.h
    
    #include <map>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    struct OffscreenContext
    {
      HWND window;
      HDC dev_context;
      HGLRC openGLContext;
      int width;
      int height;
      fbo_t *fbo;
    };
    
    #include "OffscreenContextAll.hpp"
    
    void offscreen_context_init(OffscreenContext &ctx, int width, int height)
    {
      ctx.window = (HWND)nullptr;
      ctx.dev_context = (HDC)nullptr;
      ctx.openGLContext = (HGLRC)nullptr;
      ctx.width = width;
      ctx.height = height;
      ctx.fbo = nullptr;
    }
    
    string get_os_info()
    {
      OSVERSIONINFO osvi;
    
      ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
      osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
      GetVersionEx(&osvi);
    
      SYSTEM_INFO si;
      GetSystemInfo(&si);
      map<WORD,const char*> archs;
      archs[PROCESSOR_ARCHITECTURE_AMD64] = "amd64";
      archs[PROCESSOR_ARCHITECTURE_IA64] = "itanium";
      archs[PROCESSOR_ARCHITECTURE_INTEL] = "x86";
      archs[PROCESSOR_ARCHITECTURE_UNKNOWN] = "unknown";
    
      stringstream out;
      out << "OS info: "
          << "Microsoft(TM) Windows(TM) " << osvi.dwMajorVersion << " "
          << osvi.dwMinorVersion << " " << osvi.dwBuildNumber << " "
          << osvi.szCSDVersion;
      if (archs.find(si.wProcessorArchitecture) != archs.end()) 
        out << " " << archs[si.wProcessorArchitecture];
      out << "\n";
    
      out << "Machine: " << si.dwProcessorType;
    
      return out.str();
    }
    
    string offscreen_context_getinfo(OffscreenContext * /*ctx*/)
    {
      // should probably get some info from WGL context here?
      stringstream out;
      out << "GL context creator: WGL\n"
          << "PNG generator: lodepng\n"
          << get_os_info();
      return out.str();
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) 
    {
      return DefWindowProc( hwnd, message, wparam, lparam );
    }
    
    bool create_wgl_dummy_context(OffscreenContext &ctx)
    {
      // this function alters ctx->window and ctx->openGLContext 
      //  and ctx->dev_context if successfull
    
      // create window
    
      HINSTANCE inst = GetModuleHandle(0);
      WNDCLASS wc;
      ZeroMemory( &wc, sizeof( wc ) );
      wc.style = CS_OWNDC;
      wc.lpfnWndProc = WndProc;
      wc.hInstance = inst;
      wc.lpszClassName = L"OpenSCAD";
      ATOM class_atom = RegisterClassW( &wc );
    
      if ( class_atom == 0 ) {
        cerr << "MS GDI - RegisterClass failed\n";
        cerr << "last-error code: " << GetLastError() << "\n";
        return false;
      }
    
      LPCTSTR lpClassName = L"OpenSCAD";
      LPCTSTR lpWindowName = L"OpenSCAD";
      DWORD dwStyle = WS_CAPTION | WS_POPUPWINDOW; // | WS_VISIBLE
      int x = 0;
      int y = 0;
      int nWidth = ctx.width;
      int nHeight = ctx.height;
      HWND hWndParent = nullptr;
      HMENU hMenu = nullptr;
      HINSTANCE hInstance = inst;
      LPVOID lpParam = nullptr;
    
      HWND window = CreateWindowW( lpClassName, lpWindowName, dwStyle, x, y,
        nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam );
    
      if ( window==nullptr ) {
        cerr << "MS GDI - CreateWindow failed\n";
        cerr << "last-error code: " << GetLastError() << "\n";
        return false;
      }
    
      // create WGL context, make current
    
      PIXELFORMATDESCRIPTOR pixformat;
      int chosenformat;
      HDC dev_context = GetDC( window );
      if ( dev_context == nullptr ) {
        cerr << "MS GDI - GetDC failed\n";
        cerr << "last-error code: " << GetLastError() << "\n";
        return false;
      }
    
      ZeroMemory( &pixformat, sizeof( pixformat ) );
      pixformat.nSize = sizeof( pixformat );
      pixformat.nVersion = 1;
      pixformat.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
      pixformat.iPixelType = PFD_TYPE_RGBA;
      pixformat.cGreenBits = 8;
      pixformat.cRedBits = 8;
      pixformat.cBlueBits = 8;
      pixformat.cAlphaBits = 8;
      pixformat.cDepthBits = 24;
      pixformat.cStencilBits = 8;
    
      chosenformat = ChoosePixelFormat( dev_context, &pixformat );
      if (chosenformat==0) {
        cerr << "MS GDI - ChoosePixelFormat failed\n";
        cerr << "last-error code: " << GetLastError() << "\n";
        return false;
      }
    
      bool spfok = SetPixelFormat( dev_context, chosenformat, &pixformat );
      if (!spfok) {
        cerr << "MS GDI - SetPixelFormat failed\n";
        cerr << "last-error code: " << GetLastError() << "\n";
        return false;
      }
    
      HGLRC gl_render_context = wglCreateContext( dev_context );
      if ( gl_render_context == nullptr ) {
          cerr << "MS WGL - wglCreateContext failed\n";
        cerr << "last-error code: " << GetLastError() << "\n";
          ReleaseDC( ctx.window, ctx.dev_context );
          return false;
      }
    
      bool mcok = wglMakeCurrent( dev_context, gl_render_context );
      if (!mcok) {
        cerr << "MS WGL - wglMakeCurrent failed\n";
        cerr << "last-error code: " << GetLastError() << "\n";
        return false;
      }
    
      ctx.window = window;
      ctx.dev_context = dev_context;
      ctx.openGLContext = gl_render_context;
    
      return true;
    }
    
    
    OffscreenContext *create_offscreen_context(int w, int h)
    {
      OffscreenContext *ctx = new OffscreenContext;
      offscreen_context_init( *ctx, w, h );
    
      // Before an FBO can be setup, a WGL context must be created. 
      // This call alters ctx->window and ctx->openGLContext 
      //  and ctx->dev_context if successfull
      if (!create_wgl_dummy_context( *ctx )) {
        return nullptr;
      }
    
      return create_offscreen_context_common( ctx );
    }
    
    bool teardown_offscreen_context(OffscreenContext *ctx)
    {
      if (ctx) {
        fbo_unbind(ctx->fbo);
        fbo_delete(ctx->fbo);
    
        wglMakeCurrent( nullptr, nullptr );
        wglDeleteContext( ctx->openGLContext );
        ReleaseDC( ctx->window, ctx->dev_context );
    
        return true;
      }
      return false;
    }
    
    bool save_framebuffer(OffscreenContext *ctx, std::ostream &output)
    {
      if (!ctx) return false;
      wglSwapLayerBuffers( ctx->dev_context, WGL_SWAP_MAIN_PLANE );
      return save_framebuffer_common( ctx, output );
    }