• opengl入门篇二: 索引缓冲对象EBO


    在绘制图形的过程中,顶点可能会重复。比如两个三角形组成了四边形,那么,必然有两个点是重复的。因此采用索引的方式,四个点即可描述四边形。

    // 四个顶点
        GLfloat vertices[] = {
            // Positions         // Colors
            0.5f, 0.0f, 0.0f,   1.0f, 0.0f, 0.0f,  //右
            -0.5f, 0.0f, 0.0f,   0.0f, 1.0f, 0.0f,  //左
            0.0f,  0.5f, 0.0f,   0.0f, 0.0f, 1.0f,  //上
            0.0f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f   //下, 新加入顶点
        };
        GLuint 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);
      //对比是上节内容,新加入如下代码 GLuint indices[]
    = { //顶点索引 0, 1, 2, // First Triangle 0, 1, 3 // Second Triangle }; GLuint EBO; glGenBuffers(1, &EBO);//生成顶点索引 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);//绑定GL_ELEMENT_ARRAY_BUFFER缓冲 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);//指定索引数组
    // 顶点属性描述不变 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(0); // Color attribute glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); glEnableVertexAttribArray(1); glBindVertexArray(0); // Unbind VAO

    运行结果图片如下:

     

  • 相关阅读:
    C++计算器项目的初始部分
    视频教程自学计划
    1001.A+B Format (20)解题描述
    成为理想的自己
    Sample Join Analysis
    Sample MultipleFileWordcount CombineFileInputFormat
    FileOutputFormat
    Combine small files to Sequence file
    FileInputFormat
    Sample: Write And Read data from HDFS with java API
  • 原文地址:https://www.cnblogs.com/nazhizq/p/6582511.html
Copyright © 2020-2023  润新知