glVertexAttribPointer 用法简介

2019/01/07 00:10
阅读数 272

在内存中采用交叉模式存储,向gpu传入顶点数据的方法 
这里写图片描述 
GPU:

#version 100
attribute highp vec2 aPosition;
attribute highp vec2 aTexcoord;

CPU: 
init()

//将顶点数组元素都存入一个缓存对象中
    static const float position[] = {
        -1.0f, -1.0f,0.0f,1.0f,//2 顶点位置 2 纹理坐标
        1.0f, -1.0f,1.0f,0.0f,
        -1.0f, 1.0f,0.0f,1.0f,
        1.0f, 1.0f,1.0f,1.0f
    };

    //-----------基于索引绘制
    static const GLushort plane_indices[] =
    {
        0, 1, 2, 3
    };
    glGenBuffers(1, &_index_buffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _index_buffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(plane_indices), plane_indices, GL_STATIC_DRAW);
    //--------------

    glGenBuffers(1, &_quad_vbo);
    glBindBuffer(GL_ARRAY_BUFFER, _quad_vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(position), NULL, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(position), position);

    glGenVertexArrays(1, &_vao);
    glBindVertexArray(_vao);

    GLint naPosition = mProgram->getAttribLocation("aPosition");
    glEnableVertexAttribArray(0);
    //注意,这里的offset 是内存中大小的偏移,必须准确计算出内存大小偏移量 sizeof
    glVertexAttribPointer(naPosition, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float), (GLvoid*)0);


    GLint naTexcoord = mProgram->getAttribLocation("aTexcoord");
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(naTexcoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (const GLvoid*)(2*sizeof(float)));

    glBindVertexArray(0);
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);

 

--------------------- 
作者:皮皮虾图形学 
来源:CSDN 
原文:glVertexAttribPointer 用法简介
版权声明:本文为博主原创文章,转载请附上博文链接!
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部