1

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?

grep
  • 3,986
  • 7
  • 45
  • 67
  • Two comments: 1) shouldn't the language tag be c? Not much c++ in this code. 2) I really hoped fixed pipeline and all the other stuff removed from opengl would be dead and buried by now. At least I wouldn't count on those things to be very optimized on newer hardware and drivers anymore. – Grizzly Dec 01 '11 at 21:59
  • You can't use `LINEAR_MIPMAP_LINEAR` with `TEXTURE_MAG_FILTER`. That doesn't make sense anyway; the `TEXTURE_MAG_FILTER` is for *magnification*: making the texture *bigger* than the base layer (the largest layer in the mipmap chain). You can't use mipmaps that are larger than the base layer, so you can't filter between mipmaps with `TEXTURE_MAG_FILTER`. That's probably not your problem, but you should know that anyway. – Nicol Bolas Dec 01 '11 at 22:04
  • @Grizzly: "Dead a buried?" Are you kidding? People who use OpenGL only do so because either 1) they're learning from an online resource, which almost certainly isn't shader-based, 2) they're using an old OpenGL-based codebase which also isn't shader-based, or 3) they're doing cross-platform development. And since so many GL learning resources are fixed-function, so too will be cross-platform users of it. – Nicol Bolas Dec 01 '11 at 22:10
  • @Nicol Bolas: Ha, all three actually. Have not been able to find any good resources on GLSL. But I'm really aiming for crass-platform compatibility. – grep Dec 01 '11 at 22:16
  • @Nicol Bolas: In my opinion your first point is the most common scenario leading to usage of the deprecated part of opengl, which is why I like to at least mention that those parts are a bit outdated whenever I talk about opengl (in hopes of somepeople learning it will switch to more modern functionality). In my experience learning it using fixed pipeline often leads to problems when trying more complex things, where it would be easier to use shaders. Headspin: really? I would've thought that those exist. Unfortunately I learned from a few german sides, so I can't provide an english resource. – Grizzly Dec 01 '11 at 22:18
  • I'm not disagreeing with you; I'm writing [this tutorial](http://www.arcsynthesis.org/gltut/), after all. I'm doing my part to provide better GL resources. My point is that your expectations are misguided, so long as NeHe and other sites are online and operating. – Nicol Bolas Dec 01 '11 at 22:28

1 Answers1

4

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.

Grizzly
  • 19,595
  • 4
  • 60
  • 78
  • If I draw the ground, then disable 2d texture before drawing anything else, it just keeps the floor from being textured. – grep Dec 01 '11 at 21:59
  • So if you disable texturing after drwaing the ground (and renabling it before drawing the ground the next time, the ground is untextured? That seems a bit strange, mind saying abit more about what exactly you are doing (showing where you en/disable texturing and so on) – Grizzly Dec 01 '11 at 22:02
  • Ah, you know what, at some point I commented out re-enabling. Thanks! that solved my problem. – grep Dec 01 '11 at 22:04
  • 2
    I guessed something like that (which is why I made the point of mentioning it). So you're quite welcome. If you are writing in c++ I would suggest leveraging RAII for this so writing a class which enables it on constructor and disables it on destructor (contrieved example I know, a in reality something a bit more flexible might be appropriate). – Grizzly Dec 01 '11 at 22:08
  • At the beginning of my loop I call a reset function which clears the color and depth buffer, loads identity, etc. So I added the texture enable to that, and just disabled it at the end of the function which draws the floor. Eventually I am hoping to make something more robust, like a display-list class that will handle it for me. Thanks for the advice! – grep Dec 01 '11 at 22:19