Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL view inside a layout

How do I set up an xml layout where an OpenGL view is part of it? As I do now is set the OpenGL view as the only view with setContentView(). But I would like to create an xml layout that includes the OpenGL view. Lets say I want to have the OpenGL view mainly and a small TextView at the bottom.

Is this even possible? Or can an OpenGL view only be the one and only view?

like image 270
Espen Avatar asked Nov 08 '10 17:11

Espen


3 Answers

This is what I did for my Particle Emitter: extend a GLSurfaceView and make it part of my layout. Note: implement the "ParticleRenderer" class to achieve whatever openGL stuff you want to do

My Custom view:

public class OpenGLView extends GLSurfaceView
{

    //programmatic instantiation
    public OpenGLView(Context context)
    {
        this(context, null);
    }

    //XML inflation/instantiation
    public OpenGLView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public OpenGLView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs);

        // Tell EGL to use a ES 2.0 Context
        setEGLContextClientVersion(2);

        // Set the renderer
        setRenderer(new ParticleRenderer(context));
    }

}

and in the layout...

<com.hello.glworld.particlesystem.OpenGLView
    android:id="@+id/visualizer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

The particle Render is straight forward... for some sample code, see: https://code.google.com/p/opengles-book-samples/source/browse/trunk/Android/Ch13_ParticleSystem/src/com/openglesbook/particlesystem/ParticleSystemRenderer.java

public class ParticleRenderer implements GLSurfaceView.Renderer
{
    public ParticleRenderer(Context context)
    {
        mContext = context;
    }

    @Override
    public void onDrawFrame(GL10 gl)
    {
        //DO STUFF
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height)
    {
        //DO STUFF
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config)
    {
        //DO STUFF
    }
}
like image 152
Someone Somewhere Avatar answered Nov 14 '22 05:11

Someone Somewhere


You might look at SurfaceView. It provides a dedicated drawing surface embedded inside of a view hierarchy. See also drawing with canvas.

like image 7
Cheryl Simon Avatar answered Nov 14 '22 05:11

Cheryl Simon


Create a LinearLayout inside your xml file. Then in the activity, use findViewById() to get the layout and use addView() to add the OpenGL SurfaceView in your layout:

LinearLayout l = (LinearLayout) findViewById(R.id.MyLinearLayout);  
GLSurfaceView s = new GLSurfaceView(this);
s.setRenderer(myGLRenderer);

//to add the view with your own parameters
l.addView(s, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

//or simply use
l.addView(s,0);
like image 3
Rikki Avatar answered Nov 14 '22 04:11

Rikki