22 #include "ETCHeader.h"
26 #include <GLES2/gl2ext.h>
40 GL_CHECK(glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, numberOfTextureFormats));
42 *textureFormats = (GLint *)calloc(*numberOfTextureFormats,
sizeof(GLint));
43 if(*textureFormats == NULL)
45 LOGE(
"Out of memory at %s:%i\n", __FILE__, __LINE__);
49 GL_CHECK(glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, *textureFormats));
55 bool supportETC =
false;
56 GLint *textureFormat = NULL;
57 GLint numberOfTextureFormats = 0;
62 LOGI(
"Number of texture formats supported: %d\nFormats:\n", numberOfTextureFormats);
63 for(
int allTextureFormats = 0; allTextureFormats < numberOfTextureFormats; allTextureFormats++)
65 LOGI(
"0x%.8x\t", textureFormat[allTextureFormats]);
66 switch(textureFormat[allTextureFormats])
69 LOGI(
"GL_ETC1_RGB8_OES\n");
78 for(
int allTextureFormats = 0; allTextureFormats < numberOfTextureFormats; allTextureFormats++)
91 LOGD(
"Texture compression format GL_ETC1_RGB8_OES not supported\n");
95 #elif GLES_VERSION == 3
105 unsigned char *randomTexture =
new unsigned char [width * height * 4];
106 if(randomTexture == NULL)
108 LOGE(
"Out of memory at %s:%i\n", __FILE__, __LINE__);
113 for(
unsigned int allTexels = 0; allTexels < width *
height; allTexels ++)
116 randomTexture[allTexels * 4 + 3] = 255;
118 for (
int allChannels = 0; allChannels < 3; allChannels++)
121 int randomNumber = (
int)(255 * (rand() / (
float)RAND_MAX));
122 randomTexture[allTexels * 4 + allChannels] = randomNumber;
126 *textureData = randomTexture;
131 unsigned char* newTexture =
new unsigned char [width *
height];
133 if(newTexture == NULL)
135 LOGE(
"Out of memory at %s:%i\n", __FILE__, __LINE__);
139 for (
unsigned int texelIndex = 0; texelIndex < width *
height; ++texelIndex)
141 newTexture[texelIndex] = red;
143 *textureData = newTexture;
148 *textureData =
new short [width *
height];
150 if (*textureData == NULL)
152 LOGE(
"Out of memory at %s:%i\n", __FILE__, __LINE__);
156 for (
unsigned int texelIndex = 0; texelIndex < width *
height; ++texelIndex)
158 (*textureData)[texelIndex] = red;
164 delete[] (
unsigned char*)*textureData;
169 LOGD(
"Texture loadData started for %s...\n", filename);
171 FILE *file = fopen(filename,
"rb");
174 LOGE(
"Failed to open '%s'\n", filename);
177 fseek(file, 0, SEEK_END);
178 unsigned int length = ftell(file);
179 unsigned char *loadedTexture = (
unsigned char *)calloc(length,
sizeof(
unsigned char));
180 if(loadedTexture == NULL)
182 LOGE(
"Out of memory at %s:%i\n", __FILE__, __LINE__);
185 fseek(file, 0, SEEK_SET);
186 size_t read = fread(loadedTexture,
sizeof(
unsigned char), length, file);
189 LOGE(
"Failed to read in '%s'\n", filename);
194 *textureData = loadedTexture;
196 LOGD(
"Texture loadData for %s done.\n", filename);
202 const int sizeOfETCHeader = 16;
203 unsigned char* tempTextureData = NULL;
205 loadData(filename, &tempTextureData);
207 if (textureData == NULL)
209 LOGE(
"textureData is a NULL pointer.");
212 if (etcHeader == NULL)
214 LOGE(
"etcHeader is a NULL pointer.");
218 if (tempTextureData != NULL)
220 ETCHeader tempEtcHeader(tempTextureData);
222 *etcHeader = tempEtcHeader;
223 *textureData = tempTextureData + sizeOfETCHeader;
227 LOGE(
"Could not load data from file %s.", filename);
235 GL_CHECK(glGenTextures(1, textureID));
236 GL_CHECK(glBindTexture(GL_TEXTURE_2D, *textureID));
240 string filename = filenameBase + string(
"0") + filenameSuffix;
241 unsigned char *
data = NULL;
246 LOGD(
"Base level Mipmap loaded: (%i, %i) padded to 4x4 blocks, (%i, %i) actual\n", loadedETCHeader.getPaddedWidth(), loadedETCHeader.getPaddedHeight(), loadedETCHeader.getWidth(), loadedETCHeader.getHeight());
247 int width = loadedETCHeader.getWidth();
248 int height = loadedETCHeader.getHeight();
249 int numberOfMipmaps = 1;
250 while((width > 1) || (height > 1))
253 if(width > 1) width >>= 1;
254 if(height > 1) height >>= 1;
256 LOGD(
"Requires %i Mipmap levels in total\n", numberOfMipmaps);
264 #if GLES_VERSION == 2
265 GL_CHECK(glCompressedTexImage2D(GL_TEXTURE_2D, 0,
GL_ETC1_RGB8_OES, loadedETCHeader.getWidth(), loadedETCHeader.getHeight(), 0, (loadedETCHeader.getPaddedWidth() * loadedETCHeader.getPaddedHeight()) >> 1, data + 16));
266 #elif GLES_VERSION == 3
273 for(
int allMipmaps = 1; allMipmaps < numberOfMipmaps; allMipmaps++)
277 int input = allMipmaps;
288 level = (
char *)calloc(inputLength + 1,
sizeof(
char));
291 LOGE(
"Out of memory at %s:%i\n", __FILE__, __LINE__);
294 sprintf(level,
"%i", allMipmaps);
296 filename = filenameBase + string(level) + filenameSuffix;
308 #if GLES_VERSION == 2
309 GL_CHECK(glCompressedTexImage2D(GL_TEXTURE_2D, allMipmaps,
GL_ETC1_RGB8_OES, loadedETCHeader.getWidth(), loadedETCHeader.getHeight(), 0, (loadedETCHeader.getPaddedWidth() * loadedETCHeader.getPaddedHeight()) >> 1, data + 16));
310 #elif GLES_VERSION == 3
322 for (
int pixelIndex = 0; pixelIndex < lineWidth; ++pixelIndex)
324 memcpy(destination + pixelIndex * rgbComponentsCount,
325 source + (lineWidth - pixelIndex - 1) * rgbComponentsCount,
326 rgbComponentsCount *
sizeof(
float));
static void loadPKMData(const char *filename, ETCHeader *etcHeader, unsigned char **textureData)
Load header and texture data from a pkm file into memory.
const int rgbComponentsCount
static void createTexture(unsigned int width, unsigned int height, GLvoid **textureData)
Create a texture using random data.
const GLenum textureFormats[]
GLint GLsizei GLsizei height
static void getCompressedTextureFormats(GLint **textureFormats, int *numberOfTextureFormats)
Uses glGetIntegerv to get the number of compressed texture formats and the formats themselves...
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
static void deleteTextureData(GLvoid **textureData)
Deletes previously created texture.
GLsizei GLsizei GLchar * source
static void loadCompressedMipmaps(const char *filenameBase, const char *filenameSuffix, GLuint *textureID)
Load compressed mipmaps into memory.
static void reversePixelLine(float *destination, const float *source, int lineWidth)
Copies float pixel data of one line of the image from source to destination in the reverse direction...
unsigned char * textureData
static void loadData(const char *filename, unsigned char **textureData)
Load texture data from a file into memory.
GLenum GLuint GLenum GLsizei length
GLenum GLuint GLint level
static bool isETCSupported(bool verbose=false)
Reports whether or not ETC (Ericsson Texture Compression) is supported.
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count