OpenGL常用组合API套路

2018-04-29  本文已影响21人  flow__啊

基于OpenGL 3.3 记录了一些OpenGL常用的组合API。初学的,这篇可能还是太毛了

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 == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

加载GLAD指针

        if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

创建VAO:

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

然后是VBO:

unsigned int VBO;
//绑定+复制VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

接着是EBO:

unsigned int EBO;
glGenBuffers(1, &EBO);
//绑定+复制EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

记得配置下顶点给顶点着色器使用。
然后是渲染循环:

    while (!glfwWindowShouldClose(window))
    {
        // 处理事件
        processInput(window);

        // 渲染
        // 但是觉得每一次都清除下画面的颜色,会很费时间,应该有别的解决方案
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        //  绑定纹理   bind textures on corresponding texture units
                .....
        //  创造动画 create transformations
                .....
        // render container
        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);


        //交换缓冲
        glfwSwapBuffers(window);
        //检查并调用事件
        glfwPollEvents();
    }
上一篇下一篇

猜你喜欢

热点阅读