⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gtexmanager.cc

📁 五行MMORPG引擎系统V1.0
💻 CC
📖 第 1 页 / 共 4 页
字号:

      if (probe->type == BitmapNoDownloadTexture)
      {
         probe = probe->next;
         continue;
      }
      
      if (probe->bitmap != NULL) 
      {
         if(probe->type == BitmapKeepTexture)
         {
            delete probe->bitmap;
            probe->bitmap = NULL;
         }
         else
         {
            if (probe->type == RegisteredTexture) 
            {
               createGLName(probe->bitmap, probe->clamp, 0, probe->type, probe);
            }
            else 
            {
               TextureObject* refreshed = registerTexture(probe->texFileName, probe->bitmap,
                                                          probe->type, probe->clamp);
               AssertFatal(refreshed == probe, "Error, new texture object returned.  This should not happen in resurrect");
            }

            probe = probe->next;
            continue;
         }
      }

      // Ok, what we have here is the object, with the right name, we need to load the
      // bitmap, and register the texture
      GBitmap *bmp = loadBitmapInstance(probe->texFileName);
      AssertISV(bmp != NULL, "Error resurrecting the texture cache.\n"
                "Possible cause: a bitmap was deleted during the course of gameplay.");

      TextureObject* refreshed = registerTexture(probe->texFileName, bmp,
                                                 probe->type, probe->clamp);
      AssertFatal(refreshed == probe, "Error, new texture object returned.  This should not happen in resurrect");

      probe = probe->next;
   }

   ChunkedTextureManager::resurrect();
   postTextureEvent(CacheResurrected);

   sgResurrect = false;
}

void TextureManager::flush()
{
   makeZombie();
   resurrect();
}

ConsoleFunction( flushTextureCache, void, 1, 1, "Flush the texture cache.")
{
   TextureManager::flush();
}

#ifdef TORQUE_GATHER_METRICS
void TextureManager::dumpStats()
{
   TextureObject* probe = TextureDictionary::smTOList;

   Con::errorf("aaa Texture dump");
   while (probe)
   {
      Con::errorf("aaa    %d: (%d, %s) %d (%s)", probe->type, probe->refCount, probe->holding ? "yes" : "no", probe->textureSpace, probe->texFileName ? probe->texFileName : "nil");
      probe = probe->next;
   }
}

ConsoleFunction(dumpTextureStats, void, 1, 1, "Dump texture manager statistics. Debug only!")
{
   TextureManager::dumpStats();
}
#endif


//------------------------------------------------------------------------------
GBitmap* TextureManager::createPaddedBitmap(GBitmap* pBitmap)
{
   if (isPow2(pBitmap->getWidth()) && isPow2(pBitmap->getHeight()))
      return pBitmap;

   AssertFatal(pBitmap->getNumMipLevels() == 1,
               "Cannot have non-pow2 bitmap with miplevels");

   U32 width = pBitmap->getWidth();
   U32 height = pBitmap->getHeight();

   U32 newWidth  = getNextPow2(pBitmap->getWidth());
   U32 newHeight = getNextPow2(pBitmap->getHeight());

   GBitmap* pReturn = new GBitmap(newWidth, newHeight, false, pBitmap->getFormat());

   for (U32 i = 0; i < height; i++) 
   {
      U8*       pDest = (U8*)pReturn->getAddress(0, i);
      const U8* pSrc  = (const U8*)pBitmap->getAddress(0, i);

      dMemcpy(pDest, pSrc, width * pBitmap->bytesPerPixel);

      pDest += width * pBitmap->bytesPerPixel;
      // set the src pixel to the last pixel in the row
      const U8 *pSrcPixel = pDest - pBitmap->bytesPerPixel;

      for(U32 j = width; j < newWidth; j++)
         for(U32 k = 0; k < pBitmap->bytesPerPixel; k++)
            *pDest++ = pSrcPixel[k];
   }
   
   for(U32 i = height; i < newHeight; i++)
   {
      U8* pDest = (U8*)pReturn->getAddress(0, i);
      U8* pSrc = (U8*)pReturn->getAddress(0, height-1);
      dMemcpy(pDest, pSrc, newWidth * pBitmap->bytesPerPixel);
   }

   if (pBitmap->getFormat() == GBitmap::Palettized)
   {
      pReturn->pPalette = new GPalette;
      dMemcpy(pReturn->pPalette->getColors(), pBitmap->pPalette->getColors(), sizeof(ColorI) * 256);
      pReturn->pPalette->setPaletteType(pBitmap->pPalette->getPaletteType());
   }
   
   return pReturn;
}


