java - Texture Mapping is reversed! Java3D -
i use eyes me see why texture map reversed on object. prints backwards -- if seeing in mirror. had suspected maybe reversing indices help, changed mapped without reversing reversal.
public class mobiusbanner extends applet { public static void main(string[] args) { new mainframe(new mobiusbanner(), 800, 600); } @override public void init() { graphicsconfiguration gc = simpleuniverse.getpreferredconfiguration(); canvas3d canvas = new canvas3d(gc); this.setlayout(new borderlayout()); this.add(canvas, borderlayout.center); simpleuniverse su = new simpleuniverse(canvas); su.getviewingplatform().setnominalviewingtransform(); branchgroup bg = createscenegraph(); bg.compile(); su.addbranchgraph(bg); } private branchgroup createscenegraph() { branchgroup root = new branchgroup(); shape3d shape = new shape3d(); shape.setgeometry(mobius().getindexedgeometryarray()); //scaling transform transform3d tr = new transform3d(); tr.setscale(0.5); //spin transform group transformgroup spin = new transformgroup(); spin.setcapability(transformgroup.allow_transform_write); spin.setcapability(transformgroup.allow_transform_read); root.addchild(spin); //set appearance appearance ap = createtextureappearance(); ap.setpolygonattributes(new polygonattributes(polygonattributes.polygon_fill, polygonattributes.cull_back, 0)); //set materials material mat = new material(); mat.setlightingenable(true); mat.setshininess(30); ap.setmaterial(mat); //overarching transform group transformgroup tg = new transformgroup(tr); tg.addchild(shape); spin.addchild(tg); shape.setappearance(ap); //set rotation mouserotate rotator = new mouserotate(spin); boundingsphere bounds = new boundingsphere(); rotator.setschedulingbounds(bounds); spin.addchild(rotator); //set translation mousetranslate translator = new mousetranslate(spin); translator.setschedulingbounds(bounds); spin.addchild(translator); //set zoom mousezoom zoom = new mousezoom(spin); zoom.setschedulingbounds(bounds); spin.addchild(zoom); //set background background background = new background(1.0f, 1.0f, 1.0f); background.setapplicationbounds(bounds); root.addchild(background); f//set lighting ambientlight light = new ambientlight(true, new color3f(color.black)); light.setinfluencingbounds(bounds); root.addchild(light); pointlight ptlight = new pointlight(new color3f(color.white), new point3f(0.5f, 0.5f, 1f), new point3f(1f, 0.2f, 0f)); ptlight.setinfluencingbounds(bounds); root.addchild(ptlight); return root; }//close branchgroup method //create mobius shape private geometryinfo mobius() { int m = 100; //number of row points int n = 100; //number of col points int p = 4 * ((m - 1) * (n - 1)); //faces * points per face indexedquadarray iqa = new indexedquadarray(m * n, geometryarray.coordinates | geometryarray.texture_coordinate_2, p); point3d[] vertices = new point3d[m * n]; int index = 0; //create vertices (int = 0; < m; i++) { (int j = 0; j < n; j++) { double u = * (4 * (math.pi)) / (m - 1); double v = -0.3 + (j * (0.6 / (n - 1))); double x = (1 + v * math.cos(u / 2)) * math.cos(u); double y = (1 + v * math.cos(u / 2)) * math.sin(u); double z = v * math.sin(u / 2); vertices[index] = new point3d(x, y, z); index++; }//close nested loop }//close loop index = 0; //set texture coordinates texcoord2f[] tex = new texcoord2f[m * n]; (int = 0; < m; i++) { (int j = 0; j < n; j++) { tex[index] = new texcoord2f(); tex[index] = new texcoord2f(i * 1f / m, j * 1f / n); index++; } } iqa.setcoordinates(0, vertices); iqa.settexturecoordinates(0, 0, tex); index = 0; //set index coordinates int[] texindices = new int[p]; (int = 0; < m - 1; i++) { (int j = 0; j < n - 1; j++) { iqa.setcoordinateindex(index, * m + j); texindices[index] = * m + j; index++; iqa.setcoordinateindex(index, * m + j + 1); texindices[index] = * m + j + 1; index++; iqa.setcoordinateindex(index, (i + 1) * m + j + 1); texindices[index] = (i + 1) * m + j + 1; index++; iqa.setcoordinateindex(index, (i + 1) * m + j); texindices[index] = (i + 1) * m + j; index++; }//close nested loop }//close loop iqa.settexturecoordinateindices(0, 0, texindices); //create geometry info , generate normals shape geometryinfo gi = new geometryinfo(iqa); normalgenerator ng = new normalgenerator(); ng.generatenormals(gi); return gi; } appearance createtextureappearance() { appearance ap = new appearance(); bufferedimage bi = new bufferedimage(1024, 128, bufferedimage.type_int_argb); graphics2d g2 = (graphics2d) bi.getgraphics(); g2.setcolor(color.white); g2.fillrect(0, 0, 1024, 128); g2.setfont(new font("serif", font.bold, 36)); g2.setcolor(new color(200, 0, 0)); g2.drawstring("mobius strip", 0, 100); imagecomponent2d image = new imagecomponent2d(imagecomponent2d.format_rgba, bi); texture2d texture = new texture2d(texture.base_level, texture.rgba, image.getwidth(), image.getheight()); texture.setimage(0, image); texture.setmagfilter(texture.base_level_linear); ap.settexture(texture); //combine texture , lighting textureattributes textatt = new textureattributes(); textatt.settexturemode(textureattributes.combine); ap.settextureattributes(textatt); ap.setmaterial(new material()); return ap; } }
you want reverse 1 of texture coordinates, not both. try changing
tex[index] = new texcoord2f(i * 1f / m, j * 1f / n);
to
tex[index] = new texcoord2f((m-1-i) * 1f / m, j * 1f / n);
or
tex[index] = new texcoord2f(i * 1f / m, (n-1-j) * 1f / n);
Comments
Post a Comment