1
\$\begingroup\$

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

This the texture I made using paint enter image description here

This is what I get on the screen: enter image description here

Does anyone have an idea what could cause this?

How I loaded the image:

void Main::loadStuff() {
    SDL_Surface* surface = IMG_Load("C:\\Users\\VliegendeGeit\\Desktop\\Programming\\C++ Projects\\OpenGL\\texture.png");
    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;

    glBegin(GL_QUADS);
        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.

\$\endgroup\$
1
\$\begingroup\$

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

See: https://www.opengl.org/sdk/docs/man3/xhtml/glTexImage2D.xml

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:
            if (surface->format->Rmask == 0x000000ff)
                Mode = GL_RGBA;
            else
                Mode = GL_BGRA;
            break;
        case 3:
            if (surface->format->Rmask == 0x000000ff)
                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);
\$\endgroup\$
3
  • \$\begingroup\$ 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... \$\endgroup\$ Nov 4 '15 at 8:24
  • \$\begingroup\$ 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. \$\endgroup\$
    – Andy M
    Nov 4 '15 at 12:50
  • \$\begingroup\$ 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! \$\endgroup\$ Nov 4 '15 at 13:07
0
\$\begingroup\$

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);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .