Suponiendo que está generando una nueva textura con glGenTextures
cada vez que llama al glTexImage2D
, está desperdiciando memoria y filtrándola si no realiza un seguimiento de todas las texturas que genera. glTexImage2D
toma los datos de entrada y los almacena en la memoria de la tarjeta de video. El nombre de textura que enlaza antes de llamar al glTexImage2D
- el que genera con glGenTextures
es un identificador para ese trozo de memoria de la tarjeta de video.
Si su textura es grande y está asignando memoria nueva para almacenar más y más copias de la misma cada vez que la usa, entonces rápidamente se quedará sin memoria. La solución es llamar al glTexImage2D
una vez durante la inicialización de su aplicación y solo llame al glBindTexture
cuando desee usarlo. Si desea cambiar la textura al hacer clic, solo llame al glBindTexture
y glTexImage2D
. Si su nueva imagen es del mismo tamaño que la imagen anterior, puede llamar al glTexSubImage2D
para decirle a OpenGL que sobrescriba los datos de imágenes anteriores en lugar de eliminarlos y cargarlos.
ACTUALIZACIÓN
En respuesta a su nuevo código, estoy actualizando mi respuesta con una respuesta más concreta. Está tratando con texturas OpenGL por completo. La salida de glGenTextures
es GLuint[]
y no String
o char[]
. Para cada textura que genere con glGenTextures
, OpenGL le devuelve un identificador (como un entero sin signo) a una textura. Este identificador almacena el estado que usted le da con glTexParameteri
y también un trozo de memoria en la tarjeta gráfica si le da datos con glTexImage[1/2/3]D
. Depende de usted almacenar el identificador y enviarle nuevos datos cuando desee actualizarlo. Si sobrescribe el controlador o se olvida de él, la información permanece en la tarjeta gráfica, pero no puede acceder a ella. También está diciendo OpenGL para generar texturas 3 cuando sólo se necesita 1.
Viendo como texture_data
es de un tamaño fijo, puede actualizar la textura con glTexSubImage2D
en lugar de glTexImage2D
. Aquí está su código modificado para evitar la pérdida de memoria de este problema:
texture_data = new GLubyte[width*height]();
GLuint texname; //handle to a texture
glGenTextures(1, &texname); //Gen a new texture and store the handle in texname
//These settings stick with the texture that's bound. You only need to set them
//once.
glBindTexture(GL_TEXTURE_2D, texname);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//allocate memory on the graphics card for the texture. It's fine if
//texture_data doesn't have any data in it, the texture will just appear black
//until you update it.
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, texture_data);
...
//bind the texture again when you want to update it.
glBindTexture(GL_TEXTURE_2D, texname);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, 0, GL_RGB,
GL_UNSIGNED_BYTE, texture_data);
...
//When you're done using the texture, delete it. This will set texname to 0 and
//delete all of the graphics card memory associated with the texture. If you
//don't call this method, the texture will stay in graphics card memory until you
//close the application.
glDeleteTextures(1, &texname);
El código real sería útil para rastrear este problema. –
@NicolBolas, he puesto el código, gracias por mencionar +1 – Shan