grep
grep

Reputation: 4016

OpenGL mipmap issues

I have mipmapping working properly. But for some reason, after I apply the texture, all other objects in my application take on what seems to be the average color of the texture. Even the HUD. Is there something i need to account for? Here is the code:

GLuint LoadTexture( const char * filename, int width, int height )
{
    GLuint texture;
    unsigned char * data;
    FILE * file;

    file = fopen( filename, "rb" );
    if ( file == NULL ) return 0;
    data = (unsigned char *)malloc( width * height * 3 );
    fread( data, width * height * 3, 1, file );
    fclose( file );

    glGenTextures( 1, &texture );
    glBindTexture( GL_TEXTURE_2D, texture );
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR );

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );

    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data ); 
    free( data );
    return texture;
}

That gets called before the loop and freed afterwords. This next portion of code happens inside my while loop. It is called before all other objects in the scene.

void makeGround() {

    glBindTexture( GL_TEXTURE_2D, texture );

    GLfloat mat_specular[]      = { 0.5, 0.5, 0.5, 1.0 };
    GLfloat mat_diffuse[]       = { 0.8, 0.8, 0.8, 1.0 };
    GLfloat mat_shininess[]     = { 100.0 };

    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

    //glColor3f(1.0, 1.0, 1.0);

    glPushMatrix();
        glTranslatef(-(size/2), 0.0f, -(size/2));
        glCallList(LIST1);
    glPopMatrix();
}

What should I be doing between drawing my different elements to keep this from happening?

Upvotes: 1

Views: 678

Answers (1)

Grizzly
Grizzly

Reputation: 20191

I would guess that you fogot to unbind the texture or disable texturing after you finished rendering the parts which should be textured. When you try to render untextured Objects they still get the texture, but due to the lack of texture coordinates they all use the same texcoord, so it will get just one color from the texture.

Upvotes: 4

Related Questions