Hello Guest

How to make dynamically changing textures

  • 4 Replies
How to make dynamically changing textures
« on: March 28, 2018, 23:01:35 »
Hello! I'm new here so pls forgive me if this question was already asked.
How should I make dynamically changing textures?

Now if the color of my texture has been changed I siply make a new texture:
Code: [Select]
private ByteBuffer pixels = //here I change the color;
//Maybe the problem is I put every byte to the ByteBuffer one by one
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

My problem is it's working, but my performace is really dropped from stabile 60 fps to 4-7 fps.
This drop is because I need to change the color every frame. This texture is smoothly changing from color[n] to color[n+1].
Probably this isn't the right method to do it. Or is it but I'm doing somthig wrong?
« Last Edit: March 28, 2018, 23:19:46 by pocokknight »


Offline josephdoss

  • *
  • 15
  • redbirdflyfree.blogspot.com
    • game dev
Re: How to make dynamically changing textures
« Reply #1 on: March 29, 2018, 13:19:27 »
Hey man,

I'm a newbie too, so my answer is more of a guess than anything. ( In other words, I might not know what I'm talking about. )
But, it seems like you're creating a new texture every time you draw. It's the creating that's super expensive less so than the binding.
If you have a small number of textures, you could just create them all ahead of time and then just bind them to different textureIDs in the draw method.
If that's not the case, then ... you're in for a rough time.

Would you be willing to post some pictures and a more detailed explanation of what you're trying to do? We might be able to point out a better way.


Offline Cornix

  • *****
  • 487
Re: How to make dynamically changing textures
« Reply #2 on: March 29, 2018, 14:08:04 »
It really depends on how you create your buffer and how large it is.

You need to give more information. What is it exactly that you want to do? (perhaps there is a better way to achieve your goal)
How do you generate the texture data?
How often do you do so?
Can you fill the buffer on a separate thread?

Re: How to make dynamically changing textures
« Reply #3 on: March 31, 2018, 23:59:43 »
I managed to make it working. If somebody has the same problem, I changed the colorchanging method not to put every color into it again just the new ones, but the thing what is helped a lot is I dont make every image again just use glTexSubImage2D to change the existing texture. I hope it will help somebody and you don't need to tryhard like me. :)


Offline KaiHH

  • ****
  • 225
Re: How to make dynamically changing textures
« Reply #4 on: April 01, 2018, 08:56:44 »
This sounds like shaders would be a much better fit for you.

If all you need to do is:
1. fill all texels of a texture with the same color
2. that color changes over time, so you fill all texels of that texture with the new color again
3. you map the texture onto a mesh

then simply using a fragment shader to set that uniform color when drawing your mesh should give you a HUGE performance boost.

If each texel of your texture (which is essentially ALWAYS just a function of f: (x, y) -> color) is independent of all the other texels, and that function can be computed quickly (as is the case for a constant color), a shader is pretty much always preferable.

You only ever need a real texture when your texel function is not easily definable (such as with images drawn by artists) or is too complicated to compute (such as with procedural images, in which case the texture essentially acts as a "cache"), and when you want easy antialiasing with mipmapping.