Deepak Sharma
Deepak Sharma

Reputation: 6477

Creating & Updating OpenGLES Texture on iOS

I want to create an OpenGLES 2.0 texture and update it on every draw call on the CPU side so that shaders have the correct data when they use the texture. Here is the code to create texture:

GLubyte * oglData;

- (GLuint)setupTexture
{
    oglData = (GLubyte *) calloc(256*1*4, sizeof(GLubyte));

    GLuint texName;
    glGenTextures(1, &texName);
    glBindTexture(GL_TEXTURE_2D, texName);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, oglData);

     return texName;
    }

I plan to update oglData (256x1 RGBA pixel data) everytime before glDrawArrays(). My doubts:

  1. Does iOS supports 256x1 textures? If not, what is the way out?

  2. Is it possible to update oglData multiple times and then call glTexImage2D everytime to update the texture? Is this repeated copying of data from CPU to GPU really efficient or there are better ways?

  3. Am I on the correct track?

Upvotes: 0

Views: 198

Answers (1)

Reaper
Reaper

Reputation: 757

1) I haven't worked with iOS and OpenGL ES on it, but, if those textures are not supported by default, some extension could add support for them.

2) For updating textures you should use glTexSubImage2d. The difference is that glTexImage2D reallocates the memory, while glTexSubImage2D does not. This might be efficient enough for you depending on the texture size, upload frequency etc. If there isn't much texture data, it might be better to upload it all instead of updating.

Upvotes: 1

Related Questions