0

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. =)

Community
  • 1
  • 1
pdm
  • 1,027
  • 1
  • 9
  • 24
  • 1
    Two things: first, try placing `glGetError()` after `glewInit()` so it resets the error flag; then place `glGetError()` at other locations and it either should display `0` or another error code that is non-GLEW related. Second, linking your shader program could cause linking errors and you should check for those; otherwise your shader might be broken without you knowing (check http://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/loading.php at header **Linking** as to how this is done for programs). – Joey Dewd Sep 07 '14 at 20:52
  • 1
    Oh and what might be the problem: your fragment shader's main function is actually called `mian`. Do you check error logs after compiling your shaders? This error would've been easily spotted then. Checking error logs and if shaders actually compiled succesfully is really advisable :) – Joey Dewd Sep 07 '14 at 20:54
  • Bahahahaha. Yup. `mian` doesn't throw a compilation error, but I'm sure GL is confused when that happens. XD I like the idea of calling `glGetError` after GLEW's init to clear that error, though, so thank you for both ideas! =) – pdm Sep 07 '14 at 20:58
  • Sure! Also try verifying if your shaders linked succesfully and if so print error log and try to pinpoint the exact statements that give the error. You should be able to do that now :) We'll hear more once you've pinpointed the source of the error – Joey Dewd Sep 07 '14 at 21:00
  • Yeah, it worked after I fixed the typo. =) If I add the GLSL linking check you linked in your first comment, would that have complained? I'm going to add it anyway for the sake of practice, but if it points it out, I'll include that in the "answer" to this question. – pdm Sep 07 '14 at 21:02
  • Yeah, I'm quite sure you would've caught this error in the Linking stage if you checked for linking errors and printed the error logs :) Good to hear! – Joey Dewd Sep 07 '14 at 21:03

1 Answers1

1

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. =)

Community
  • 1
  • 1
pdm
  • 1,027
  • 1
  • 9
  • 24