glDrawArrays由于某些原因无法正常工作?

时间:2020-05-22 08:21:47

标签: c++ opengl glfw

我不明白为什么glDrawArrays不起作用。

如果您发现任何不适当的地方或丢失的东西,我真的需要知道。

#define GLEW_STATIC
#include <GL/glew.h>

static 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 length;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);

        char* message = (char*)alloca(length * sizeof(char));
        glGetShaderInfoLog(id, length, &length, message);

        std::cout << message ;
    }

    return id;
}

static unsigned int CreateShader(const std::string& Vertexshader, const std::string& Fragmentshader) {

    unsigned int program = glCreateProgram();
    unsigned int vertex = CompileShader(GL_VERTEX_SHADER, Vertexshader);
    unsigned int fragment = CompileShader(GL_FRAGMENT_SHADER, Fragmentshader);

    glAttachShader(program, vertex);
    glAttachShader(program, fragment);

    glLinkProgram(program);
    glValidateProgram(program);

    return program;
}

int main(void)
{
    GLFWwindow* window;

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

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    if (GLEW_OK == glewInit())
    {

    }

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

    unsigned int buffer1;

    glGenBuffers(1, &buffer1);
    glBindBuffer(GL_ARRAY_BUFFER, buffer1);
    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), vertices, GL_STATIC_DRAW);


    glVertexAttribPointer(0, 6, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);

    glEnableVertexAttribArray(0);

    std::string Vertexshader =

        "#version 330 core\n"
        "\n"
        "layout (location = 0)in vec4 position;"
        "\n"
        "void main()\n"
        "{\n"
        "    gl_Position = position;\n"
        "}\n";

    std::string Fragmentshader =

        "#version 330 core\n"
        "\n"
        "layout (location = 0)out vec4 color;"
        "\n"
        "void main()\n"
        "{\n"
        "    color = vec4(1.0, 0.0, 0.0, 1.0);\n"
        "}\n";


    unsigned int shader = CreateShader(Vertexshader, Fragmentshader);
    glUseProgram(shader);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(shader);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

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

    glfwTerminate();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

glVertexAttribPointer的第二个参数是坐标的元组大小,而不是顶点数组中的浮点数。
数组中的每个顶点都包含2个分量,因此 size 参数必须为2而不是6:

glVertexAttribPointer(0, 6, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);