批处理四边形会产生错误的颜色混合opengl es2

时间:2013-04-30 01:25:29

标签: ios objective-c opengl-es opengl-es-2.0

我正在尝试批处理一堆矩形/四边形并进行一些奇怪的混合:

enter image description here

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

这是我的测试代码:

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放入循环并使用每个四边形而不是按预期批处理渲染而不是最佳来单独绘制它们。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

正在发生的事情的最佳选择是GL_TRIANGLE_STRIP存在问题。它看起来像是在每个矩形之后绘制了一个额外的三角形(你可以看到它形成了每个矩形的前2个顶点到下一个矩形的左下角。因为这些顶点有不同的颜色,它看起来像某种混合物。)

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

如果您想要做的只是让那些具有特定颜色的矩形没有混合,那么您似乎只需将GL_TRIANGLE_STRIP切换为GL_TRIANGLES