简体   繁体   English

OpenGL和C ++:实时处理和绑定缓冲区?

[英]OpenGL and C++: Real-Time Handling and Binding Buffers?

I have been toying with OpenGL for about 2 weeks, creating hardcoded VBOs and making rotating triangles, but the question that has been lingering in my head is how the heck do I create more than one VBO/VAO in real-time so I can handle multiple objects? 我一直在玩OpenGL,玩了大约2周,创建了硬编码的VBO,并制作了旋转的三角形,但是一直困扰着我的问题是,我到底该如何实时创建多个VBO / VAO,这样我才能处理多个对象? And how do I render multiple objects on the screen? 以及如何在屏幕上渲染多个对象? The binding system seems limiting and restrictive. 绑定系统似乎是限制性的和限制性的。 Eventually, I want to be able to create objects and draw them using a single Lua function. 最终,我希望能够使用单个Lua函数创建对象并绘制它们。 Sorry for the no-code post, but I have no code to begin with as I am trying to figure this out conceptually. 很抱歉,没有代码,但是我没有代码开头,因为我正在尝试从概念上解决问题。

When you said that the OpenGL binding system is limiting and restrictive you where right, it is. 当您说OpenGL绑定系统限制并限制了您的权利时,它就是正确的。 However, it is easy to get used to. 但是,这很容易习惯。 Your workflow when dealing with GL objects consists basically of: 处理GL对象时的工作流程主要包括:

  • Bind resource 绑定资源
  • Use (draw/modify/query) 使用(绘制/修改/查询)
  • Unbind resource 解除资源绑定

Now, going into specifics, how would you model a C++ class that encapsulate a VAO/VBO? 现在,具体来说,您将如何为封装VAO / VBO的C ++类建模? One very simple way, with implicit binding, could be: 具有隐式绑定的一种非常简单的方法可能是:

class MyVAO {
public:

    void Draw()
    {
        glBindVertexArray(vao);
        glDrawElements(...);
        // etc...
        glBindVertexArray(0);
    }

private:

    GLuint vb, ib, vao;
};

// Then in the client code:
MyVAO vao;
...
vao.Draw();

With explicit binding: 使用显式绑定:

class MyVAO {
public:

    void Bind()
    {
        glBindVertexArray(vao);
    }

    void Unbind()
    {
        glBindVertexArray(0);
    }

    void Draw()
    {
        glDrawElements(...);
        // etc...
    }

private:

    GLuint vb, ib, vao;
};

// Then in the client code:
MyVAO vao;
...
vao.Bind();
vao.Draw();
vao.Unbind();

The choice of explicit or implicit binding is yours. 显式或隐式绑定是您的选择。 Implicit binding automates the work a bit, but it can be wasteful when you are sharing resources in the code. 隐式绑定可以使工作自动化,但是当您共享代码中的资源时,这样做可能很浪费。 It can also lead to some hard to track bugs if you forget to bind/unbind inside the class implementation. 如果您忘记在类实现中进行绑定/取消绑定,那么也会导致一些难以跟踪的错误。 Explicit binding allows you to do things like leaving a resource bound for a long time and use it for a while, but it can be a nightmare to debug cases when you forget to bing/unbind the resource yourself. 显式绑定允许您执行一些操作,例如长时间绑定资源并使用一段时间,但是如果您忘记自己绑定/解除绑定资源,调试实例可能是一场噩梦。

I personally tend to prefer the explicit binding though. 我个人倾向于使用显式绑定。 I dislike having things happening behind the curtains. 我不喜欢幕后发生的事情。 I prefer to know when a resource is current. 我更想知道什么时候有资源。 But ultimately, you should choose the best fit for your project. 但最终,您应该选择最适合您的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM