分享
三行代码  ›  专栏  ›  技术社区  ›  chiefus

glDrawArrays由于某种原因无法工作?

  •  1
  • chiefus  · 技术社区  · 5 天前

    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 回复  |  直到 5 天前
        1
  •  1
  •   Rabbid76    5 天前

    第二个参数 glVertexAttribPointer 是坐标的元组大小,而不是顶点数组中的浮点数。
    大小 参数必须是2而不是6:

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

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