SDL_image / C ++ Programa de OpenGL: IMG_Load () produce imágenes borrosas
Pregunta
Estoy intentando cargar un archivo de imagen y usarla como una textura para un cubo. Estoy usando SDL_image de hacer eso.
He utilizado esta imagen, ya lo he encontrado en varios formatos de archivo (TGA, TIF, JPG, PNG, BMP)
El código:
SDL_Surface * texture;
//load an image to an SDL surface (i.e. a buffer)
texture = IMG_Load("/Users/Foo/Code/xcode/test/lena.bmp");
if(texture == NULL){
printf("bad image\n");
exit(1);
}
//create an OpenGL texture object
glGenTextures(1, &textureObjOpenGLlogo);
//select the texture object you need
glBindTexture(GL_TEXTURE_2D, textureObjOpenGLlogo);
//define the parameters of that texture object
//how the texture should wrap in s direction
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//how the texture should wrap in t direction
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//how the texture lookup should be interpolated when the face is smaller than the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//how the texture lookup should be interpolated when the face is bigger than the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//send the texture image to the graphic card
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGB, GL_UNSIGNED_BYTE, texture-> pixels);
//clean the SDL surface
SDL_FreeSurface(texture);
El código se compila sin errores o advertencias!
Me he cansado todos los formatos de archivos, pero esto produce siempre que el resultado feo:
Estoy usando: SDL_image 1.2.9 y 1.2.14 SDL con XCode 3.2 bajo 10.6.2
¿Alguien sabe cómo solucionar este problema?
Solución
La razón se distorsiona la imagen se debe a que no está en el formato RGBA que has especificado. Compruebe el texture->format
para averiguar el formato que se encuentra en y seleccione la constante GL_
apropiado que representa la formato. (O, transformar usted mismo para el formato de su elección.)
Otros consejos
greyfade tiene la respuesta correcta, pero otra cosa que debes tener en cuenta es la necesidad de bloquear las superficies. Esto probablemente no es el caso, ya que se trabaja con una superficie en memoria, pero normalmente se necesita para bloquear las superficies antes de acceder a sus datos de píxeles con SDL_LockSurface()
. Por ejemplo:
bool lock = SDL_MUSTLOCK(texture);
if(lock)
SDL_LockSurface(texture); // should check that return value == 0
// access pixel data, e.g. call glTexImage2D
if(lock)
SDL_UnlockSUrface(texture);
Si usted tiene un Chanel alfa de cada píxel es de 4 bytes sin signo, si no que es de 3 bytes sin signo. Esta imagen no tiene transpareny y cuando intento para guardarlo, es un .jpg.
cambio
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, la textura> w, la textura> h, 0, GL_RGB, GL_UNSIGNED_BYTE, la textura> píxeles);
a
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, la textura> w, la textura> h, 0, GL_RGB, GL_UNSIGNED_BYTE, la textura> píxeles);
Eso debería arreglarlo.
Para una .png con un uso canal alfa
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, la textura> w, la textura> h, 0, GL_RGBA, GL_UNSIGNED_BYTE, la textura> píxeles);