1

I am trying to add a skin to a cube by using the following code, but the skin will not work at all. I tested to see if it will add to a default cube and it will (but repeats the full image on each side instead of wrapping around it, which is why I am making a custom cube to prevent this). Any help would be greatly appreciated.

private void buildGraphics()
{
    Image dieImage = new Image(getClass().getResourceAsStream("images/die.gif"));

    PhongMaterial material = new PhongMaterial();
    material.setDiffuseMap(dieImage);
    material.setSpecularColor(Color.RED);

    float hw = 100/2f;
    float hh = 100/2f;
    float hd = 100/2f;

    float points[] = 
        {
            hw, hh, hd,
            hw, hh, -hd,
            hw, -hh, hd,
            hw, -hh, -hd,
            -hw, hh, hd,
            -hw, hh, -hd,
            -hw, -hh, hd,
            -hw, -hh, -hd,
        };

    float tex[] =
        {
            100, 0,
            200, 0,
            0, 100,
            100, 100,
            200, 100,
            300, 100,
            400, 100,
            0, 200,
            100, 200,
            200, 200,
            300, 200,
            400, 200,
            100, 300,
            200, 300
        };

    int faces[] =
        {
            0, 10, 2, 5, 1, 9,
            2, 5, 3, 4, 1, 9,
            4, 7, 5, 8, 6, 2,
            6, 2, 5, 8, 7, 3,
            0, 13, 1, 9, 4, 12,
            4, 12, 1, 9, 5, 8,
            2, 1, 6, 0, 3, 4,
            3, 4, 6, 0, 7, 3,
            0, 10, 4, 11, 2, 5,
            2, 5, 4, 11, 6, 6,
            1, 9, 3, 4, 5, 8,
            5, 8, 3, 4, 7, 3
        };

    TriangleMesh mesh = new TriangleMesh();
    mesh.getPoints().addAll(points);
    mesh.getTexCoords().addAll(tex);
    mesh.getFaces().addAll(faces);

    MeshView box = new MeshView(mesh);
    box.setMaterial(material);

    graphicGroup.getChildren().add(box);

    Box box2 = new Box(100, 100, 100);
    box2.setTranslateX(150);
    box2.setMaterial(material);


    graphicGroup.getChildren().add(box2);
    world.getChildren().add(graphicGroup);
}
  • Got the answer to my question from PavelSafrata on Oracles OTN, the tex coords have to be proportional... – Jamie Coleshill Oct 21 '13 at 21:13
  • 1
    So in this situation my texture coords should be as follows: 'code' float tex[] = { 0.25f, 0f, 0.5f, 0f, 0f, 0.33f, 0.25f, 0.33f, 0.5f, 0.33f, 0.75f, 0.33f, 1f, 0.33f, 0f, 0.66f, 0.25f, 0.66f, 0.5f, 0.66f, 0.75f, 0.66f, 1f, 0.66f, 0.25f, 1f, 0.5f, 1f}; – Jamie Coleshill Oct 21 '13 at 21:17

1 Answers1

0

You are correct... I have also been playing around with making basic shapes.. like Capsule, Height Maps, Torus, etc...

The texCoords are a pain, but faces are even more of a pain when doing more complex shapes.

As you commented, texCoords should be a value between 0.0(0%), and 1.0(100%) of your rectangle Image

I still have not been able to figure out my texCoords for my Torus...

I still think the guys behind the scenes need to get rid of their system for building TriangleMesh's.

Point3D makes perfect sense for the Points[], yet they use a float[] ... same with texCoords... Point2D would make a much better fit imho.. The Faces[] is ok... As it is a simple Integer[]...

I may even just create a SimpleMesh class that handles thing in that way... anyways, Glad you figured it out.

jdub1581
  • 659
  • 5
  • 10
  • 2
    Java's current lack of [value objects](http://openjdk.java.net/jeps/169) means that using an array of objects such as Point3D (with 3 double members), consumes much more memory than an array of floats, such as is currently used. Also, it is likely that the float array can be input directly into lower layer native rendering logic (via JNI) rather than undergoing a conversion such as you would need with an array of Point3D objects. You could always create a new, higher level API layer for JavaFX which works based on Point3D objects, but it would be relatively inefficient. – jewelsea Jul 26 '14 at 01:12
  • thanks for that info .. I guess I was trying to say that it would be much nicer if there was a class like Point3D(x,y,z) or Vector3(x, y, z), for creating the points, and a class that maps to 2d for uv/texCoords something similar to FloatMap.. It's just easier in for loops. Ive been working on creating a MeshMaker allowing you to see vertexes and add points with mouse... – jdub1581 Jul 26 '14 at 02:20