Jump to content
  • Advertisement
Sign in to follow this  
Tommato

Changing texture

This topic is 711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

- 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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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".

Share this post


Link to post
Share on other sites

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 

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!