VIZ++ Class: OpenGLRenderContext

 VIZ++ Class Library  VIZ++ Samples  VIZ++ ClassTree 

Source code

/*
 * OpenGLRenderContext.h 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2015/07/22
// 2016/07/01 Modified to use glewInit in OpenGLContext constructor.

#pragma once

#include <viz++/opengl/OpenGLObject.h>
#include <viz++/ClientDC.h>

namespace VIZ {

class OpenGLRenderContext :public OpenGLObject {
private:
  HDC    hDC;
  HGLRC  hGLRC;  //Handle to an OpengGL Render Context

public:
  //Modified to pass majorVersion and minorVersion of OpenGL
  OpenGLRenderContext(ClientDC* clientDC, int majorVersion=3, int minorVersion=1)
  :hDC(NULL),
  hGLRC(NULL)
  {
    if (clientDC == NULL) {
      throw IException("Invalid argument: ClientDC is NULL");  
    }
    hDC = clientDC -> get();
    
    if (hDC) {
      hGLRC = wglCreateContext(hDC);
      if (hGLRC == NULL) {
        throw IException("Failed to wglCreateContext: Error(%s)", errorString() );
      }
    } else {
      throw IException("Invalid argument: HDC is NULL");
    }
    makeCurrent();
    
    //<added date~"2016/07/01">
    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK) {
      throw IException("Failed to glewInit");
    }
    
    int attributes[] = {  
      WGL_CONTEXT_MAJOR_VERSION_ARB, majorVersion,
      WGL_CONTEXT_MINOR_VERSION_ARB, minorVersion,
      WGL_CONTEXT_FLAGS_ARB,         0,
      0,      
    };
    
    if (wglewIsSupported("WGL_ARB_create_context") == GL_TRUE) {
#ifdef SDEBUG
      char text[256];
      sprintf_s(text, sizeof(text), "Call WGL_ARB_create_context: majorVersion=%d, minorVersion=%d",
          majorVersion, minorVersion);
      MessageBox(NULL, text, "Debug", MB_OK);
#endif
      HGLRC  hNewRC = wglCreateContextAttribsARB(hDC, NULL, attributes); 
      if (hNewRC == NULL) {
      
        throw IException("Failed to WGL_ARB_create_context: majorVersion=%d, minorVersion=%d",
             majorVersion, minorVersion);
      }
      HGLRC  hPrevRC = hGLRC;
      wglMakeCurrent(hDC, NULL);  
      hGLRC = hNewRC; 
      makeCurrent();
      wglDeleteContext(hPrevRC);
    } else {
      MessageBox(NULL, "WGL_ARB_create_context is not supported", "Warning", MB_OK);
    }
    //</added>
  }

  ~OpenGLRenderContext()
  {
     wglMakeCurrent(hDC, NULL);
     wglDeleteContext(hGLRC);

     hDC   = NULL;
     hGLRC = NULL;
  }

  void makeCurrent()
  {
    if (wglMakeCurrent(hDC, hGLRC) == FALSE) {
      throw IException("Failed to wglMakeCurrent: Error(%s)", errorString());
    }
  }
};

}

Last modified: 10 Feb 2017

Copyright (c) 2009-2017 Antillia.com ALL RIGHTS RESERVED.