2

我正在编写一个本机应用程序,它应该只显示一个带有纹理的小三角形。
但不幸的是,它每次都只显示一个白色三角形。
我的代码非常简单。

首先加载一个 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;
}

加载纹理

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

然后绘制

    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);

我知道,这段代码没有优化,但它只用于调试。

我的应用程序的 logcat 打印:
成功加载 tga [handle = 1, FILE_ID = "", width = 64, height = 128, depth = 32] :)

但质地保持白色。

4

1 回答 1

2

刚刚发现错误,已为加载的纹理启用了纹理 mipmapping,但从未创建过 mipmap。

更改此行:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);  

这将禁用纹理的 mipmap。

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  
于 2012-12-31T11:36:32.517 回答