Android'白盒'在某些设备上纹理OpenGL ES 1.0

时间:2015-01-28 15:31:39

标签: android opengl-es texture-mapping texture2d render-to-texture

我有下一个问题。 我写了一些使用opengl es 1.0的android应用程序 我正在使用GLSurfaceView和自定义渲染器。以下是我的自定义渲染器 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);

}

我的Square类渲染纹理:

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

所以,接下来的问题是:在三星S5上,阿尔卡特One Touch 4033D和Motorolla Droid Razr纹理都很好。但在三星Galaxy Nexus上,我只看到了白盒'。

我试图将纹理的大小更改为2(512 x 512)的幂,但它没有帮助。

1 个答案:

答案 0 :(得分:2)

在Square类中添加函数:

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

当你生成纹理时,添加:

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

取而代之的是:

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