//------------------------------------------------------------------------------
GBitmap* TextureManager::createMipBitmap(const GBitmap* pBitmap)
{
   AssertFatal(pBitmap != NULL, "Error, no bitmap");
   AssertFatal(pBitmap->getNumMipLevels() != 1, "Error, no mips to maintain");

   GBitmap* pRetBitmap = new GBitmap(pBitmap->getWidth(1),
                                     pBitmap->getHeight(1),
                                     true,
                                     pBitmap->getFormat());

   for (U32 i = 1; i < pBitmap->getNumMipLevels(); i++)
   {
      void* pDest      = pRetBitmap->getWritableBits(i - 1);
      const void* pSrc = pBitmap->getBits(i);

      dMemcpy(pDest, pSrc, (pBitmap->getWidth(i)  *
                            pBitmap->getHeight(i) *
                            pBitmap->bytesPerPixel));
   }

   return pRetBitmap;
}


//------------------------------------------------------------------------------
void TextureManager::freeTexture(TextureObject *to)
{
#ifdef TORQUE_GATHER_METRICS
   AssertFatal(to->textureSpace <= smTextureSpaceLoaded, "Error, that shouldn't happen!");
   smTextureSpaceLoaded -= to->textureSpace;
#endif

   if((gDGLRender || sgResurrect) && to->texGLName)
      glDeleteTextures(1, (const GLuint*)&to->texGLName);
   if((gDGLRender || sgResurrect) && to->smallTexGLName)
      glDeleteTextures(1, (const GLuint*)&to->smallTexGLName);

   delete to->bitmap;
   TextureDictionary::remove(to);
   delete to;
}


//------------------------------------------------------------------------------
static void getSourceDestByteFormat(GBitmap *pBitmap, U32 *sourceFormat, U32 *destFormat, U32 *byteFormat)
{
   *byteFormat = GL_UNSIGNED_BYTE;

   switch(pBitmap->getFormat()) 
   {
      case GBitmap::Intensity:
         *sourceFormat = GL_INTENSITY;
         break;

      case GBitmap::Palettized:
         *sourceFormat = GL_COLOR_INDEX;
         break;

      case GBitmap::Luminance:
         *sourceFormat = GL_LUMINANCE;
         break;
      case GBitmap::RGB:
         *sourceFormat = GL_RGB;
         break;
      case GBitmap::RGBA:
         *sourceFormat = GL_RGBA;
         break;
      case GBitmap::Alpha:
         *sourceFormat = GL_ALPHA;
         break;

      case GBitmap::RGB565:
      case GBitmap::RGB5551:
#if defined(TORQUE_OS_MAC)
         *sourceFormat = GL_BGRA_EXT;
         *byteFormat   = GL_UNSIGNED_SHORT_1_5_5_5_REV;
#else
         *sourceFormat = GL_RGBA;
         *byteFormat   = GL_UNSIGNED_SHORT_5_5_5_1_EXT;
#endif
         break;
   };

   if(*byteFormat == GL_UNSIGNED_BYTE)
   {
      if (*sourceFormat != GL_COLOR_INDEX)
         *destFormat = *sourceFormat;
      else
         *destFormat = GL_COLOR_INDEX8_EXT;

      if (pBitmap->getNumMipLevels() > 1 &&
          pBitmap->getFormat() != GBitmap::Palettized &&
          (sgAllowTexCompression && dglDoesSupportTextureCompression()))
      {
         if (*sourceFormat == GL_RGB)
            *destFormat = GL_COMPRESSED_RGB_ARB;
         else if (*sourceFormat == GL_RGBA)
            *destFormat = GL_COMPRESSED_RGBA_ARB;
      }
   }
   else
   {
#if defined(TORQUE_OS_MAC)
      *destFormat = GL_RGB5_A1; /// hmmm.  seems to be working, but I'm guessing it might be defaulting back to RGBA
#else
      *destFormat = GL_RGB5_A1;
#endif
   }

   if (sgForce16BitTexture)
   {
      for (U32 i = 0; sg16BitMappings[i].end != true; i++)
      {
         if (*destFormat == sg16BitMappings[i].wanted)
         {
            *destFormat = sg16BitMappings[i].forced;
            return;
         }
      }
   }
   else
   {
      if(*destFormat == GL_RGB)
         *destFormat = GL_RGB8;
      else if(*destFormat == GL_RGBA)
         *destFormat = GL_RGBA8;
   }
}


