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

lyr*_*a42 21 c++ opengl glut glew

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

glxinfo | grep "OpenGL version"
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

OpenGL version string: 3.3.0 NVIDIA 295.40
Run Code Online (Sandbox Code Playgroud)

但是当我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));
}
Run Code Online (Sandbox Code Playgroud)

gen*_*ult 33

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));
}
Run Code Online (Sandbox Code Playgroud)

  • #include <cstdio> ;;感谢您的代码片段 (2认同)