直接上代码:

 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
void getGlslVersion(int* major, int* minor)
{
    int gl_major, gl_minor;
    getGlVersion(&gl_major, &gl_minor);

    *major = *minor = 0;

    if (gl_major == 1) {
        // GL v1.x can only provide GLSL v1.00 as an extension
        const char* extstr = (const char*)glGetString(GL_EXTENSIONS);
        if ((extstr != NULL) &&
            (strstr(extstr, "GL_ARB_shading_lanuage_100") != NULL)) {
            *major = 1;
            *minor = 0;
        }
    } else if (gl_major >= 2) {
        // GL v2.0 and greater must parse the version string
        const char* verstr =
            (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);

        if ((verstr != NULL) ||
            (sscanf(verstr, "%d.%d", major, minor) != 2)) {
            *major = *minor = 0;
            fprintf(stderr,
                    "Invalid GL_SHADING_LANGUAGE_VERSION format!!!n");
        }
    }
}