OpenGL rendering white texture bug

497 Views Asked by At

I use OpenGL and SDL2 to render spine animations. In a specific z-order this animations are disposed like white blocks. All texture get white. I guess this error is in OpenGL draw code.

glPushMatrix();

float texw=0, texh=0;
if (texture) {
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    if (SDL_GL_BindTexture(texture, &texw, &texh) != 0)
        printf("WTF\n");
}

glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(color[0], color[1], color[2], color[3]);

glVertexPointer(2, GL_FLOAT, 0, vertices);


glTexCoordPointer(2, GL_FLOAT, 0, uvs);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// if (num_vertices > 0) {
glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_SHORT, indices);

glDisableClientState(GL_VERTEX_ARRAY);
// glDisableClientState(GL_COLOR_ARRAY);


if (texture) {
    SDL_GL_UnbindTexture(texture);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

glColor4f(1.0, 1.0, 1.0, 1.0);
glPopMatrix();

This is my code, some one see something wrong in this code ? Why i'm getting white textures ? enter image description here

1

There are 1 best solutions below

4
On

Two-dimensional texturing has to be enabled by glEnable(GL_TEXTURE_2D) and can be disabled by glDisable(GL_TEXTURE_2D).
If texturing is enables then the texture wich is currently bound when the geometry is drawn is wrapped on the geometry.

If texturing is enabled, then by default the color of the texel is multiplied by the current color, because by default the texture environment mode (GL_TEXTURE_ENV_MODE) is GL_MODULATE. See glTexEnv. This causes that the color of the texels of the texture is "mixed" by the last color which you have set by glColor4f.

glEnable(GL_TEXTURE_2D);
glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_SHORT, indices);
glDisable(GL_TEXTURE_2D);

Note that all of this only applies in immediate mode if you are not using a shader program.