OpenGL纹理平铺无法正常工作

时间:2016-05-08 16:27:16

标签: opengl texture-mapping

我尝试在矩形100x100内平铺大小为16x16的纹理。我将GL_TEXTURE_WRAP_SGL_TEXTURE_WRAP_T设置为GL_REPEAT,但它没有效果(见下图)。如何平铺图像?我应该使用不同的绘图方法而不是glDrawArrays吗?

原始纹理16x16

Original texture 16x16

实际结果:

Actual result

代码:

glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture.getId());
glTexParameterf(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// GL_REPEAT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glPushMatrix();
glVertexPointer(VERTEX_POINTER_COUNT, GL_FLOAT, 0, vertexPointer);
glColorPointer(COLOR_POINTER_COUNT, GL_FLOAT, 0, colorPointer);
glTexCoordPointer(TEXTURE_COORD_POINTER_COUNT, GL_FLOAT, 0, texCoordPointer);
glDrawArrays(GL_TRIANGLES, 0, 6);
glPopMatrix();

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, NULL_ID);
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_BLEND);

UPD。

// Texture coordinates
private float[] uv = new float[]{0, 0, 0, 1, 1, 1, 1, 0};
public final FloatBuffer texCoordPointer = BufferUtils.createFloatBuffer(2 * 3 * 2 * 2);
texCoordPointer.clear();
texCoordPointer.put(uv[0]).put(uv[1]);
texCoordPointer.put(uv[2]).put(uv[3]);
texCoordPointer.put(uv[4]).put(uv[5]);
texCoordPointer.put(uv[0]).put(uv[1]);
texCoordPointer.put(uv[4]).put(uv[5]);
texCoordPointer.put(uv[6]).put(uv[7]);
texCoordPointer.flip(); 

1 个答案:

答案 0 :(得分:1)

如果你想重复纹理6次,你的纹理坐标应该在[0.0,6.0]范围内