Image Atlas from 512x512 image but which scale?

I have a 512x512 image which contains 0…15x0…15 images a 32x32.

Then I would like to create a mesh with one of the 32x32 images.

Must I fill the TexCoord buffer with 0…1 values or can I use 0…512 and scale the texture?



Currently I do this:



[java]

texCoord.get(bloxelType).addAll(assetManager.getTextureCoordinates(bloxelType, FACE_BACK));

texCoord.get(bloxelType).addAll(assetManager.getTextureCoordinates(bloxelType, FACE_FRONT));

// … and so on for all six faces of a cube

// then later I create the mesh …

chunkMesh.setBuffer(Type.TexCoord, 2,BufferUtils.createFloatBuffer(texCoord.get(bloxelType).toArray(new Vector2f[texCoord.size()])));

[/java]



The image-atlas code is here:



[java]

private List<Vector2f> uvMapTexture(final Vector2f coord) {

if (coord == null) {

// use complete image as texture

final Vector2f bottomLeft = new Vector2f(0, 0);

final Vector2f bottomRight = new Vector2f(1, 0);

final Vector2f topLeft = new Vector2f(0, 1);

final Vector2f topRight = new Vector2f(1, 1);

return Lists.newArrayList(bottomLeft, bottomRight, topLeft, topRight);

}

// each image is 32x32, the whole image-atlas is 512x512

// coord.x: 0…15

// coord.y: 0…15

final float s = 32f / 512f;

final float x = coord.x * s;

final float y = coord.y * s;

final Vector2f bottomLeft = new Vector2f(x, y);

final Vector2f bottomRight = new Vector2f(x, y + s);

final Vector2f topLeft = new Vector2f(x + s, y);

final Vector2f topRight = new Vector2f(x + s, y + s);

// return Lists.newArrayList(bottomLeft, bottomRight, topLeft, topRight); ??!? each face is 90 degree rotated clockwise

return Lists.newArrayList(topLeft, bottomLeft, bottomRight, topRight); // this looks better but the texture is a little bit stretched

}

[/java]

http://i.imgur.com/Jww8a.png


the face textures a little bit stretched ... why?

Here is my image … the good old minecraft texture :slight_smile: … the grass image have the coordinates up:0,15;down:3,14;left:3,15;right:3,15;front:3,15;back:3,15 (bottom,left=0,0/top,right=15,15)



http://i.imgur.com/KVxoo.png

It looks like two of the coordinates might be swapped… like the top ones. But it’s just a guess since I don’t know what vertexes you are using.



Edit: I’m 99% sure that’s what it is. The texture is mirrored along the diagonal where the triangles split the quad.

looks like the top 2 and bottom 2 have been entered back to front ?



I’m guessing this will do it :



[java] final Vector2f bottomLeft = new Vector2f(x, y);

final Vector2f bottomRight = new Vector2f(x+s, y);

final Vector2f topLeft = new Vector2f(x, y+s);

final Vector2f topRight = new Vector2f(x + s, y + s);[/java]



good luck

Thanks guys … but now its looks like this:

http://i.imgur.com/ylRtu.jpg

I think I’m understanding the concept of custom mesh (the basics) :wink: But this texture-buffer-stuff is a miracle to me :-[

Can someone please post a advanced texture example in the documentation. The documentation should describe the relation between indizes and texture coordinates.



Maybe my mesh generator cause the problem?! I’m using this indizes:



http://i.imgur.com/BDTaY.png



[java]

private static final ArrayList<Integer> TRIANGLE_INDIZES = Lists.newArrayList(2, 0, 1, 1, 3, 2);

[/java]

Please help again :smiley:

Kind Regards

Andreas

Solved :slight_smile:



Thanks pspeed and thetoucher … now I understand the concept :slight_smile:



The order of the returned image coordinates must match with the TRIANGLE_INDIZES order!



http://i.imgur.com/0MdrM.jpg



Cya

Andreas

position buffer, normal buffer, texture buffer, tangent buffer, color buffer, etc… all relate one-to-one. position[5] matches normal[5] matches texture[5]. They all represent the vertex attributes.



The index buffer relates triangles to their vertexes (which includes the attributes for those vertexes).



I don’t know if that makes sense or is helpful at this point.