首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么glGetString(GL_VERSION)返回null /0而不是OpenGL版本?

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

Stack Overflow用户
提问于 2012-08-29 18:43:44
回答 2查看 39.4K关注 0票数 30

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

代码语言:javascript
运行
复制
glxinfo | grep "OpenGL version"

我得到以下输出:

代码语言:javascript
运行
复制
OpenGL version string: 3.3.0 NVIDIA 295.40

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

代码语言:javascript
运行
复制
#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));
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-29 18:48:50

glutInit()不创建GL上下文或创建一个电流。您需要一个当前GL上下文才能使glewInit()glGetString()工作。

试试这个:

代码语言:javascript
运行
复制
#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));
}
票数 47
EN

Stack Overflow用户

发布于 2019-03-10 10:16:50

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

包括以下文件:

代码语言:javascript
运行
复制
#include "GL/glew.h"
#include "GLFW/glfw3.h"

然后你就可以:

代码语言:javascript
运行
复制
    // 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));
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12184506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档