LWJGL pixel manipulation ?

Hi.

In java2D I used BufferedImage and array of pixels to modify it and then render it on the screen. Is there something similar to this in lwjgl ?

You can modify the bytes and re-upload them to the GPU with glTexImage2D or glTexSubImage2D. Usually this is fast enough (i.e. if you are doing it at initialization, or even a couple times per frame) but for better performance and asynchronous transfers you can use a Pixel Buffer Object.

Depending on the situation (i.e. inverting the colours of an image and other post-processing effects), you might be better off just using fragment (i.e. “pixel”) shaders.

You can also use “render to texture” techniques (e.g. Frame Buffer Objects) depending on how you are trying to manipulate a texture.

So the real question is: what are you trying to accomplish?

Damn, that’s a load of informations :slight_smile:

I’m trying to do simple stuff like multypling image by color, or change pixels of i.e. pink color to red… or stuff like that. I would like to learn those fragment shaders stuff too.

Do you know what’s the best technique to use ?
Also do you have a link on some good fragment shaders tutorials ?

Definitely do that kind of stuff with a fragment shader. You should know that you can multiply a texture by a color easily by just setting glColor() to whatever you want it multiplied by, right?

Didn’t know that LOL :slight_smile:

But I still wanna learn fragment shaders :slight_smile:

Vertex coloring is the easiest way to modify an image like that, but it’s a bit limited.

Regarding shaders:

What are you using for image decoding / texture loading? If it’s SlickUtil, I’d recommend pulling the latest Slick code and using the ShaderProgram utility. LibGDX also has a ShaderProgram utility, or you can write your own very easily.

Then I’d recommend browsing some tutorials like this or reading some OpenGL books like this (see ra4king’s Java ports) to learn the theory behind it. There is also more info at the following websites:
http://open.gl/

I’d also recommend “learning by example” – see here for some Slick-specific examples.

I’m having trouble.

Can someone show me some simple stuff with explanation, doesn’t matter is it shader or anything else. What I want to do is take color ( purple ) fom texture and replace it with anoder color ( pink ).

How would you perform this ?

Are you doing this at initialization? Or do you need to be doing it in real-time, e.g. every frame?

If the former:

Grab the pixels into a ByteBuffer (either through something like PNGDecoder, or glGetTexImage). Create a new buffer with the same size. Loop through the returned pixels putting each into the new buffer – however, if any are equal to “purple” then you substitute it with “pink.” Upload the new buffer into the old texture with glTexImage2D.

I need it in real time. The ByteBuffer thing is I don’t know how to use. That’s what I need explained.

I’d make the texture black-and-white. By doing that, you can just set the color of the white/gray parts using glColor(), since the color from glColor() is multiplied per pixel with the texture sample color.

White texture color * red glColor() color = (1, 1, 1) * (1, 0, 0) = (1, 0, 0) = red
Gray texture color * green glColor() color = (0.5, 0.5, 0.5) * (0, 1, 0) = (0, 0.5, 0) = somewhat dark green
etc…

I know that but I still want to learn different techniques ( I hope I spelled last word correctly ).

Like?

The ones davedes mentioned.