Maxim Shoustin
Maxim Shoustin

Reputation: 77904

Draw line on background image, OpenGL Android

I have class that draws white line:

public class Line {

    //private FloatBuffer vertexBuffer;
    private FloatBuffer frameVertices;

    ByteBuffer diagIndices;


    float[] vertices = {
            -0.5f, -0.5f, 0.0f,
            -0.5f, 0.5f,  0.0f
    };

    public Line(GL10 gl) {
        // a float has 4 bytes so we allocate for each coordinate 4 bytes
        ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
        vertexByteBuffer.order(ByteOrder.nativeOrder());

        // allocates the memory from the byte buffer
        frameVertices = vertexByteBuffer.asFloatBuffer();

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

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

    }


    /** The draw method for the triangle with the GL context */
    public void draw(GL10 gl) {

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, frameVertices);
        gl.glColor4f(1.0f, 1.0f, 1.0f, 1f);

        gl.glDrawArrays(GL10.GL_LINE_LOOP , 0, vertices.length / 3);
        gl.glLineWidth(5.0f);

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }
}

It works fine. The problem is: When I add BG image, I don't see the line

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        glView = new GLSurfaceView(this);           // Allocate a GLSurfaceView
        //glView.setEGLContextClientVersion(1);
        glView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        glView.setRenderer(new mainRenderer(this)); // Use a custom renderer

        glView.setBackgroundResource(R.drawable.bg_day); // <- BG overlaps my line
        glView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        glView.getHolder().setFormat(PixelFormat.TRANSLUCENT);  

        this.setContentView(glView);   // This activity sets to GLSurfaceView
     }

How to get rid of that?

I'm sure it should be something simple.

Thanks,

Upvotes: 0

Views: 705

Answers (1)

Stefan Hanke
Stefan Hanke

Reputation: 3518

You need to place the GLSurfaceView on top of the activities window. See setZOrderOnTop. Should be as simple as

    glView = new GLSurfaceView(this);           // Allocate a GLSurfaceView
    glView.setZOrderOnTop(true);

Control whether the surface view's surface is placed on top of its window. Normally it is placed behind the window, to allow it to (for the most part) appear to composite with the views in the hierarchy. By setting this, you cause it to be placed above the window. This means that none of the contents of the window this SurfaceView is in will be visible on top of its surface.

Related questions include: Android GLSurfaceView with drawable background

Upvotes: 1

Related Questions