Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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
share|improve this question
    
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. –  Grieverheart Feb 16 '13 at 16:18
    
I have compiled and ran your program without any problems, so the code is fine. What are you using to compile? –  Grieverheart Feb 16 '13 at 16:31
    
@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. –  snape Feb 16 '13 at 17:05
1  
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. –  Grieverheart Feb 16 '13 at 17:21
    
@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. –  snape Feb 16 '13 at 17:38
add comment

1 Answer

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.

share|improve this answer
    
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. –  snape Feb 16 '13 at 17:19
    
I'd start with passing reasonable values. Like in an example: glfwOpenWindow(800, 600, 5, 6, 5, 0, 8, 0, GLFW_WINDOW) –  Byte56 Feb 16 '13 at 17:30
    
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. –  snape Feb 16 '13 at 17:39
    
@snape: Everything worked? With a window of size 0, and no color/depth bits? –  Nicol Bolas Feb 16 '13 at 23:08
2  
With GLFW 3, you also need to set the newly opened window as the current rendering context using glfwMakeContextCurrent –  Mokosha Oct 29 '13 at 18:29
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.