This discussion is archived
7 Replies Latest reply: Oct 31, 2010 12:56 PM by 803051 RSS

Textures: It Shouldn't Be This Hard

803051 Newbie
Currently Being Moderated
I've got a quad, 4 points in 3D space. It has an Appearance including a material, and it successfully displays.

I've got a texture, 32x32. I can load it and attach it to the Appearance. It's not part of that multi-texture structure, because I haven;t gotten to that yet.

I cannot get the QuadArray containing my points to accept texture coordinates. The closest I've gotten is to have it want a texCoordSetMap. Nothing I have tried can breach the gap. I keep getting a Null Pointer Exception.

I'm sure this is something every new programmer runs into. Therefore, the answer should be easy and well-known. My inability to find an example on the internet is therefore exasperating.

Please, someone, provide me a functional example I can dissect.
  • 1. Re: Textures: It Shouldn't Be This Hard
    793415 Pro
    Currently Being Moderated
    800048 wrote:
    I've got a quad, 4 points in 3D space. ..
    .. QuadArray ..
    I'm sure this is something every new programmer runs into. Therefore, the answer should be easy and well-known. My inability to find an example on the internet is therefore exasperating.
    You might be surprised at how few people are:
    a) Coding for the client-side in Java (a random sampling of job ads suggests that server-side programming outstrips client-side by around 20 to 1).
    b) Using Java 3D on the client-side!
    Please, someone, provide me a functional example I can dissect.
    That is not how it works. Your best chance of getting help (which is already slim) is to post code that shows the best attempt you have made.
  • 2. Re: Textures: It Shouldn't Be This Hard
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Moderator action: Moved from Java Programming.

    db
  • 3. Re: Textures: It Shouldn't Be This Hard
    803051 Newbie
    Currently Being Moderated
    All right then. Here's some code. Where is it wrong?
            Appearance ap=new Appearance();
            material=new Material();
            material.setAmbientColor(new Color3f(Color.WHITE));
            material.setDiffuseColor(new Color3f(Color.GRAY));
            material.setSpecularColor(new Color3f(0.0f,0.0f,0.0f));
            material.setEmissiveColor(new Color3f(0.0f,0.0f,0.0f));
            material.setShininess(64.0f);
            material.setCapability(Material.ALLOW_COMPONENT_WRITE);
            material.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
            material.setLightingEnable(true);
    
            ap.setMaterial(material);
    
    ...
    
            TextureLoader texImage = new TextureLoader("TextureAtlas.png",observer);
    
    ...
    
            double[] ptArray={loc[i][j].x,loc[i][j].y,loc[i][j].z,
                loc[i][j+1].x,loc[i][j+1].y,loc[i][j+1].z,
                loc[i+1][j+1].x,loc[i+1][j+1].y,loc[i+1][j+1].z,
                loc[i+1][j].x,loc[i+1][j].y,loc[i+1][j].z  };
    
            GeometryInfo gi=new GeometryInfo(GeometryInfo.QUAD_ARRAY);
            gi.setCoordinates(ptArray);
            NormalGenerator normalGenerator = new NormalGenerator();
            normalGenerator.generateNormals( gi );//Down to here works, because I get shading when displaying with just the material
    
            TexCoord2f[] texpt=new TexCoord2f[4];
         texpt[0]=new TexCoord2f(0.0f,0.0f);
         texpt[1]=new TexCoord2f(1.0f,0.0f);
             texpt[2]=new TexCoord2f(1.0f,1.0f);
             texpt[3]=new TexCoord2f(0.0f,1.0f);
    
            gi.setTextureCoordinateParams(1, 2);
            gi.setTextureCoordinates(0, texpt);
    
            ap.setTexture (texImage.getTexture());
    
            int[] textmap=gi.getTexCoordSetMap();
    
            QuadArray ga=new QuadArray(4,GeometryArray.COORDINATES|
                    GeometryArray.TEXTURE_COORDINATE_2|
                    GeometryArray.NORMALS
                    ,4,textmap);               //This is the line where the error occurs.
                ga.setCoordinates(0, gi.getCoordinates());
                ga.setNormals(0,gi.getNormals());
                ga.setTextureCoordinates(0,0, texpt);
    This is the error I get:
    Exception in thread "main" java.lang.IllegalArgumentException: GeometryArray: texCoord set mapping is not specified
            at javax.media.j3d.GeometryArray.<init>(GeometryArray.java:806)
            at javax.media.j3d.GeometryArray.<init>(GeometryArray.java:592)
            at javax.media.j3d.QuadArray.<init>(QuadArray.java:85)
            at texmapper.MapGrid.<init>(MapGrid.java:134)
    Of secondary importance, in that last line, instead of "texpt" I've tried "gi.getTextureCoordinates(0)" which doesn't work at all.

    I do find the dearth of client-side Java3D surprising. It's very interesting.

    Apologies to the moderator for misposting this wherever I put it before it wound up here.
  • 4. Re: Textures: It Shouldn't Be This Hard
    793415 Pro
    Currently Being Moderated
    800048 wrote:
    All right then. Here's some code. Where is it wrong?
    ..
    I hope I did not wrongly lead you to believe that I personally could spot the problem. J-3D is not an API I have ever dealt with..

    OTOH, some minor points:
    <li> The first post mentioned a NullPointerException, whereas the stack trace explicitly states it is an IllegalArgumentException that is the problem. Always copy/past stack traces. Always!
    <li> The IAE says it pertains to texCoord, which is not referenced in the commented code line.
    <li> There is no mention of where texCoord is declared or initialized. For this, and other, reasons I recommend people post an SSCCE (http://pscode.org/sscce.html).
    I do find the dearth of client-side Java3D surprising. It's very interesting.
    (shrugs) It would have been fun for applets, but the problem was the J-3D natives which meant that J-3D needed to be installed before surfing to the applet. Not very practical. More recently (1.6.0_10+), applets could hook into JWS functionality, which took care of installing natives, so there might be an upswing in usage.
    Apologies to the moderator for misposting this wherever I put it before it wound up here.
    I asked the mods. to decide whether it should be moved from Java Programming to either of New To Java or Java 3D. The first was being a little facetious, but I do know of one mod. that would have happily moved it there. Fortunately you got a mod. that is more interested in aligning threads to topics - so Java 3D it was!

    And I doubt that either of us expects any sort of apologies for your choice of forum, just as we would offer none for our subsequent actions. ;)
  • 5. Re: Textures: It Shouldn't Be This Hard
    803051 Newbie
    Currently Being Moderated
    The difference in error is due to the fact that I've been working on the problem. The code shown reflects the current state of the art, which gets the error I copied. I'm pretty sure I'm now closer to right than when I first posted.

    In retrospect, I phrased that comment poorly. It's the statement which ends on the line I commented that causes the error. The line number given in the error is the start of that statement. "QuadArray ga=" and so on. If the "TexCoord2f [] texpt" isn't the "texCoord" mentioned in the error, I don't know what is or where to create it.

    Honestly, "New to Java" would also apply. I guess I've been at this about a month.
  • 6. Re: Textures: It Shouldn't Be This Hard
    InteractiveMesh Explorer
    Currently Being Moderated
    Hi 800048, let GeometryInfo do the whole job:
    GeometryInfo gi= new GeometryInfo(GeometryInfo.QUAD_ARRAY);
    gi.setCoordinates(ptArray);
    gi.setTextureCoordinateParams(1, 2);
    gi.setTextureCoordinates(0, texpt);
    
    // If the 'texCoordSetMap' hasn't been set, GeometryInfo assumes one set of 
    // texture coordinates only and one texture state unit 
    
    // The 'texCoordSetMap' is an array that maps texture coordinate sets 
    // to texture units. For a single texture:
    
    // int[] textmap = {0};
    // gi.setTexCoordSetMap(textmap);
    
    NormalGenerator normalGenerator = new NormalGenerator();
    normalGenerator.generateNormals(gi);
    
    // This will return a QuadArray due to 'GeometryInfo.QUAD_ARRAY'
    GeometryArray myQuadArray = gi.getGeometryArray();
    For more details see javadoc of GeometryArray and GeometryInfo. August
  • 7. Re: Textures: It Shouldn't Be This Hard
    803051 Newbie
    Currently Being Moderated
    That worked. Hooray! I knew it had to be easier than I was making it.

    Now to figure out how to grab just part of the texture to display, and I think I have an idea on that.

    Thank you.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points