Why does glGetString returns a NULL string

Posted by snape on Game Development See other posts from Game Development or by snape
Published on 2013-02-16T15:10:12Z Indexed on 2013/10/29 22:23 UTC
Read the original article Hit count: 282

Filed under:
|

I am trying my hands at GLFW library. I have written a basic program to get OpenGL renderer and vendor string. Here is the code

 #include <GL/glew.h>
#include <GL/glfw.h>
#include <cstdio>
#include <cstdlib>
#include <string>


using namespace std;

void shutDown(int returnCode) {
    printf("There was an error in running the code with error %d\n",returnCode);
    GLenum res = glGetError();
    const GLubyte *errString = gluErrorString(res);
    printf("Error is %s\n", errString);
    glfwTerminate();
    exit(returnCode);
}

int main() {
    // start GL context and O/S window using GLFW helper library
    if (glfwInit() != GL_TRUE)
        shutDown(1);
    if (glfwOpenWindow(0, 0, 0, 0, 0, 0, 0, 0, GLFW_WINDOW) != GL_TRUE)
        shutDown(2);
    // start GLEW extension handler
    glewInit();

    // get version info
    const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
    const GLubyte* version = glGetString (GL_VERSION); // version as a string
    printf("Renderer: %s\n", renderer);
    printf("OpenGL version supported %s\n", version);

    // close GL context and any other GLFW resources
    glfwTerminate();
    return 0;
}

I googled this error and found out that we have to initialize the OpenGL context before calling glGetString(). Although I have initialized OpenGL context using glfwInit() but still the function returns a NULL string. Any ideas?

Edit I have updated the code with error checking mechanisms. This code on running outputs the following

There was an error in running the code with error 2
Error is no error

© Game Development or respective owner

Related posts about opengl

Related posts about glfw