1
votes

I'm having problems trying to load textures in an OpenGL GLUT project using classes. Here's some code that includes the texturing stuff:

Declaring a Textured Model from a subclass of a model class.

TextureModel * title = new TextureModel("Box.obj", "title.raw");

Constructor method of TextureModel subclass:

TextureModel(string fName, string tName) : Model(fName), textureFile(tName)
{   
    material newMat = {{0.63,0.52,0.1,1.0},{0.63,0.52,0.1,1.0},{0.2,0.2,0.05,0.5},10};
    Material = newMat;
    // enable texturing
    glEnable(GL_TEXTURE_2D);

    loadcolTexture(textureFile);
    glGenTextures(1, &textureRef);
    // specify the filtering method
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    // associate the image read in to the texture to be applied
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256, GL_RGB, GL_UNSIGNED_BYTE, image_array);
}

Texture loading function to read in data in RAW file:

int loadcolTexture(const string fileName) {
ifstream inFile;
inFile.open(fileName.c_str(), ios::binary );

if (!inFile.good())
{
    cerr  << "Can't open texture file " << fileName << endl;
    return 1;
}
inFile.seekg (0, ios::end);
int size = inFile.tellg();
image_array = new char [size];
inFile.seekg (0, ios::beg);
inFile.read (image_array, size);
inFile.close();
return 0;}

Method to draw the triangles:

virtual void drawTriangle(int f1, int f2, int f3, int t1, int t2, int t3, int n1, int n2, int n3)
{
    glColor3f(1.0,1.0,1.0);
    glBegin(GL_TRIANGLES);
    glBindTexture(GL_TEXTURE_2D, textureRef);
    glNormal3fv(&normals[n1].x);
    glTexCoord2f(textures[t1].u, textures[t1].v);
    glVertex3fv(&Model::vertices[f1].x);

    glNormal3fv(&normals[n2].x);
    glTexCoord2f(textures[t2].u, textures[t2].v);
    glVertex3fv(&Model::vertices[f2].x);

    glNormal3fv(&normals[n3].x);
    glTexCoord2f(textures[t3].u, textures[t3].v);
    glVertex3fv(&Model::vertices[f3].x);
    glEnd();
}

I also have Lighting, Depth Testing and Double Buffering enabled.

Models and Lighting work fine, but the textures don't appear. Any reasons why it won't work will be great.

1
You're at least missing a call to glBindTexture in the TextureModel constructor.user786653

1 Answers

2
votes

To add to the comment, I see a few things here:

  1. As mentioned in the comment, you need to bind a texture before you can upload data to it. Once you generate the texture with glGenTextures, you need to set it to be the active texture before you try to load data or set set parameters with glTexParameteri

  2. You're building mipmaps, but not using them. Either set the GL_TEXTURE_MIN_FILTER to GL_NEAREST_MIPMAP_LINEAR to utilize the mipmaps, or don't build them in the first place. As is you're just wasting texture memory.

  3. It's not legal to bind a texture inbetween glBegin/glEnd as you have done in drawTriangle. Bind it before the glBegin.

  4. Please, please, please start using glGetError in your code. This will tell you if you are doing wrong things before you have to come and ask to find your mistakes. (you would have found 2/3 of the mistakes here if you had been using it).