bricklore bricklore - 3 months ago 29
C++ Question

android native app opengl es white textures

I'm writing a native app that should only display a little triangle with a texture.

But unfortunately, it everytime only displays a white triangle.

My code is very simple.

First to load a tga Image:

static const GLenum gl_format[4] = { GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA };


unsigned int LoadTGATextureFromFile(const char* filename)
{
unsigned int handle;
unsigned char hdr[18];
unsigned char file_id[256 + 1];
int file;

file = open(filename, O_RDONLY);
if(file < 0)
{
Log(LOGLEVEL_ERROR, APPNAME, "Error: Failed to open tga file '%s' for read ing\n", filename);
return 0;
}

if(read(file, hdr, 18) != 18 || read(file, file_id, hdr[0]) != hdr[0])
{
Log(LOGLEVEL_ERROR, APPNAME, "Error: Unexpected EOF while reading header of '%s'\n", filename);
close(file);
return 0;
}
file_id[hdr[0]] = 0;
if(hdr[1] != 0 || (hdr[2] != 2 && hdr[2] != 3) || (hdr[16] != 8 && hdr[16] != 16 && hdr[16] != 24 && hdr[16] != 32))
{
Log(LOGLEVEL_ERROR, APPNAME, "Error: File '%s' has invalid format\n", filename);
close(file);
return 0;
}
int width = *(short*)(hdr + 12);
int height = *(short*)(hdr + 14);
if((width & (width - 1)) != 0 || (height & (height - 1)) != 0)
{
Log(LOGLEVEL_ERROR, APPNAME, "Error: File '%s' has invalid resolution %dx%d\n", filename, width, height);
close(file);
return 0;
}
int components = hdr[16] / 8;

unsigned char* data = new unsigned char [width * height * components];
if (read(file, data, width * height * components) != width * height * components)
{
Log(LOGLEVEL_ERROR, APPNAME, "Error: Unexpected EOF while reading image data of '%s'\n", filename);
close(file);
return 0;
}
close(file);

char dummy;
if(read(file, &dummy, 1) == 1)
Log(LOGLEVEL_ERROR, APPNAME, "Warning: TGA file '%s' has overlength\n", filename);

switch (components - 1)
{
char tmp;
case 2:
for (int i = 0; i < width * height; i += 3)
{
tmp = data[i];
data[i] = data[i + 2];
data[i + 2] = tmp;
}
break;

case 3:
for (int i = 0; i < width * height; i += 4)
{
tmp = data[i];
data[i] = data[i + 2];
data[i + 2] = tmp;
}
break;

default:
break;
}

glGenTextures(1, &handle);
glBindTexture(GL_TEXTURE_2D, handle);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glBindTexture(GL_TEXTURE_2D, 0);

delete [] data;
Log(LOGLEVEL_ERROR, APPNAME, "'%s' successfully loaded [handle = %d, FILE_ID = \"%s\", width = %d, height = %d, depth = %d] :)\n", filename, handle, file_id, width, height, components * 8);

return handle;
}


Loading the texture:

int texture = LoadTextureFormFile("/sdcard/test.tga");


Then to draw:

float tricoords[6] = { 0.0, 0.0, 1.0, 1.0, 0.0, 1.0 };
float texcoords[6] = { 0.0, 0.0, 1.0, 1.0, 0.0, 1.0 };

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(2, GL_FLOAT, 0, tricoords);
glTexCoordPointer(2, GL_FLOAT, 0, texcoords);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);


I know, that this code isnt optimized, but its only for debugging.

The logcat of my app prints:

successfully loaded tga [handle = 1, FILE_ID = "", width = 64, height = 128, depth = 32] :)

But the texture stays white.

Answer

Just found the mistake, texture mipmapping was enabled for the loaded texture the mipmaps were never created.

Changing this line:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);  

to this will disable mipmaps for the texture.

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);