2
\$\begingroup\$

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
\$\endgroup\$
5
  • \$\begingroup\$ Out of curiousity, why do you create a window with zero size? I see though from the glfw manual, it reverts to a default 640x480. Anyway, you should try using glGetError after each opengl call and see what kind of error you get. \$\endgroup\$ Feb 16 '13 at 16:18
  • \$\begingroup\$ I have compiled and ran your program without any problems, so the code is fine. What are you using to compile? \$\endgroup\$ Feb 16 '13 at 16:31
  • \$\begingroup\$ @Grieverheart g++ triangle.cpp -o main -lGLEW -lglfw -lGLU -lGL -lm this is the command I use to compile my program. I will try using glGetError. That's a good idea. \$\endgroup\$
    – snape
    Feb 16 '13 at 17:05
  • 1
    \$\begingroup\$ From your edit, it seems glfw fails to open a window. There is a reference in the glfw troubleshooting indicating you may have not compiled glfw properly. \$\endgroup\$ Feb 16 '13 at 17:21
  • \$\begingroup\$ @Grieverheart You were right. Thanks a lot! Instead of building and installing directly from the tar ball, I installed it from ubuntu's repository. That worked. I have no idea why. \$\endgroup\$
    – snape
    Feb 16 '13 at 17:38
4
\$\begingroup\$

glfwInit() isn't actually creating a valid context. glfwOpenWindow should do that, but I suspect you're passing invalid parameters. You should check the return of glfwOpenWindow (and glfwInit()) to ensure that it's successfully creating a context. This thread also mentions creating a context manually, supposedly without window creation.

\$\endgroup\$
6
  • \$\begingroup\$ as suggested by the Edit, there is some problem with glfwopenwindow command. The arguments given to this function are valid. I checked the documentation for that. If you have any other ideas why this error might be occurring please do tell me. \$\endgroup\$
    – snape
    Feb 16 '13 at 17:19
  • \$\begingroup\$ I'd start with passing reasonable values. Like in an example: glfwOpenWindow(800, 600, 5, 6, 5, 0, 8, 0, GLFW_WINDOW) \$\endgroup\$
    – House
    Feb 16 '13 at 17:30
  • \$\begingroup\$ Problem was not with glfwOpenWindow arguments. It was with glfw installation. I installed it again from the ubuntu repositories instead of tar ball installation, everything worked smoothly. \$\endgroup\$
    – snape
    Feb 16 '13 at 17:39
  • \$\begingroup\$ @snape: Everything worked? With a window of size 0, and no color/depth bits? \$\endgroup\$ Feb 16 '13 at 23:08
  • 3
    \$\begingroup\$ With GLFW 3, you also need to set the newly opened window as the current rendering context using glfwMakeContextCurrent \$\endgroup\$
    – Mokosha
    Oct 29 '13 at 18:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .