コード例 #1
0
 void createBufferObject()
 {
   VL_CHECK_OGL();
   VL_CHECK(Has_BufferObject)
   if (Has_BufferObject && handle() == 0)
   {
     VL_CHECK(mByteCountBufferObject == 0)
     VL_glGenBuffers( 1, &mHandle ); VL_CHECK_OGL();
     mByteCountBufferObject = 0;
     VL_CHECK(handle())
   }
    /** 
      * Calls glBindFramebuffer(target, 0) thus activating the the framebuffer 0, that is, the normal OpenGL buffers.
      * \note This method is overridden in FBORenderTarget in order to activate the appropriate framebuffer object.
      */
    virtual void bindFramebuffer(EFrameBufferBind target = FBB_FRAMEBUFFER)
    {
      VL_CHECK_OGL()

      // the base render target is the framebuffer 0, that is, the normal OpenGL buffers
      VL_glBindFramebuffer(target, 0); VL_CHECK_OGL()

      // bind draw buffers
      bindDrawBuffers();

      // bind read buffer
      bindReadBuffer();

      VL_CHECK_OGL()
    }
コード例 #3
0
  inline std::string getOpenGLExtensions()
  {
    std::string ext;
    if (Has_GL_Version_3_0||Has_GL_Version_4_0)
    {
      int count = 0;
      glGetIntegerv(GL_NUM_EXTENSIONS, &count);
      for( int i=0; i<count; ++i )
      {
        ext += std::string((char*)glGetStringi(GL_EXTENSIONS, i)) + " ";
        VL_CHECK_OGL();
      }
    }
    else
    {
      VL_CHECK(glGetString(GL_EXTENSIONS));
      ext = (const char*)glGetString(GL_EXTENSIONS);
      // make sure also the last extension ends with a space
      ext.push_back(' ');
    }

    return ext;
  }
コード例 #4
0
//-----------------------------------------------------------------------------
bool vl::initializeOpenGL()
{
  Is_OpenGL_Initialized = false;

  // clear errors
  glGetError();

  // check OpenGL context is present
  if (glGetError() != GL_NO_ERROR)
    return false;

  // - - - OpenGL function pointers - - -

  // opengl function pointer initialization
  #if defined(VL_OPENGL)
    #define VL_GL_FUNCTION(TYPE, NAME) NAME = (TYPE)getGLProcAddress(#NAME);
    #include <vlGraphics/GL/GLFunctionList.hpp>
  #endif

  // opengl function pointer initialization
  #if defined(VL_OPENGL_ES1)
    #define VL_GL_FUNCTION(TYPE, NAME) NAME = (TYPE)getGLProcAddress(#NAME);
    #include <vlGraphics/GL/GLES1FunctionList.hpp>
  #endif

  // opengl function pointer initialization
  #if defined(VL_OPENGL_ES2)
    #define VL_GL_FUNCTION(TYPE, NAME) NAME = (TYPE)getGLProcAddress(#NAME);
    #include <vlGraphics/GL/GLES2FunctionList.hpp>
  #endif

  // - - - OpenGL versions - - -

  // GLES detect
  #if defined(VL_OPENGL_ES1)
    Has_GLES = Has_GLES_Version_1_1 = true;
  #endif

  #if defined(VL_OPENGL_ES2)
    Has_GLES = Has_GLES_Version_2_0 = true;
  #endif

  // GL versions
  // OpenGL ES returns "OpenGL ES-XX N.M"
  const char* version_string = (const char*)glGetString(GL_VERSION);

  // These stay zero for GLES
  const int vmaj = Has_GLES ? 0 : version_string[0] - '0';
  const int vmin = Has_GLES ? 0 : version_string[2] - '0';

  // Check fixed function pipeline
#if defined(VL_OPENGL_ES2)
  Is_OpenGL_Core_Profile = false;
  Is_OpenGL_Forward_Compatible = false;
  Has_Fixed_Function_Pipeline = false;
#elif defined(VL_OPENGL_ES1)
  Is_OpenGL_Core_Profile = false;
  Is_OpenGL_Forward_Compatible = false;
  Has_Fixed_Function_Pipeline = true;
#else
  Is_OpenGL_Forward_Compatible = false;
  if( vmaj >= 3 )
  {
    int forward_compatible = 0;
    glGetIntegerv( GL_CONTEXT_FLAGS, &forward_compatible ); VL_CHECK_OGL();
    Is_OpenGL_Forward_Compatible = (forward_compatible & GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT) != 0;
  }

  Is_OpenGL_Core_Profile = false;
  const int version = vmaj*10 + vmin;
  if( version >= 32 )
  {
    // Valid for WGL and GLX
    #define CONTEXT_CORE_PROFILE_BIT          0x00000001
    #define CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002
    #define CONTEXT_PROFILE_MASK              0x9126

    // Note: 
    // - This should be non-0 by when using wglCreateContextAttribs() and is == 0 when creating a GL context in the old way.
    // - Creating a context in the old way returns the highest compatible OpenGL version available thus the presence 
    //   of CONTEXT_COMPATIBILITY_PROFILE_BIT is not enough, we need to check the absence of CONTEXT_CORE_PROFILE_BIT
    int context_flags = 0;
    glGetIntegerv( CONTEXT_PROFILE_MASK, &context_flags ); VL_CHECK_OGL();
    Is_OpenGL_Core_Profile = (context_flags & CONTEXT_CORE_PROFILE_BIT) != 0;
  }

  Has_Fixed_Function_Pipeline = !Is_OpenGL_Forward_Compatible && !Is_OpenGL_Core_Profile;
#endif

  Has_GL_Version_1_1 = (vmaj == 1 && vmin >= 1) || (vmaj > 1 && Has_Fixed_Function_Pipeline);
  Has_GL_Version_1_2 = (vmaj == 1 && vmin >= 2) || (vmaj > 1 && Has_Fixed_Function_Pipeline);
  Has_GL_Version_1_3 = (vmaj == 1 && vmin >= 3) || (vmaj > 1 && Has_Fixed_Function_Pipeline);
  Has_GL_Version_1_4 = (vmaj == 1 && vmin >= 4) || (vmaj > 1 && Has_Fixed_Function_Pipeline);
  Has_GL_Version_1_5 = (vmaj == 1 && vmin >= 5) || (vmaj > 1 && Has_Fixed_Function_Pipeline);
  Has_GL_Version_2_0 = (vmaj == 2 && vmin >= 0) || (vmaj > 2 && Has_Fixed_Function_Pipeline);
  Has_GL_Version_2_1 = (vmaj == 2 && vmin >= 1) || (vmaj > 2 && Has_Fixed_Function_Pipeline);
  Has_GL_Version_3_0 = (vmaj == 3 && vmin >= 0) || (vmaj > 3 && Has_Fixed_Function_Pipeline);
  Has_GL_Version_3_1 = (vmaj == 3 && vmin >= 1) || (vmaj > 3 && Has_Fixed_Function_Pipeline);
  Has_GL_Version_3_2 = (vmaj == 3 && vmin >= 2) || (vmaj > 3 && Has_Fixed_Function_Pipeline);
  Has_GL_Version_3_3 = (vmaj == 3 && vmin >= 3) || (vmaj > 3 && Has_Fixed_Function_Pipeline);
  Has_GL_Version_4_0 = (vmaj == 4 && vmin >= 0) || (vmaj > 4 && Has_Fixed_Function_Pipeline);
  Has_GL_Version_4_1 = (vmaj == 4 && vmin >= 1) || (vmaj > 4 && Has_Fixed_Function_Pipeline);

  // - - - Extension strings init - - -

  std::string extensions = getOpenGLExtensions();

  #define VL_EXTENSION(extension) Has_##extension = strstr(extensions.c_str(), #extension" ") != NULL;
    #include <vlGraphics/GL/GLExtensionList.hpp>
  #undef VL_EXTENSION

  #define VL_GLES_EXTENSION(extension) Has_##extension = strstr(extensions.c_str(), #extension" ") != NULL;
    #include <vlGraphics/GL/GLESExtensionList.hpp>
  #undef VL_GLES_EXTENSION

#if defined(VL_OPENGL_ES1)
  // mic fixme
  // POWERVR emulator bugs: http://www.imgtec.com/forum/forum_posts.asp?TID=1379
  if (Has_GL_OES_texture_cube_map && glTexGenfOES == 0)
  {
    Has_GL_OES_texture_cube_map = false;
    Has_Cubemap_Textures = false;
    Log::error("GL_OES_texture_cube_map exposed but glTexGenfOES == NULL!\n"); /*VL_TRAP();*/
  }
  if(Has_GL_OES_blend_func_separate && glBlendFuncSeparateOES == 0)
  {
    Has_GL_OES_blend_func_separate = false;
    Log::error("GL_OES_blend_func_separate exposed but glBlendFuncSeparateOES == NULL!\n"); /*VL_TRAP();*/
  }
  if (Has_GL_OES_fixed_point && glAlphaFuncxOES == NULL)
  {
    Log::warning("GL_OES_fixed_point functions are not exposed with their OES suffix!\n");
  }
  if (Has_GL_OES_single_precision && glDepthRangefOES == NULL)
  {
    Log::warning("GL_OES_single_precision functions are not exposed with their OES suffix!\n");
  }
#endif

  // - - - Helper defines - - - 

  // Note that GL extensions pertaining to deprecated features seem to be exposed under Core profiles even if they are not supported (like Has_GL_SGIS_generate_mipmap)

  Has_GLSL = Has_GL_ARB_shading_language_100 || Has_GL_Version_2_0 || Has_GL_Version_3_0 || Has_GL_Version_4_0 || Has_GLES_Version_2_0;
  Has_GLSL_120_Or_More = Has_GL_Version_2_1 || Has_GL_Version_3_0 || Has_GL_Version_4_0;
  Has_GLSL_130_Or_More = Has_GL_Version_3_0 || Has_GL_Version_4_0;
  Has_GLSL_140_Or_More = Has_GL_Version_3_1 || Has_GL_Version_4_0;
  Has_GLSL_150_Or_More = Has_GL_Version_3_2 || Has_GL_Version_4_0;
  Has_GLSL_330_Or_More = Has_GL_Version_3_3 || Has_GL_Version_4_0;
  Has_GLSL_400_Or_More = Has_GL_Version_4_0;
  Has_GLSL_410_Or_More = Has_GL_Version_4_1;
  Has_Geometry_Shader  = Has_GL_NV_geometry_shader4 || Has_GL_EXT_geometry_shader4 || Has_GL_ARB_geometry_shader4 || Has_GL_Version_3_2 || Has_GL_Version_4_0;
  Has_BufferObject = Has_GL_ARB_vertex_buffer_object || Has_GL_Version_1_5 || Has_GL_Version_3_0 || Has_GL_Version_4_0 || Has_GLES;
  Has_FBO = Has_GL_EXT_framebuffer_object || Has_GL_ARB_framebuffer_object || Has_GL_Version_3_0 || Has_GL_Version_4_0 || Has_GL_OES_framebuffer_object || Has_GLES_Version_2_0;
  Has_PBO = Has_GL_ARB_pixel_buffer_object || Has_GL_EXT_pixel_buffer_object || Has_GL_Version_2_1 || Has_GL_Version_3_0 || Has_GL_Version_4_0;
  // We only support GL_ANGLE_framebuffer_blit for GLES, see also:
  // http://www.khronos.org/registry/gles/extensions/IMG/IMG_multisampled_render_to_texture.txt
  // http://www.khronos.org/registry/gles/extensions/APPLE/APPLE_framebuffer_multisample.txt
  Has_FBO_Multisample = Has_GL_Version_4_0 || Has_GL_Version_3_0 || Has_GL_ARB_framebuffer_object || Has_GL_EXT_framebuffer_multisample || Has_GL_ANGLE_framebuffer_multisample;
  Has_Cubemap_Textures = Has_GL_ARB_texture_cube_map || Has_GL_Version_1_3 || Has_GL_Version_3_0 || Has_GL_Version_4_0 || Has_GL_OES_texture_cube_map || Has_GLES_Version_2_0;
  Has_Texture_Rectangle = Has_GL_ARB_texture_rectangle || Has_GL_NV_texture_rectangle || Has_GL_Version_3_1 || Has_GL_Version_4_0;
  Has_Texture_Array = Has_GL_EXT_texture_array || Has_GL_Version_3_0 || Has_GL_Version_4_0;
  Has_Texture_Buffer = Has_GL_ARB_texture_buffer_object || Has_GL_EXT_texture_buffer_object || Has_GL_Version_3_1 || Has_GL_Version_4_0;
  Has_Texture_Multisample = Has_GL_ARB_texture_multisample || Has_GL_Version_3_2 || Has_GL_Version_4_0;
  Has_Texture_3D = Has_GL_EXT_texture3D || Has_GL_Version_1_2 || Has_GL_Version_3_0 || Has_GL_Version_4_0 || Has_GL_OES_texture_3D;
  Has_Multitexture = Has_GL_ARB_multitexture || Has_GL_Version_1_3 || Has_GL_Version_3_0 || Has_GL_Version_4_0 || Has_GLES;
  Has_Primitive_Restart = Has_GL_Version_3_1 || Has_GL_Version_4_0;
  Has_Occlusion_Query = Has_GL_ARB_occlusion_query || Has_GL_Version_1_5 || Has_GL_Version_3_0 || Has_GL_Version_4_0;
  Has_Transform_Feedback = Has_GL_NV_transform_feedback || Has_GL_EXT_transform_feedback || Has_GL_Version_3_0 || Has_GL_Version_4_0;
  Has_glGenerateMipmaps = Has_GL_ARB_framebuffer_object || Has_GL_Version_3_0 || Has_GL_Version_4_0 || Has_GLES_Version_2_0;
  Has_GL_GENERATE_MIPMAP = (Has_GL_SGIS_generate_mipmap && Has_Fixed_Function_Pipeline) || Has_GL_Version_1_4 || Has_GLES_Version_1_1;
  Has_Point_Sprite = Has_GL_NV_point_sprite || Has_GL_ARB_point_sprite || Has_GLSL || Has_GLES_Version_1_1;
  Has_Base_Vertex = Has_GL_Version_3_2 || Has_GL_Version_4_0 || Has_GL_ARB_draw_elements_base_vertex;
  Has_Primitive_Instancing = Has_GL_Version_3_1 || Has_GL_Version_4_0 || Has_GL_ARB_draw_instanced || Has_GL_EXT_draw_instanced;

  // - - - Resolve supported enables - - -

  // Common ones
  Is_Enable_Supported[EN_BLEND]        = true;
  Is_Enable_Supported[EN_CULL_FACE]    = true;
  Is_Enable_Supported[EN_DEPTH_TEST]   = true;
  Is_Enable_Supported[EN_STENCIL_TEST] = true;
  Is_Enable_Supported[EN_DITHER]       = true;
  Is_Enable_Supported[EN_POLYGON_OFFSET_FILL]  = true;
  Is_Enable_Supported[EN_POLYGON_OFFSET_LINE]  = !Has_GLES;
  Is_Enable_Supported[EN_POLYGON_OFFSET_POINT] = !Has_GLES;
  Is_Enable_Supported[EN_COLOR_LOGIC_OP]       = !Has_GLES_Version_2_0;
  Is_Enable_Supported[EN_MULTISAMPLE]          = !Has_GLES_Version_2_0;

  // Smooth
  Is_Enable_Supported[EN_POINT_SMOOTH]   = Has_GL_Version_1_1||Has_GLES_Version_1_1;
  Is_Enable_Supported[EN_LINE_SMOOTH]    = !Has_GLES_Version_2_0;
  Is_Enable_Supported[EN_POLYGON_SMOOTH] = !Has_GLES;

  // Stipple
  Is_Enable_Supported[EN_LINE_STIPPLE]    = Has_GL_Version_1_1;
  Is_Enable_Supported[EN_POLYGON_STIPPLE] = Has_GL_Version_1_1;

  // Point sprites
  // Point sprites when !Has_Fixed_Function_Pipeline is considered always enabled but GL_POINT_SPRITE should not be called even if GL_OES_point_sprite, GL_ARB_point_sprite etc. are exposed!
  // Note that calling glIsEnabled() with the two below under a Core profile returns true for the same reason.
  Is_Enable_Supported[EN_POINT_SPRITE]       = (Has_GL_NV_point_sprite||Has_GL_ARB_point_sprite||Has_GL_Version_2_0||Has_GL_OES_point_sprite||Has_GLES_Version_1_1) && Has_Fixed_Function_Pipeline;
  Is_Enable_Supported[EN_PROGRAM_POINT_SIZE] = Has_GLSL && !Has_GLES_Version_2_0; // Only OpenGL ES 2 does not support glPointSize()/GL_POINT_SIZE

  // Fixed function pipeline
  Is_Enable_Supported[EN_ALPHA_TEST]     = Has_GL_Version_1_1||Has_GLES_Version_1_1;
  Is_Enable_Supported[EN_LIGHTING]       = Has_GL_Version_1_1||Has_GLES_Version_1_1;
  Is_Enable_Supported[EN_COLOR_SUM]      = Has_GL_Version_1_1;
  Is_Enable_Supported[EN_FOG]            = Has_GL_Version_1_1||Has_GLES_Version_1_1;
  Is_Enable_Supported[EN_NORMALIZE]      = Has_GL_Version_1_1||Has_GLES_Version_1_1;
  Is_Enable_Supported[EN_RESCALE_NORMAL] = Has_GL_Version_1_2||Has_GLES_Version_1_1;

  // Available only under OpenGL 2.x
  Is_Enable_Supported[EN_VERTEX_PROGRAM_TWO_SIDE]   = ((Has_GL_ARB_vertex_program||Has_GL_NV_vertex_program) && Has_GL_Version_1_1) || Has_GL_Version_2_0;

  // OpenGL 3.2
  Is_Enable_Supported[EN_TEXTURE_CUBE_MAP_SEAMLESS] = Has_GL_AMD_seamless_cubemap_per_texture||Has_GL_ARB_seamless_cube_map||Has_GL_Version_3_2||Has_GL_Version_4_0;
  
  // Clipping planes
  int max_clip_planes = 0;
  // OpenGL ES 2 is the only one without clipping planes!
  if (!Has_GLES_Version_2_0)
  {
    glGetIntegerv(GL_MAX_CLIP_DISTANCES, &max_clip_planes); // GL_MAX_CLIP_DISTANCES == GL_MAX_CLIP_PLANES
  }
  Is_Enable_Supported[EN_CLIP_DISTANCE0] = max_clip_planes >= 1;
  Is_Enable_Supported[EN_CLIP_DISTANCE1] = max_clip_planes >= 2;
  Is_Enable_Supported[EN_CLIP_DISTANCE2] = max_clip_planes >= 3;
  Is_Enable_Supported[EN_CLIP_DISTANCE3] = max_clip_planes >= 4;
  Is_Enable_Supported[EN_CLIP_DISTANCE4] = max_clip_planes >= 5;
  Is_Enable_Supported[EN_CLIP_DISTANCE5] = max_clip_planes >= 6;
  Is_Enable_Supported[EN_CLIP_DISTANCE6] = max_clip_planes >= 7;
  Is_Enable_Supported[EN_CLIP_DISTANCE7] = max_clip_planes >= 8;

  // Multisampling
  Is_Enable_Supported[EN_SAMPLE_ALPHA_TO_COVERAGE] = Has_GL_Version_1_3||!Has_Fixed_Function_Pipeline||Has_GLES;
  Is_Enable_Supported[EN_SAMPLE_ALPHA_TO_ONE]      = Has_GL_Version_1_3||Has_GL_Version_3_0||Has_GL_Version_4_0||Has_GLES_Version_1_1;
  Is_Enable_Supported[EN_SAMPLE_COVERAGE]          = Has_GL_Version_1_3||!Has_Fixed_Function_Pipeline||Has_GLES;

#ifndef NDEBUG
  // Enables management debug code
  VL_CHECK_OGL();
  bool got_error = false;
  for(int i=0; i<EN_EnableCount; ++i)
  {
    glDisable(Translate_Enable[i]); // glIsEnabled() for some reason is not reliable!
    bool supported = glGetError() == GL_NO_ERROR;
    if (supported != Is_Enable_Supported[i])
    {
      Log::error( Say("%s: capability %s supported! This is a harmless glitch either in your GL driver or in VL.\n") << Translate_Enable_String[i] << (supported? "*IS*" : "*IS NOT*") );
      got_error = true;
    }
  }
  if(got_error)
  {
    printf("OpenGL Version = %s\n", glGetString(GL_VERSION));
    #define PRINT_INFO(STRING) printf(#STRING" = %d\n", STRING?1:0)
    PRINT_INFO(Is_OpenGL_Core_Profile);
    PRINT_INFO(Is_OpenGL_Forward_Compatible);
    PRINT_INFO(Has_GL_Version_4_1);
    PRINT_INFO(Has_GL_Version_4_0);
    PRINT_INFO(Has_GL_Version_3_3);
    PRINT_INFO(Has_GL_Version_3_2);
    PRINT_INFO(Has_GL_Version_3_1);
    PRINT_INFO(Has_GL_Version_3_0);
    PRINT_INFO(Has_GL_Version_2_1);
    PRINT_INFO(Has_GL_Version_2_0);
    PRINT_INFO(Has_GL_Version_1_5);
    PRINT_INFO(Has_GL_Version_1_4);
    PRINT_INFO(Has_GL_Version_1_3);
    PRINT_INFO(Has_GL_Version_1_2);
    PRINT_INFO(Has_GL_Version_1_1);
    // VL_TRAP();
  }
#endif

  VL_CHECK_OGL();
  return Is_OpenGL_Initialized = true;
}