问题
I just started OpenGL for iPhone, using GLKit. My programming background is almost just java and objective c with little,little experiences with C, C++ over ten years ago. All what remained is a remote memory of how I struggled with pointers - and I think I failed.
Now it seems as if it all comes back to me ...
I went to some iterations of the great, great tutorial series of Ian Terrel, which really helped me (Thanks!!!).
This question is about the following parts of the code (which is mostly directly taken from the tutorials):
@interface AAAShape : NSObject
{
NSMutableData *vertexData;
// ...
}
@property(readonly) int vertexCount;
@property(readonly) GLKVector2 *vertices;
//...
@end
@implementation AAAShape
//...
-(GLKVector2 *)vertices
{
if(!vertexData)
{
vertexData = [NSMutableData dataWithLength:sizeof(GLKVector2)*self.vertexCount];
}
return [vertexData mutableBytes];
}
-(void)renderInScene:(AAAScene *)scene
{
//... Effect Stuff
//...
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, self.vertices);
//...
}
//...
@end
//Using the Shape:
//(AATriangle is of AAAShape and implements the vertextCount with "return 3;")
AAATriangle *triangle = [[AAATriangle alloc]init];
triangle.vertices[0] = GLKVector2Make(2., .0);
triangle.vertices[1] = GLKVector2Make(-2., .0);
triangle.vertices[2] = GLKVector2Make(.0, -3.);
//...
All of this works really fine, but then I stumbled upon the following in Apple's OpenGl Guide:
[...], but is inefficient. Each time DrawModel is called, the index and vertex data are copied to OpenGL ES, and transferred to the graphics hardware.[...] can impact performance. [...]your application should store its vertex data in a vertex buffer object (VBO). [...]
The suggested code sample there, for doing that (other sources showing nearly the same) looks like this:
GLuint vertexBuffer;
void CreateVertexBuffers()
{
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
}
and later drawing with:
void DrawModelUsingVertexBuffers()
{
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(vertexStruct), (void*)offsetof(vertexStruct,position));
glEnableVertexAttribArray(ATTRIB_POSITION);
//...
}
I've a few questions about it:
- How significant are the mentioned performance impacts? Is it necessary to change the code?
- What the ... is really going on in the first code samples above (Ian's/my code)?
- Why is it possible to set vertices[i] if vertices is readonly and where and how memory is allocated for vertices?
- Where could I put the code above (the buffer-creating and -binding stuff) in Ian's/my approach and why is there no connection (in means of variables are method calls or something) between the binding and the drawing?
回答1:
- How significant are the mentioned performance impacts? Is it necessary to change the code?
If the data is constantly changing, there is little gained from a VBO. If however the geometry is static it makes a huge difference, as it saves precious bandwidth between CPU and GPU not to copy it all the time.
- What the ... is really going on in the first code samples above (Ian's/my code)?
An array is filled with the vertex data. Then a pointer in OpenGL is set to that data. When glDraw… is called, the pointers are dereferenced, and the data fetch from process memory to the render side, processed there in drawing operations.
- Why is it possible to set vertices[i] if vertices is readonly and where and how memory is allocated for vertices?
Because only the pointer is readonly. The variable it points to is read/write
- Where could I put the code above (the buffer-creating and -binding stuff) in Ian's/my approach
glGenBuffers and glBufferData go to the vertex data creation. glVertexPointer stays where it is. and glBindBuffer is used at both sites.
and why is there no connection (in means of variables are method calls or something) between the binding and the drawing?
I'm note sure what you mean by that question.
来源:https://stackoverflow.com/questions/8430108/opengl-sending-vertex-pointers-or-generating-buffers