某些设备上的 Android 'white box' 纹理 OpenGL ES 1.0

标签 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 和摩托罗拉 Droid Razr 纹理渲染良好。但在三星 Galaxy Nexus 上,我只看到“白框”。

我曾尝试将纹理大小更改为 2 的幂 (512 x 512),但无济于事。

最佳答案

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

关于某些设备上的 Android 'white box' 纹理 OpenGL ES 1.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28196139/

相关文章:

android - 将 OpenGL 应用程序移植到 NDK。着色器效果看起来不同

javascript - 合并网格上的均匀纹理

android - Google OAuth2 的客户端密码到底是什么?

java - 我的应用程序崩溃致命异常无法添加窗口

android - 切换到 OpenGL 会提高渲染速度吗?

opengl - 线性/非线性纹理映射扭曲的四边形

android - Libav 和网络摄像头捕获

java - 如何在 Android 中从 sqlite 数据库存储和检索日期?

iphone - Open GL 是 2d 纸牌游戏的矫枉过正吗?