OPENGL class4 顶点属性

今天是第四节课,讲顶点属性,今天能把三角形还是不能显示出来,不着急,我们先把知识点理解一下。 OpenGL是管道式工作。就类似于我们提供数据给显卡,他就包含了我们需要绘制所有内容,我们使用 一个着色器,一个在显卡上执行的程序,读取数据,显示在屏幕上,通常,我们需要绘制几何图形的方 式就是使用顶点缓冲区,就是我们代码中使用api把position数据存储到显存中,着色器读取顶点缓冲区 里的数据时,他需要知道缓冲区的数据结构,缓冲区的数据结构就是一堆的浮点数。例如下面的代码。

float postion[6] =
{
-0.5f, -0.5f,
0.f, 0.5f,
0.5f, -0.5f
};

他们指定了顶点位置,其实我们还有纹理,法线之类的数据没有写入这个结构里面。所以我们需要通过 OpenGL告诉显卡,我们顶点缓冲区结构是怎么样的,是如何布局的?不然,显卡只能看到一堆字节, 这就像c++定义字符数组,传送到三方程序,没有定义结构体,你的数据就是乱的,没法解析是一个意 思。例如我们需要告诉显卡,我们前面12个字节是位置信息,后8个字节是纹理坐标。当然我们接下来 会通过代码去实现这个过程。我们这里使用一个关键函数:glVertexAttribPointer。 注意:顶点位置和顶点是两个概念,顶点不止有位置,顶点包含很多属性,颜色,纹理等属性。之所以 上节课没有绘制出三角形,其实是因为opengl他不知道你的position是顶点位置,他只知道是浮点数。 就比如说我这么定义position,代码也是能通过的,但Opengl在逻辑上就不知道你干嘛了。

float postion[] =
{
-0.5f, -0.5f,0.f,
0.f, 0.5f,0.f,
0.5f, -0.5f,0.f
};
glVertexAttribPointer
Parameters
index
Specifies the index of the generic vertex attribute to be modified.这个属性索引在哪
里
size
Specifies the number of components per generic vertex attribute. Must be 1, 2,
3, 4. Additionally, the symbolic constant GL_BGRA is accepted by
glVertexAttribPointer. The initial value is 4.有多少个这样浮点数,例如本节例子中有xy坐
标,就是size=2
type
好了,但我们今天讲了顶点属性这一个知识,不想一个视频占用太多时间,我们下节课再做分享。谢
谢!!!
 
Specifies the data type of each component in the array. The symbolic constants
GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, and
GL_UNSIGNED_INT are accepted by glVertexAttribPointer and
glVertexAttribIPointer. Additionally GL_HALF_FLOAT, GL_FLOAT, GL_DOUBLE,
GL_FIXED, GL_INT_2_10_10_10_REV, GL_UNSIGNED_INT_2_10_10_10_REV and
GL_UNSIGNED_INT_10F_11F_11F_REV are accepted by glVertexAttribPointer. GL_DOUBLE
is also accepted by glVertexAttribLPointer and is the only token accepted by the
type parameter for that function. The initial value is GL_FLOAT.
normalized
For glVertexAttribPointer, specifies whether fixed-point data values should be
normalized (GL_TRUE) or converted directly as fixed-point values (GL_FALSE) when
they are accessed.
stride
Specifies the byte offset between consecutive generic vertex attributes. If
stride is 0, the generic vertex attributes are understood to be tightly packed
in the array. The initial value is 0.这个表示每个顶点的偏移量是多少个字节,就是一个顶点有
多少个浮点数,因为opengl需要知道这数据才能从索引0跳转到索引1.
pointer
Specifies a offset of the first component of the first generic vertex attribute
in the array in the data store of the buffer currently bound to the
GL_ARRAY_BUFFER target. The initial value is 0.这个表示我如有有12个字节位置信息,8个字
节纹理信息,我需要知道纹理信息的索引,我这里就填入12,要位置信息填0.
To enable and disable a generic vertex attribute array, call
glEnableVertexAttribArray
/* Loop until the user closes the window */
    // 顶点缓冲区
    unsigned int buffer;
    glGenBuffers(1, &buffer);// 这表明buffer是显卡生成缓冲区id
    glBindBuffer(GL_ARRAY_BUFFER, buffer);// 相当于告诉显卡,buffer是顶点数组
    float postion[6] =
    {
        -0.5f, -0.5f,
         0.f,   0.5f,
         0.5f, -0.5f
    };
    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), postion, GL_STATIC_DRAW);
    ///第四节课代码
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);

好了,但我们今天讲了顶点属性这一个知识,不想一个文章占用篇幅,我们下节课再做分享。谢 谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值