PDA

View Full Version : multiple textures



tnt1674
06-18-2016, 08:29 AM
my code for loading textures and binding them to my opengl program

//generate texture object
glGenTextures(2, TXO);
texture_unit = unit;

//loading the texture
glActiveTexture(GL_TEXTURE0 + texture_unit);
glBindTexture(GL_TEXTURE_2D, TXO[0]);

unsigned char* image;
int w, h, comp;
CFileDialog dlg(TRUE, _T(".jpeg"), NULL, NULL, _T("*.jpeg|*.*"));
if (dlg.DoModal() == IDOK)
{
std::string s((LPCTSTR)dlg.GetPathName());
image = stbi_load(s.c_str(), &w, &h, &comp, 4);
}

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
stbi_image_free(image);
GLuint Tex_loc = glGetUniformLocation(program, "texMap");
glUniform1i(Tex_loc, texture_unit);

//loading the normal mapping
glActiveTexture(GL_TEXTURE0 + texture_unit + 1);
glBindTexture(GL_TEXTURE_2D, TXO[1]);

unsigned char* image2;
int w2, h2, comp2;
CFileDialog dlg2(TRUE, _T(".jpeg"), NULL, NULL, _T("*.jpeg|*.*"));
if (dlg.DoModal() == IDOK)
{
std::string s((LPCTSTR)dlg2.GetPathName());
image2 = stbi_load(s.c_str(), &w2, &h2, &comp2, 4);
}

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w2, h2, 0, GL_RGBA, GL_UNSIGNED_BYTE, image2);
stbi_image_free(image2);
Tex_loc = glGetUniformLocation(program, "normalMap");
glUniform1i(Tex_loc, texture_unit + 1);

there is two problem occuring:
1)when i try to chagne program (meaning trying to use different vshader and fshader files ) the texture is missing , and i get blank screen .
to be more exact it merely occur when i load the shader which call two textures (one of the model and second of normal mapping) , if i just changing to a program that do texture only once it is okay.
2)when i draw multiple models with multiple texture , the textures get mixed .

i know that mix the textures with the active textures for some reasons but i dont know how to fix it .