java - GLSurfaceView 在 Android < 4.2 上工作,但在 > 4.2 上黑屏

标签 java android opengl-es glsurfaceview

以下代码在较旧的 Android 版本上完美运行,但在较新的版本(我猜是 4.2 及更高版本)上仅显示黑屏。 我是 OpenGL 的新手,我曾经使用 Canvas/Sur​​faceView,但现在我需要 GLSurfaceView 来提高性能,所以我从各处窃取了一些代码,但并不完全了解它的作用。我没有开始一个全新的项目,我只是将我的一些“ Canvas 代码”重写为 GLSurfaceView 循环。这就是为什么它看起来奇怪且效率低下的原因。

代码说明: “Sprite”类包含位图、xPos 和 yPos。在“defineStuff();”我将一些不同的“Sprites”附加到列表中,然后 GLSurfaceView 从列表中绘制所有内容。 这不是全部代码,我删除了很多代码以使其更清晰。

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);      //so its Fullscreen

    defineStuff();
    glSurfaceView = new GLSurfaceView(this);
    glSurfaceView.setRenderer(new GlRenderer());
    glSurfaceView.setOnTouchListener(this);
    setContentView(glSurfaceView);
}

Sprite 类:

    public class Sprite {

    private FloatBuffer vertexBuffer;   // buffer holding the vertices
    private FloatBuffer textureBuffer;  // buffer holding the texture coordinates
    private float texture[];
    private float vertices[];

    public Bitmap bmp;
    public boolean visible;
    public String name;
    public float posX;
    public float posY;


    public Sprite(String name, Bitmap bitmap, float posX, float posY, boolean visible) {

        this.name = name;
        this.visible = visible;

        setBuffers(bitmap, posX, posY);
    }

    public void setBuffers(Bitmap bitmap, float posX, float posY) {

        bmp = bitmap;
        }
        this.posX = posX;
        this.posY = posY;


        float tempVertices[] = {
                posX, posY + bitmap.getHeight(),  0.0f,                         // V1 - bottom left
                posX, posY,  0.0f,                                              // V2 - top left
                posX + bitmap.getWidth(), posY + bitmap.getHeight(),  0.0f,     // V3 - bottom right
                posX + bitmap.getWidth(), posY,  0.0f                           // V4 - top right
        };
        vertices = new float[tempVertices.length];
        System.arraycopy(tempVertices, 0, vertices, 0, tempVertices.length);

        // a float has 4 bytes so we allocate for each coordinate 4 bytes
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());

        // allocates the memory from the byte buffer
        vertexBuffer = byteBuffer.asFloatBuffer();

        // fill the vertexBuffer with the vertices
        vertexBuffer.put(vertices);

        // set the cursor position to the beginning of the buffer
        vertexBuffer.position(0);

        float tempTexture[] = {
                // 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)
        };
        texture = new float[tempTexture.length];
        System.arraycopy(tempTexture, 0, texture, 0, tempTexture.length);

        byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        textureBuffer = byteBuffer.asFloatBuffer();
        textureBuffer.put(texture);
        textureBuffer.position(0);
    }

    /** The texture pointer */
    private int[] textures = new int[1];

    public void loadGLTexture(GL10 gl) {  // every Sprite has to be loaded before being drawn

        // generate one texture pointer
        gl.glGenTextures(1, textures, 0);
        // ...and bind it to our array
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

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

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

    }

    public void draw(GL10 gl, float interpolation) {

        gl.glPushMatrix();

        gl.glTranslatef (speedInX * interpolation, speedInY * interpolation, 0f);

        // bind the previously generated texture
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        // Point to our buffers
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

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

        // 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.glPopMatrix();
    }
}

我的渲染器:

    public class GlRenderer implements Renderer {

    @Override
    public void onDrawFrame(GL10 gl) {

        // 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(0.0f, 0.0f, -5.0f);     // move 5 units INTO the screen
                                                // is the same as moving the camera 5 units away

        updateLogic(gl); //there was very much code around this but I removed it to make it more clear

        float interpolation = (float) (SystemClock.uptimeMillis() + SKIP_TICKS - nextGameTick) / SKIP_TICKS;

         spriteList.toFirst();
         while (spriteList.getObject() != null) {
             ((Sprite) spriteList.getObject()).draw(gl, interpolation);
              spriteList.next();
         }
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);                     // or some matrix uniform if using shaders
        gl.glLoadIdentity();
        gl.glOrthof(0, width, height, 0, -1, 1); // this will allow to pass vertices in 'canvas pixel' coordinates
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glDisable(GL10.GL_DITHER);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

        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);    //Set 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
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); // So alpha works, but it still doesnt work everytime :)

                     spriteList.toFirst();
         while (spriteList.getObject() != null) {
             ((Sprite) spriteList.getObject()).loadGLTexture(gl);
              spriteList.next();
         }


    }
}

最佳答案

因为你有

gl.glOrthof(0,width,height,0,-1,1);

你的代码不需要深度测试,尝试去掉

 | GL10.GL_DEPTH_BUFFER_BIT

gl.glClearDepthf(1.0f);

gl.glDepthFunc(GL10.GL_LEQUAL);

gl.glTranslatef(0.0f, 0.0f, -5.0f);

关于java - GLSurfaceView 在 Android < 4.2 上工作,但在 > 4.2 上黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18674087/

相关文章:

ios - 在许多 EAGLContext 之间切换的成本?

java - Hibernate Criteria SubQuery 大小写不敏感问题

java - 找出上述条件的正则表达式

java - Spring Boot休息 Controller : Return default Error JSON

android - 我怎么知道 OnResume 在 onCreate 之后呢?

java - 如何在 Android 5.0 及更高版本中以编程方式设置 wifi 代理主机名、端口和旁路

java - 在 Android 和 OpenGL ES 中将字符串渲染为纹理

opengl-es - OpenGL着色器无法绑定(bind)属性

javascript - 为 JS 和 Java 编写代码

android - 三星的 Zirconia 许可检查器出错