using multiple textures

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

If a model (say .obj) has multiple textures you need to split the model into different triangle sets - one for each texture. You bind a texture then render the triangles that use that texture. Bind the next texture and render only the triangle associated with the new texture. (As a note .obj can include both triangles and quads in the same object). How to know which part of an object is associated with which texture depends on the file strcture of the mesh you are loading.

I allready know which triangles are associated with which textures (When loading quads are instantly converted to triangles). And each time a new texture is needed I use the glBindTexture(GL_TEXTURE_2D, image); code. (as shown in my previous post). But somehow it only uses my first texture and objects which use the second or third texture are all rendered with that first texture.

Do you have an glActiveTexture(); before your render binding?

Thanks for the response. You were right about the glActiveTexture(). I didn’t have it. But apperently my problem is with the sfml library that I’m using instead of openGL. So I will be asking there further information.

Just one question: Is opengl able to use multiple textures and call-lists at the same time? I suppose it is, but out of nothing I suddenly get a “acces reading violation” error at the moment the call

glCallList(call_list);

is made. Strange thing is that when I comment the line “glBindTexture(…);” the error is no longer.
So is there any link between calllists and textures? or is this just something in my code?

I don’t use call lists so I don’t know much about them; but I do know they are a bit touchy about what can and can’t happen in the displaylist.

It’s not a call list, it’s a display list. And yes, you can switch the texture unit inside a display list.

An access violation is not the result of calling something inside a display list, it’s a sign that your application or a library used by your application is accessing system memory which isn’t supposed to be accessed. The classic case leading to an access violation is trying to dereference a null-pointer.