0
\$\begingroup\$

I have been following the LearnOpenGL Hello Triangle tutorial, and nothing is rendering except the clear color.

Any help finding what is wrong would be appreciated.

Using LWJGL 3.2.3 OpenGL and GLFW bindings on MacOS.

My code:

// ---- WINDOW SETUP ----------------------------------
    glfwInit();

    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);

    long window = glfwCreateWindow(WIDTH, HEIGHT, TITLE, NULL, NULL);
    if (window == NULL) throw new IllegalStateException("Could not create window");

    glfwMakeContextCurrent(window);
    GL.createCapabilities();

    int[] fbWidth = new int[1];
    int[] fbHeight = new int[1];
    glfwGetFramebufferSize(window, fbWidth, fbHeight);
    glViewport(0, 0, fbWidth[0], fbHeight[0]);
    log.info("Framebuffer size: {} x {}", fbWidth[0], fbHeight[0]);

    glfwShowWindow(window);
    log.info("Created window");

    // ---- BUFFER SETUP ----------------------------------

    float[] vertices = {
            -0.5f, -0.5f, 0.0f,
             0.5f, -0.5f, 0.0f,
             0.0f,  0.5f, 0.0f
    };

    int vao = glGenBuffers();
    glBindVertexArray(vao);

    int vbo = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0L);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);

    log.info("Created buffers");

    // ---- SHADER SETUP ----------------------------------

    String vertexShaderSource =
            "#version 330 core\n" +
            "layout(location = 0) in vec3 aPos;\n" +
            "void main() {\n" +
            "    gl_Position = vec4(aPos, 1.0);\n" +
            "}";
    String fragmentShaderSource =
            "#version 330 core\n" +
            "out vec4 fragColor;\n" +
            "void main() {\n" +
            "    fragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" +
            "}";

    int vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, vertexShaderSource);
    glCompileShader(vertexShader);
    if (glGetShaderi(vertexShader, GL_COMPILE_STATUS) == GL_FALSE) {
        log.error("Vertex shader compilation failed:");
        log.error(glGetShaderInfoLog(vertexShader));
        return;
    }

    int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, fragmentShaderSource);
    glCompileShader(fragmentShader);
    if (glGetShaderi(fragmentShader, GL_COMPILE_STATUS) == GLFW_FALSE) {
        log.error("Fragment shader compilation failed:");
        log.error(glGetShaderInfoLog(fragmentShader));
        return;
    }

    int shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    if (glGetProgrami(shaderProgram, GL_LINK_STATUS) == GL_FALSE) {
        log.error("Shader program linking failed:");
        log.error(glGetProgramInfoLog(shaderProgram));
        return;
    }

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    log.info("Loaded shaders");

    // ---- MAIN LOOP ------------------------------------

    while (!glfwWindowShouldClose(window)) {
        glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(shaderProgram);

        glBindVertexArray(vao);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glBindVertexArray(0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // ---- CLEANUP --------------------------------------

    glDeleteProgram(shaderProgram);
    glDeleteBuffers(vbo);
    glDeleteVertexArrays(vao);

    glfwDestroyWindow(window);
    glfwTerminate();
\$\endgroup\$
3
  • \$\begingroup\$ Stride should be 3 * sizeof(float), not 0 in glVertexAttribPointer. \$\endgroup\$
    – Rish
    Commented Jul 12, 2021 at 2:28
  • 1
    \$\begingroup\$ @Rish - 0 is a valid value indicating tightly-packed data: khronos.org/registry/OpenGL-Refpages/gl4/html/… \$\endgroup\$ Commented Jul 12, 2021 at 4:30
  • \$\begingroup\$ @Maximus Minimus Thanks, TIL. \$\endgroup\$
    – Rish
    Commented Jul 12, 2021 at 13:02

1 Answer 1

1
\$\begingroup\$
int vao = glGenBuffers();
glBindVertexArray(vao);

This should be using glGenVertexArrays, not glGenBuffers.

\$\endgroup\$

You must log in to answer this question.

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