Hello Triangle

作者: 美不胜收oo | 来源:发表于2018-10-25 23:09 被阅读0次

    使用OpenGL绘制图形的步骤

    1. 一般来说,顶点着色器和片段着色器需要我们自定义的。所以首先自定义顶点着色器和片段着色器,按照格式写出他们的源码。
    2. 使用GLFW完成窗口的初始创建。
    3. 使用GLAD加载所有OpenGL函数指针。
    4. 创建着色器,并检查他们是否创建成功。
    5. 创建连接程序。我觉得就是把渲染管线搭好。检查是否搭好,然后就能把之前创建的着色器都删除了。
    6. 架子搭好了,然后就应该塞数据了。创建顶点缓冲对象,顶点数组对象;绑定顶点数组对象;绑定顶点缓冲对象;往顶点缓冲对象里边塞数据;设置顶点属性;解绑。
    7. 然后while循环进行绘制。
    8. 窗口销毁后,删除顶点数组对象,顶点缓冲对象。

    代码

    #include <glad/glad.h>
    #include<GLFW/glfw3.h>
    
    #include <iostream>
    
    void framebuffer_size_callback(GLFWwindow* window, int width, int height)
    {
        glViewport(0, 0, width, height);
    }
    
    void processInput(GLFWwindow* window)
    {
        if (glfwGetKey(window,GLFW_KEY_ESCAPE) == GLFW_PRESS)
        {
            glfwSetWindowShouldClose(window, true);
        }
    }
    
    //settings
    const unsigned int SCR_WIDTH = 800;
    const unsigned int SCR_HEIGHT = 600;
    
    const char *vertexShaderSource = "#version 330 core\n"
    "layout (location = 0) in vec3 aPos;\n"
    "void main()\n"
    "{\n"
    "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
    "}\0";
    const char *fragmentShaderSource = "#version 330 core\n"
    "out vec4 FragColor;\n"
    "void main()\n"
    "{\n"
    "   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
    "}\n\0";
    
    int main()
    {
        //GLFW初始化
        glfwInit();
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    
        //创建窗口
        GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
        if (!window)
        {
            std::cout << "Failed to create GLFW window" << std::endl;
            glfwTerminate();
            return -1;
        }
        glfwMakeContextCurrent(window);
        glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    
        //glad: 加载所有OpenGL函数的指针
        if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
        {
            std::cout << "Failed to initialize GLAD" << std::endl;
            return - 1;
        }
    
        //窗口初始化完成,并加载了OpenGL的函数,下面进行OpenGL相关的操作
    
        //着色器
        //----------------------------------------------------
        //创建顶点着色器
        int vertexShader = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
        glCompileShader(vertexShader);
        //检查顶点着色器是否创建成功
        int success;
        char infoLog[512];
        glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
        if (!success)
        {
            glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
            std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
        }
        //创建片段着色器
        int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
        glCompileShader(fragmentShader);
        //检查片段着色器是否创建成功
        glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
        if (!success)
        {
            glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
            std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
        }
    
        //创建完顶点着色器和片段着色器之后,我们应该把他们链接到一起
        int shaderProgram = glCreateProgram();
        glAttachShader(shaderProgram, vertexShader);
        glAttachShader(shaderProgram, fragmentShader);
        glLinkProgram(shaderProgram);
        //检查是否链接成功
        glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
        if (!success)
        {
            glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
            std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << std::endl;
        }
        //链接成功,删除创建的Shaders
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
    
        //至此,OpenGL渲染管线已经搭建完毕,我们要塞数据了
        //----------------------------------------------------------------------------------------------
        //设置三个顶点
        float vertices[] = {
            -0.5f, -0.5f, 0.0f, // left  
             0.5f, -0.5f, 0.0f, // right 
             0.0f,  0.5f, 0.0f  // top   
        };
        //创建顶点缓冲对象,顶点数组对象
        unsigned int VBO, VAO;
        glGenVertexArrays(1, &VAO);
        glGenBuffers(1, &VBO);
        //绑定顶点数组对象,然后绑定并设置顶点缓冲,之后再设置顶点属性
        //-------------------------------------------------------------------
        //绑定顶点数组对象
        glBindVertexArray(VAO);
        //绑定顶点缓冲对象
        glBindBuffer(GL_ARRAY_BUFFER, VBO);
        //往顶点缓冲对象里边塞数据
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW);
        //设置顶点属性
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);
        //解绑?
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    
        while (!glfwWindowShouldClose(window))
        {
            processInput(window);
            glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT);
            //开始绘制三角形!
            glUseProgram(shaderProgram);
            glBindVertexArray(VAO);
            glDrawArrays(GL_TRIANGLES, 0, 3);
    
            glfwSwapBuffers(window);
            glfwPollEvents();
        }
    
        glDeleteVertexArrays(1, &VAO);
        glDeleteBuffers(1, &VBO);
    
        glfwTerminate();
        return 0;
    
    }
    

    效果

    image.png

    相关文章

      网友评论

        本文标题:Hello Triangle

        本文链接:https://www.haomeiwen.com/subject/apehtqtx.html