某些设备上的 Android“白盒"纹理 OpenGL ES 1.0 [英] Android 'white box' textures OpenGL ES 1.0 on some devices

查看:74
本文介绍了某些设备上的 Android“白盒"纹理 OpenGL ES 1.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个问题.我正在编写一些使用 opengl es 1.0 的 android 应用程序我正在使用 GLSurfaceView 和自定义渲染器.以下是我的自定义渲染器的方法 onDrawFrame、onSurfaceChanged、onSurfaceChanged:

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

}

和我的 Square 类来渲染纹理:

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

那么,接下来的问题是:在三星 S5 上,阿尔卡特 One Touch 4033D 和摩托罗拉 Droid Razr 纹理渲染得很好.但在三星 Galaxy Nexus 上,我只看到白框".

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

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

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

推荐答案

在 Square 类中添加函数:

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

改为:

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

这篇关于某些设备上的 Android“白盒"纹理 OpenGL ES 1.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