Search code examples
c++linuxopenglglfwglx

OpenGL 3/GLFW Blank Viewport


I'm going through the second "chapter" on http://www.open.gl and running into a drawing issue which I can't figure out.

int main()
{
    //Initialize GLFW, create the window and the context
    glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow* window = glfwCreateWindow(1024, 768, "Open.GL Drawing 1", nullptr, nullptr);

    glfwMakeContextCurrent(window);

    //initialize GLEW after context creation
    glewExperimental = GL_TRUE;
    glewInit();

    //loading, compiling, and checking shaders takes place here
    [...]

    //create, link, and use the shader program
    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glBindFragDataLocation(shaderProgram, 0, "outColor");
    glLinkProgram(shaderProgram);
    glUseProgram(shaderProgram);

    //create and bind the vao for the position data
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    //triangle vertecies
    GLfloat verticies[] = {
         0.0f,  0.5f,
         0.5f, -0.5f,
        -0.5f, -0.5f
    };

    //vbo for verticies
    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verticies), verticies, GL_STATIC_DRAW);

    //link, define, and enable the position attribute (aka, the verticies)
    GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
    glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(posAttrib);

    //main loop!
    while(!glfwWindowShouldClose(window))
    {
        glfwPollEvents();

        if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(window, GL_TRUE);

        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glDrawArrays(GL_TRIANGLES, 0, 3);

        glfwSwapBuffers(window);
    }

    //clean up after yourself
    glfwTerminate();

    glDeleteProgram(shaderProgram);
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    glDeleteBuffers(1, &vbo);
    glDeleteVertexArrays(1, &vao);

    return EXIT_SUCCESS;
}

Vertex shader:

#version 150

in vec2 position;

void main()
{
    gl_Position = vec4(position, 0.0, 1.0);
}

Fragment shader:

#version 150

out vec4 outColor;

void mian()
{
    outColor = vec4(1.0, 1.0, 1.0, 1.0);
}

When compiled with

g++ -fdiagnostics-color -Wall -Wextra -std=c++11 -lGLEW -lGLU -lGL  -lglfw -o drawing1 drawing1.cpp

I get no warnings, but a blank screen when the program runs. I checked the example code he links as well as the github copy of the example source. Unfortunately, he uses SFML whereas I'm using GLFW, so I attempted to compensate for that difference.

I did my best to mimic his example code (ordering of things, etc) as well as adding in the last few lines of main() and the main loop (there was no mention of deleting shaders or clearing the viewport's color in the tutorial, but those statements were present in his example code) but still wound up with the same result. Checking glGetError() gave me an invalid enum error, but this post says that might be an expected behavior with glewExperimental.

After that, I'm not sure how to further isolate the issue. I look forward to someone pointing out what will obviously be a simple mistake. =)


Solution

  • As Joey Dewd pointed out, I didn't proof read my shader code very well. For the record, it was me typing mian instead of main in my fragment shader that caused the issues. On his suggestion, I added code to check for errors in linking (I was already checking for compilation errors) and sure enough, when I recreated my typo, the linker complained.

    Fragment info
    -------------
    (0) : error C3001: no program defined
    

    Thanks Joey. =)