0

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.

John_He
  • 3
  • 1
  • 3

2 Answers2

0

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

James Coote
  • 1,975
  • 19
  • 29
  • Thanks for reply. I checked the value returned by `glGenTextures()`. It did throw random numbers larger than 100,000. I used manually supplied number 0 and it worked! According to the [documentation](http://www.khronos.org/opengles/documentation/opengles1_0/html/glBindTexture.html), 0 seems to be a reserved texture name which _represent the default texture for each texture target_ and _were bound to them at initialization_, so I tried 1 at first but failed. – John_He Dec 23 '11 at 02:43
  • Still have problems, see EDIT. – John_He Dec 28 '11 at 11:00
  • In `init()`, you should call `glBindTexture()` after `glGenTexture` (or manually assigning an ID), and then set the texture parameters. That is probably why it is not working, as you're not setting texture parameters, except on the first texture. Added my implementation to my answer. Mine is all in java, but it shouldn't matter. Works on a kindle fire running 2.3.4. – James Coote Dec 28 '11 at 13:31
  • I added bindings and parameter settings after `glGenTextures`, it helps. Thanks a lot. I found one more issue about using OpenGL ES in JNI but not Java code, see my answer. – John_He Dec 29 '11 at 08:47
0

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
John_He
  • 3
  • 1
  • 3