【问题标题】:How to store a VBO in a custom object如何将 VBO 存储在自定义对象中
【发布时间】:2014-03-04 06:34:35
【问题描述】:

我想以顶点缓冲区对象的形式在每个实例中存储我的Cat 对象的顶点、法线和纹理信息,但我不知道如何。我想要这样的东西:

@property(nonatomic, assign) int *indices; // vertex indices for glDrawElements
@property(nonatomic, assign) GLKVertexAttrib vertexBufferObject; // ideally
@property(assign) GLKVector2 position;
@property(assign) GLKVector2 velocity;

如果你不能创建包含特定 VBO 的对象,你会怎么做?

【问题讨论】:

    标签: opengl opengl-es glkit vertex-buffer vertex-attributes


    【解决方案1】:

    我最终这样做了:

    @property(nonatomic, assign) int *indices;
    @property(nonatomic, assign) GLuint vertexBuffer; // good
    @property(assign) GLKVector2 position;
    @property(assign) GLKVector2 velocity;
    
    - (id)initWithVertices: (SceneVertex [])vertices size: (uint) size; // very good
    - (void)render;
    

    然后是这个:

    - (id) initWithVertices:(SceneVertex [])vertices size:(uint)size
    {
        if (self = [super init])
        {
            glGenBuffers(1, &_vertexBuffer);
            glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    
            glBufferData(GL_ARRAY_BUFFER, size * sizeof(SceneVertex), vertices, GL_STATIC_DRAW);
    
            glEnableVertexAttribArray(GLKVertexAttribPosition);
            int stride = sizeof(GLKVector3) * 2;
            glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET(0));
            glEnableVertexAttribArray(GLKVertexAttribNormal);
            glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET(sizeof(GLKVector3)));
        }
    
        return self;
    }
    

    结果很好。现在我所有的信息都包含在每个对象中。太棒了!

    【讨论】:

      猜你喜欢
      • 2011-01-19
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-16
      相关资源
      最近更新 更多