Advertisement
Guest User

Raw

a guest
Jul 28th, 2017
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include    <memory>
  2.  
  3. #include    "hud.h"
  4. #include    "cl_util.h"
  5.  
  6. #include    "logging.h"
  7.  
  8. #include    "gl_texture.h"
  9.  
  10. cl_texture_t    m_pTextures[ MAX_TEXTURES ];
  11. int             m_iNumTextures;
  12. GLuint          curTexID    =   TEXTURE_BASE;
  13.  
  14. cl_texture_t*   PrecacheVTFFile( const char* szFileName )
  15. {
  16.     cl_texture_t*       pTexture    =   NULL;   // Tidy stuff.
  17.     char                szPath[ MAX_PATH ],
  18.                         szFinalPath[ MAX_PATH ];
  19.     VTFLib :: CVTFFile  vtf;    // Put all of this here?
  20.     int iMipLevel                   =   MIP_DUMMY;
  21.  
  22.     if( !gVTFM.IsVTFAllowed( ) || !g_pFileSystem || !gOGLM.IsGLAllowed( ) )
  23.         return  nullptr;
  24.  
  25.     if( !g_pFileSystem -> GetLocalPath( "materials", szPath, sizeof( szPath ) ) )
  26.         return  nullptr;
  27.  
  28.     snprintf( szFinalPath, sizeof( szFinalPath ), "%s\\%s.vtf", szPath, szFileName );
  29.  
  30.     if( !vtf.Load( szFinalPath ) || !vtf.IsLoaded( ) )  // This can probably be improved along with the first checks.
  31.         return  nullptr;
  32.  
  33.     pTexture                =   &m_pTextures[ m_iNumTextures ]; // Some Trinity thing, figure out what this is.
  34.  
  35.     int someSizeData        =   vtf.ComputeImageSize( vtf.GetWidth( ), vtf.GetHeight( ), vtf.GetDepth( ), iMipLevel, vtf.GetFormat( ) );    // Raw image (for testing) actually has 7 (or 8?) mipmaps that I can use, implement that.
  36.  
  37.     vlByte* rawImageData    =   vtf.GetData( 0, 0, 0, 0 );  // Can't get raw data so just replace with them all with 0.
  38.     auto    allocMemData    =   std :: make_unique< vlByte[ ] >( someSizeData );    // Don't forget deletions of it!
  39.  
  40.     if( ( !vtf.GetFormat( ) == IMAGE_FORMAT_RGBA8888 ) && !vtf.Convert( rawImageData, allocMemData.get( ), vtf.GetWidth( ), vtf.GetHeight( ), vtf.GetFormat( ), IMAGE_FORMAT_RGBA8888 ) )
  41.         return  nullptr;    // This check is quite stupid, redo.
  42.  
  43.     glGenTextures( 1, &curTexID );  // Nasty displaying hack. Look more into for future bug report.
  44.     glBindTexture( GL_TEXTURE_2D, curTexID );   // I'm a noob at OpenGL, investigate at a later date to make sure this works.
  45.     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, vtf.GetWidth( ), vtf.GetHeight( ), 0, GL_RGBA8, GL_UNSIGNED_BYTE, allocMemData.get( ) );  // Figure out if uploading like this is okay.
  46.  
  47.     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  48.     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  49.     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
  50.     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
  51.  
  52.     strcpy( pTexture -> szName, szFileName );
  53.  
  54.     pTexture -> iWidth  =   vtf.GetWidth( );    // Texture information so I can recall it. Expand upon this.
  55.     pTexture -> iHeight =   vtf.GetHeight( );
  56.     pTexture -> iFormat =   vtf.GetFormat( );   // Strange reason, but it doesn't output the new format I converted it to, but the original.
  57.     pTexture -> iIndex  =   curTexID;
  58.  
  59.     gLS.Log( 1, "iWidth: %d\niHeight: %d\niFormat: %d\niIndex: %d\nm_iNumTextures: %d\ncurTexID: %d\nszFileName: %s\nszPath: %s\nszFinalPath %s\nszName: %s\n\n", pTexture -> iWidth, pTexture -> iHeight, pTexture -> iFormat, pTexture -> iIndex, m_iNumTextures, curTexID, szFileName, szPath, szFinalPath, pTexture -> szName );
  60.  
  61.     m_iNumTextures++;
  62.     curTexID++;
  63.  
  64.     return  pTexture;   // Good?
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement