12 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
14 # include "gl_core_3_3.h"
16 # include "gl_compat_4_4.h"
21 int GL_ARB_texture_non_power_of_two = 1;
22 int GL_EXT_texture_filter_anisotropic = 1;
35 enum IMGFormatType { any, rgb24, bfr24, rgba32, bgra32, grayScale };
43 class TextureFlags : uint32 { bool invalid:1; }
45 class Texture : struct
55 flags = { invalid = true };
59 property uint32 orderMask { set { orderMask = value; } }
61 static bool setData( IMGImage image, int internalformat, int mipmapmode, float anisotropy, int maxresolution )
66 if( image.format.bytesPerPixel == 1 )
74 else if( image.format.bytesPerPixel == 2 )
76 else if( image.format.bytesPerPixel == 3 )
78 else if( image.format.bytesPerPixel == 4 )
82 fprintf( stderr, "ERROR: Bad image format.\n" );
85 if( internalformat == -1 )
86 internalformat = glformat;
88 width = image.format.width;
89 height = image.format.height;
90 if( !( GL_ARB_texture_non_power_of_two ) )
92 if( !( ccIsPow2Int32( width ) ) || !( ccIsPow2Int32( height ) ) )
94 fprintf( stderr, "ERROR: Non-power of two texture used and GL_ARB_texture_non_power_of_two not supported.\n" );
99 glGenTextures( 1, &this.glTex );
100 glBindTexture( GL_TEXTURE_2D, this.glTex );
102 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
103 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
104 glTexImage2D( GL_TEXTURE_2D, 0, internalformat, image.format.width, image.format.height, 0, glformat, GL_UNSIGNED_BYTE, image.data );
106 if( ( GL_EXT_texture_filter_anisotropic ) && ( anisotropy > 1.0 ) )
107 ; //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy );
110 this.height = height;
111 widthinv = 1.0f / (float)width;
112 heightinv = 1.0f / (float)height;
117 bool build( IMGImage image, int mipmapmode, float anisotropy, int maxresolution )
120 glDeleteTextures( 1, &glTex );
121 if( !setData( image, -1, mipmapmode, anisotropy, maxresolution ) )
123 fprintf( stderr, "ERROR: Failed to create texture.\n" );
126 flags.invalid = false;
131 bool load( const String path, int mipmapmode, float anisotropy, int maxresolution )
136 #if TEXMG_ENABLE_PNG_SUPPORT
137 if( !( imgReadPngFile( &image, path, 0 ) ) )
139 fprintf( stderr, "ERROR: Loading texture %s failed.\n", path );
143 fprintf( stderr, "ERROR: File support disabled, %s:%d\n", __FILE__, __LINE__ );
147 result = build( image, mipmapmode, anisotropy, maxresolution );
149 fprintf( stderr, "ERROR: Bad format for texture %s.\n", path );
158 glDeleteTextures( 1, &glTex );