GLFW 和 GLEW 不显示三角形红色

GLFW and GLEW not showing triangle red

我的代码没有显示红色三角形,而是黑屏。我正在关注 The Cherno 的 OpenGL 教程。这发生在第7集的结尾。我在 Windows 10 Home 上使用带有 Visual C++ 的 Visual Studio 2019。我的显卡是 NVidia 820m.Link for tutorial I was following.

#include <gl/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>

static unsigned int CompileShader(unsigned int type, const std::string& source)
{
    unsigned int id = glCreateShader(type);
    const char* src = source.c_str();
    glShaderSource(id, 1, &src, nullptr);
    glCompileShader(id);

    int result;
    
    glGetShaderiv(id, GL_COMPILE_STATUS, &result);
        if (result = GL_FALSE) 
        {
            int legnth;
            glGetShaderiv(id, GL_INFO_LOG_LENGTH, &legnth);
            char* message = (char*)alloca(legnth * sizeof(char));
            glGetShaderInfoLog(id, legnth, &legnth, message);
            std::cout << "Failed to Compile" << (type == GL_VERTEX_SHADER ? "Vertex: Shader:\n" : "Fragment Shader:\n");
            glDeleteShader(id);
            
            return 0;
        }

    return id;
}

static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader) 
{
    unsigned int program = glCreateProgram();
    unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
    unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
    glAttachShader(program, vs);
    glAttachShader(program, fs);
    glLinkProgram(program);
    glValidateProgram(program);

    glDeleteShader(vs);
    glDeleteShader(fs);

    return program;
}

int main(void)
{
    GLFWwindow* window;
    if (!glewInit())
        return -1;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */

    window = glfwCreateWindow(640, 480, "Cooked Pixel", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    
    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK) 
    {
        std::cout << "Error: GLEW is not initialized \n";
    }

    std::cout << "GL Version" << glGetString(GL_VERSION) << "\n";
    
    float positions[6] = {
        -0.5, -0.5,
        0.0, -0.5,
        0.5, -0.5
    };
    unsigned int buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
    
    std::string vertexShader = R"glsl(
    #version 330 core
    layout(location = 0) in vec4 position;
    void main()
    {
        gl_Position = position;
    }
        
    )glsl";

    std::string fragmentShader = R"glsl(
    #version 330 core
    layout(location = 0) out vec4 color;
    void main()
    {
        color = vec4(1.0, 0.0, 0.0, 1.0);
    }
        
    )glsl";
    unsigned int shader = CreateShader(vertexShader, fragmentShader);

    glUseProgram(shader);

    glfwMaximizeWindow(window);
    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        /* Very Old Code
        
        glBegin(GL_TRIANGLES);
        glVertex2f(-1.0, -1.0);
        glVertex2f(0, -1.0);
        glVertex2f(0, -3.0);
        glEnd();
        */
        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

你的“三角形”不是三角形,它只是一条直线。

float positions[6] = {
  -0.5, -0.5,
   0.0, -0.5,
   0.5, -0.5
};

更改顶点坐标:

float positions[6] = {
    -0.5, -0.5,
     0.0, -0.5,
     0.5, 0.5
};

不要调用 glewInit() 两次。在使用 glfwMakeContextCurrent.

使 OpenGL 上下文成为当前上下文后仅调用一次

另外你应该创建一个 Vertex Array Object:

unsigned int vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);