44#include < cstdio>
55
66unsigned int g_texture_memory_usage = 0 ;
7-
8- extern int g_textureFiltering;
7+ int g_textureFiltering = 0 ; // 0 - bilinear, 1 - trilinear, 2 - aniso
8+ int g_textureMipmaps = 0 ; // 0 - off, 1 - on
99
1010static const GLint textureWrap = GL_REPEAT;
1111
@@ -18,42 +18,58 @@ void subMem(int w, int h)
1818 memory -= w * h * 2 ;
1919}
2020
21- GLuint load_texture (const Pict2& pict, bool /* bmipmap*/ )
21+ GLuint load_texture (Pict2 pict, bool bmipmap)
2222{
23+ pict.cropNpotH ();
24+
2325 memory += pict.w () * pict.h () * 2 ;
2426 // printf("texmem usage: %u\n", memory); fflush(stdout);
2527
26- if (bits_count (pict.w ()) != 1 )
27- return 0 ;
28- if (bits_count (pict.h ()) != 1 && bits_count (pict.h ()+1 ) != 1 )
28+ if (g_textureMipmaps == 0 )
29+ {
30+ bmipmap = false ;
31+ }
32+ GLint packAlignment;
33+ glGetIntegerv (GL_PACK_ALIGNMENT, &packAlignment);
34+
35+ pict.pack16 (packAlignment);
36+
37+ if (bits_count (pict.w ()) != 1 || bits_count (pict.h ()) != 1 )
2938 return 0 ;
39+ /* if (bits_count(pict.h()) != 1 && bits_count(pict.h()+1) != 1)
40+ return 0;*/
3041 {
3142 GLuint textura;
3243 glGenTextures (1 , &textura); checkGL ();
3344 glBindTexture (GL_TEXTURE_2D, textura); checkGL ();
34- // if (!bmipmap) // no mipmaps
35- {
36- unsigned int pict_h = pict.h ();
37- if (bits_count (pict.h ()) != 1 && bits_count (pict.h ()+1 ) == 1 ) // předpřipravené mipmapy
38- pict_h = (pict.h ()+1 )/2 ;
3945
40- if (pict.packed565 )
46+ for (int level = 0 ;;++level)
47+ {
48+ unsigned int pict_h = bits_crop_npot (pict.h ());
49+ g_texture_memory_usage += 4 *pict.w ()*pict_h;
50+ if (pict.p_hasAlpha )
4151 {
42- glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGB , pict.w (), pict_h, 0 , GL_RGB, GL_UNSIGNED_SHORT_5_6_5 , pict.c_px ()); checkGL ();
52+ glTexImage2D (GL_TEXTURE_2D, level, GL_RGBA , pict.w (), pict_h, 0 , GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 , pict.c_ppx ()); checkGL ();
4353 }
4454 else
4555 {
46- glTexImage2D (GL_TEXTURE_2D, 0 , GL_RGBA , pict.w (), pict_h, 0 , GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4 , pict.c_px ()); checkGL ();
56+ glTexImage2D (GL_TEXTURE_2D, level, GL_RGB , pict.w (), pict_h, 0 , GL_RGB, GL_UNSIGNED_SHORT_5_6_5 , pict.c_ppx ()); checkGL ();
4757 }
58+ if (!bmipmap || (pict_h == 1 && pict.w () == 1 )) break ;
59+ int newW = pict.w () >> 1 ;
60+ int newH = pict_h >> 1 ;
61+ if (newW == 0 ) newW = 1 ;
62+ if (newH == 0 ) newH = 1 ;
63+ pict.scale (newW, newH);
64+ pict.pack16 (packAlignment);
65+ }
4866
67+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, bmipmap ? (g_textureFiltering == 0 ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR_MIPMAP_LINEAR) : GL_LINEAR); checkGL ();
68+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); checkGL ();
4969
50- g_texture_memory_usage += 4 *pict.w ()*pict_h;
51- glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); checkGL ();
52- glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); checkGL ();
70+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, textureWrap); checkGL ();
71+ glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, textureWrap); checkGL ();
5372
54- glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, textureWrap); checkGL ();
55- glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, textureWrap); checkGL ();
56- }
5773 glBindTexture (GL_TEXTURE_2D, 0 ); checkGL ();
5874 return textura;
5975 }
0 commit comments