OpenGL GLFW + GLAD 程序不绘制三角形

OpenGL GLFW + GLAD Program not drawing triangle

我正在尝试用 GLFW 和 Glad 画一个三角形。但是屏幕不显示任何东西(我只能清屏)。

我可以打开 Window,并将屏幕清除为深蓝色(或任何其他颜色)

但是我无法在屏幕上绘制三角形,我也不知道为什么。

我想也许我的 GPU 没有默认着色器。

#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <string.h>
#include <vector>

void Log(const char* str) {
    std::cout << str << std::endl;
}


void error_callback(int error, const char* description)
{
    std::cout << "Error Code ["<< stderr << "] Error: \n" << description << std::endl;
}

class Shader {

public:
    Shader(const std::string& vertexSrc, const std::string& fragSrc) {
        // Create an empty vertex shader handle
        GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);

        const GLchar* source = vertexSrc.c_str();
        glShaderSource(vertexShader, 1, &source, 0);

        glCompileShader(vertexShader);

        GLint isCompiled = 0;
        glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &isCompiled);
        if (isCompiled == GL_FALSE)
        {
            GLint maxLength = 0;
            glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &maxLength);

            std::vector<GLchar> infoLog(maxLength);
            glGetShaderInfoLog(vertexShader, maxLength, &maxLength, &infoLog[0]);

            glDeleteShader(vertexShader);

            return;
        }

        GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

        source = fragSrc.c_str();
        glShaderSource(fragmentShader, 1, &source, 0);

        glCompileShader(fragmentShader);

        glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &isCompiled);
        if (isCompiled == GL_FALSE)
        {
            GLint maxLength = 0;
            glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &maxLength);

            std::vector<GLchar> infoLog(maxLength);
            glGetShaderInfoLog(fragmentShader, maxLength, &maxLength, &infoLog[0]);

            glDeleteShader(fragmentShader);
            glDeleteShader(vertexShader);

            return;
        }

        m_RendererID = glCreateProgram();

        glAttachShader(m_RendererID, vertexShader);
        glAttachShader(m_RendererID, fragmentShader);

        glLinkProgram(m_RendererID);

        // Note the different functions here: glGetProgram* instead of glGetShader*.
        GLint isLinked = 0;
        glGetProgramiv(m_RendererID, GL_LINK_STATUS, (int*)&isLinked);
        if (isLinked == GL_FALSE)
        {
            GLint maxLength = 0;
            glGetProgramiv(m_RendererID, GL_INFO_LOG_LENGTH, &maxLength);

            std::vector<GLchar> infoLog(maxLength);
            glGetProgramInfoLog(m_RendererID, maxLength, &maxLength, &infoLog[0]);

            glDeleteProgram(m_RendererID);
            glDeleteShader(vertexShader);
            glDeleteShader(fragmentShader);

            return;
        }

        Log("Shader compiled Successfully at:");
        std::cout << m_RendererID << std::endl;

        glDetachShader(m_RendererID, vertexShader);
        glDetachShader(m_RendererID, fragmentShader);
    }
    ~Shader() {
        glDeleteProgram(m_RendererID);
    }

    void Bind() const {
        glUseProgram(m_RendererID);
    }
    void Unbind() const {
        glUseProgram(0);
    }
private:
    uint32_t m_RendererID = 0;
};

int main() {

    Log("Initializing...");

    glfwSetErrorCallback(error_callback);

    if (!glfwInit())
    {
        // Initialization failed
        Log("Initialization Failed");
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

    GLFWwindow* window = glfwCreateWindow(1080, 720, "GLFW_GLAD TEST", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        Log("Window or OpenGL Context Failed");
    }
    glfwMakeContextCurrent(window);
    gladLoadGL();
    glfwSwapInterval(1);

    int width, height;
    glfwGetFramebufferSize(window, &width, &height);
    glViewport(0, 0, width, height);

    //APP CODE
    unsigned int m_VertexArray;
    unsigned int m_VertexBuffer;
    unsigned int m_IndexBuffer;

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

    // Vertex Array
    glGenVertexArrays(1, &m_VertexArray);
    glBindVertexArray(m_VertexArray);

    // Vertex Buffer
    glGenBuffers(1, &m_VertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glDisableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);


    // Index Buffer
    glGenBuffers(1, &m_IndexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_IndexBuffer);

    unsigned int indices[3] = {
        0, 1, 2,
    };
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    std::string vertexSrc = R"(
        #version 330
    
        layout(location = 0) in vec3 a_Pos;
        void main()
        {
            gl_Position = vec4(a_Pos, 1);
        }
    )";
    std::string fragmentSrc = R"(
        #version 330
        
        layout(location = 0) out vec4 FragColor;
        void main()
        {
            FragColor = vec4(0.8f, 0.5f, 0.2f, 1.0f);
        }
    )";

    Shader* shader = new Shader(vertexSrc, fragmentSrc);

    shader->Bind();


    while (!glfwWindowShouldClose(window))
    {
        glViewport(0, 0, width, height);
        glClearColor(0.1f, .1f, .14f, 1);
        glClear(GL_COLOR_BUFFER_BIT);

        glBindVertexArray(m_VertexArray);
        glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);


        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
     glfwTerminate();
    return 0;
}

这里绝对是错误的。

glDisableVertexAttribArray(0);

你这里的意思是glEnableVertexAttribArray,所以你设置的属性指针实际上是使用的。对于禁用的属性数组,所有着色器调用都将获得一个常量值。

您的着色器也使用 #version 330 并且实际上需要 GLSL 330 语法,但是:

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

您仅请求 GL 2.0 上下文,而 GL 2.0 仅要求支持 GLSL 1.10。这是否可行将完全取决于您的驱动程序,您可能会得到一些 >= 3.3 的兼容性配置文件,但您不能指望它。

您应该明确创建 3.3 上下文,并明确请求核心配置文件。