Dynamic Texture generation

Hi all,

I’m relatively new to JME but so far it’s looking great for what I want.

Basically I’m needing to have a computer in my game, and i need to update the screen, so what I have done so far is make a box where the screen should go and set its texture to an image.

What I need to do is have some way to modify that image and in-turn the texture on the box dynamically, sort of like making a BufferedImage in Java2D and using .getGraphics() to get a canvas and then draw to it.

If you have another way I could accomplish this, or a way to make my idea work, I’d appreciate all the help I can get.

Cheers,
Lucas

Welcome to the community =)
I recommend you take a look at ImageRaster and the ImagePainter plugin which makes ImageRaster a bit friendlier to work with. With this you can paint on a texture.

This will be the easiest solution, but not the most performant since you have to send the new texture of the CPU<->GPU bus after every change.

1 Like
@kwando said:

This will be the easiest solution, but not the most performant since you have to send the new texture of the CPU<->GPU bus after every change.

Cheers for the reply,

I think this approach will work and even though it may be slow, I’m only updating every few seconds or so, so it should be fine :slight_smile:

Thanks heaps,
Lucas

Hi again,

So I tried to implement what kwando suggested like this:

[java] static Texture tex;

public static void initComputer(AssetManager assetManager, Node root, BulletAppState bas){

    ImagePainter scr = new ImagePainter(Image.Format.RGBA8, 150, 150);
    scr.wipe(ColorRGBA.Black);
    scr.paintLine(1, 1, 99, 99, 1, ColorRGBA.Blue, ImagePainter.BlendMode.SET);
    
    tex.setImage(scr.getImage());
}[/java]

But that doesn’t work. I’m getting a NullPointerException on the tex.setImage(); method, can anyone suggest what i am doing wrong?

Thanks for the help,
Lucas

tex is null. You’ve never set it to anything.

(insert tips here about learning Java before doing the hardest kind of programming in an attempt to be helpful that’s rarely taken that way)

1 Like
@pspeed said: tex is null. You've never set it to anything.

(insert tips here about learning Java before doing the hardest kind of programming in an attempt to be helpful that’s rarely taken that way)

Hahaha, stupid me, can’t believe I didn’t spot that. This is what happens when you program at 2am.
I actually know Java proficiently, made numerous games in it over the last 4 years.

Well, a NullPointerException is the single easiest exception to track down… so don’t be surprised if people think “newb coder” if you post one here. :slight_smile:

@pspeed said: Well, a NullPointerException is the single easiest exception to track down... so don't be surprised if people think "newb coder" if you post one here. :)

I know, I know… I tried setting it to something and still got errors, I just glazed over them and thought it was the same thing, turns out it was from a different place in my code.

Cheers for spotting it :slight_smile:

I like NPEs because they are kind of like a puzzle. You don’t even have to know anything about the classes or anything to track them down. If you can see the line then you can tease back from there.

For example, the NPE had to be caused by one of these two dereferences:

Has to be. And then even without knowing anything else but the previous line, I can already tell with 100% certainty that ‘tex’ is null because otherwise the NPE would have happened on the previous line.

Just a puzzle. :slight_smile:

Bw @pspeed if you like those things, ever took a look at Java Pzzlers?

http://www.javapuzzlers.com/java-puzzlers-sampler.pdf

I found it quite amusing to read and try to figure out what actually does happen instead of the desired result.