Ios 批处理四边形会创建错误的颜色混合opengl es2

Ios 批处理四边形会创建错误的颜色混合opengl es2,ios,objective-c,opengl-es,opengl-es-2.0,Ios,Objective C,Opengl Es,Opengl Es 2.0,我尝试批量处理一组矩形/四边形,并得到一些奇怪的混合: 每个矩形似乎都有一点邻居的颜色 以下是我的测试代码: typedef struct { Vertex bl; Vertex br; Vertex tl; Vertex tr; } TexturedQuad; typedef struct { CGPoint geometryVertex; } Vertex; 通过将GLDrawArray放入循环中并使用每个四边形来单独绘制它们,而不是按预期进行批处理渲染(但不是最佳渲染)

我尝试批量处理一组矩形/四边形,并得到一些奇怪的混合:

每个矩形似乎都有一点邻居的颜色

以下是我的测试代码:

typedef struct {
 Vertex bl;
 Vertex br;
 Vertex tl;
 Vertex tr;
} TexturedQuad;

typedef struct {
 CGPoint geometryVertex;
} Vertex;

通过将GLDrawArray放入循环中并使用每个四边形来单独绘制它们,而不是按预期进行批处理渲染(但不是最佳渲染)


任何帮助都将不胜感激

最好的办法是你的
GL\u三角带出现问题。它看起来像是在每个矩形后面画了一个额外的三角形(你可以看到它从每个矩形的顶部2个顶点到下一个矩形的左底部。因为这些顶点有不同的颜色,所以看起来像某种混合)

无论如何,对于三角形条带,每个矩形只需要2个顶点+2个开始点(或结束点,如果你愿意),但是如果你做得正确,你会在整个场景中得到一个很好的混合幽灵,因为顶点也会有不同的颜色

如果你想做的只是让这些矩形具有某种颜色而不混合,那么你需要做的似乎就是将
GL\u三角形\u条
切换到
GL\u三角形

- (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);
}