开发者

How to create a multicolored squared grid in OpenGL ES 2.0?

Using Open GL ES 2.0, I want to create a large grid of squares, where 开发者_C百科each square can take a certain color as specified by a definition file. So this is not a simple checkerboard of only black and white squares...

What is the best way to do this in order to avoid artifacts?

Thanks in advance


There's nothing that will implicitly cause artifacts when drawing a grid of squares. There's no inherent z-fighting, depth, or transparency problems with the drawing as described in this question.

In the application, create a vertex array with two attributes (position, color) for each vertex. For each square in the grid you'll need 4 vertices. This will describe 2 independent triangles that make up each grid square. Avoid using triangle strips because you don't want the color attribute shared or interpolated between adjacent grid squares.

So your vertex array in memory will be:

square0Pos0
color0
square0Pos1
color0
square0Pos2
color0
square0Pos3
color0
square1Pos0
color1
...

Create a simple pass-through shader pair that sends the vertex color through as a varying to the fragment shader. The fragment color simply sets the output color to the value of the varying.

If you layout the grid positions from -1.0 through 1.0, you don't even need to add viewing transform uniforms or corresponding shader logic.

Make a single draw call to draw your vertex array with glDrawArrays(GL_TRIANGLES, ...).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