不能图在真实设备上使用OpenGL ES纹理

问题描述:

我试过一个简单的程序,动态生成纹理并将其映射到使用NDK四。一切都很好在模拟器上,但未能在真实设备上。
这里是我的code:

I've tried a simple program that dynamically generates a texture and map it to a quad using NDK. Everything is fine on an emulator but failed on real devices. Here is my code:

private static class Renderer implements GLSurfaceView.Renderer
{
    @Override
    public void onDrawFrame(GL10 gl)
    {
        nativeDrawFrame();
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height)
    {
        nativeInit(width, height);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config)
    {
        // do nothing...
    }
}

和本土code:

const static GLfloat vertices_f[4][2] = 
{
    { 0.0f,    0.0f },
    { 100.0f,    0.0f },
    { 100.0f,    100.0f },
    { 0.0f,    100.0f }
};

const static GLfloat texCoords_f[4][2] = 
{
    { 0.0f,    0.0f },
    { 1.0f,    0.0f },
    { 1.0f,    1.0f },
    { 0.0f,    1.0f }
};

JNIEXPORT void JNICALL Java_com_sangfor_gltest_GLView_nativeInit(JNIEnv * env, jobject obj, jint width, jint height)
{
    if (!bitmap)
    {
        // allocate dynamic texture memory
        bitmap = memalign(16, 1024*1024);
        if (!bitmap)
        {
            __android_log_print(ANDROID_LOG_ERROR, "native-render", "failed allocation.");
            return;
        }
    }

    glViewport(0, 0, width, height);
    //glMatrixMode(GL_PROJECTION);
    //glLoadIdentity();
    //glOrthox(0, 0x10000, 0, 0x10000, 0x10000, -0x10000);
    //glClearColorx(0, 0, 0, 0);
    glGenTextures(1, &texture);
    __android_log_print(ANDROID_LOG_INFO, "native-render", "texture = %d", texture);
    // glVertexPointer(2, GL_FIXED, 0, vertices);
    // glTexCoordPointer(2, GL_FIXED, 0, texCoords);
    //glEnableClientState(GL_COLOR_ARRAY);
    glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
    glEnable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);
    glDisable(GL_ALPHA_TEST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}

JNIEXPORT void JNICALL Java_com_sangfor_gltest_GLView_nativeDrawFrame(JNIEnv * env, jobject obj)
{
    struct timeval tv;
    unsigned char color_value;

    glClear(GL_COLOR_BUFFER_BIT);

    // fill texture according to current timestamp
    gettimeofday(&tv, NULL);
    color_value = (unsigned char)(tv.tv_usec * 0.000255f);
    memset(bitmap, color_value, 1024*1024);
    __android_log_print(ANDROID_LOG_INFO, "native-render", "color_value = %d", color_value);

    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, bitmap);

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

    glTexCoordPointer(2, GL_FLOAT, 0, texCoords_f);
    glVertexPointer(2, GL_FLOAT, 0, vertices_f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
    glFlush();
}

在一个模拟器它只是工作如我所料:屏幕上的一个区域不断变化的颜色。
然而,当我在真实设备(三星Galaxy S 2.3.3和华硕TF101变压器3.2.1)运行它,它只是显示一个白色块和纹理映射似乎不工作。

On an emulator it just works as I expected: an area on the screen keeps changing its color. However, when I run it on real devices (Samsung Galaxy S 2.3.3 and Asus Transformer TF101 3.2.1), it just show a white block and texture mapping seems not working.

我尝试添加和注释掉投影变换,通过调用使纹理映射过glEnable(GL_TEXTURE_2D),通过调用禁用alpha混合,并测试 glDisable( ...),移动 glBindTexture glTexImage2D 来初始化函数,改变纹理尺寸为32×32,但没有这些工作。

I tried add and comment out projection transform, enable texture mapping by calling glEnable(GL_TEXTURE_2D), disable alpha blending and testing by calling glDisable(...), move glBindTexture and glTexImage2D to init function, change texture size to 32 x 32 but none of these work.

谁能帮我弄清楚为什么纹理贴图失败,只是在真实设备?是否有一个GPU的限制还是什么?

