我正在尝试批量处理一堆矩形/四边形并进行一些奇怪的混合:
每个矩形似乎都获得了一点其邻居的颜色。
这是我的测试代码:
typedef struct {
Vertex bl;
Vertex br;
Vertex tl;
Vertex tr;
} TexturedQuad;
typedef struct {
CGPoint geometryVertex;
} Vertex;
...
- (void)setupTextureQuad {
self.quads = [NSMutableArray arrayWithCapacity:1];
for (int i = 0; i < 10; i++) {
TexturedQuad newQuad;
newQuad.bl.geometryVertex = CGPointMake(i * self.contentSize.width, 0.);
newQuad.br.geometryVertex = CGPointMake(self.contentSize.width + (i * self.contentSize.width), 0.);
newQuad.tl.geometryVertex = CGPointMake(i * self.contentSize.width, self.contentSize.height);
newQuad.tr.geometryVertex = CGPointMake(self.contentSize.width + (i * self.contentSize.width), self.contentSize.height);
[self.quads addObject:[NSValue value:&newQuad withObjCType:@encode(TexturedQuad)]];
}
_vertices = malloc([self.quads count] * sizeof(CGPoint) * 4);
_colors = malloc([self.quads count] * sizeof(GLKVector4) * 4);
}
...
- (void)render {
[self.effect prepareToDraw];
int i = 0;
int c = 0;
for (NSValue *aQuad in self.quads) {
TexturedQuad quad;
[aQuad getValue:&quad];
long offset = (long)&quad;
float randomRed = (arc4random() % 100)/100.;
float randomeGreen = (arc4random() % 100)/100.;
float randomBlue = (arc4random() % 100)/100.;
_vertices[i] = quad.bl.geometryVertex;
++i;
_vertices[i] = quad.br.geometryVertex;
++i;
_vertices[i] = quad.tl.geometryVertex;
++i;
_vertices[i] = quad.tr.geometryVertex;
++i;
_colors[c] = GLKVector4Make(randomRed, randomeGreen, randomBlue, 1.);
++c;
_colors[c] = GLKVector4Make(randomRed, randomeGreen, randomBlue, 1.);
++c;
_colors[c] = GLKVector4Make(randomRed, randomeGreen, randomBlue, 1.);
++c;
_colors[c] = GLKVector4Make(randomRed, randomeGreen, randomBlue, 1.);
++c;
}
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, _vertices);
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, 0, _colors);
glDrawArrays(GL_TRIANGLE_STRIP, 0, i);
}
通过将 glDrawArrays 放在循环中并使用每个四边形来单独绘制它们,而不是按预期但不是最佳的批处理渲染。
任何帮助将不胜感激!