2014-03-04 81 views
0

我想以每個實例的頂點緩衝區對象的形式存儲我的Cat對象的頂點,常規和紋理信息,但我不知道如何。我想是這樣的:如何在自定義對象中存儲VBO

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

如果您不能創建包含特定對象維也納組織,你會怎麼做?

回答

0

我落得這樣做:

@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; 
} 

而且它的工作就好了。現在我的所有信息都包含在每個對象中。真棒!

相關問題