在OpenGL和OpenGLES中使用glVertexAttribPointer [英] glVertexAttribPointer in OpenGL and in OpenGLES

查看:164
本文介绍了在OpenGL和OpenGLES中使用glVertexAttribPointer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关OpenGL和OpenGLES的教程,我对在这两个API中使用函数 glVertexAttribPointer 感到有些困惑。

I'm reading tutorials about OpenGL and OpenGLES and I'm a bit confused about the use of the function glVertexAttribPointer in these two APIs.

OpenGL 教程中,此函数使用数字偏移作为最后一个参数(使用转换为const GLVoid *),我认为顶点直接取自当前数组缓冲区。

In the OpenGL tutorial this function uses as last parameter a numeric offset (with a casting to const GLVoid*) and I suppose that the vertices are directly taken from the current Array Buffer.

glVertexAttribPointer(vs_position, 2, GL_FLOAT, GL_TRUE, 5 * sizeof(GLfloat), (const GLvoid*) (3*sizeof(GLfloat)) );

OpenGLES 教程中,最后一个参数直接指向代表的结构顶点:

In the OpenGLES tutorial the last parameter points directly to a structure which represents vertices:

GLFloat vertices[] = {...definition};
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);

我无法理解这两个函数是如何工作的。它们完全不同吗?

I can't understand how these 2 functions work. Are they totally different functions?

推荐答案

它们是以两种不同方式使用的相同功能。我会解释为什么它会以这种方式运作,但你不会在乎,这是非常愚蠢和无关紧要的原因。

They are the same function being used in two different ways. I'd explain why it works this way, but you won't care, and it's for very stupid and irrelevant reasons.

重要的是他们正在做什么。他们正在做的事情取决于你没有展示的东西:什么是 GL_ARRAY_BUFFER

What matters is what they're doing. And what they're doing depends on something that you didn't show: what is bound to GL_ARRAY_BUFFER.

请参阅 glVertexAttribPointer 更改的行为,具体取决于此。如果在调用 glVertexAttribPointer 时没有绑定到 GL_ARRAY_BUFFER 的缓冲区对象,则该函数将假定最终值为指针(如函数名称所示:glVertexAttrib Pointer )。具体来说,它是指向客户端拥有的内存的指针。

See, the behavior of glVertexAttribPointer changes depending on that. If there is no buffer object bound to GL_ARRAY_BUFFER when you call glVertexAttribPointer, then the function will assume that the final value is a pointer (like the function's name says: glVertexAttribPointer). Specifically, it is a pointer into client-owned memory.

当需要渲染时,顶点属性数据将来自先前提供的指针。因此,第二个示例仅使用以标准C样式声明的客户端数据数组作为源数据。不涉及缓冲区对象。

When it comes time to render, the vertex attribute data will come from the previously provided pointer. Thus, the second example is just using an array of client data, declared in standard C style, as the source data. No buffer objects are involved.

注意:OpenGL 3.1+的核心配置文件删除了使用客户端内存的能力;在那里,你必须使用缓冲区对象,如下所述。

Note: the core profile of OpenGL 3.1+ removed the ability to use client memory; there, you must use buffer objects, as explained below.

如果缓冲区对象绑定到 GL_ARRAY_BUFFER 当调用 glVertexAttribPointer 时,会发生一些特殊情况。 OpenGL将假装指针(这是最终参数就C / C ++而言)实际上是一个字节偏移进入缓冲区绑定到 GL_ARRAY_BUFFER 。它会将指针转换为整数,然后存储该整数偏移量和当前绑定到 GL_ARRAY_BUFFER 的缓冲区对象。

If a buffer object is bound to GL_ARRAY_BUFFER when glVertexAttribPointer is called, then something special happens. OpenGL will pretend that the pointer (which is what the final parameter is as far as C/C++ is concerned) is actually a byte offset into the buffer bound to GL_ARRAY_BUFFER. It will convert the pointer into an integer and then store that integer offset and the buffer object currently bound to GL_ARRAY_BUFFER.

因此上面的代码采用 3 * sizeof(GLfloat),字节偏移量,并将其转换为指针。 OpenGL将获取指针并将其转换回偏移量,再次产生 3 * sizeof(GLfloat)

So the above code takes 3*sizeof(GLfloat), the byte offset, and converts it into a pointer. OpenGL will take the pointer and convert it back into an offset, yielding 3*sizeof(GLfloat) again.

当渲染时,OpenGL将使用先前给定的偏移量从先前给定的缓冲区对象读取。

When it comes time to render, OpenGL will then read from the previously given buffer object, using the previously given offset.

第一个示例将顶点数据放入缓冲区对象在GPU内存中。第二个例子将顶点数据放在CPU内存中的常规C / C ++数组中。

The first example puts the vertex data into a buffer object in GPU memory. The second example puts the vertex data in a regular C/C++ array, in CPU memory.

这篇关于在OpenGL和OpenGLES中使用glVertexAttribPointer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