UVW Map on a trimesh

I've added a trimesh (a planar quad whit upper vertices a little nearer than the bottoms).

Applying a texture with a cross on, the cross shows distorced.



I've tried the same thing in 3DMax and had the same result unless the use of UVW map.



How can I simulate the thing in Jme ?



(If you can point me somewhere for info i will appriciate).

Take a look at HelloTriMesh.

When you create a trimesh you can create the vertices of your mesh and the coordinates on the texture they correspond to. 0 is top/left, 1 is bottom/right I think.

I guess you're talking about setting TextureBuffer, isn't it ?

About tbuff I've putted      0,0    -    1,0  -  0,1    -    1,1; this works for squares or rectangles but when the shape is streched the texture result weird.

Square





Not square

I guess you are creating your TriMesh programmatically, from what you wrote.

Something in your texture coordinates is obviously not the way you intended. Could you post your TriMesh creation code?


public class MathTriMesh extends TriMesh {
   
   private boolean               doDraw               = true;
   
   private FloatBuffer          vertexBuff            = null;

   private ArrayList<Vector3f>      points               = null;
   private ArrayList<Vector3f>      normals               = null;
   
   public MathTriMesh(String name, ArrayList<Vector3f> points, int xQ, int yQ, ArrayList<Vector3f>normals){
      super (name);
      
      this.points = points;
      this.normals = normals;
      
      setVertexData();
      setNormalData();
      setDefaultColor(ColorRGBA.white);
      setIndexData(xQ, yQ);
      setTextureData(xQ, yQ);      
   }
   
   public MathTriMesh(String name, Vector3f[] points, int xQ, int yQ, ArrayList<Vector3f>normals){
      super (name);
      
      this.points = new ArrayList<Vector3f>();
      for (Vector3f v3f : points) {
         this.points.add(v3f);
      }
      
      this.normals = normals;
      
      setVertexData();
      setNormalData();
      setDefaultColor(ColorRGBA.white);
      setIndexData(xQ, yQ);
      setTextureData(xQ, yQ);      
   }
   
   private void setVertexData() {
      TriangleBatch batch = getBatch(0);
      batch.setTriangleQuantity(points.size() -2);
      batch.setVertexCount(points.size());
      vertexBuff = BufferUtils.createVector3Buffer(batch.getVertexBuffer(), points.size());
      batch.setVertexBuffer(vertexBuff);      
      batch.getVertexBuffer().rewind();
      
      for (Vector3f v : points){
         batch.getVertexBuffer().put(v.x).put(v.y).put(v.z);
      }
   }      
   
   private void setNormalData () {
      TriangleBatch batch = getBatch(0);
       if (batch.getNormalBuffer() == null) {
          batch.setNormalBuffer(BufferUtils.createVector3Buffer(points.size()));
         
          if (normals != null) {
             for (Vector3f normal : normals){
                batch.getNormalBuffer().put(normal.x).put(normal.y).put(normal.z);
             }
          } else {
             for (int c = 0; c < points.size(); c++){
                batch.getNormalBuffer().put(1).put(1).put(1);
             }
          }
       }
   }
   
   private void setColorData() {
      TriangleBatch batch = getBatch(0);
      ColorRGBA[] colors = new ColorRGBA[getVertexCount()];
      for (int c = 0; c < getVertexCount(); c++){
         colors[c] = ColorRGBA.randomColor();
      }
      
      batch.setColorBuffer(BufferUtils.createFloatBuffer(colors));
   }
   
   private void setIndexData(int xQ, int yQ) {
        TriangleBatch batch = getBatch(0);
       if (batch.getIndexBuffer() == null) {
          int intxQ = xQ;
          int intyQ = yQ;
          int[] indices = new int[(intxQ - 1) * (intyQ - 1) * 2 * 3];
          int count = 0;
          for (int i = 0; i < intyQ - 1; i++){
             for (int v = 0; v < intxQ - 1; v++){
                indices[count]    = v + (i * intxQ);             
                count++;
                indices[count]    = v + (i * intxQ) + intxQ;      
                count++;
                indices[count]    = v + (i * intxQ) + intxQ + 1;
                count++;
                
                indices[count]    = v + (i * intxQ);
                count++;
                indices[count]    = v + (i * intxQ) + intxQ + 1;   
                count++;
                indices[count]    = v + (i * intxQ) + 1;
                count++;
                
             }
          }
          
          batch.setIndexBuffer(BufferUtils.createIntBuffer(indices));
       }
   }   
   
   private void setTextureData(int xQ, int yQ) {
        TriangleBatch batch = getBatch(0);
       if (batch.getTextureBuffers().get(0) == null) {
          int xPrecision = (int) xQ - 1;
         int yPrecision = (int) yQ - 1;
          batch.getTextureBuffers().set(0, BufferUtils.createVector2Buffer(xPrecision * yPrecision * 2 * 2));
         
          int count = 0;
          for (int x = 0; x < xPrecision; x++){
             for (int y = 0; y < yPrecision; y++){
                batch.getTextureBuffers().get(0).put(0f).put(0f);
                batch.getTextureBuffers().get(0).put(1f).put(0f);
                batch.getTextureBuffers().get(0).put(0.1f).put(1f);
                batch.getTextureBuffers().get(0).put(1f).put(1f);
             }
          }
       }
   }
   
   public void setDraw (boolean doDraw){
      this.doDraw = doDraw;
   }
   
   public void draw(Renderer renderer){
      if (doDraw)
         super.draw(renderer);
   }      
}



Points are {
(-25, 50, 0),
(25, 50, 0),
(-50, -50, 0),
(50, 50, 0)
}


Gives you some clue ?

for (int x = 0; x < xPrecision; x++){

for (int y = 0; y < yPrecision; y++){

batch.getTextureBuffers().get(0).put(0f).put(0f);

batch.getTextureBuffers().get(0).put(1f).put(0f);

    batch.getTextureBuffers().get(0).put(0.1f).put(1f);

    batch.getTextureBuffers().get(0).put(1f).put(1f);

    }

}

Set that number back to 0 and it should be similar to TriMesh.



Heres a comparison:

http://www.stevydoos.com/comparison.png

I think it might have been rotated.



And a test image:

http://www.stevydoos.com/testtexture.png

No, that 0.1 was one of the tests.



No change with 0.



Looking at comparison.png seems, in TriMesh too, the textures become strached irregulary.



Pls try with this txt

I might have misunderstood you, ellettrozero, in what you want your trapezoid to look like, and can't think of a good wording for the two possible cases I have in mind. Maybe you should provide an illustration!



But if I understood you right, te following should look the way you want (and also demonstrate how to showcase your problem in a runnable program with less than twenty lines :wink: ):

public class TroubleWithTexCoords extends SimpleGame{
   
    public static void main(String[] args) {
        new TroubleWithTexCoords().start();
    }

    protected void simpleInitGame() {
        TextureState ts = display.getRenderer().createTextureState();
        ts.setTexture(
                TextureManager.loadTexture(getClass().getResource("tex.jpg")));
        rootNode.setRenderState(ts);
       
        FloatBuffer verts = BufferUtils.createFloatBuffer(
                -25,50,0,   25,50,0,    -50,-50,0,  50,-50,0 );
        FloatBuffer tex = BufferUtils.createFloatBuffer(
                 0.25f,1,     0.75f,1,        0,0,        1,0 );
        IntBuffer inx = BufferUtils.createIntBuffer(new int[]{0,1,2,    1,3,2});
        Spatial s = new TriMesh("", verts, null, null, tex, inx);
       
        s.setLocalTranslation(0, 0, -100);
        s.setLightCombineMode(LightState.OFF);
       
        rootNode.attachChild(s);
    }
}



You can see that I have adjusted the texture coordinates of the upper corners of the trapezoid according to their spatial position.

However if I misunderstood you, and this is not what you want, your problem might be that you haven't entirely understood how texture coordinates work.
http://www.google.com/search?q=how+texture+coordinates+work should get you started.
I especially like http://www.opengl.org/resources/code/samples/sig99/advanced99/notes/node52.html , but you probably should check other results too.

You are right twice :

  1. yes is what I was looking for
  2. yes, I thougt I've undesrtood texureBuffer but is not so







    Thanks a lot, going to surf links