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.
glBindTexture
in theTextureModel
constructor. – user786653