Advertisement
Guest User

HD Tga textures in hl

a guest
May 2nd, 2011
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. #include "hud.h"
  2. #include "cl_util.h"
  3. #include "com_model.h"
  4.  
  5. #include <windows.h>
  6. #include <gl/gl.h>
  7. #include <gl/glext.h>
  8.  
  9. #pragma comment(lib,"Opengl32.lib")
  10. #pragma warning(disable:4018)
  11.  
  12. #pragma pack(push,1)
  13. typedef struct
  14. {
  15.    
  16.     byte  identsize;          // size of ID field that follows 18 byte header (0 usually)
  17.     byte  colourmaptype;      // type of colour map 0=none, 1=has palette
  18.     byte  imagetype;          // type of image 0=none,1=indexed,2=rgb,3=grey,+8=rle packed
  19.    
  20.     short colourmapstart;     // first colour map entry in palette
  21.     short colourmaplength;    // number of colours in palette
  22.     byte  colourmapbits;      // number of bits per palette entry 15,16,24,32
  23.    
  24.     short xstart;             // image x origin
  25.     short ystart;             // image y origin
  26.     short width;              // image width in pixels
  27.     short height;             // image height in pixels
  28.     byte  bits;               // image bits per pixel 8,16,24,32
  29.     byte  descriptor;         // image descriptor bits (vh flip bits)
  30.    
  31.    
  32.    
  33. } TGA_HEADER;
  34. #pragma pack(pop)
  35.  
  36. int glLoadTgaFromPak(char* name,int dest)
  37. {
  38.     // Длина файла и буффер под него:
  39.     int len;
  40.     byte*buff=gEngfuncs.COM_LoadFile(name,5,&len); // грузим файл
  41.    
  42.    
  43.     int width,height; // размеры картинки
  44.     TGA_HEADER hdr; // Заголовок файла
  45.    
  46.     if (!buff) // Если буффер=0 то файл не найден
  47.     {
  48.         return 0;
  49.     }
  50.     // копируем заголовок в отдельную переменную
  51.     memcpy(&hdr,&buff[0],18);
  52.    
  53.     width=hdr.width;
  54.     height=hdr.height;
  55.    
  56.     // предполагаем что у нас 24 битная картинка
  57.     int type=GL_RGB;
  58.     int bpp;
  59.     // проверяем это
  60.     switch(hdr.bits)
  61.     {
  62.     case 24:
  63.         bpp=3;
  64.         break;
  65.     case 32:
  66.         type=GL_RGBA;
  67.         bpp=4;
  68.         break;
  69.     default:
  70.         gEngfuncs.Con_Printf("glLoadTga(): file %s has unsupported bpp num: %d !\n",hdr.bits);
  71.         return -1;
  72.     }
  73.     int imgsize=width*height*bpp;
  74.     byte temp;
  75.  
  76.     // преобразуем BGR в RGB
  77.     for(int i=18; i<int(imgsize); i+=bpp)
  78.     {                                                    
  79.         temp=buff[i];                      
  80.         buff[i]=buff[i+2];
  81.         buff[i+2] = temp;                
  82.     }
  83.  
  84.     byte*oldbuff=(byte*)malloc(imgsize);
  85.  
  86.     memcpy(oldbuff,&buff[18],imgsize);
  87.  
  88.     int dest_offset=18;
  89.    
  90.     for(i=height-1;i>=0;i--)
  91.     {
  92.         byte*src_line=&oldbuff[width*bpp*i];
  93.        
  94.         memcpy(&buff[dest_offset],src_line,width*bpp);
  95.         dest_offset+=width*bpp;
  96.     }
  97.  
  98.     free(oldbuff);
  99.  
  100.     glBindTexture(GL_TEXTURE_2D, dest); // делаем её текушей
  101.     // Настраиваем мип маппинг
  102.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  103.     glHint( GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST );
  104.     glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE );
  105.     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  106.     glTexImage2D(GL_TEXTURE_2D, 0, type,width,height,0, type, GL_UNSIGNED_BYTE, &buff[18]);
  107.    
  108.     gEngfuncs.COM_FreeFile(buff);
  109.  
  110.     return dest;
  111.    
  112. }
  113.  
  114. bool g_tex_loaded=false;
  115. // запихнуть в CHud::Redraw - просто и надежно.
  116. void UploadWorldHDTextures()
  117. {
  118.     if (g_tex_loaded) return;
  119.     g_tex_loaded=true;
  120.  
  121.     model_t*world=gEngfuncs.GetEntityByIndex(0)->model;
  122.  
  123.     for(int i=0;i<world->numtextures;i++)
  124.     {
  125.         char file_path[260];
  126.  
  127.         sprintf(file_path,"gfx/textures/%s.tga",world->textures[i]->name);
  128.  
  129.         glLoadTgaFromPak(file_path,world->textures[i]->gl_texturenum);
  130.     }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement