How to change single texel in OpenGL texture

别说谁变了你拦得住时间么 提交于 2019-11-30 19:43:46

OpenGL has the glTexSubImage2D function, which is exactly for your purpose.

Here's a functions that changes the color of one texel:

void changeTexelColor(GLuint id, GLint x, GLint y, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
    uint8_t data[4];
    data[0] = r;
    data[1] = g;
    data[2] = b;
    data[3] = a;
    glBindTexture(GL_TEXTURE_2D, id);
    glTexSubImage2D(GL_TEXTURE_2D,
                    0,
                    x,
                    y,
                    1,
                    1,
                    GL_RGBA,
                    GL_UNSIGNED_BYTE,
                    data);
}

Performance-wise, you may be better to store the map locally as your own array and draw it to the screen as a set of untextured quads.

Rendering primitives is heavily optimised, especially compared to creating or modifying textures.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!