[英]Batching quads creates bad color blend 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放入循环中并分别处理每个四边形(而不是按批处理渲染)来分别绘制它们,这不是预期的,而是不是最佳的。
任何帮助将不胜感激!
最好的选择是GL_TRIANGLE_STRIP
存在问题。 它看起来像是在每个矩形之后绘制了一个额外的三角形(您可以看到它形成了除了最后一个矩形的每个顶点的顶部2个顶点,再到下一个矩形的左底部。因为这些顶点具有不同的颜色,所以看起来像某种混合)。
无论如何,对于三角形带,每个矩形仅需要2个顶点+在开始处(或如果需要的话,在末端)需要2个顶点,但是如果操作正确,您将在整个场景中得到一个很好的混合幽灵,因为顶点将再次具有不同的颜色。
如果您要做的只是使这些具有特定颜色的矩形不混合,那么您似乎要做的就是将GL_TRIANGLE_STRIP
切换为GL_TRIANGLES
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.