Texture mapping custom meshes

Hi!

I’ve only recently found out about JME and I’ve been slowly learning how to use it.
I’m not really experienced with computer graphics as I’m a college student but I’m trying to get into it as I intend to do my thesis on procedural 3d content generation, which brings us to this problem I’m having.

I’ve been playing with creating custom meshes to, eventually, procedurally create buildings. For now I’m focusing on simply mapping a cube, without the bottom or top faces, correctly. I am aware that I could use a textureAtlas, and although I have absolutely no experience with it, from what I’ve seen, it doesn’t really help in my case when, in the future, i’ll be generating textures for each face of a randomly generated building. But I might be wrong… am I?

In any case, I’d like to understand why the following code doesn’t work like I intended.

[java]
public void simpleInitApp() {

``````	Mesh mesh = new Mesh();

Vector3f [] vertices = new Vector3f[10];
vertices[0] = new Vector3f(0,0,0);
vertices[1] = new Vector3f(0,0,1);
vertices[2] = new Vector3f(1,0,1);
vertices[3] = new Vector3f(1,0,0);
vertices[4] = new Vector3f(0,1,0);
vertices[5] = new Vector3f(0,1,1);
vertices[6] = new Vector3f(1,1,1);
vertices[7] = new Vector3f(1,1,0);
vertices[8] = new Vector3f(0,0,0);
vertices[9] = new Vector3f(0,1,0);

Vector2f[] texCoord = new Vector2f[10];
texCoord[0] = new Vector2f(0f,0);
texCoord[1] = new Vector2f(0.25f,0);
texCoord[2] = new Vector2f(0.5f,0);
texCoord[3] = new Vector2f(0.75f,0);
texCoord[4] = new Vector2f(0f,1);
texCoord[5] = new Vector2f(0.25f,1);
texCoord[6] = new Vector2f(0.5f,1);
texCoord[7] = new Vector2f(0.75f,1);
texCoord[8] = new Vector2f(1,0);
texCoord[9] = new Vector2f(1,1);

int [] indexes = { 0,4,7, 7,3,0,
3,7,2, 2,7,6,
2,6,1, 1,6,5,
1,5,8, 8,5,9};

mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
mesh.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
mesh.setBuffer(Type.Index,    3, BufferUtils.createIntBuffer(indexes));
mesh.updateBound();

Geometry geo = new Geometry("OurMesh", mesh); // using our custom mesh object
geo.setMaterial(generateTexture());
rootNode.attachChild(geo);
``````

}[/java]

Here’s the result I get. Those long small cylinders are the xyz axis, from (0,0,0) following the positive direction, just for orientation. x = red, y = orange, z = pink.

And this is the texture I used. It’s 256512 with that red rectangle being 64something, 32 i think.

And here’s a schematic to help figure out the indexes and how i built the cube.

Vertex 8 and 9 overlap 0 and 4 as, as far as I’m aware, there’s no way to map different texCoords to the same vertex.

So, with all this, I wanted the texture to wrap cleanly around the cube, with uv(0,0) mapping to vertex 0 and uv(1,0) to vertex 8.
The texture should follow the direction sequence of the vertexes, so that little red rectangle should be on the face that is parallel to the Z axis, and since it’s 64 wide out of 256, it should be stretched to match the entire face.
So, what am I doing wrong?

Thanks!

Ups, I just realized that on Firefox the pictures can’t be seen

The output:
Dropbox - Error

Dropbox - Error

Schematic:

Dropbox - Error

Texture

Dropbox - Error