Can anyone help me figure out why texture mapping fails just on real devices? Is there a GPU limitation or something?

编辑:我试过勺的建议,并找到了真正的问题。不管我绑定的纹理,该器件采用纹理命名的 0 的渲染四边形,所以 glBindTexture(GL_TEXTURE_2D,0)工作正常,但 glBindTexture(GL_TEXTURE_2D,1) glGenTextures(...)返回任何失败。这意味着我可以只保存一个纹理,但我必须使用2个或更多。

I've tried Spoon's suggestion and found the real problem. No matter which texture I bind, the device uses texture named 0 to render quads, so glBindTexture(GL_TEXTURE_2D, 0) works fine but glBindTexture(GL_TEXTURE_2D, 1) and anything returned by glGenTextures(...) fails. This means I can save only one texture, but I have to use 2 or more.

白色块表示纹理是工作,但纹理还没有正确加载。

White block means texturing is working but the textures haven't loaded properly.

如果实际的映射是一个问题,你会看到纹理,但扭曲或搞砸了。在所有的颜色

If the actual mapping were a problem, you would see the textures but all the colours warped or messed up.

检查由 glGenTextures给出的实际值()。我发现,在Android上使用更高版本的OpenGL ES 1.x的时候(2.2+)的 glGenTextures()抛出了一些真正的随机数,而不是给人0的id, 1,2,3等,这可能是你必须手动提供自己的ID,而不是使用 glGenTextures()

Check the actual values given by glGenTextures(). I found that when using openGL-es 1.x on later versions of android (2.2+) that glGenTextures() threw out some really random numbers rather than giving id's of 0,1,2,3 etc. It may be you have to manually supply your own ID's and not use glGenTextures()

此外,检查像素数据被正确地从文件中加载。如果你有一个错误的文件名或文件丢失,但已经把一些错误处理,允许应用程序继续空白纹理可能出现。和/或不要忘了刷新RES文件夹,甚至清理项目,当您添加新的纹理

Alternatively, check the pixel data is being correctly loaded from file. Blank white textures may appear if you have a filename wrong or file missing but have put in some error handling that allows the app to continue. And/or don't forget to refresh res folders or even clean the project when you add new textures

更新:

这是我做的:

public class Texture{

private int textureId = -1;
// initialise to -1

public int getTextureId(){
    return textureId;
}

public void loadTexture(GL10 gl, Context context){

    String[] filenamesplit = filename.split("\\.");

    name = filenamesplit[filenamesplit.length-2];

    int[] textures = new int[1];
    //Generate one texture pointer...
    //GLES20.glGenTextures(1, textures, 0);             
    textures[0] = ((IridianGraphicsEngine)context).texturecount;
    ((IridianGraphicsEngine)context).texturecount++;

    //...and bind it to our array
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);

    //Create Nearest Filtered Texture
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

    //Different possible texture parameters, e.g. GLES20.GL_CLAMP_TO_EDGE
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);

    Bitmap bitmap = FileUtil.openBitmap(name, context);

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

    bitmap.recycle();   

    textureId = textures[0];

}
}

然后再绘制时:

public class Mesh{

private Texture texture;

public void draw(GL10 gl){

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

    // Pass the vertex buffer in
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0,
                             vertices);

    int textureId = texture.getTextureId();

    if(textureID>=0){

        // Enable Textures
        gl.glEnable(GL10.GL_TEXTURE_2D);

        // Get specific texture.
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID);

        // Use UV coordinates.
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        // Pass in texture coordinates          
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureCoordinates);

    } 

    // Pass in vertex normals
    gl.glNormalPointer(GL10.GL_FLOAT, 0, normals);

    gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);

    gl.glDrawElements(GL10.GL_TRIANGLES, numindices,GL10.GL_UNSIGNED_SHORT, indices);

    gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);

    if(textureID>=0){
        // Disable buffers          
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

}

这是但都在java和使用OpenGL-ES 2.0纹理加载和OpenGL-ES 1.1的一个奇怪的混合绘图

This however is all in java and uses a weird mix of opengl-es 2.0 for texture loading and opengl-es 1.1 for drawing