好的,只有一些背景知识...我有一个功能齐全的游戏,该游戏使用画布进行绘制,并且我正在尝试学习并将绘制转换为OpenGL ES 1.0的过程。 (以保持兼容性。)

我一直试图做的事情,是找到一种将GLSurfaceView和Renderer合并到程序中的方法,以便我可以逐个切换图形。 (有很多精灵,我需要经常更新,所以我不希望我花一个月的时间来发布下一个更新。)

有人建议我在当前SurfaceView的顶部使用GLSurfaceView,虽然到达那里但遇到了障碍。

我基本上是在使用LunarLander示例,并尝试添加一个简单的OpenGL渲染器,该渲染器在正方形上绘制纹理并将其渲染。目标是在画布顶部绘制正方形。

这是 Activity 中的相关代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        setContentView(R.layout.lunar_layout);


        LinearLayout detailListView = (LinearLayout) findViewById(R.id.dets); // Find the layout where you want to add button

    Button button = new Button(this);
    button.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
    detailListView.addView(button);//add view to add



    mGLSV = new BasicGLSurfaceView(this);
    mGLSV.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
    detailListView.addView(mGLSV);

    //clipped

lunar_layout文件如下所示:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <com.example.android.lunarlander.LunarView
        android:id="@+id/lunar"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

    <LinearLayout
       android:id="@+id/dets"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true"
       android:layout_marginLeft="0dp"
       android:layout_marginTop="50dp"
       android:orientation="vertical" >

         <com.google.ads.AdView
             android:id="@+id/adView"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             ads:adSize="BANNER"
             ads:adUnitId="a14ecf8f2d9ef49"
             ads:loadAdOnCreate="true" >
         </com.google.ads.AdView>

    </LinearLayout>



     <RelativeLayout
         android:id="@+id/relLayout"
         android:layout_width="match_parent"
         android:layout_height="match_parent" >

         <TextView
             android:id="@+id/text"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerInParent="true"
             android:gravity="center_horizontal"
             android:text="@string/lunar_layout_text_text"
             android:textColor="#88ffffff"
             android:textSize="24sp"
             android:visibility="visible" />

      </RelativeLayout>

</FrameLayout>

很简单,我正在尝试使用LinearLayout在Surface视图的顶部添加内容。注意,我还添加了一个草稿按钮,仅用于检查概念是否正常。
我最终得到的是画布,并且按钮正确显示,但是没有GLSurfaceView的迹象。我究竟做错了什么?

最佳答案

我建议您已经将BasicGLSurfaceView以XML添加到框架布局中,并使用findViewById(R.id.basicGLSurfaceView1)来保存它,而不是尝试以编程方式创建它:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.android.lunarlander.BasicGLSurfaceView
        android:id="@+id/glSurfaceView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.example.android.lunarlander.BasicGLSurfaceView>

    <com.example.android.lunarlander.LunarView
      android:id="@+id/lunar"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

    <LinearLayout
       android:id="@+id/dets"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true"
       android:layout_marginLeft="0dp"
       android:layout_marginTop="50dp"
       android:orientation="vertical" >

         <com.google.ads.AdView
             android:id="@+id/adView"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             ads:adSize="BANNER"
             ads:adUnitId="a14ecf8f2d9ef49"
             ads:loadAdOnCreate="true" >
         </com.google.ads.AdView>

    </LinearLayout>



     <RelativeLayout
         android:id="@+id/relLayout"
         android:layout_width="match_parent"
         android:layout_height="match_parent" >

         <TextView
             android:id="@+id/text"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerInParent="true"
             android:gravity="center_horizontal"
             android:text="@string/lunar_layout_text_text"
             android:textColor="#88ffffff"
             android:textSize="24sp"
             android:visibility="visible" />

      </RelativeLayout>

</FrameLayout>

您需要告诉GLSurfaceView开始渲染。 GLturfaceView中的You have to call (or even override) GLSurfaceView.start()。另外,setRenderer是公共的,因此,如果您在Activity中调用mGLSV.setRenderer(myRenderer);,而myRenderer是MyRenderer implements GLSurfaceView.Renderer的实例,则它将自动启动渲染,而无需您触摸start()
编辑:实际上,当布局视图时(即当您将start()或将其添加到您设置为内容视图的任何布局中时)调用setContentView(),而不是在调用setRenderer时调用

这听起来像是从头开始的,但是如果您只想以标准的android / opengl-es方式渲染某些东西,则实现Renderer是可以的,而如果您实际上想深入了解并编辑android渲染的方式,则可以扩展GLSurfaceView。实际上,我建议不要完全扩展GLSurfaceView,而应在GLSurfaceView.Renderer中进行尽可能多的扩展。我没有对GLSurfaceView进行扩展就对整个3D游戏进行了编程。我的代码如下所示:
public class stackoverflowTest extends Activity {

MyGLSurfaceView glSurface;
MyRenderer myRenderer;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    myRenderer = new MyRenderer();

    //Get hold of our GLSurfaceView
    glSurface = (MyGLSurfaceView) findViewById(R.id.graphics_glsurfaceview1);
    // optional
    glSurface.setEGLConfigChooser(true);

    glSurface.setRenderer(myRenderer);
    //Set the renderer. setRenderer will call start() and start the GL thread, which then calls onSurfaceCreated, onDraw etc in the Renderer



}

    @Override
public void onPause(){
    super.onPause();
     glSurface.onPause();
}

@Override
public void onResume() {
       super.onResume();
       glSurface.onResume();
    }
}

}

渲染器:
public class MyRenderer implements Renderer {

@Override
public void onDrawFrame(GL10 gl) {
    // openGL drawing code goes here


}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {

    // etc


}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // etc


}

}

GLSurfaceView:
public class MyGLSurfaceView extends GLSurfaceView {

public MyGLSurfaceView(Context context) {
    super(context);

}

public MyGLSurfaceView(Context context, AttributeSet attribs) {
    super(context, attribs);

}

@Override
public void onPause(){
    super.onPause();
}

@Override
public void onResume(){
    super.onResume();
}

}

如果您可以升级到Android 2.1(opengl-es 1.1),则可以使用点精灵,而不是渲染带纹理的四边形或其他任何东西

09-10 02:53
查看更多