SDL_image / C ++ OpenGL Program: IMG_Load () creates fuzzy images

I am trying to upload an image file and use it as a texture for a cube. I use SDL_image to do this.

original image

I used this image because I found it in various file formats (TGA, TIF, JPG, PNG, BMP)

Code:

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);

The code compiles without errors and warnings!

I am tired of all file formats, but this always leads to a terrible result:

result

I use: SDL_image 1.2.9 and SDL 1.2.14 with Xcode 3.2 under 10.6.2

Does anyone know how to fix this?

+3
source share
3 answers

, RGBA, . texture->format, , , GL_, . ( .)

+10

, greyfade , , , - . , , , , SDL_LockSurface(). :

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);
+2

-, 4 , 3 . , , a.jpg.

glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, texture-> w, texture-> h, 0, GL_RGB, GL_UNSIGNED_BYTE, texture-> pixels);

to

glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, texture-> w, texture-> h, 0, GL_RGB, GL_UNSIGNED_BYTE, texture-> pixels);

That should fix it.

For .png using alpha channel

glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, texture-> w, texture-> h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture-> pixels);

0
source

Source: https://habr.com/ru/post/1722740/


All Articles