问题描述
我是OpenGL的新手.我了解如何创建顶点和片段着色器,以及如何创建顶点数组并将其放在缓冲区中,但是我如何链接两者?含义-当我运行程序时,如何知道当前处于活动状态的缓冲区上的顶点数组是否应馈入"顶点着色器?
I'm new to OpenGL.I understand how to create the vertex and fragments shaders and how to create vertex arrays and put them on the buffer, but how do i link the two?Meaning - when i run my program, how does it know that the vertices array that is on the currently active buffer should be "fed" to the vertex shader?
是通过使用glVertexAttribPointer
来完成的吗?
Is that being done simply by using glVertexAttribPointer
?
谢谢!
推荐答案
实际上没有当前活动缓冲区"之类的东西. (好吧,但是只有在VAO规范中使用非直接状态访问API时才有意义.)
There's no really such thing as "currently active buffer". (Well, there is but it's relevant only at the time of VAO specification with the non direct-state-access API.)
顶点数组对象(VAO)将指针(通过glVertexAttribPointer
设置)保存到内存缓冲区内的数据中.与这些缓冲区的绑定是VAO状态的一部分.绑定VAO时,随后的glDraw*
命令会将顶点从绑定到当前VAO的缓冲区传输到管道.
The vertex array object (VAO) holds pointers (set through glVertexAttribPointer
) into the data within the memory buffers. The bindings to those buffers are a part of the state of the VAO. When you bind a VAO then subsequent glDraw*
commands will transfer the vertices from the buffers bound to the current VAO to the pipeline.
为了简化理解,以下是VAO的大致含义,其中带有用于设置该状态的相关OpenGL 4.5直接状态访问函数名称:
For simplicity of comprehension, here is what a VAO roughly is, with the relevant OpenGL 4.5 direct-state-access function names used to set that state:
struct VertexArrayObject
{
// VertexArrayElementBuffer
uint element_buffer;
struct Binding {
// VertexArrayVertexBuffers
uint buffer;
intptr offset;
sizei stride;
// VertexArrayBindingDivisor
uint divisor;
} bindings[];
struct Attrib {
// VertexArrayAttribBinding
uint binding; // This is an index into bindings[]
// EnableVertexArrayAttrib
bool enabled;
// VertexArrayAttrib*Format
int size;
enum type;
boolean normalized;
boolean integer;
boolean long;
uint relativeoffset;
} attribs[];
};
调用glVertexAttribPointer
时,它实际上对当前绑定的VAO执行以下操作:
When you call glVertexAttribPointer
it essentially does the following on the currently bound VAO:
vao.attribs[index].binding = index;
vao.attribs[index].size = size;
vao.attribs[index].type = type;
vao.attribs[index].normalized = normalized;
vao.attribs[index].relativeoffset = 0;
vao.bindings[index].buffer = current ARRAY_BUFFER;
vao.bindings[index].offset = pointer;
vao.bindings[index].stride = stride; // if stride == 0 computes based on size and type
这篇关于OpenGL-需要简单的概念说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!