views:

339

answers:

2

Hi !

I want to change the some texels in a OpenGL texture for a given location. Can anyone help me with this pls ?

This is the functionnality that I want,

void ChangeTexelColor(int x, int y, GLuint id, int texW, int texH, GLenum format)
{
   //What is here ?     
}

This will use to maintain the minimap of my game (if anyone have a better idea of maintaining a dynamic map-texture-). Btw, this must done fast. Thanks.

+5  A: 

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);
}
Nikolai Ruhe
wow... Great, thank you very much Nikolai. I'll try it.
Morpheus
Yeah... that was the functionality I wanted. You increase my game's FPS by lot. lol. Thanks again.
Morpheus
Do you need to have other commands above or below this? It does not seem to work for me.
Geoff
A: 

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.

Zooba
Don't take this for granted, it depends heavily on the size of the map and the number of texels in each change. If the map was 100 x 100 texels it would be very inefficient to draw it using geometry.
Nikolai Ruhe