Question

Following code works perfectly on older Android versions, but only shows a black screen on newer versions (I guess 4.2 and higher). Im very new to OpenGL and I used to work with Canvas/SurfaceView, but now I need GLSurfaceView for the performance, so I leeched some code from here and there without exactly knowing what it does. I didnt start a whole new project, I just rewrote some of my "Canvas-Code" to a GLSurfaceView loop. Thats why it might look strange and inefficient.

Explanation for the code: The class "Sprite" contains a Bitmap, xPos and yPos. In "defineStuff();" I append a few different "Sprites" to a List and the GLSurfaceView draws everything from the List. This is not the whole code, I removed pretty much to make it more clear.

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

The Sprite class:

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

My renderer:

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


    }
}
Was it helpful?

Solution

brecause you have

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

your code doesnt require depth testing, try getting rid of

 | GL10.GL_DEPTH_BUFFER_BIT

gl.glClearDepthf(1.0f);

gl.glDepthFunc(GL10.GL_LEQUAL);

gl.glTranslatef(0.0f, 0.0f, -5.0f);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top