Jump to content
  • Advertisement
Sign in to follow this  
Enjoy

Modifying a texture

This topic is 3789 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

Hello, I am loading texture from TGA file. How can I modify a loaded texture? And how can I generate a texture on my own? For example if I want to generate a cloud texture and after this to attach this to a billboard. I am storing Image data like this: unsigned char *data Thank you in advance.

Share this post


Link to post
Share on other sites
Advertisement
To generate a texture on your own simply allocate the required amount of memory and write the pixel data to the allocated pixels.
You can modify an already uploaded OpenGL texture with glTexSubImage.

Share this post


Link to post
Share on other sites
Assuming that your .TGA loader loads the image into a bitmap array in the RGB (or RGBA) format, it should be as easy as just changing the colors yourself:


unsigned char* image;

image[0] = Red of #1 pixel
image[1] = Green of #1pixel
image[2] = Blue of #1 pixel
image[3] = Alpha of #1 pixel
image[4] = Red of #2 pixel
image[5] = Green of #2pixel
image[6] = Blue of #2 pixel
image[7] = Alpha of #2 pixel
image[8] = Red of #3 pixel
// Etc..






And generating your own texture is just as easy


int width = 256;
int height = 256;
int format = 3; // 3 bytes per pixel, RGB
unsigned char* texture = new unsigned char[width*height*format];

for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
texture[((y*width) + x)*format] = Red;
texture[((y*width) + x)*format+1] = Green;
texture[((y*width) + x)*format+2] = Blue;
}
}

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture);






I hope this helps.

Share this post


Link to post
Share on other sites
Like polymorphed showed, modifying a texture when loading it is as easy as changing individual pixels. This way you can also generate textures - the only thing you need is some algorithm for generating procedural textures.

Updating a texture that is loaded to video memory can be done via glTexSubImage2D. This function let's you update a rectangular region of the texture with new data.

Share this post


Link to post
Share on other sites
Many thanks,
I just didn't know how to write pixel data.

I will try if it is working for me.

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!