OpenGL - 需要一个简单的概念澄清 [英] OpenGL - need a simple concept clarification

查看:24
本文介绍了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 - 需要一个简单的概念澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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