Changing texture

Started by
7 comments, last by WoopsASword 7 years, 6 months ago

Hi All

I'm looking for convenient and fast way to create textures from one original with modified size and/or content:

- use a part (cropped rectangle) of whole image

- invert RGB or Alpha or both

- convert RGB to grayscale

- deactivate Alpha (fill with 1.0)

I can load original pixels into CPU RAM, modify them and pass back to GPU (create a new texture). Is there a better way/technique to process all on GPU?

Thank you

Advertisement

In the simplest form, you could use a very simple fragment shader so render your texture into another texture (Google Render to Texture). But if you only need to do it once, it's probably not a lot faster than doing it on the CPU, but quite a bit more work, especially if you don't have render buffer support in your engine already.

Is there a better way/technique to process all on GPU?

You might be able to do it on the fragment shader, depending on your requirements.

Compute shaders are also able to generate images.

You can also do it with OpenCL or any other computation languages.

- use a part (cropped rectangle) of whole image

That is what the UV map is for.

- invert RGB or Alpha or both

To effect the colors of a texture you will need a shader to do it or directly effect the texture's pixel buffer, this is the math:

If you can't effect a channel on it's own then just multiply the others by 1, it keeps them the same.

(R,G,B)*-1

(A)*-1

- convert RGB to grayscale

(R + G + B) / 3. for average.

(max(R, G, B) + min(R, G, B)) / 2. for lightness.

0.21 R + 0.72 G + 0.07 B. luminosity.

Note the above is what Gimp uses, you can tweak them to match what you need.

- deactivate Alpha (fill with 1.0)

(A) = 1

If you can't write your own fragment shader then just Google how, it is very easy and is the very basic shader.

If you are using a engine, it will have a shortcut to effecting the pixels in a shader, you will need to check the documents.

Thx for your replies but I'm not sure that changing RGB/Alpha in shader (instead of another texture) is a solution here. I must change original pixels, not a sampled value received in shader. It's not same.

Thx for your replies but I'm not sure that changing RGB/Alpha in shader (instead of another texture) is a solution here. I must change original pixels, not a sampled value received in shader. It's not same.

In the OP you mention:
"I can load original pixels into CPU RAM, modify them and pass back to GPU (create a new texture). Is there a better way/technique to process all on GPU?"
So that's:
Texture->CPU Code->New Texture
If you want to run that on the GPU, you get:
Texture->GPU Code->New Texture

"Code" on a GPU is a shader. You want code that loads the old texture, makes some changes, and writes a new texture. If you want to run that code on the GPU, you write it in a shader language.
If you want to update the original texture instead of creating a new one, then copy the new texture over the old one at the end:
Texture->glDraw->New Texture->glCopyTexImage2D->Texture

I must change original pixels, not a sampled value received in shader. It's not same.

OK, so it is my understanding that you want to chance the texture permanintly and not only for an effect.

Use glReadPixels or glGetTexImage and copy the result data into a empty buffer.

The empty buffer is now a digital copy of the original texture, make your changes now using math and then save the texture to your drive using File.write or any one of the hundreds of ways to save.

You should now have a texture that you can use as normal, you can also use the digital copy for your work.

There are many tutorials on doing this just search for "OpenGL saving a texture".

If you want to run that on the GPU, you get: Texture->GPU Code->New Texture
Yes, but how can I load the original to modify it with shaders? For example if I "draw some (textured) quad" then I've sampled (not original) pixels.

For now using CPU looks much simple, maybe slower but it;s not a catastrophe

Thx all for help

Render it to a FBO using a proper shader to process your image.

Cropping is a different thing, I think OpenGL can crop textures using internal functionality.

I kind of forgot the function.

This topic is closed to new replies.

Advertisement