glDrawArray artifacts
I'm trying to render a grid of texture using glDrawArray and GL_TRIANGLE_STRIP, but there are artifacts when drawing, but distributed unevenly across the screen.
Screenshot of the problem.
This is the code I use :
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; texC开发者_JAVA技巧oords[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();
}
Any idea what might be the problem?
I'm a little rusty on OGL, but you should change the texture render parameters, like this:
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GL_NEAREST
filtering may work, but you may also want to consider changing your texture's edge wrapping (or even border) properties:
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
精彩评论