Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: using multiple textures

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    17

    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:
    Code :
    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:
    Code :
    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
    Code :
    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

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,098
    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.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    17
    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.

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,098
    Do you have an glActiveTexture(); before your render binding?

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    17
    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
    Code :
    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?

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,098
    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.

  7. #7
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •