Search code examples
c++openglgraphicstexturestexture2d

OpenGL texture doesn't load properly


I'm working with Xcode 7.0 on OSX 10.10.5 and I get a very strange result when I try to draw a texture on a square:

enter image description here

The image I'm trying to draw is:

enter image description here

I've bee looking for quite a while and haven't been able to find anything similar. Here is the code I use to draw the texture/quad:

TI = new SDL_Surface *[1];
TI[0] = IMG_Load ("media/dragonite.png");
texture = new GLuint[1];
glGenTextures (1,&texture[0]);
glBindTexture (GL_TEXTURE_2D,texture[0]);
glTexImage2D(GL_TEXTURE_2D,0,3,TI[0]->w,TI[0]->h,0,GL_BGR,GL_UNSIGNED_BYTE,TI[0]->pixels);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
SDL_FreeSurface (TI[0]);
delete[] TI;
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();

rtri = rquad = 0;
SDL_Event e;

while (true) {
    if (SDL_PollEvent(&e)) {
        if (e.type == SDL_QUIT)
            break;
    }

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1, 1, 1);
    glLoadIdentity();
    glTranslatef(0, 0, SCREEN_ZOOM);
    glRotatef(rquad, 3, 0.5, 1);

    glBegin (GL_QUADS);
    glTexCoord2f(0,0);glVertex3f (1,1,0);
    glTexCoord2f(1,0);glVertex3f (-1,1,0);
    glTexCoord2f(1,1);glVertex3f (-1,-1,0);
    glTexCoord2f(0,1);glVertex3f (1,-1,0);
    glEnd ();

    //rquad -= 0.35;
    SDL_GL_SwapWindow (w);

}

Any help is appreciated. Thank you.


Solution

  • Third parameter to glTexImage2D is internalFormat. Use enum instead of magic number 3. Try using GL_RGB.

    I was able to properly show this texture in my own renderer. Parameter format should be also GL_RGB or GL_RGBA.

    I recommend look and OpenGL docs : http://docs.gl/gl4/glTexImage2D