4 Replies Latest reply on Nov 24, 2010 11:06 PM by sproketboy-JavaNet

    How to not stretch the texture.

      Hi All,

      In my game I have a setting for wall height when creating walls. When I make the walls higher the texture image I use gets stretched and blurry. I would rather if the image didn't stretch and instead repeated itself. Any ideas how to do that?
      I've tried setting TextureAttributes as MODULATE, DECAL, BLEND, REPLACE, or COMBINE and it doesn't seem to make a difference.

      I also tried:

      Primitive wall = new Box(THICKNESS, height, RADIUS, flags, wallApp, texHeight);

      I'm assuming that the Box constructor's last param (called numTexUnit) is the correct way.

      But I still see the texture stretch... Sad

      The int texHeight is 1 or 2 or 3.

        • 1. Re: How to not stretch the texture.
          Have you set the boundary mode(s) of the texture to WRAP?
          • 2. Re: How to not stretch the texture.
            Thanks. I tried that but it didn't make a difference. My objects are simple (just walls) so I can manipulate the buffered image object instead.
            • 3. Re: How to not stretch the texture.
              Not sure whats causing the problem then. The attached example runs fine for me; texture is always repeated and never stretched.
              package java3d;
              import java.awt.Dimension;
              import java.awt.GraphicsConfiguration;
              import java.awt.event.WindowAdapter;
              import java.awt.event.WindowEvent;
              import javax.media.j3d.Appearance;
              import javax.media.j3d.BranchGroup;
              import javax.media.j3d.Canvas3D;
              import javax.media.j3d.QuadArray;
              import javax.media.j3d.Shape3D;
              import javax.media.j3d.Texture2D;
              import javax.swing.JFrame;
              import javax.vecmath.Point3f;
              import javax.vecmath.TexCoord2f;
              import com.sun.j3d.utils.image.TextureLoader;
              import com.sun.j3d.utils.universe.SimpleUniverse;
              public class TextureTest {
                   public static void create_scene(Canvas3D canvas_3d) {
                        SimpleUniverse su = new SimpleUniverse(canvas_3d);
                        BranchGroup obj_root = new BranchGroup();
                        QuadArray qa = new QuadArray(4, QuadArray.COORDINATES | QuadArray.TEXTURE_COORDINATE_2);          
                        qa.setCoordinate(0, new Point3f(-0.9f,  0.3f, 0.0f));
                        qa.setCoordinate(1, new Point3f(-0.9f, -0.3f, 0.0f));
                        qa.setCoordinate(2, new Point3f( 0.9f, -0.3f, 0.0f));
                        qa.setCoordinate(3, new Point3f( 0.9f,  0.3f, 0.0f));
                        float repeat = 4.0f;          
                        qa.setTextureCoordinate (0, 0, new TexCoord2f(0.0f, 0.0f));
                        qa.setTextureCoordinate (0, 1, new TexCoord2f(0.0f, 1.0f));
                        qa.setTextureCoordinate (0, 2, new TexCoord2f(repeat, 1.0f));
                        qa.setTextureCoordinate (0, 3, new TexCoord2f(repeat, 0.0f)); 
                        TextureLoader tl = new TextureLoader("data/marble10.jpg", null);          
                        Texture2D tex2d = (Texture2D)tl.getTexture();
                        Appearance ap_wall = new Appearance();
                        Shape3D shp_wall = new Shape3D(qa, ap_wall);
                    * @param args
                   public static void main(String[] args) {
                        JFrame win = new JFrame("Texture Repeat Test");
                        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
                        Canvas3D canvas_3d = new Canvas3D(config);
                        canvas_3d.setPreferredSize(new Dimension(512, 512));
                        win.addWindowListener(new WindowAdapter() {
                                  public void windowClosing(WindowEvent e) {
              To test it you just have to adjust the texture file name ("data/marble10.jpg" -> some image on your computer) and change the repeat value. Currently it is set to repeat the texture n-times in x-direction.
              • 4. Re: How to not stretch the texture.
                Thanks! I'll compare to my code.