glcontext.cpp
上传用户:center1979
上传日期:2022-07-26
资源大小:50633k
文件大小:7k
源码类别:

OpenGL

开发平台:

Visual C++

  1. // glcontext.h
  2. //
  3. // Copyright (C) 2003, Chris Laurel <claurel@shatters.net>
  4. //
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. #include <algorithm>
  10. #include <celutil/debug.h>
  11. #include "gl.h"
  12. #include "glext.h"
  13. #include "glcontext.h"
  14. using namespace std;
  15. static VertexProcessor* vpNV = NULL;
  16. static VertexProcessor* vpARB = NULL;
  17. static FragmentProcessor* fpNV = NULL;
  18. GLContext::GLContext() :
  19.     renderPath(GLPath_Basic),
  20.     vertexPath(VPath_Basic),
  21.     vertexProc(NULL),
  22.     maxSimultaneousTextures(1)
  23. {
  24. }
  25. GLContext::~GLContext()
  26. {
  27. }
  28. void GLContext::init(const vector<string>& ignoreExt)
  29. {
  30.     char* extensionsString = (char*) glGetString(GL_EXTENSIONS);
  31.     if (extensionsString != NULL)
  32.     {
  33.         char* next = extensionsString;
  34.         
  35.         while (*next != '')
  36.         {
  37.             while (*next != '' && *next != ' ')
  38.                 next++;
  39.             string ext(extensionsString, next - extensionsString);
  40.             // scan the ignore list
  41.             bool shouldIgnore = false;
  42.             for (vector<string>::const_iterator iter = ignoreExt.begin();
  43.                  iter != ignoreExt.end(); iter++)
  44.             {
  45.                 if (*iter == ext)
  46.                 {
  47.                     shouldIgnore = true;
  48.                     break;
  49.                 }
  50.             }
  51.             if (!shouldIgnore)
  52.                 extensions.insert(extensions.end(), ext);
  53.             if (*next == '')
  54.                 break;
  55.             next++;
  56.             extensionsString = next;
  57.         }
  58.     }
  59.     // Initialize all extensions used
  60.     for (vector<string>::const_iterator iter = extensions.begin();
  61.          iter != extensions.end(); iter++)
  62.     {
  63.         InitExtension(iter->c_str());
  64.     }
  65.     if (extensionSupported("GL_ARB_multitexture") &&
  66.         glx::glActiveTextureARB != NULL)
  67.     {
  68.         glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB,
  69.                       (GLint*) &maxSimultaneousTextures);
  70.     }
  71.     if (extensionSupported("GL_ARB_vertex_program") &&
  72.         glx::glGenProgramsARB)
  73.     {
  74.         DPRINTF(1, "Renderer: ARB vertex programs supported.n");
  75.         if (vpARB == NULL)
  76.             vpARB = vp::initARB();
  77.         vertexProc = vpARB;
  78.     }
  79.     else if (extensionSupported("GL_NV_vertex_program") &&
  80.              glx::glGenProgramsNV)
  81.     {
  82.         DPRINTF(1, "Renderer: nVidia vertex programs supported.n");
  83.         if (vpNV == NULL)
  84.             vpNV = vp::initNV();
  85.         vertexProc = vpNV;
  86.     }
  87.     if (extensionSupported("GL_NV_fragment_program") &&
  88.         glx::glGenProgramsNV)
  89.     {
  90.         DPRINTF(1, "Renderer: nVidia fragment programs supported.n");
  91.         if (fpNV == NULL)
  92.             fpNV = fp::initNV();
  93.         fragmentProc = fpNV;
  94.     }
  95.     // Initialize GLX_SGI_video_sync blindly. At most, it will be null.
  96.     InitExtension("GLX_SGI_video_sync");
  97. }
  98. bool GLContext::setRenderPath(GLRenderPath path)
  99. {
  100.     if (!renderPathSupported(path))
  101.         return false;
  102.     switch (path)
  103.     {
  104.     case GLPath_Basic:
  105.     case GLPath_Multitexture:
  106.     case GLPath_NvCombiner:
  107.         vertexPath = VPath_Basic;
  108.         break;
  109.     case GLPath_NvCombiner_NvVP:
  110.         vertexPath = VPath_NV;
  111.         break;
  112.     case GLPath_DOT3_ARBVP:
  113.     case GLPath_NvCombiner_ARBVP:
  114.     case GLPath_ARBFP_ARBVP:
  115.     case GLPath_NV30:
  116.     case GLPath_GLSL:
  117.         vertexPath = VPath_ARB;
  118.         break;
  119.     default:
  120.         return false;
  121.     }
  122.     renderPath = path;
  123.     return true;
  124. }
  125. bool GLContext::renderPathSupported(GLRenderPath path) const
  126. {
  127.     switch (path)
  128.     {
  129.     case GLPath_Basic:
  130.         return true;
  131.     case GLPath_Multitexture:
  132.         return (maxSimultaneousTextures > 1 &&
  133.                ( extensionSupported("GL_EXT_texture_env_combine") ||
  134.                  extensionSupported("GL_ARB_texture_env_combine")) );
  135.     case GLPath_NvCombiner:
  136.         return false;
  137.         /*
  138.         // No longer supported; all recent NVIDIA drivers also support
  139.         // the vertex_program extension, so the combiners-only path
  140.         // isn't necessary.
  141.         return extensionSupported("GL_NV_register_combiners");
  142.         */
  143.     case GLPath_DOT3_ARBVP:
  144.         return (extensionSupported("GL_ARB_texture_env_dot3") &&
  145.                 extensionSupported("GL_ARB_vertex_program") &&
  146.                 vertexProc != NULL);
  147.     case GLPath_NvCombiner_NvVP:
  148.         // If ARB_vertex_program is supported, don't report support for
  149.         // this render path.
  150.         return (extensionSupported("GL_NV_register_combiners") &&
  151.                 extensionSupported("GL_NV_vertex_program") &&
  152.                 !extensionSupported("GL_ARB_vertex_program") &&
  153.                 vertexProc != NULL);
  154.     case GLPath_NvCombiner_ARBVP:
  155.         return (extensionSupported("GL_NV_register_combiners") &&
  156.                 extensionSupported("GL_ARB_vertex_program") &&
  157.                 vertexProc != NULL);
  158.     case GLPath_ARBFP_ARBVP:
  159.         return false;
  160.         /*
  161.         return (extensionSupported("GL_ARB_vertex_program") &&
  162.                 extensionSupported("GL_ARB_fragment_program") &&
  163.                 vertexProc != NULL);
  164.         */
  165.     case GLPath_NV30:
  166. /* This render path is deprecated; GLSL is now preferred */
  167. return false;
  168. /*
  169.         return (extensionSupported("GL_ARB_vertex_program") &&
  170.                 extensionSupported("GL_NV_fragment_program"));
  171. */
  172.     case GLPath_GLSL:
  173.         return (extensionSupported("GL_ARB_shader_objects") &&
  174.                 extensionSupported("GL_ARB_shading_language_100") &&
  175.                 extensionSupported("GL_ARB_vertex_shader") &&
  176.                 extensionSupported("GL_ARB_fragment_shader"));
  177.     default:
  178.         return false;
  179.     }
  180. }
  181. GLContext::GLRenderPath GLContext::nextRenderPath()
  182. {
  183.     GLContext::GLRenderPath newPath = renderPath;
  184.     do {
  185.         newPath = (GLRenderPath) ((int) newPath + 1);;
  186.         if (newPath > GLPath_GLSL)
  187.             newPath = GLPath_Basic;
  188.     } while (newPath != renderPath && !renderPathSupported(newPath));
  189.     renderPath = newPath;
  190.     return renderPath;
  191. }
  192. bool GLContext::extensionSupported(const string& ext) const
  193. {
  194.     return (find(extensions.begin(), extensions.end(), ext) != extensions.end());
  195. }
  196. bool GLContext::bumpMappingSupported() const
  197. {
  198.     return renderPath > GLPath_Multitexture;
  199. }
  200. GLContext::VertexPath GLContext::getVertexPath() const
  201. {
  202.     return vertexPath;
  203. }
  204. VertexProcessor* GLContext::getVertexProcessor() const
  205. {
  206.     return vertexPath == VPath_Basic ? NULL : vertexProc;
  207. }
  208. FragmentProcessor* GLContext::getFragmentProcessor() const
  209. {
  210.     if (renderPath == GLPath_NV30 /* || renderPath == GLPath_ARGFP_ARBVP */ )
  211.         return fragmentProc;
  212.     else
  213.         return NULL;
  214. }