java - Texture not showing properly in java3d -


similar to: java3d: texture not applied obj model properly

except generate indexedtrianglearray myself , map texture.

the geometry fine takes colour of bottom left corner of texture image instead of stretching on entire image. testure.jpg 256*256 image.

edit: strangely, non-indexed trianglearray works fine. assume it's problem way i'm setting texture coordinates?

( switch between indexedtrianglearray , trianglearray, comment out line in main function )

import com.sun.j3d.utils.universe.*; import javax.vecmath.*; import javax.media.j3d.*; import java.awt.*; import org.j3d.geom.*; import com.sun.j3d.utils.geometry.*; import com.sun.j3d.utils.image.*;  import java.io.*;   public class semvce{   public static indexedtrianglearray getita(int gridsize, float h) {  //creates terrain using indexedtrianglearray geometry      int    rows = gridsize,             cols = gridsize;      int vertexcount = gridsize*gridsize;        int cols_1 = cols-1,         rows_1 = rows-1;      indexedtrianglearray ta = new indexedtrianglearray( vertexcount, indexedtrianglearray.coordinates  | indexedtrianglearray.texture_coordinate_2, 6*rows_1*cols_1);       point3f coords[] = new point3f[vertexcount];      texcoord2f texco[] = new texcoord2f[cols*rows];       //set vertices      for(int i=0;i<cols;i++){         for(int j=0;j<rows;j++){             if(i%2==1 && j%2==1 )                 coords[i*rows+j] = new point3f(i,h,j);             else                 coords[i*rows+j] = new point3f(i,0,j);              //texco[i*rows+j] = new texcoord2f((float)i/(gridsize-1),(float)j/(gridsize-1) );         }     }       (int i=0;i<vertexcount;i++){         texco[i] = new texcoord2f(coords[i].x /(gridsize-1), coords[i].z /(gridsize-1));     }       ta.setcoordinates(0,coords);     ta.settexturecoordinates(0,0, texco);      //do indexes     int offset = 0;     for(int i=0;i<cols_1;i++){         for(int j=0;j<rows_1;j++){             offset = (i*rows_1+j)*6;             ta.setcoordinateindex(offset+0, (i*rows + j));                           ta.setcoordinateindex(offset+1, (i*rows+ j+1 ));                         ta.setcoordinateindex(offset+2, ((i+1)*rows + j));              ta.setcoordinateindex(offset+3, ((i+1)*rows + j));             ta.setcoordinateindex(offset+4, (i*rows+ j+1 ));             ta.setcoordinateindex(offset+5, ((i+1)*rows + j+1));         }     }      return ta; }  public static trianglearray gettrianglearray(int gridsize, float h){ //creates terrain using trianglearray geometry      int    rows = gridsize,             cols = gridsize;      int cols_1 = cols-1,         rows_1 = rows-1;       int vertexcount = 6*rows_1*cols_1;        trianglearray ta = new trianglearray( vertexcount, indexedtrianglearray.coordinates  | indexedtrianglearray.texture_coordinate_2);       point3f coords[] = new point3f[vertexcount];      texcoord2f texco[] = new texcoord2f[vertexcount];        //do vertices     int offset = 0;     float height = 0;     for(int i=0;i<cols_1;i++){         for(int j=0;j<rows_1;j++){             if(i%3==1 && j%3==1)                 height= h;             else                 height = 0;              offset = (i*rows_1+j)*6;             coords[offset+0] = new point3f(i,0, j);             coords[offset+1] = new point3f((i+1), 0,j);             coords[offset+2] = new point3f(i, 0, j+1);              coords[offset+3] =  new point3f( (i+1), 0, j);             coords[offset+4] = new point3f((i+1), 0,j+1);             coords[offset+5] = new point3f(i,0, j+1);              if(i%2==1 && j%2==1)                 coords[offset+0] = new point3f(i,h, j);             else if(i%2==0 && j%2==0)                 coords[offset+4] = new point3f((i+1), h,j+1);             else if( (i+1)%2==1 && j%2==1){                 coords[offset+1] = new point3f(i+1,h,j);                 coords[offset+3] = new point3f(i+1,h,j);             }             else if( i%2==1 && (j+1)%2==1){                 coords[offset+2] = new point3f(i,h, j+1);                 coords[offset+5] = new point3f(i,h, j+1);             }         }     }      int gridsize_1 = gridsize-1;     (int i=0;i<vertexcount;i++){         texco[i] = new texcoord2f(coords[i].x / gridsize_1, coords[i].z / gridsize_1);     }      ta.setcoordinates(0,coords);     ta.settexturecoordinates(0,0, texco);      return ta; }  public static texture2d loadtexture(string filename){     //imageobserver iobserve = new imageobserver();     textureloader loader = new textureloader(filename, new container());     imagecomponent2d image = loader.getimage();     texture2d texture = new texture2d(texture2d.base_level, texture2d.rgba, image.getwidth(), image.getheight() );     texture.setimage(0, image);     texture.setenable( true );     texture.setboundarymodes(texture.wrap);      texture.setboundarymodet(texture.wrap);     return texture; }  public static appearance getappearance(){     appearance appear = new appearance();     texture2d texture = loadtexture("texture.jpg");     appear.settexture(texture);     polygonattributes polygonattributes = new polygonattributes();     polygonattributes.setcullface(polygonattributes.cull_none);     //appear.settexcoordgeneration(new texcoordgeneration());     appear.setpolygonattributes(polygonattributes);      return appear; }  public static void main(string args[]){     simpleuniverse world = new simpleuniverse();     int gridsize=11;      //comment out according want test     //geometryarray geometry = gettrianglearray(gridsize,2);     geometryarray geometry = getita(gridsize,2);      shape3d shape= new shape3d(geometry,getappearance());      branchgroup bg = new branchgroup();     bg.addchild(shape);      world.addbranchgraph(bg);     setcameraposition(world.getviewingplatform(), new point3d(gridsize/2,5,-10), new point3d(gridsize/2,0,gridsize/2), new vector3d(0,1,0));//new point3d(gridsize/2,gridsize/2,gridsize/2), new point3d(gridsize/2,0,gridsize/2), new vector3d(0,1,0));  }   public static void setcameraposition(viewingplatform vp,point3d from, point3d to, vector3d v){     transformgroup viewtransform = vp.getviewplatformtransform();     transform3d t3d = new transform3d();      t3d.lookat(from,to,v);     t3d.invert();     viewtransform.settransform(t3d);  }   } 

admittedly, could/should have noticed earlier, ... has been while since have actively been using java3d - , since then, i've been squeezing out meager knowledge , memories answer forum , stackoverfow questions ;-)

however: did set indices vertices, using the

ta.setcoordinateindex(...); 

calls. additionally have set indices texture coordinates, using

ta.settexturecoordinateindex(0, ...); 

the 0 refers texture coordinate set (there may multiple textures, different coordinate sets). apart that, indices same vertices here.

here again, mcve. (the image name changed. lena rules!)

import java.awt.borderlayout; import java.awt.container; import java.awt.graphicsconfiguration;  import javax.media.j3d.appearance; import javax.media.j3d.branchgroup; import javax.media.j3d.canvas3d; import javax.media.j3d.geometryarray; import javax.media.j3d.imagecomponent2d; import javax.media.j3d.indexedtrianglearray; import javax.media.j3d.polygonattributes; import javax.media.j3d.shape3d; import javax.media.j3d.texture; import javax.media.j3d.texture2d; import javax.media.j3d.transform3d; import javax.media.j3d.transformgroup; import javax.media.j3d.trianglearray; import javax.swing.jframe; import javax.vecmath.point3d; import javax.vecmath.point3f; import javax.vecmath.texcoord2f; import javax.vecmath.vector3d;  import com.sun.j3d.utils.image.textureloader; import com.sun.j3d.utils.universe.simpleuniverse; import com.sun.j3d.utils.universe.viewingplatform;  public class semvce {     public static indexedtrianglearray getita(int gridsize, float h)     {         int rows = gridsize, cols = gridsize;         int vertexcount = gridsize * gridsize;          int cols_1 = cols - 1, rows_1 = rows - 1;          indexedtrianglearray ta =             new indexedtrianglearray(vertexcount,                 indexedtrianglearray.coordinates |                  indexedtrianglearray.texture_coordinate_2,                  6 * rows_1 * cols_1);          point3f coords[] = new point3f[vertexcount];         texcoord2f texco[] = new texcoord2f[cols * rows];          // set vertices         (int = 0; < cols; i++)         {             (int j = 0; j < rows; j++)             {                 if (i % 2 == 1 && j % 2 == 1)                     coords[i * rows + j] = new point3f(i, h, j);                 else                     coords[i * rows + j] = new point3f(i, 0, j);             }         }         (int = 0; < vertexcount; i++)         {             texco[i] =                 new texcoord2f(                     coords[i].x / (gridsize - 1),                      coords[i].z / (gridsize - 1));         }          ta.setcoordinates(0, coords);         ta.settexturecoordinates(0, 0, texco);          // indexes         int offset = 0;         (int = 0; < cols_1; i++)         {             (int j = 0; j < rows_1; j++)             {                 offset = (i * rows_1 + j) * 6;                 ta.setcoordinateindex(offset + 0, (i * rows + j));                 ta.setcoordinateindex(offset + 1, (i * rows + j + 1));                 ta.setcoordinateindex(offset + 2, ((i + 1) * rows + j));                  ta.setcoordinateindex(offset + 3, ((i + 1) * rows + j));                 ta.setcoordinateindex(offset + 4, (i * rows + j + 1));                 ta.setcoordinateindex(offset + 5, ((i + 1) * rows + j + 1));                  ta.settexturecoordinateindex(0, offset + 0, (i * rows + j));                 ta.settexturecoordinateindex(0, offset + 1, (i * rows + j + 1));                 ta.settexturecoordinateindex(0, offset + 2, ((i + 1) * rows + j));                  ta.settexturecoordinateindex(0, offset + 3, ((i + 1) * rows + j));                 ta.settexturecoordinateindex(0, offset + 4, (i * rows + j + 1));                 ta.settexturecoordinateindex(0, offset + 5, ((i + 1) * rows + j + 1));              }         }          return ta;     }      public static trianglearray gettrianglearray(int gridsize, float h)     { // creates terrain using trianglearray geometry         int rows = gridsize, cols = gridsize;          int cols_1 = cols - 1, rows_1 = rows - 1;          int vertexcount = 6 * rows_1 * cols_1;          trianglearray ta =             new trianglearray(vertexcount, indexedtrianglearray.coordinates                 | indexedtrianglearray.texture_coordinate_2);          point3f coords[] = new point3f[vertexcount];         texcoord2f texco[] = new texcoord2f[vertexcount];          // vertices         int offset = 0;         float height = 0;         (int = 0; < cols_1; i++)         {             (int j = 0; j < rows_1; j++)             {                 if (i % 3 == 1 && j % 3 == 1)                     height = h;                 else                     height = 0;                  offset = (i * rows_1 + j) * 6;                 coords[offset + 0] = new point3f(i, 0, j);                 coords[offset + 1] = new point3f((i + 1), 0, j);                 coords[offset + 2] = new point3f(i, 0, j + 1);                  coords[offset + 3] = new point3f((i + 1), 0, j);                 coords[offset + 4] = new point3f((i + 1), 0, j + 1);                 coords[offset + 5] = new point3f(i, 0, j + 1);                  if (i % 2 == 1 && j % 2 == 1)                     coords[offset + 0] = new point3f(i, h, j);                 else if (i % 2 == 0 && j % 2 == 0)                     coords[offset + 4] = new point3f((i + 1), h, j + 1);                 else if ((i + 1) % 2 == 1 && j % 2 == 1)                 {                     coords[offset + 1] = new point3f(i + 1, h, j);                     coords[offset + 3] = new point3f(i + 1, h, j);                 }                 else if (i % 2 == 1 && (j + 1) % 2 == 1)                 {                     coords[offset + 2] = new point3f(i, h, j + 1);                     coords[offset + 5] = new point3f(i, h, j + 1);                 }             }         }          int gridsize_1 = gridsize - 1;         (int = 0; < vertexcount; i++)         {             texco[i] =                 new texcoord2f(coords[i].x / gridsize_1, coords[i].z                     / gridsize_1);         }          ta.setcoordinates(0, coords);         ta.settexturecoordinates(0, 0, texco);          return ta;     }      public static texture2d loadtexture(string filename)     {         // imageobserver iobserve = new imageobserver();         textureloader loader = new textureloader(filename, new container());         imagecomponent2d image = loader.getimage();         texture2d texture =             new texture2d(texture2d.base_level, texture2d.rgba,                 image.getwidth(), image.getheight());         texture.setimage(0, image);         texture.setenable(true);         texture.setboundarymodes(texture.wrap);          texture.setboundarymodet(texture.wrap);         return texture;     }      public static appearance getappearance()     {         appearance appear = new appearance();         texture2d texture = loadtexture("lena512color.png");         appear.settexture(texture);         polygonattributes polygonattributes = new polygonattributes();         polygonattributes.setcullface(polygonattributes.cull_none);         // appear.settexcoordgeneration(new texcoordgeneration());         appear.setpolygonattributes(polygonattributes);          return appear;     }      public static void main(string args[])     {         system.setproperty("sun.awt.noerasebackground","true");          graphicsconfiguration config =             simpleuniverse.getpreferredconfiguration();         canvas3d c = new canvas3d(config);          jframe f = new jframe();         f.getcontentpane().add(c, borderlayout.center);          simpleuniverse u = new simpleuniverse(c);         u.getviewingplatform().setnominalviewingtransform();          int gridsize=11;          // comment out according want test         //geometryarray geometry = gettrianglearray(gridsize,2);         geometryarray geometry = getita(gridsize, 2);          shape3d shape = new shape3d(geometry, getappearance());          branchgroup bg = new branchgroup();         bg.addchild(shape);         u.addbranchgraph(bg);          setcameraposition(u.getviewingplatform(),              new point3d(gridsize/2,5,-10),              new point3d(gridsize/2,0,gridsize/2),              new vector3d(0,1,0));           f.setsize(600,600);         f.setlocationrelativeto(null);         f.setvisible(true);     }      public static void setcameraposition(viewingplatform vp, point3d from,         point3d to, vector3d v)     {         transformgroup viewtransform = vp.getviewplatformtransform();         transform3d t3d = new transform3d();          t3d.lookat(from, to, v);         t3d.invert();         viewtransform.settransform(t3d);      }  } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -