Texturing faces

Hi everyone,



I have a question about adding a texture to a mesh.

Is there a way to assign uv-coordinates to a face rather than to a vertex ??



Currently, as I see in many examples, when we set buffers to the mesh we always assign the same amount of uvCoords as there are vertices.

This way we need to have more vertices in out mesh if we want our faces to have different images on them.



Look at the example. In the first situation we have a simple plane with uvCoods assigned to vertices.

[java]Mesh m = new Mesh();



// Vertex positions in space

Vector3f [] vertexes = new Vector3f[4];

vertexes[0] = new Vector3f(0,0,0);

vertexes[1] = new Vector3f(3,0,0);

vertexes[2] = new Vector3f(0,3,0);

vertexes[3] = new Vector3f(3,3,0);



Texture coordinates

Vector2f [] texCoord = new Vector2f[4];

texCoord[0] = new Vector2f(0,0);

texCoord[1] = new Vector2f(1,0);

texCoord[2] = new Vector2f(0,1);

texCoord[3] = new Vector2f(1,1);



// Indexes. We define the order in which mesh should be constructed

int [] indexes = {2,0,1,1,3,2};



// Setting buffers

m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertexes));

m.setBuffer(Type.TexCoord2, 2, BufferUtils.createFloatBuffer(texCoord));

m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));[/java]



And here is what I have in mind:

[java]Mesh m = new Mesh();



// Vertex positions in space

Vector3f [] vertexes = new Vector3f[4];

vertexes[0] = new Vector3f(0,0,0);

vertexes[1] = new Vector3f(3,0,0);

vertexes[2] = new Vector3f(0,3,0);

vertexes[3] = new Vector3f(3,3,0);



// Texture coordinates

Vector2f [] texCoord = new Vector2f[6];

texCoord[0] = new Vector2f(0,0);

texCoord[1] = new Vector2f(1,0);

texCoord[2] = new Vector2f(0,1);

texCoord[3] = new Vector2f(0,1);

texCoord[4] = new Vector2f(1,1);

texCoord[5] = new Vector2f(0,0);



// Indexes. We define the order in which mesh should be constructed

int [] indexes = {2,0,1,1,3,2};



// Setting buffers

m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertexes));

m.setBuffer(Type.TexCoord2, 2, BufferUtils.createFloatBuffer(texCoord));

m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));[/java]



UV coordinates assgined to the faces and not to the vertices.

It could help to reduce the amount of vertices in textured models.



So is something like that possible in jMonkeyEngine ??



Cheers,

Kaelthas

uh…i don’t really get your point.

anyway a texture coordinate for each vertex is not a JME design choice. It works like this in opengl.

I really don’t see how it could reduce the number of vertices of a model though…

OK, it seems like we cannot really do much about it, if it is how OpenGL works.



But what I was thinking about was a kind of Blender approach to texturing.

If you set UV-coords in Blender, you can attach several coordinates to a single vertex by marking seams on the mesh

and unwrapping it.



Take a simple cube for example. You can have only 8 vertices but 6 different textures, each on one face.

In jMonkeyEngine you’ll need 24 vertices for that. Every 4-vertices for one face and a texture assigned to that face.

So I thought that if uv-coords were assigned to faces (like in blender) rather than to vertices (like in OpenGL)

we could save a lot of memory and computing time.





If my explanation is still unclear I’ll post some pics next time :slight_smile:

I’m still not sure i understand, but if you’re talking about 12 triangles (6 quads / faces) sharing 8 vertices on a cube, rather than having 24 vertices, it’s certainly possible and defined by the “indices” buffer.



I do it on some simple meshes to create “dual side” polygons.

Actuall you need someplace to store it, thats the vertex (driver internal) so you cannot store a texcoord without a vertex.

If you’re not sharing the vertices for texcoords, then you can’t share them for positions. This is how it works in OpenGL so you don’t gain any performance by doing it your way.