2
votes

I'm trying to load several textures (minecraft ones) and applying them to cubes. I load the textures with SOIL:


GLuint loadTex(const char * path)
{
    GLuint image = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture
    (
        path,
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_MIPMAPS | 
        SOIL_FLAG_INVERT_Y | 
        SOIL_FLAG_TEXTURE_REPEATS | 
        //SOIL_FLAG_MULTIPLY_ALPHA |
        SOIL_FLAG_NTSC_SAFE_RGB | 
        SOIL_FLAG_COMPRESS_TO_DXT
    );

    if (!image)
    {
        printf("SOIL loading error: '%s'\n", SOIL_last_result());
    }

    std::cout << "texId: " << image << std::endl;
    return image;
}

I'm testing it by loading 2 different textures (and they get different Id's). I pass these textures to a function that creates a cube with a given texture id:

GLuint createTexturedCube(GLuint texId)
{
    GLuint index = glGenLists(1);
    glNewList(index, GL_COMPILE);
        glPushMatrix();
        glEnable(GL_TEXTURE_2D);

        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
        glBindTexture(GL_TEXTURE_2D, texId);

        glBegin(GL_QUADS);

        glNormal3f(0, 1, 0);
        glTexCoord2f(1, 1); glVertex3f(0.5f, 0.5f, -0.5f);
                ...
}

I'm loading two texures and creating two cubes. The ouput from SOIL shows that they are id's 1 and 2, but when I draw the cubes, only the last texture loaded shows correctly. The other one appears kind of blurried.

Result here:

The block on the left is the last texture loaded, the one on the right is the first texture loaded. It happens this way with any number of textures, always the last one is the right one.

I've tried to use glActiveTexture but that throws an exception (segfault trying to access #0000000)

My texture settings are the following:

        glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glEnable(GL_ALPHA_TEST);
    glAlphaFunc(GL_GREATER, 0.1);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
        GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
        GL_NEAREST);
1

1 Answers

4
votes

glTexParameteri doesn't change global settings (or settings of a sampler object).
glTexParameter sets the parameters of the texture object which is currently bound to the specified target.
You've to set the texture parameters for each texture object.

Note, the initial value for GL_TEXTURE_MIN_FILTER is GL_NEAREST_MIPMAP_LINEAR and the initial value for GL_TEXTURE_MAG_FILTER is GL_LINEAR.
In case of the image (in the question) the textures are magnified. This causes the blurry look of the object, with the texture with unchanged texture parameters.