'Assign' texture to class objects

Making an n body integrator using Qt, OpenGL and C++. Everything is working except for the texture part now. I’d like to ‘assign’ each planet its own texture but so far all I can do is assign each planet the same one. I know why this is happening but because I don’t really understand the code that well I’m not sure how to fix it.

So within my spaceObject class is a function that loads textures.

Code was lifted from another site so some odd looking things in it like no file open error check and the tex1 = buf line for example. These will all be sorted out soon but for now they cause no probs.


GLuint SpaceObject::loadTexture(string fileName, int dWidth, int dHeight)
{
QImage tex1, buf;

    QString fName = fileName.c_str();

    // Load the image from file, no error check for now
    buf.load( fName );

    // If no desired width given, use the image dimensions
    if (dWidth == -1) dWidth = buf.width();
    if (dHeight == -1) dHeight = buf.height();

    tex1 = buf;
    tex1 = QGLWidget::convertToGLFormat( tex1 );

    // Get a unique texture name
    GLuint texName;
    glGenTextures(1, &texName);
    glBindTexture(GL_TEXTURE_2D, texName);

    // Set texture parameters
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    // Load texture data
    glTexImage2D( GL_TEXTURE_2D, 0, 3, tex1.width(), tex1.height(), 0,
                  GL_RGBA, GL_UNSIGNED_BYTE, tex1.bits() );

    return texName;
}

There is another draw function within the class that gets called after EVERY class object has been initialized, i.e. the above code is run for each class object before any drawing takes place.

The problem is occuring in these lines…

GLuint texName;
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);

Since &texName is the same for every object (checked with debug), only the last texture I upload is getting used and so every planet has the same texture on it.

What do I do to avoid this? Do I store all textures somewhere? Each class object I make now has a objectRef int value associated with it as I thought I could use that to help somehow but it just gives errors saying that I cannot destroy paint object before it is painted (Qt based error).

For reference I’m drawing the sphere using the icosahedron method, no glu/glut calls done. Code is several thousand lines long so won’t be posting it all but I’m pretty sure it’s because &texName is the same for each object.

Edit: I just noticed that I don’t actually use the function to return anything. As in I just call it as it is with so really the return value is pointless. Some structuring issues to be resolved I see…

Just to say this is solved.

I have a spaceSystem class that controls every spaceObject in the system and I loaded all textures into that then just used

glBindTexture(GL_TEXTURE_2D, textures[i]);

before each draw call of each spaceObject. Works now although I would’ve liked a solution that didn’t involve having to call glBindTexture every draw step. I’m sure there is one somewhere but for now this will do.