javaprocessingtexturesloadimage

Processing PApplet loading image not working


I'm using the processing.core.PApplet library in a simple Java Project.
I load multiple images in the setting function and I tried to draw them in the draw function but oddly the textures doesn't appear ?

There is the code I use to load them :

public void init() throws FileNotFoundException { // this get executed in the 'setting' function of my sketch
    Registry.register(getImage("void"), "void");
}

public processing.core.PImage getImage(String name) throws FileNotFoundException {
    String path = "src\\main\\resources\\blocks\\textures\\" + name + ".png";
    File file = new File(path);

    if(file.exists()) {
        Logger.info("Texture " + file.getAbsolutePath() + " found.", "TextureMapping");
        return sketch.loadImage(path);
    } else {
        throw new FileNotFoundException("File " + file.getAbsolutePath() + " not found." );
    }
}

And the code I use to draw one of them :

// I create and draw a new cube in the 'draw' function of my sketch
// But it appears without any texture
public Cube(processing.core.PApplet sketch, BlockPos pos, @NotNull Block block) { 
    this.sketch = sketch;
    this.block = block;
    position = pos;
    texture = Registry.getTextures().get("minecraft:void");
    texture.loadPixels();
}

public void draw() {
    sketch.pushMatrix();
    sketch.translate(position.getX(), position.getY(), position.getZ());
    sketch.box(10);
    sketch.texture(texture); // Doin' nothing
    sketch.popMatrix();
}

And the file are there, my Logger say that they are found, I get no error, and yet the texture has all the properties of an PImage ?

And second oddly thing :
Before the draw method, I do this in the draw function :

sketch.image(Registry.getTextures().get("minecraft:void"), 10, 10);

And there, the image loads perfectly ???

yes I'm doin' a Minecraft clone


Solution

  • I found !

    The texture() is only working when runned between the preDraw() and postDraw() functions, but the box() function has these steps into it, so it cannot work, you have to create a cube using Vertex.
    Processing offers us an example to make it there !

    What I did to customize this example is a Box class that create vertex, a size can be set also, there it is :

    public class Box {
        private final PApplet sketch;
        private final int scale;
    
        public Box(PApplet sketch, int scale) {
            this.sketch = sketch;
            this.scale = scale;
        }
    
        public void generateVertex(PImage texture) {
            sketch.scale(scale);
            sketch.beginShape(sketch.QUADS);
            sketch.texture(texture);
    
            // +Z "front" face
            sketch.vertex(-1, -1, 1, 0, 0);
            sketch.vertex(1, -1, 1, 1, 0);
            sketch.vertex(1, 1, 1, 1, 1);
            sketch.vertex(-1, 1, 1, 0, 1);
    
            // -Z "back" face
            sketch.vertex(1, -1, -1, 0, 0);
            sketch.vertex(-1, -1, -1, 1, 0);
            sketch.vertex(-1, 1, -1, 1, 1);
            sketch.vertex(1, 1, -1, 0, 1);
    
            // +Y "bottom" face
            sketch.vertex(-1, 1, 1, 0, 0);
            sketch.vertex(1, 1, 1, 1, 0);
            sketch.vertex(1, 1, -1, 1, 1);
            sketch.vertex(-1, 1, -1, 0, 1);
    
            // -Y "top" face
            sketch.vertex(-1, -1, -1, 0, 0);
            sketch.vertex(1, -1, -1, 1, 0);
            sketch.vertex(1, -1, 1, 1, 1);
            sketch.vertex(-1, -1, 1, 0, 1);
    
            // +X "right" face
            sketch.vertex(1, -1, 1, 0, 0);
            sketch.vertex(1, -1, -1, 1, 0);
            sketch.vertex(1, 1, -1, 1, 1);
            sketch.vertex(1, 1, 1, 0, 1);
    
            // -X "left" face
            sketch.vertex(-1, -1, -1, 0, 0);
            sketch.vertex(-1, -1, 1, 1, 0);
            sketch.vertex(-1, 1, 1, 1, 1);
            sketch.vertex(-1, 1, -1, 0, 1);
    
            sketch.endShape();
        }
    
        public int getScale() {
            return scale;
        }
    }
    

    And this solve perfectly my problem, now I have a cube with textures !