3 This file is part of libmspgl
4 Copyright © 2007, 2009-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #define GLX_GLXEXT_PROTOTYPES
13 #include <msp/strings/formatter.h>
14 #include <msp/strings/utils.h>
15 #include "arb_shader_objects.h"
16 #include "arb_vertex_buffer_object.h"
17 #include "arb_vertex_program.h"
18 #include "arb_vertex_shader.h"
19 #include "ext_framebuffer_object.h"
21 #include "extension.h"
23 #include "nv_primitive_restart.h"
24 #include "version_1_2.h"
25 #include "version_1_3.h"
32 bool is_supported(const string &ext)
34 static set<string> extensions;
35 static bool init_done = false;
39 if(const char *gl_ext = reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)))
41 vector<string> exts = split(gl_ext);
42 extensions.insert(exts.begin(), exts.end());
45 /* XXX Conceptually a bit weird place for this, but I couldn't really come up
46 with anything better that would still be transparent. */
47 if(extensions.count("GL_ARB_shader_objects"))
48 init_arb_shader_objects();
49 if(extensions.count("GL_ARB_vertex_shader"))
50 init_arb_vertex_shader();
51 if(extensions.count("GL_ARB_vertex_program"))
52 init_arb_vertex_program();
53 if(extensions.count("GL_EXT_framebuffer_object"))
54 init_ext_framebuffer_object();
55 if(extensions.count("GL_ARB_vertex_buffer_object"))
56 init_arb_vertex_buffer_object();
57 if(extensions.count("GL_NV_primitive_restart"))
58 init_nv_primitive_restart();
63 return extensions.count(ext);
66 void require_extension(const string &ext)
68 if(!is_supported(ext))
69 throw UnsupportedExtension(ext);
72 const Version &get_gl_version()
74 static Version version;
75 static bool init_done = false;
79 string gl_ver = reinterpret_cast<const char *>(glGetString(GL_VERSION));
80 vector<string> parts = split(gl_ver.substr(0, gl_ver.find(' ')), '.');
81 version.major = lexical_cast<unsigned>(parts[0]);
82 version.minor = lexical_cast<unsigned>(parts[1]);
84 unsigned combined = version.major*0x100+version.minor;
96 bool is_version_at_least(unsigned a, unsigned b)
98 const Version &ver = get_gl_version();
99 return (ver.major>a || (ver.major==a && ver.minor>=b));
102 void require_version(unsigned a, unsigned b)
104 if(!is_version_at_least(a, b))
105 throw UnsupportedExtension(format("OpenGL %d.%d", a, b));
108 ExtFunc *get_proc_address(const string &name)
111 return glXGetProcAddressARB(reinterpret_cast<const unsigned char *>(name.c_str()));
113 return reinterpret_cast<ExtFunc *>(wglGetProcAddress(name.c_str()));