Jump to content
  • Advertisement
Sign in to follow this  
Piyush Verma

OpenGL Multiple Texture using multiple shader programs

This topic is 1109 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi folks,

 

I am trying to do a scene in OpenGL to simulate earth from space. I have two spheres right now, one for earth, and another slightly big for clouds. The earth and the cloud sphere objects have their own shader programs to keep it simple. The earth shader program takes 4 textures (day, night, specmap and normalmap) and the cloud shader program takes 2 textures (cloudmap and normalmap). I have an object class which has a render function, and in that function I use this logic:

//bind the current object's texture
for (GLuint i = 0; i < texIDs.size(); i++){
	glActiveTexture(GL_TEXTURE0 + i);
	if (cubemap)
		glBindTexture(GL_TEXTURE_CUBE_MAP, texIDs[i]);
	else
		glBindTexture(GL_TEXTURE_2D, texIDs[i]);
}
	if (samplers.size()){
	for (GLuint i = 0; i < samplers.size(); i++){
		glUniform1i(glGetUniformLocation(program, samplers[i]), i);
	}
}

It starts from the 0th texture unit, and binds N number of textures to N number of texture units starting from GL_TEXTURE0. Then it binds the the samplers starting from 0 to N in the shader program. The samplers are provided by me while loading the textures:

void Object::loadTexture(const char* filename, const GLchar* sampler){
	int texID;
	texID = SOIL_load_OGL_texture(filename, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS);
	if(texID == 0){
		cerr << "SOIL error: " << SOIL_last_result();
	}
	cout << filename << " Tex ID: " << texID << endl;
	texIDs.push_back(texID);
	samplers.push_back(sampler);
	//glBindTexture(GL_TEXTURE_2D, texID);
}

When I do this, all the textures in the first sphere (earth) gets loaded successfully, but in the seconds sphere I get no textures and I just get a black sphere. My query is, how should I manage multiple textures and samplers if I'm using different shader programs for each object? Any insight on that matter would be really helpful.

Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement

Probably a bit late on replying, but I was able to solve it by implementing texture management from scratch in my engine. Somewhere down the line, I was managing textures wrong on the C++ side and that was causing this issue. I wrote a dedicated TextureManager class to handle textures and it did the trick for me. 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!