1
\$\begingroup\$

I am following the book 'OpenGL SuperBible' and am trying to run one of the first examples of drawing a point to the screen, bit instead of using there lib, im using GLFW. I'm not sure where I am going wrong.

Using GLFW 3.0 and Opengl 4.5

Creating shader

static const GLchar *vertex_shader_source[] = {
    "#version 450 core \n",
    " \n",
    "void main(void){ \n",
    "gl_Position = vec4(0.0,0.0,0.5,1.0); \n",
    "} \n",
    " \n",
};
static const GLchar *fragment_shader_source[] = {
    "#version 450 core \n",
    "out vec4 color; \n",
    "void main(void){ \n",
    "color = vec4(0.0,0.8,1.0,1.0); \n",
    "} \n",
    " \n",
};

vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, vertex_shader_source, NULL);
glCompileShader(vertex_shader);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, fragment_shader_source, NULL);
glCompileShader(fragment_shader);

program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);

glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);

Setting up the VAO

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

Rendering

glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
glUseProgram(program);
glPointSize(40.0f);
glDrawArrays(GL_POINT, 0, 1);

I'm still quite new to game development, so i'm sorry if its something silly i missed

EDIT

I finally got it working after a little modification. I was deleting the shaders without detaching them from the program after linking which seemed to cause the shaders to never be called. Also since i'm using GLFW I needed to use glewExperimental = true; before initilizing glew.

Here is the final code

static void error_callback(int error, const char* description){
    fprintf(stderr, "Error: %s\n", description);
}

void main() {
    GLFWwindow* window;
    glfwSetErrorCallback(error_callback);
    if (!glfwInit()) {
        exit(EXIT_FAILURE);
    }
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
    glewExperimental = true;
    if (!window){
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    glfwMakeContextCurrent(window);
    if (glewInit() != GLEW_OK) {
        return;
    }
    static const GLchar *vertex_shader_source[] = {
        "#version 450 core \n"
        "void main(void){ \n"
        "gl_Position = vec4(0.0,0.0,0.5,1.0); \n"
        "} \n"
    };
    static const GLchar *fragment_shader_source[] = {
        "#version 450 core\n"
        "out vec4 color; \n"
        "void main(void){ \n"
        "color = vec4(0.0,0.8,1.0,1.0); \n"
        "} \n"
    };
    const GLubyte* renderer = glGetString(GL_RENDERER);
    const GLubyte* version = glGetString(GL_VERSION);
    std::cout << "GPU: " << renderer << std::endl;
    std::cout << "OpenGL Version: " << version << std::endl;
    GLuint vertex_shader;
    GLuint fragment_shader;
    GLuint program;
    GLuint vertex_array_object;

    vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shader, 1, vertex_shader_source, NULL);
    glCompileShader(vertex_shader);

    GLint isCompiled = 0;
    glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &isCompiled);
    if (isCompiled == GL_FALSE){
        return;
    }

    fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment_shader, 1, fragment_shader_source, NULL);
    glCompileShader(fragment_shader);

    glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &isCompiled);
    if (isCompiled == GL_FALSE){
        return;
    }

    program = glCreateProgram();
    glAttachShader(program,vertex_shader);
    glAttachShader(program, fragment_shader);
    glLinkProgram(program);

    GLint isLinked = 0;
    glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
    if (isLinked == GL_FALSE){
        return;
    }

    glDetachShader(program, vertex_shader);
    glDetachShader(program, fragment_shader);

    glDeleteShader(vertex_shader);
    glDeleteShader(fragment_shader);

    glCreateVertexArrays(1, &vertex_array_object);
    glBindVertexArray(vertex_array_object);

    while (!glfwWindowShouldClose(window)){
        float currentTime = glfwGetTime();
        const GLfloat color[] = { 
            (float)sin(currentTime)*0.5f+0.5f,
            (float)cos(currentTime)*0.5f+0.5f,
            0.0f,
            0.1f
        };
        glClearBufferfv(GL_COLOR, 0, color);

        glUseProgram(program);


        glBindVertexArray(vertex_array_object);
        glPointSize(40.0f);
        glDrawArrays(GL_POINTS, 0, 1);


        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwDestroyWindow(window);
    glfwTerminate();
    exit(EXIT_SUCCESS);
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

It looks like you're trying to draw a 40x40 pixel aqua colored square at (0.0, 0.0, 0.5), is that right? OpenGL producing no output is probably the most common symptom you'll run into. I immediately see several things that look a little off:

  1. Your shaders are requiring OpenGL 4.5. Are you sure your current OS/drivers/etc. implement OpenGL 4.5?
  2. You set the clear color (to 40% blue, but completely transparent? What does that mean?) but you never actually clear the color buffer by calling glClear(GL_COLOR_BUFFER_BIT);. This is actually a really good test. I'd clear the buffer to something with a 100% alpha, like glClearColor(0.0, 0.0, 0.4, 1.0); glClear(GL_COLOR_BUFFER_BIT); and see if you see a completely blue frame. If not, then you aren't even drawing anything.
  3. You never check glGetError(); to see if anything succeeded. (This goes back to #1 above.) If you do, you might figure out what exactly is going wrong.
  4. You don't show any code for setting any vertex positions, so you don't appear to be drawing any geometry.
\$\endgroup\$
2
  • \$\begingroup\$ 1. I'm sure I have 4.5 on my GPU (GTX 1080 8gb) 2. I set it to those 2 lines and you are right I do still see blue. 3. I added a basic glGetError() function and it says I have a invalid enum, but on the line that is calling the check error function. (No clue why that is happening) 4. From what I have understood in the book it says if I set a VAO and don't add any VBO's to it, it will still call the shader and output a point as I define its position in the shader itself and not in a VBO? \$\endgroup\$
    – 0xen
    Jul 3, 2016 at 9:22
  • \$\begingroup\$ After fixing the getError bug i found the problem and it was down to me not using 'glewExperimental = true;' and also deleting the shaders without detaching them from the program first, but your answer helped me, thanks! \$\endgroup\$
    – 0xen
    Jul 3, 2016 at 14:39

You must log in to answer this question.

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