首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理PApplet加载图像不起作用

处理PApplet加载图像不起作用
EN

Stack Overflow用户
提问于 2020-04-21 17:08:49
回答 1查看 369关注 0票数 1

我在一个简单的Java中使用processing.core.PApplet库。

我在setting函数中加载了多个图像,并试图在draw函数中绘制它们,但奇怪的是,纹理没有出现吗?

下面是我用来加载它们的代码:

代码语言:javascript
复制
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." );
    }
}

我用来画其中之一的代码:

代码语言:javascript
复制
// 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();
}

文件在那里,我的Logger说它们被找到了,我没有错误,但是纹理有一个PImage的所有属性?

还有第二件奇怪的事:

draw方法之前,我在draw函数中这样做:

代码语言:javascript
复制
sketch.image(Registry.getTextures().get("minecraft:void"), 10, 10);

在那里,图像加载得很完美?

是的,我在做一个“我的克隆人”

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-22 13:33:42

我找到了!

texture()只有在preDraw()函数和postDraw()函数之间运行时才能工作,但是box()函数有这些步骤,所以它不能工作,您必须使用顶点创建一个多维数据集。

处理为我们提供了一个使其成为那里的例子!

我为自定义这个示例所做的是一个创建顶点的Box类,也可以设置一个大小,就是:

代码语言:javascript
复制
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;
    }
}

这完全解决了我的问题,现在我有一个立方体的纹理!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61349128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档