glDrawArray工件

时间:2011-07-27 01:17:43

标签: iphone opengl-es cocos2d-iphone

我正在尝试使用glDrawArray和GL_TRIANGLE_STRIP渲染纹理网格,但绘制时会出现伪影,但在屏幕上分布不均匀。

Screenshot of the problem.

这是我使用的代码:

void draw() {
    glDisableClientState(GL_COLOR_ARRAY);
    glBindTexture(GL_TEXTURE_2D, tex.name);

    glPushMatrix();
    glScalef(cell_size, cell_size, 1);

    GLfloat vertices[w*8];

    { // populate only once and translate
        int i = 0;
        for (int x=0; x<w; x++) {
            vertices[i++] = x; vertices[i++] = 0;
            vertices[i++] = x; vertices[i++] = 1;
            vertices[i++] = x+1; vertices[i++] = 0;
            vertices[i++] = x+1; vertices[i++] = 1;
        }
    }

    GLfloat texCoords[w*8];     
    const float off = 1.00f/16.0f;

    for (int y=0; y<h; y++) {
        int i = 0;
        for (int x=0; x<w; x++) {
            const int v = tiles[x+y*w];
            const float boff = v*off;
            texCoords[i++] = boff; texCoords[i++] = 0;
            texCoords[i++] = boff; texCoords[i++] = 1;
            texCoords[i++] = boff+off; texCoords[i++] = 0;
            texCoords[i++] = boff+off; texCoords[i++] = 1;
        }

        glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
        glVertexPointer(2, GL_FLOAT, 0, vertices);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, w*4);

        glTranslatef(0, 1, 0);
    }
    glPopMatrix();

}

知道可能是什么问题吗?

2 个答案:

答案 0 :(得分:5)

我在OGL上有点生疏,但你应该改变纹理渲染参数,如下所示:

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

答案 1 :(得分:0)

GL_NEAREST过滤可能有效,但您可能还需要考虑更改纹理的边缘包装(甚至是边框)属性:

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);