This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

J6 entry:how to create mipmap for compressed texture with opengles

I have a PVR file whic has 512*512 size I create it with pvrtextool ,and I upload to my J6 entry board GPU as texture using glcompressedtexImg2D, However now I want to create mipmap for this texture, I know that I cannot create mipmap for it automatically using glgeneratemipmap since it is compressed ,I have to upload it manually level by level.I do like the following:

///parse PVR
 
FILE* m_pFile;
 
unsigned char* m_pData =NULL;
 
int nChannel;
 
EN_FMT_COLOR emFlag;
 
int nPicWid;
 
int nPicHgt;
 
int nSize;
 
int nLevel;
 
 
  
 m_pFile = fopen("./Blue512.pvr", "rb");
             
 fseek(m_pFile, 0, SEEK_END);
      
 int nFileLen = ftell(m_pFile);
       
if(nFileLen < 52)
       
{
            return 0;
        }

              
m_pData = new unsigned char[nFileLen];
       
memset(m_pData, 0, nFileLen);
  
      
 rewind(m_pFile);
          
 int nError = fread(m_pData,1,nFileLen,m_pFile);       
  if (m_pData[0] != 'P' || m_pData[1] != 'V' || m_pData[2] != 'R')
     
  {
           
     return 0;
       
   }


              
int nFormat = *(int*)(m_pData +8);
 
 emFlag   = HIK3D_FMT_RGBA_4BPP;
     
     
 nPicWid = *(int*)(m_pData +24);
       
nPicHgt = *(int*)(m_pData +28);
       
int nOffcet = *(int*)(m_pData +48);
       
nLevel = *(int*)(m_pData +44);
      
 m_pData += nOffcet + 52;
       
nSize    = nFileLen - nOffcet - 52;
       
RUN_INFO("size is %d\r\n",nSize);
  
int* pnLevelSize;
       
unsigned char* pCurrentPos = m_pData;

      
 pnLevelSize = new int[nLevel];


       
int nWidth  = nPicWid;
       
int nHeight = nPicHgt;
       
float bpp = 0.5f;
       
     
for(int i = 0; i < nLevel; i++)
       
{
           
    pnLevelSize[i]= nWidth * nHeight * bpp;
   
if(pnLevelSize[i] < 32)
   
{
    
  pnLevelSize[i] = 32;
   
}
      
   
 nWidth  = max(1,nWidth/2 );
           
nHeight = max(1,nHeight/2 );
      
 }
      
  
 
 
  glGenTextures(1, &m_nTexture);
  
 printf("gen 1 tex %d\r\n", m_nTexture);
 
  glBindTexture(GL_TEXTURE_2D, m_nTexture);
  
  
GLenum p;
  
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  
GLenum internalformat;
 
internalformat = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
 
nWidth  = nPicWid;
       
nHeight = nPicHgt;

  
for (int i=0; i < nLevel; i++)
  
{
       
glCompressedTexImage2D(GL_TEXTURE_2D, i, internalformat, nWidth, nHeight, 0 , pnLevelSize[i], (void*)pCurrentPos);
 pCurrentPos = pCurrentPos + pnLevelSize[i];  
    
nWidth  = max(8,nWidth/2 );
   
nHeight = max(8,nHeight/2);
 
}
   
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
   
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
  
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  
  
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

I notice I cannot upload texture level which width is less than 8 if so I get error from glgeterror, so I make all levels that width less than 8 same size as the one with 8 as width. this texture is totally black if I choose GL_LINEAR_MIPMAP_NEAREST as GL_TEXTURE_MIN_FILTER ,it is correct if I set it to GL_LINEAR. I think there is something wrong with the way I create mipmap ,could anyone give me some advice? Thanks a lot.
 

  • Hi,

    -0- On the issue of black mipmaps

    Is this black always seen when using this texture or only during when those particular mipmap levels (< 8x8) are used.

    I believe there is a restriction for dimensions to be atleast 8x8 for PVR compressed images. Let me confirm on this. Here for the last level you would have to select non mip-mapped setting for MIN_FILTER. Giving a dimension of 8x8 for lower levels would mean that the original dimension of texture is much larger and would be treated as a separate texture.

    -1- On the levels of <8x8 size for mipmaps

    Are you seeing this only for compressed textures or also with normal textures.

    Thanks,
    Gowtham

  • Thank you .I finally figure out this problem.when use glCompressedTexImage2D to upload texture level by level we should make width and height down to 1*1 however we should keep the size same as the 8*8 one which is 32 and data pointer should also be moved use 32 for the last three level. This problem only occur for compressed texture  because for normal texture we can use glgenrate mipmap to do mipmaping instead of do it manually. Anyway thanks again for your help