Texture Nightmare

Hi

I am moderately new to Java3D and have like most other people delved into the world of terrain generation. My problem lies with texture mapping my generated terrain. I have been through the many tutorials on offer via Sun and Java world and can tile textures fairly ok, but what I want to do is drape a single texture over the whole terrain like a blanket which for some reason I can not do.

The size of the world I am trying to texture ranges from
-64 to 64 on the x and z axis and the y axis varies from 0 to 10. Therefore obviously I want the bottom left of the world to correlate with the bottom left of the loaded texture, the bottom right to the bottom right of the texture and so on. Is it possible to stretch a texture in this way and if so how? I would be most grateful if you could help as I have been trying to do this for a very long time and now beginning to get a little frustrated.

In order to drape a single texture over your landscape you need a texture attributes on the appearance, a square terrain, and texture coordinates on the geometry.

It sounds like the nature of the problem you are having has to do with texture coordinates. Texture coordinates are u,v pairs that start at (0,0) at the lower left corner and go to (1,1) at the upper right corner of the texture/terrain. Because your terrain varies between (x,z) = (-64,-64) to (+64, +64), the texture coordinates should be u = (x+64)/128, and v = (z+64/128 ). Be sure the values are floats or doubles.

The confusing part can be how the texture coordinates relate to the vertices in the geometry. For example, if your terrain is 2 by 2 squares using a QuadArray then there are 16 vertices that need to have texture coordinates. Four vertices (at the center) all share the same texture coordinate values. As the number of quads grow, so does the overlapped vertices.

If your texture is draped but looks upside down, consider using the yUp parameter on the ImageComponent2D.

Mike

Thanks Mike!

I kinda think (? :slight_smile: ) I know what you’re getting at, but I’m not sure how to work it in (been trying all morning) so I will post some code and if you can give me some pointers I would be most grateful.

BoundingBox boundBox = new BoundingBox(shape.getBounds());
System.out.println("BOUNDING BOX = " + boundBox);
Point3d lower = new Point3d();
Point3d upper = new Point3d();
boundBox.getLower(lower);
boundBox.getUpper(upper);

           float width = (float)(upper.x - lower.x);
      float height = (float)(upper.y - lower.y);
            float depth = (float)(upper.z - lower.z);
  
              RenderingAttributes ra = new RenderingAttributes();
          ra.setDepthBufferEnable(true);
      ra.setIgnoreVertexColors(true);

      PolygonAttributes pa = new PolygonAttributes();
          pa.setCullFace(PolygonAttributes.CULL_NONE);
      pa.setPolygonMode(pa.POLYGON_FILL);
      
      


      TextureLoader texLoader = new TextureLoader( image,null ); 
              texture = (Texture2D) texLoader.getTexture();
              texture.setEnable(true);

       TextureAttributes ta = new TextureAttributes();
  ta.setTextureMode(ta.MODULATE);
  Transform3D trans = new Transform3D( );
  trans.setScale( 1.0 );
  ta.setTextureTransform( trans );
       
  // Setup the automatic texture coordinate generation

  Vector4f planeS = new Vector4f( (float)(1.0/width), 0.0f, 0.0f, (float)(-lower.x/width));
  Vector4f planeT = new Vector4f( 0.0f, (float)(1.0/height), 0.0f, (float)(-lower.y/height));  
  TexCoordGeneration texCoord = new TexCoordGeneration(
     TexCoordGeneration.TEXTURE_COORDINATE_2, TexCoordGeneration.OBJECT_LINEAR, planeS, planeT);
        
              Material material = new Material();
  material.setLightingEnable(true);

  material.setEmissiveColor(5,5,5);
        
  Appearance a = new Appearance();
          a.setRenderingAttributes(ra);
          a.setPolygonAttributes(pa);
  a.setTexture(texture);
  a.setTextureAttributes(ta);
  a.setMaterial(material);
  a.setTexCoordGeneration(texCoord);
          
  // set up 
  shape.setAppearance(a);

I hope thats clear

My post had to do with generating your own texture coordinates rather than using the TexCoordGeneration class. I have not used the TexCoordGeneration but I do have a few observations.

Because the texture is a 2D texture, I believe you can use the default S and T plane coefficients. Your sample code appears to be setting the Q coefficient which appears to be part of 4D textures, not 2D textures.

Color RGB values range from 0 to 1, but the emissive color is set to 5,5,5.

There is no need to set the texture transform and scale in the manner shown because those are defaulted for you.

Hope that helps.

Mike