29

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

glxinfo | grep "OpenGL version"

我得到以下输出:

OpenGL version string: 3.3.0 NVIDIA 295.40

但是当我glGetString(GL_VERSION)在我的应用程序中运行时,结果为空。为什么这段代码没有得到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));
}
4

2 回答 2

42

glutInit()不创建GL 上下文 使一个当前。您需要当前的 GL 上下文才能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));
}
于 2012-08-29T18:48:50.623 回答
2

您还可以使用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));
于 2019-03-10T10:16:50.750 回答