21 enum IMGFormatType { any, rgb24, bfr24, rgba32, bgra32, grayScale };
29 class TextureFlags : uint32 { bool invalid:1; }
31 class Texture : struct
42 flags = { invalid = true };
46 property uint32 orderMask { set { orderMask = value; } }
48 static bool setData( IMGImage image, int internalformat, int mipmapmode, float anisotropy, int maxresolution )
54 if( image.format.bytesPerPixel == 1 )
56 #if defined(_GLES) || defined(_GLES2)
57 glformat = GL_ALPHA, swizzle = 1;
61 if(glCaps_legacyFormats)
62 glformat = GL_ALPHA, swizzle = 1;
65 glformat = GL_RED, swizzle = 2;
68 else if( image.format.bytesPerPixel == 2 )
70 #if defined(_GLES) || defined(_GLES2)
71 glformat = GL_LUMINANCE_ALPHA;
74 if(glCaps_legacyFormats)
75 glformat = GL_LUMINANCE_ALPHA;
81 else if( image.format.bytesPerPixel == 3 )
83 else if( image.format.bytesPerPixel == 4 )
87 fprintf( stderr, "ERROR: Bad image format.\n" );
90 if( internalformat == -1 )
91 internalformat = glformat;
93 width = image.format.width;
94 height = image.format.height;
95 if(!glCaps_nonPow2Textures)
97 if( !( ccIsPow2Int32( width ) ) || !( ccIsPow2Int32( height ) ) )
99 fprintf( stderr, "ERROR: Non-power of two texture used and GL_ARB_texture_non_power_of_two not supported.\n" );
104 glGenTextures( 1, &this.glTex );
105 glBindTexture( GL_TEXTURE_2D, this.glTex );
107 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
108 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
110 memset(image.data, 0, image.format.width * image.format.height);
111 glTexImage2D( GL_TEXTURE_2D, 0, internalformat, image.format.width, image.format.height, 0, glformat, GL_UNSIGNED_BYTE, image.data );
113 #if GL_TEXTURE_MAX_ANISOTROPY_EXT
115 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy );
119 this.height = height;
120 this.swizzle = swizzle;
121 widthinv = 1.0f / (float)width;
122 heightinv = 1.0f / (float)height;
127 bool build( IMGImage image, int mipmapmode, float anisotropy, int maxresolution )
130 glDeleteTextures( 1, &glTex );
131 if( !setData( image, -1, mipmapmode, anisotropy, maxresolution ) )
133 fprintf( stderr, "ERROR: Failed to create texture.\n" );
136 flags.invalid = false;
141 bool load( const String path, int mipmapmode, float anisotropy, int maxresolution )
146 #if TEXMG_ENABLE_PNG_SUPPORT
147 if( !( imgReadPngFile( &image, path, 0 ) ) )
149 fprintf( stderr, "ERROR: Loading texture %s failed.\n", path );
153 fprintf( stderr, "ERROR: File support disabled, %s:%d\n", __FILE__, __LINE__ );
157 result = build( image, mipmapmode, anisotropy, maxresolution );
159 fprintf( stderr, "ERROR: Bad format for texture %s.\n", path );
168 glDeleteTextures( 1, &glTex );