12 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
14 # include "gl_core_3_3.h"
16 # include "gl_compat_4_4.h"
20 #if defined(__EMSCRIPTEN__)
22 #include <GLES2/gl2.h>
26 int GL_ARB_texture_non_power_of_two = 1;
27 int GL_EXT_texture_filter_anisotropic = 1;
40 enum IMGFormatType { any, rgb24, bfr24, rgba32, bgra32, grayScale };
48 class TextureFlags : uint32 { bool invalid:1; }
50 class Texture : struct
60 flags = { invalid = true };
64 property uint32 orderMask { set { orderMask = value; } }
66 static bool setData( IMGImage image, int internalformat, int mipmapmode, float anisotropy, int maxresolution )
71 if( image.format.bytesPerPixel == 1 )
73 #if defined(SHADERS) && !defined(__EMSCRIPTEN__)
79 #if defined(SHADERS) && !defined(__EMSCRIPTEN__)
80 else if( image.format.bytesPerPixel == 2 )
83 else if( image.format.bytesPerPixel == 3 )
85 else if( image.format.bytesPerPixel == 4 )
89 fprintf( stderr, "ERROR: Bad image format.\n" );
92 if( internalformat == -1 )
93 internalformat = glformat;
95 width = image.format.width;
96 height = image.format.height;
97 if( !( GL_ARB_texture_non_power_of_two ) )
99 if( !( ccIsPow2Int32( width ) ) || !( ccIsPow2Int32( height ) ) )
101 fprintf( stderr, "ERROR: Non-power of two texture used and GL_ARB_texture_non_power_of_two not supported.\n" );
106 glGenTextures( 1, &this.glTex );
107 glBindTexture( GL_TEXTURE_2D, this.glTex );
109 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
110 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
111 glTexImage2D( GL_TEXTURE_2D, 0, internalformat, image.format.width, image.format.height, 0, glformat, GL_UNSIGNED_BYTE, image.data );
113 if( ( GL_EXT_texture_filter_anisotropic ) && ( anisotropy > 1.0 ) )
114 ; //glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy );
117 this.height = height;
118 widthinv = 1.0f / (float)width;
119 heightinv = 1.0f / (float)height;
124 bool build( IMGImage image, int mipmapmode, float anisotropy, int maxresolution )
127 glDeleteTextures( 1, &glTex );
128 if( !setData( image, -1, mipmapmode, anisotropy, maxresolution ) )
130 fprintf( stderr, "ERROR: Failed to create texture.\n" );
133 flags.invalid = false;
138 bool load( const String path, int mipmapmode, float anisotropy, int maxresolution )
143 #if TEXMG_ENABLE_PNG_SUPPORT
144 if( !( imgReadPngFile( &image, path, 0 ) ) )
146 fprintf( stderr, "ERROR: Loading texture %s failed.\n", path );
150 fprintf( stderr, "ERROR: File support disabled, %s:%d\n", __FILE__, __LINE__ );
154 result = build( image, mipmapmode, anisotropy, maxresolution );
156 fprintf( stderr, "ERROR: Bad format for texture %s.\n", path );
165 glDeleteTextures( 1, &glTex );