简体   繁体   中英

opengl multitexturing for a starter

i have animation project which requires multiple textures ,i intend to see all of them yet only 1 texture covers on my model.i cant see all tga files on the object.

    GLuint LoadTexture(char *TexName)
  {
TGAImg Img;        // Image loader

  // Load our Texture
   if(Img.Load(TexName)!=IMG_OK)
    return -1;

   glGenTextures(1,textures);            // Allocate space for texture
   glBindTexture(GL_TEXTURE_2D,textures[0]); // Set our Tex handle as current

   // Create the texture
    if(Img.GetBPP()==24)
     glTexImage2D(GL_TEXTURE_2D,0,3,Img.GetWidth(),Img.GetHeight(),0,GL_RGB,GL_UNSIGNED_BYTE,Img.GetImg());
    else if(Img.GetBPP()==32)
     glTexImage2D(GL_TEXTURE_2D,0,4,Img.GetWidth(),Img.GetHeight(),0,GL_RGBA,GL_UNSIGNED_BYTE,Img.GetImg());
    else
     return -1;

   // Specify filtering and edge actions
   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_CLAMP);
   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);

   return textures[0];
  }

void Draw()
{
    glEnable(GL_TEXTURE_2D);

    glGenTextures(17,textures);

    for (int i = 0; i<17; i++){
         textures[i] = LoadTexture(textureNames[i   
         glBindTexture(GL_TEXTURE_2D, textures[i]);
    }

    glEnableClientState(GL_VERTEX_ARRAY);                   
    glEnableClientState(GL_NORMAL_ARRAY);
    glVertexPointer(3,GL_FLOAT,0,triangleArr);              
    glNormalPointer(GL_FLOAT, 0, normals);                  
    glDrawArrays(GL_TRIANGLES, 0, TotalConnectedTriangles); 
    glDisableClientState(GL_VERTEX_ARRAY);                  
    glDisableClientState(GL_NORMAL_ARRAY);  
}

void initialize () 
{
    glViewport(0, 0, 500, 500);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, 1, 0.1, 1000.0);
    glMatrixMode(GL_MODELVIEW);

    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    glShadeModel( GL_SMOOTH );
    glEnable( GL_DEPTH_TEST );

    glEnable(GL_TEXTURE_2D);    
    textures[textureIndex] = LoadTexture(textureNames[textureIndex]);   
    glBindTexture(GL_TEXTURE_2D, textures[textureIndex]);

    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);


    glEnableClientState(GL_TEXTURE_COORD_ARRAY);    
    glTexCoordPointer(2, GL_FLOAT, 0, textureArr);

}
</code>`enter code here`

i am not sure if i need to post my obj file?

Look at the docs of glActiveTexture . Calling glBindTexture in a loop doesn't do what you think it does.

Your code is wrong for many reasons :

  1. if the draw() function is your rendering callback, then you are going to create a set of textures and load data into them. It is wrong, since you need to do it only once.
  2. LoadTexture() is going to create a new texture, and load it with data from a file. This is wrong, since you already created 17 textures i function
  3. Then you are binding each texture in a loop, and only the last is going to be active
  4. Then you dump all vertexes from a VBO to be rendered. I do not see where you initialized them, but they are going to use only the last binded texture.
  5. You have another call to LoadTexture() in the initialize(). Actually all textures should be generated and loaded there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM