簡體   English   中英

opengl多重紋理入門

[英]opengl multitexturing for a starter

我有一個需要多個紋理的動畫項目,我打算看到所有這些紋理,但是我的模型上只有1個紋理封面。我看不到對象上的所有tga文件。

    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`

我不確定是否需要發布我的obj文件?

查看glActiveTexture的文檔。 在循環中調用glBindTexture並不會執行您認為的操作。

您的代碼是錯誤的,原因有很多:

  1. 如果draw()函數是您的渲染回調,那么您將創建一組紋理並將數據加載到其中。 這是錯誤的,因為您只需要執行一次。
  2. LoadTexture()將創建一個新紋理,並使用文件中的數據加載它。 這是錯誤的,因為您已經創建了17個紋理
  3. 然后,將每個紋理綁定到一個循環中,只有最后一個處於活動狀態
  4. 然后,從VBO轉儲所有頂點以進行渲染。 我看不到您在哪里初始化它們,但是它們將僅使用最后綁定的紋理。
  5. 您需要在initialize()中再次調用LoadTexture()。 實際上,所有紋理都應在此處生成並加載

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM