# OpenGL SDL Texture doing really weird things

I am trying to render a .png image to the screen using C++, SDL and glut.

This the texture I made using paint

This is what I get on the screen:

Does anyone have an idea what could cause this?

void Main::loadStuff() {
if (!surface) {
std::cout << "errror";
return;
}

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

int Mode = GL_RGB;

if(surface->format->BytesPerPixel == 4){
std::cout << "errror2";
Mode = GL_RGBA;
}

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w,surface->h, 0, GL_RGB,GL_UNSIGNED_BYTE,surface->pixels);

SDL_FreeSurface(surface);
}


How I rendered the image:

///TEXTURE
glBindTexture(GL_TEXTURE_2D,texture);
glEnable(GL_TEXTURE_2D);

int X = 0;
int Y = 0;
int Width = 1;
int Height = 1;

glTexCoord2f(0, 0); glVertex3f(X, Y, 0);
glTexCoord2f(0, 1); glVertex3f(X + Width, Y, 0);
glTexCoord2f(1, 1); glVertex3f(X + Width, Y + Height, 0);
glTexCoord2f(1, 0); glVertex3f(X, Y + Height, 0);
glEnd();
glDisable(GL_TEXTURE_2D);


Ps. Yes I made the GLuint texture variable, it is static defined in the header file, oh and there are no errors it could not load or something.

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

You need to pass the surface->format->BytesPerPixel instead of GL_RGBA for the 3rd parameter.

Additionally, are you sure about the format? You can get it for sure with:

        switch (surface->format->BytesPerPixel)
{
case 4:
Mode = GL_RGBA;
else
Mode = GL_BGRA;
break;
case 3:
Mode = GL_RGB;
else
Mode = GL_BGR;
break;
default:
std::cout << "Error, image is not truecolor." << std::endl;
return;
}


You would then call glTexImage2D like this:

glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w,surface->h, 0, Mode,GL_UNSIGNED_BYTE,surface->pixels);

• Okay the texture shows, but only when I use GL_RGBA, and the color is not working correctly. My image is blue and when I render it on a white quad, the image is displayed as light blue... Nov 4, 2015 at 8:24
• Right, the switch case is to make the code more flexible, there will be only one correct format for a given image. Try it without coloring the quad white, sounds like the colors are blending. Nov 4, 2015 at 12:50
• Hi, I fixed it by making the white background quad transparent using blending. Oh and GL_BGR and GL_BGRA did not work. Compiler said that they were not defined? I have included glut.h gl.h and glu.h... But that does not matter anymore, it works thanks! Nov 4, 2015 at 13:07

Try changing:

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


To (if surface has alpha):

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


Or (if surface doesn't have alpha):

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