Sign in to follow this  

Modifying a texture

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

This topic is 3578 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this