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.