Hy,
I’m fairly new to OpenGL. I’m running into a problem with multiple textures:
I’m creating a game. For this, I’m loading in models from external files. All the materials and vertices are loaded in perfectly and can be dislayed.
The problem starts when I have multiple textures in one model. I think it’s just a basic concept of textures that I don’t see yet, so I’ll just post my code.
The loading from external file happens:
bool Gr_Object::load_materials(ifstream* file, string dir, unsigned int size) {
for (unsigned int i = 0; i < size; ++i) {
Material m;
/* read properties like diffuse, specular,... form file
...
*/
unsigned char length;
file->read( reinterpret_cast<char*>(&length), sizeof(unsigned char) );
if (length != 0) {
char texname[30];
file->read(texname, length);
texname[length] = 0; // add 0-character
m.image = load_text_image(texname);
} else {
m.image = 0;
}
addMaterial(m);
}
return true;
}
the load_text_image(texname); function is this:
GLuint Gr_Object::load_text_image(string file) {
if (file == "") {
return NULL;
}
sf::Image img_data;
if (!img_data.loadFromFile(file)) {
cout << "file was not found or could not be read" << endl;
return NULL;
}
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img_data.getSize().x, img_data.getSize().y,0,GL_RGBA,GL_UNSIGNED_BYTE,img_data.getPixelsPtr());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT);
return texture;
}
Note: I’m using a library sfml to load in the image.
Now I’m not sure how to set which texture to use? I thought it was done with: glBindTexture
void Material::set_material() {
/* set material settings
...
*/
if (image != 0) {
glBindTexture(GL_TEXTURE_2D, image); /* image variable is a GLuint which reffers to the texture */
}
}
But this doesn’t work. The problem is that only the first texture is used. And it is used on all objects which contain any texture.
So my question is: how are textures set? How can I decide which texture to set “active”?
Thanks for all the help
Greetings