So I tried to tackle textures today. I have a simple texture that looks like this:
However, when I apply the texture to the cubes, it looks like this:
Obviously I did something very wrong. I basically load the texture up using Slick, create a buffer that holds 2 * 3 * 6 * 4096 (vertex size, amount of vertices per face, faces, number of blocks in a chunk) bytes or whatever you measure that in, bind the texture and put the data into the buffer. I then render it out. Here’s the relevant code:
public class BaseBlock {
public static final int OFFSET = 1;
private BlockType type;
private Texture texture;
private int x, z, y;
private boolean isActive;
public BaseBlock(BlockType type, int x, int z, int y, boolean isActive) {
this.setType(type);
this.x = x;
this.z = z;
this.y = y;
this.setActive(isActive);
try {
this.texture = TextureLoader.getTexture("PNG", new FileInputStream(new File(type.tex_loc)));
} catch (FileNotFoundException e) {
System.err.println("Texture Location Not Found");
} catch (IOException e) {
System.err.println("Texture Not Loaded Correctly");
}
}
public void bindTexture() {
texture.bind();
}
public static float[] getQuadRenderCoords(int x, int z, int y, int offSet) {
return new float[] { x, z, y,
x + offSet, z, y,
x + offSet, z, y + offSet,
x, z, y + offSet,
x, z, y + offSet,
x + offSet, z, y + offSet,
x + offSet, z + offSet, y + offSet,
x, z + offSet, y + offSet,
x + offSet, z, y,
x + offSet, z + offSet, y,
x + offSet, z + offSet, y + offSet,
x + offSet, z, y + offSet,
x, z + offSet, y,
x, z, y,
x, z, y + offSet,
x, z + offSet, y + offSet,
x, z + offSet, y + offSet,
x + offSet, z + offSet, y + offSet,
x + offSet, z + offSet, y,
x, z + offSet, y,
x, z + offSet, y,
x + offSet, z + offSet, y,
x + offSet, z, y,
x, z, y };
}
public static float[] getQuadTextureCoords(){
return new float[]{
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1,
0, 0,
1, 0,
1, 1,
0, 1,
};
}
Part of my chunk class:
public BaseChunk(int startX, int startZ, int startY, boolean isChunkActive) {
this.startX = startX;
this.startZ = startZ;
this.startY = startY;
BaseChunk.isChunkActive = isChunkActive;
vHandle = BufferUtils.createFloatBuffer(vertSize * vertAmount * faceAmount * blockAmount);
tHandle = BufferUtils.createFloatBuffer(texSize * vertAmount * faceAmount * blockAmount);
}
public void fillChunkWith(BlockType type) {
isChunkActive = true;
System.out.println(startX);
for (int x = startX; x < CHUNK_SIZE; x++) {
for (int z = startZ; z < CHUNK_SIZE; z++) {
for (int y = 0; y < CHUNK_SIZE; y++) {
setBlock(type, x, z, y);
}
}
}
rebuildChunk(startX, startZ, startY);
}
public void rebuildChunk(int startX, int startZ, int startY) {
boolean render;
isChunkActive = true;
int x = startX;
int z = startZ;
int y = startY;
for (x = startX; x < CHUNK_SIZE; x++) {
for (z = startZ; z < CHUNK_SIZE; z++) {
for (y = startY; y < CHUNK_SIZE; y++) {
render = checkFaces(x, z, y);
if (render) {
vHandle.put(BaseBlock.getQuadRenderCoords(x, z, y, BaseBlock.OFFSET));
tHandle.put(BaseBlock.getQuadTextureCoords());
blocks[x][z][y].bindTexture();
}
}
}
}
vHandle.flip();
tHandle.flip();
vboV = genBuffer(vboV);
vboT = genBuffer(vboT);
bindBuffer(vboV, vHandle);
bindBuffer(vboT, tHandle);
render();
}
Then how I render:
public void bindBuffer(int vboV, FloatBuffer vHandle) {
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboV);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vHandle, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboT);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, tHandle, GL15.GL_STATIC_DRAW);
}
public int genBuffer(int id) {
return id = GL15.glGenBuffers();
}
public void render() {
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboV);
glVertexPointer(vertSize, GL_FLOAT, 0, 0L);
GL15.glBindBuffer(GL15.GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING, vboT);
glTexCoordPointer(2, GL_FLOAT, 0, 0L);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glPushMatrix();
glDrawArrays(GL_QUADS, 0, vertSize * vertAmount * faceAmount * currentBlockCount);
glPopMatrix();
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
Could it be because I didn’t wrap the texture? Is the texture not the right resolution? Its 16 * 16, but the block size is currently set at 1.