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 have started to try to learn OpenGL, and I decided to use GLFW to assist in window creation. The problem is, since GLFW3 is so new, there are no tutorials on it or how to use it with modern OpenGL (3.3, specifically).

Using the GLFW3 tutorial found on the website, which uses older OpenGL rendering (glBegin(GL_TRIANGLES), glVertex3f(), and such), I can get a triangle to render to the screen. The problem is, using new OpenGL, I can't get the same triangle to render to the screen.

I am new to OpenGL, and GLFW3 is new to most people, so I may be completely missing something obvious, but here is my code:

static const GLuint g_vertex_buffer_data[] = 
{
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};

int main(void)
{
    GLFWwindow* window;

    if(!glfwInit())
    {
        fprintf(stderr, "Failed to initialize GLFW.");
        return -1;
    }

    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    window = glfwCreateWindow(800, 600, "Test Window", NULL, NULL);
    if(!window)
    {
        glfwTerminate();
        fprintf(stderr, "Failed to create a GLFW window");
        return -1;
    }
    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;
    GLenum err = glewInit();
    if(err != GLEW_OK)
    {
        glfwTerminate();
        fprintf(stderr, "Failed to initialize GLEW");
        fprintf(stderr, (char*)glewGetErrorString(err));
        return -1;
    }

    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);

    GLuint programID = LoadShaders("SimpleVertexShader.glsl", "SimpleFragmentShader.glsl");

    GLuint vertexBuffer;
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

    while(!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glUseProgram(programID);

        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

        glDrawArrays(GL_TRIANGLES, 0, 3);

        glDisableVertexAttribArray(0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glDeleteBuffers(1, &vertexBuffer);
    glDeleteProgram(programID);

    glfwDestroyWindow(window);

    glfwTerminate();
    exit(EXIT_SUCCESS);
}

I know it is not my shaders, they are super simple and I've checked them against GLFW 2.7 so I know that they work. I'm assuming that I've missed something crucial to using the OpenGL context with GLFW3, so any help locating the problem would be greatly appreciated.

share|improve this question
    
What have you tested? Did you add core profile, shaders, and VBO use all in one go? Do it in steps, check each bit along the way. –  Sean Middleditch Jun 28 '13 at 8:57
    
Also, I'd recommend avoiding Core Profile. It is not supported on many older-but-still-relevant graphics chips, not supported on some Linux drivers, is often more buggy than compat profile when it is available, and is only required on OSX (which doesn't support the 3.3 version you're using). You're going to have an uphill battle with driver support if you use anything besides GL 2.1 + extensions (and only ones you outright need). I doubt driver problems are your issue here, though; the simple cases all work. –  Sean Middleditch Jun 28 '13 at 9:00

1 Answer 1

up vote 4 down vote accepted

You define your vertex data as GLuints:

static const GLuint g_vertex_buffer_data[] = 

However you bind this data as GL_FLOATs:

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

-1.0f when cast to GLuint and then interpreted as a float represents NaN (not a number) and lead to unspecified results, as the OpenGL 3.3 Core Profile specification states:

The special values Inf and Inf encode values with magnitudes too large to be represented; the special value NaN encodes “Not A Number” values resulting from undefined arithmetic operations such as 0/0. Implementations are permitted, but not required, to support Infs and NaNs in their floating-point computations. Any representable floating-point value is legal as input to a GL command that requires floating-point data. The result of providing a value that is not a floating- point number to such a command is unspecified, but must not lead to GL interrup- tion or termination.

And since I don't see anything else obviously wrong with your code, changing GLuint to GLfloat should do the trick.

share|improve this answer
    
Oh wow, I can't believe I didn't notice that. Thanks, it's working perfectly now. –  sm81095 Jun 28 '13 at 19:21

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.