Reset texture Scaling

Hey all,

currently we are using texturescaling to spread a tile texture over a plane shape.

We do this by using

final Mesh spaMesh = spaGeom.getMesh();
spaMesh.scaleTextureCoordinates(new Vector2f(height / 2f, width / 2f));

height and width are the height of the gamemap and we divide it by 2 cause there are 4 tiles per texture (2x2).

A problem is occuring when we restart our game (not the whole application) then the textures are already tiled.
So now we try to figure out if the texture is already scaled or not.

After looking into the scaleTextureCoordinates method I see that the method is modifying a texture coord buffer, so I tried to use this to reset the buffer.

if (vertexBuffer == null) {
VertexBuffer oldBuffer = spaMesh.getBuffer(VertexBuffer.Type.TexCoord).clone();
vertexBuffer = oldBuffer;
spaMesh.scaleTextureCoordinates(new Vector2f(height / 2f, width / 2f));
System.out.println(“Scaled without loading old buffer”);
}else{
VertexBuffer tc = spaMesh.getBuffer(VertexBuffer.Type.TexCoord);
FloatBuffer floatBuffer = (FloatBuffer) vertexBuffer.getData();
floatBuffer.clear();
tc.updateData(floatBuffer);
spaMesh.scaleTextureCoordinates(new Vector2f(height / 2f, width / 2f));
System.out.println(“Scaled after reseting to old buffer”);
}

But thats not working properly. Can you guys help me?

1 Like

No one an idea?

1 Like

It’s a bit difficult to say. What do you mean “not working properly”? Are the variables “height” and “width” floats? Because if not, you’re gonna have truncated fraction results. Also, you can’t modify the buffer on-the-fly like that - you just have a clone of the data, so you need to send it back to the mesh:

mesh.getBuffer(VertexBuffer.Type.TexCoord).setData(newBuffer);
1 Like

My take is he’s trying to scale the texture coordinates twice because he doesn’t know the state of his own data structures. So he wants to know if the textures are already scaled or not.

A redesign would fix the issue… so that you don’t try to reinitialize the same mesh more than once. Or keep track of what you set the scale to so that you can unset it. (Or if you are super paranoid, keep a copy of the original texture buffer around.)

1 Like