0
我想批一堆矩形/四邊形,並得到了一些奇怪的混合:配料四邊形產生不良顏色混合的OpenGL ES2
每個矩形似乎得到了鄰居的顏色一點點。
這裏是我的測試代碼:
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和工作用每一獨立畫他們四分之一,而不是批量渲染如預期的那樣,但不是最佳的。
任何幫助將不勝感激!