21 #include <GLES3/gl3.h>
34 GL_CHECK(glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, numberOfTextureFormats));
36 *textureFormats = (GLint *)calloc(*numberOfTextureFormats,
sizeof(GLint));
37 if(*textureFormats == NULL)
39 LOGE(
"Out of memory at %s:%i\n", __FILE__, __LINE__);
43 GL_CHECK(glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, *textureFormats));
53 unsigned char *randomTexture =
new unsigned char [width * height * 4];
54 if(randomTexture == NULL)
56 LOGE(
"Out of memory at %s:%i\n", __FILE__, __LINE__);
61 for(
unsigned int allTexels = 0; allTexels < width *
height; allTexels ++)
64 randomTexture[allTexels * 4 + 3] = 255;
66 for (
int allChannels = 0; allChannels < 3; allChannels++)
69 int randomNumber = (
int)(255 * (rand() / (
float)RAND_MAX));
70 randomTexture[allTexels * 4 + allChannels] = randomNumber;
74 *textureData = randomTexture;
77 void Texture::createTexture(
unsigned int width,
unsigned int height,
unsigned int red, GLvoid **textureData)
79 unsigned char* newTexture =
new unsigned char [width *
height];
81 if(newTexture == NULL)
83 LOGE(
"Out of memory at %s:%i\n", __FILE__, __LINE__);
87 for (
unsigned int texelIndex = 0; texelIndex < width *
height; ++texelIndex)
89 newTexture[texelIndex] = red;
91 *textureData = newTexture;
96 *textureData =
new short [width *
height];
98 if (*textureData == NULL)
100 LOGE(
"Out of memory at %s:%i\n", __FILE__, __LINE__);
104 for (
unsigned int texelIndex = 0; texelIndex < width *
height; ++texelIndex)
106 (*textureData)[texelIndex] = red;
112 delete[] (
unsigned char*)*textureData;
118 FILE *file = fopen(filename,
"rb");
122 LOGE(
"Failed to open '%s'\n", filename);
126 fseek(file, 0, SEEK_END);
128 unsigned int length = ftell(file);
129 unsigned char *loadedTexture = (
unsigned char *)calloc(length,
sizeof(
unsigned char));
131 ASSERT(loadedTexture != NULL,
"Could not allocate memory to store PKM file data.")
133 fseek(file, 0, SEEK_SET);
135 size_t read = fread(loadedTexture, sizeof(
unsigned char), length, file);
137 ASSERT(read == length, "Failed to read PKM file.");
141 *textureData = loadedTexture;
146 void Texture::
loadPKMData(const
char *filename, ETCHeader* etcHeader,
unsigned char **textureData)
149 const int sizeOfETCHeader = 16;
150 unsigned char* tempTextureData = NULL;
152 loadData(filename, &tempTextureData);
154 ASSERT(textureData != NULL,
"textureData is a NULL pointer.");
155 ASSERT(etcHeader != NULL,
"etcHeader is a NULL pointer.");
156 ASSERT(tempTextureData != NULL,
"Could not load data from PKM file.");
158 ETCHeader tempEtcHeader(tempTextureData);
160 *etcHeader = tempEtcHeader;
161 *textureData = tempTextureData + sizeOfETCHeader;
169 for (
int pixelIndex = 0; pixelIndex < lineWidth; ++pixelIndex)
171 memcpy(destination + pixelIndex * rgbComponentsCount,
172 source + (lineWidth - pixelIndex - 1) * rgbComponentsCount,
173 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...
static void deleteTextureData(GLvoid **textureData)
Deletes previously created texture.
GLsizei GLsizei GLchar * source
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
static bool isETCSupported(bool verbose=false)
Reports whether or not ETC (Ericsson Texture Compression) is supported.