Android 'white box' textures OpenGL ES 1.0

2019-08-10 07:55发布

问题:

I have next problem. I'm write some android app which uses opengl es 1.0 I'm using GLSurfaceView and custom Renderer. Here are methods of my custom renderer onDrawFrame, onSurfaceChanged, onSurfaceChanged:

@Override
public void onDrawFrame(GL10 gl) {

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    // clear Screen and Depth Buffer
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    // Reset the Modelview Matrix
    gl.glLoadIdentity();

    // Drawing
    gl.glTranslatef(0f, 0f, -3.0f);     // move 5 units INTO the screen
                                            // is the same as moving the camera 5 units away
    //      gl.glScalef(0.5f, 0.5f, 0.5f);          // scale the square to 50%
                                            // otherwise it will be too large
    square.draw(gl, mScrollable.getIndexA(), mScrollable.getIndexB(), mScrollable.getAlphaValue());            // Draw the triangle
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    if(height == 0) {                       //Prevent A Divide By Zero By
        height = 1;                         //Making Height Equal One
    }

    gl.glViewport(0, 0, mWidth, mHeight);   //Reset The Current Viewport
    gl.glMatrixMode(GL10.GL_PROJECTION);    //Select The Projection Matrix
    gl.glLoadIdentity();                    //Reset The Projection Matrix

    //Calculate The Aspect Ratio Of The Window
    GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);

    gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select The Modelview Matrix
    gl.glLoadIdentity();                    //Reset The Modelview Matrix
}


@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // Load the texture for the square
    square.loadGLTexture(gl, this.context);

    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    gl.glEnable(GL10.GL_TEXTURE_2D);            //Enable Texture Mapping ( NEW )
    gl.glShadeModel(GL10.GL_SMOOTH);            //Enable Smooth Shading
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);    //Black Background
    gl.glClearDepthf(1.0f);                     //Depth Buffer Setup
    gl.glEnable(GL10.GL_DEPTH_TEST);            //Enables Depth Testing
    gl.glDepthFunc(GL10.GL_LEQUAL);             //The Type Of Depth Testing To Do

    //Really Nice Perspective Calculations
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);

}

And my Square class to render textures:

private int[] textures;
private GL10 mGl;
private FloatBuffer vertexBuffer;   // buffer holding the vertices
private float vertices[] = {
        -1.0f, -1.8f,  0.0f,        // V1 - bottom left
        -1.0f,  1.3f,  0.0f,        // V2 - top left
         1.0f, -1.8f,  0.0f,        // V3 - bottom right
         1.0f,  1.3f,  0.0f         // V4 - top right
};

private FloatBuffer textureBuffer;  // buffer holding the texture coordinates
private float texture[] = {         
        // Mapping coordinates for the vertices
        0.0f, 1.0f,     // top left     (V2)
        0.0f, 0.0f,     // bottom left  (V1)
        1.0f, 1.0f,     // top right    (V4)
        1.0f, 0.0f      // bottom right (V3)
};
private GlAnimationCache cacheHelper;

public void loadGLTexture(GL10 gl, Context context) {
    // loading texture
    mGl = gl;
    gl.glGenTextures(mImageEntityList.size(), textures, 0);

    cacheHelper = new GlAnimationCache(mContext, mImageEntityList, mGlRenderer, mGlSurfaceView);
    cacheHelper.initCache(); //it init cache in background thread
}   

public void removeTextureAt(int position){
    mGl.glDeleteTextures(1, textures, position);
}

public void bindTextureAt(final Bitmap bitmap, int position){

    // generate one texture poi, 0);
    // ...and bind it to our array
    mGl.glBindTexture(GL10.GL_TEXTURE_2D, textures[position]);


    mGl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
    mGl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

    // create nearest filtered texture
    mGl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    mGl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

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

    // Use Android GLUtils to specify a two-dimensional texture image from our bitmap
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLUtils.getInternalFormat(bitmap), bitmap, GLUtils.getType(bitmap), 0);
    bitmap.recycle();
}

private int prevIndex = 0;

/** The draw method for the square with the GL context */
public void draw(GL10 gl, int indexA, int indexB, float alpha) {
    // bind the previously generated texture

    if(prevIndex != indexA){
        cacheHelper.synchronizeCache(indexA);
        prevIndex = indexA;
    }
    // Point to our buffers
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[indexA]);

    // Set the face rotation
    gl.glFrontFace(GL10.GL_CW);

    // Point to our vertex buffer
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

    gl.glColor4f(1, 1, 1, 1f);

    // Draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);



    gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[indexB]);

    // Set the face rotation
    gl.glFrontFace(GL10.GL_CW);

    // Point to our vertex buffer
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

    gl.glColor4f(1, 1, 1, Math.abs(alpha));

    // Draw the vertices as triangle strip
    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

    //Disable the client state before leaving
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}

So, the problem is next: On Samsung S5, Alcatel One Touch 4033D and Motorolla Droid Razr textures are rendered fine. But on Samsung Galaxy Nexus I see only 'white box'.

I have tried to change size of texture to power of 2(512 x 512) but it not helpes.

回答1:

In Square class add function:

    public boolean isNPOTSupported(GL10 gl) {
        String extensions = gl.glGetString(GL10.GL_EXTENSIONS);
        return extensions.indexOf("GL_OES_texture_npot") != -1;
    }

And when you Generate textures, add this:

        final boolean isNPOTSupported = isNPOTSupported(gl);
        if(isNPOTSupported) {
            gl.glGenTextures(mImageEntityList.size(), textures, 0);
        }else{
            for(int i = 0; i < mImageEntityList.size(); i++){
                gl.glGenTextures(1, textures, i);
            }
        }

Instead this:

gl.glGenTextures(mImageEntityList.size(), textures, 0);