glVertexAttribPointer 用法简介

Posted rainbow70626

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了glVertexAttribPointer 用法简介相关的知识,希望对你有一定的参考价值。

在内存中采用交叉模式存储,向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 用法简介
版权声明:本文为博主原创文章,转载请附上博文链接!




以上是关于glVertexAttribPointer 用法简介的主要内容,如果未能解决你的问题,请参考以下文章

缓冲区和 glVertexAttribPointer 之间的关系

如何使用 glVertexAttribPointer() 添加非数组值

Opengl顶点着色器为每个顶点设置布尔值(glVertexAttribPointer)

glVertexAttribPointer 出现 JOGL 错误

glVertexAttribPointer 返回 GL_INVALID_OPERATION

OpenGL glVertexAttribPointer 正常