//--------------------------------------
void TextureManager::refresh(TextureObject *to)
{
   if (!(gDGLRender || sgResurrect))
      return;

   U32 sourceFormat, destFormat, byteFormat;
   GBitmap *pBitmap = to->bitmap;

   getSourceDestByteFormat(pBitmap, &sourceFormat, &destFormat, &byteFormat);

   if (!to->texGLName)
      glGenTextures(1,&to->texGLName);

   glBindTexture(GL_TEXTURE_2D, to->texGLName);
   GBitmap *pDL = createPaddedBitmap(pBitmap);

   U32 maxDownloadMip = pDL->getNumMipLevels();
   if (to->type == BitmapTexture ||
       to->type == BitmapKeepTexture ||
       to->type == BitmapNoDownloadTexture)
   {
      maxDownloadMip = 1;
   }

   if (pDL->getFormat() == GBitmap::Palettized)
   {
      glColorTableEXT(GL_TEXTURE_2D,
                      pDL->getPalette()->getPaletteType() == GPalette::RGB ? GL_RGB : GL_RGBA,
                      256,
                      GL_RGBA,
                      GL_UNSIGNED_BYTE,
                      pDL->getPalette()->getColors());
   }

   if (sgDisableSubImage)
   {
      for (U32 i = 0; i < maxDownloadMip; i++)
      {
         glTexImage2D(GL_TEXTURE_2D,
                      i,
                      destFormat,
                      pDL->getWidth(i), pDL->getHeight(i),
                      0,
                      sourceFormat,
                      byteFormat,
                      pDL->getBits(i));
      }
   }
   else
   {
      for (U32 i = 0; i < maxDownloadMip; i++)
      {
         glTexSubImage2D(GL_TEXTURE_2D,
                         i,
                         0, 0,
                         pDL->getWidth(i), pDL->getHeight(i),
                         sourceFormat,
                         byteFormat,
                         pDL->getBits(i));
      }
   }

   if ((to->type == InteriorTexture || to->type == MeshTexture) &&
       pDL->getNumMipLevels() > 4)
   {
      //
      if (!to->smallTexGLName)
         glGenTextures(1,&to->smallTexGLName);

      glBindTexture(GL_TEXTURE_2D, to->smallTexGLName);

      if (pDL->getFormat() == GBitmap::Palettized)
      {
         glColorTableEXT(GL_TEXTURE_2D,
                         pDL->getPalette()->getPaletteType() == GPalette::RGB ? GL_RGB : GL_RGBA,
                         256,
                         GL_RGBA,
                         GL_UNSIGNED_BYTE,
                         pDL->getPalette()->getColors());
      }

      if (sgDisableSubImage)
      {
         for (U32 i = 4; i < maxDownloadMip; i++)
         {
            glTexImage2D(GL_TEXTURE_2D,
                         i - 4,
                         destFormat,
                         pDL->getWidth(i), pDL->getHeight(i),
                         0,
                         sourceFormat,
                         byteFormat,
                         pDL->getBits(i));
         }
      }
      else
      {
         for (U32 i = 4; i < maxDownloadMip; i++)
         {
            glTexSubImage2D(GL_TEXTURE_2D,
                            i - 4,
                            0, 0,
                            pDL->getWidth(i), pDL->getHeight(i),
                            sourceFormat,
                            byteFormat,
                            pDL->getBits(i));
         }
      }
   }
   else
   {
      if (to->smallTexGLName != 0)
         glDeleteTextures(1, &to->smallTexGLName);
      to->smallTexGLName = 0;
   }

   if(pDL != pBitmap)
      delete pDL;
}

void TextureManager::refresh(TextureObject *to, GBitmap* bmp)
{
   if (!(gDGLRender || sgResurrect)) return;

   U32 sourceFormat, destFormat, byteFormat;
   GBitmap* pBitmap = bmp;

   getSourceDestByteFormat(pBitmap, &sourceFormat, &destFormat, &byteFormat);

   if (!to->texGLName)
      glGenTextures(1,&to->texGLName);

   glBindTexture(GL_TEXTURE_2D, to->texGLName);
   GBitmap* pDL = createPaddedBitmap(pBitmap);

   U32 maxDownloadMip = pDL->getNumMipLevels();
   if (to->type == BitmapTexture ||
       to->type == BitmapKeepTexture ||
       to->type == BitmapNoDownloadTexture)
   {
      maxDownloadMip = 1;
   }

   if (pDL->getFormat() == GBitmap::Palettized)
   {
      glColorTableEXT(GL_TEXTURE_2D,
                      pDL->getPalette()->getPaletteType() == GPalette::RGB ? GL_RGB : GL_RGBA,
                      256,
                      GL_RGBA,
                      GL_UNSIGNED_BYTE,
                      pDL->getPalette()->getColors());
   }

   if (sgDisableSubImage)
   {
      for (U32 i = 0; i < maxDownloadMip; i++)
      {
         glTexImage2D(GL_TEXTURE_2D,
                      i,
                      destFormat,
                      pDL->getWidth(i), pDL->getHeight(i),
                      0,
                      sourceFormat,
                      byteFormat,
                      pDL->getBits(i));
      }
   }
   else
   {
      for (U32 i = 0; i < maxDownloadMip; i++)
      {
         glTexSubImage2D(GL_TEXTURE_2D,
                         i,
                         0, 0,
                         pDL->getWidth(i), pDL->getHeight(i),
                         sourceFormat,
                         byteFormat,
                         pDL->getBits(i));
      }
   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -