为什么glGetString(GL_VERSION)返回null / zero而不是OpenGL版本?

时间:2012-08-29 18:43:44

标签: c++ opengl glut glew

我在Linux Mint 13 XFCE上。我的问题是当我在终端中运行命令:

glxinfo | grep "OpenGL version"

我得到以下输出:

OpenGL version string: 3.3.0 NVIDIA 295.40

但是当我在我的应用程序中运行glGetString(GL_VERSION)时,结果为null。为什么这段代码不能获得gl_version

#include <stdio.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glext.h>

int main(int argc, char **argv) {

    glutInit(&argc, argv);
    glewInit();

    printf("OpenGL version supported by this platform (%s): \n",
        glGetString(GL_VERSION));
}

2 个答案:

答案 0 :(得分:31)

glutInit()不会创建GL context 使一个当前。您需要glewInit()glGetString()的当前总帐上下文才能正常工作。

试试这个:

#include <GL/glew.h>
#include <GL/glut.h>
#include <cstdio>

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("GLUT");

    glewInit();
    printf("OpenGL version supported by this platform (%s): \n", glGetString(GL_VERSION));
}

答案 1 :(得分:1)

您还可以使用glfw来创建GL上下文,然后查询版本:

包括此文件:

#include "GL/glew.h"
#include "GLFW/glfw3.h"

然后您可以这样做:

    // Initialise GLFW
    glewExperimental = true; // Needed for core profile
    if (!glfwInit())
    {
        return "";
    }

    // We are rendering off-screen, but a window is still needed for the context
    // creation. There are hints that this is no longer needed in GL 3.3, but that
    // windows still wants it. So just in case.
    glfwWindowHint(GLFW_VISIBLE, GL_FALSE); //dont show the window

    // Open a window and create its OpenGL context
    GLFWwindow* window;
    window = glfwCreateWindow(100, 100, "Dummy window", NULL, NULL);
    if (window == NULL) {
        return "";
    }
    glfwMakeContextCurrent(window); // Initialize GLEW
    if (glewInit() != GLEW_OK)
    {
        return "";
    }

    std::string versionString = std::string((const char*)glGetString(GL_VERSION));
相关问题