简体   繁体   中英

Can't map textures with OpenGL ES on real devices

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...
    }
}

and the native 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();
}

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.

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.

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

EDIT: 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.

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

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

UPDATE:

This is how I did it:

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

}
}

Then when drawing:

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

}

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

I moved my JNI native methods to a separate wrapper class, similar to gl2-jni example in Android NDK, then the problem solved. I don't know why implementing static native code in a wrapper class makes differences, but anyway, it works.

GLJNILib.java

// Wrapper for native library
public class GLJNILib {

     static {
         System.loadLibrary("gljni");
     }

    /**
     * @param width the current view width
     * @param height the current view height
     */
     public static native void init(int width, int height);
     public static native void step();
}

GLJNIView.java

// some other stuff

private class Renderer implements GLSurfaceView.Renderer {
    private static final String TAG = "Renderer";
    public void onDrawFrame(GL10 gl) {
        GLJNILib.step();
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLJNILib.init(width, height);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // Do nothing.
    }
}

// some other stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM