C++ 为什么 glGetString(GL_VERSION) 返回空/零而不是 OpenGL 版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12184506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:58:43  来源:igfitidea点击:

Why does glGetString(GL_VERSION) return null / zero instead of the OpenGL version?

c++openglglutglew

提问by lyra42

I'm on Linux Mint 13 XFCE. My problem is that when I run in terminal the command:

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

glxinfo | grep "OpenGL version"

I get the following output:

我得到以下输出:

OpenGL version string: 3.3.0 NVIDIA 295.40

But when I run the glGetString(GL_VERSION)in my application then the result is null. Why doesn't this code get the gl_version?

但是当我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));
}

回答by genpfault

glutInit()doesn't create a GL contextormake one current. You need a current GL context for glewInit()and glGetString()to work.

glutInit()不创建GL 上下文使一个当前。你需要一个当前GL上下文glewInit()glGetString()工作。

Try this:

尝试这个:

#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));
}

回答by arielhad

You can also use glfwin order to create GL context and then query the version:

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

Include this files:

包括这个文件:

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

And then you can do:

然后你可以这样做:

    // 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));