2011-08-11 14 views
3

は、私はこれらのボタンのそれぞれは、レンダリングされたビューと対話することができるようにRelativeLayoutにいくつかのボタンの横のレンダラを使用してのOpenGLビュー(GLSurfaceView)を持つように探しています。ボタン、OpenGLの、およびレンダラ - Androidの

は私が1 GLSurfaceViewと押されたときには、ランダムなソリッドカラーにGLSurfaceViewを変更することをその下に一つのボタンを持っているのは、このRelativeLayoutで言ってみましょう。私はOpenGLを描画する方法を理解していますが、レンダラーの外からレンダラーとやり取りして、ビュー自体のタッチイベントに関連付けられていないユーザー入力によってサーフェスを変更できるようにする方法がわかります。私の研究から

、私はいくつかの並べ替えのスレッドを必要とするだろうと推測している、と私は方法、queueEvent(Runnableを)を使用する必要があるかもしれません。どこから行くのかわからない。

XML(main.xml)

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/RL1" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 

<android.opengl.GLSurfaceView 
    android:id="@+id/GLView" 
    android:layout_width="200dip" 
    android:layout_height="200dip" 
    android:layout_centerHorizontal="true" 

    /> 


<Button 
    android:id="@+id/Button1" 
    android:layout_width="150dip" 
    android:layout_height="100dip" 
    android:text="Click." 
    android:layout_below="@id/GLView" 
    android:layout_centerHorizontal="true" 
    /> 

活動(Basic.java)

import android.app.Activity; 
import android.opengl.*; 
import android.os.Bundle; 


public class Basic extends Activity { 
/** Called when the activity is first created. */ 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    GLSurfaceView glView = (GLSurfaceView)findViewById(R.id.GLView); 
    glView.setRenderer(new BasicRenderer(this)); 


} 
} 

レンダラ(OpenGLRenderer.java)私はこれを知っている

import javax.microedition.khronos.egl.EGLConfig; 
import javax.microedition.khronos.opengles.GL10; 

import android.content.Context; 
import android.opengl.GLU; 
import android.opengl.GLSurfaceView.Renderer; 

public class BasicRenderer implements Renderer { 

private Context mContext; 
private float mWidth, mHeight; 
public BasicRenderer(Context context){ 
    mContext=context; 
} 

@Override 
public void onDrawFrame(GL10 gl) { 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | 
      GL10.GL_DEPTH_BUFFER_BIT); 



} 

@Override 
public void onSurfaceChanged(GL10 gl, int width, int height) { 
    mWidth = (float)width; 
    mHeight = (float)height; 

    gl.glViewport(0, 0, width, height); 

    gl.glMatrixMode(GL10.GL_PROJECTION); 

    gl.glLoadIdentity(); 

    GLU.gluPerspective(gl, 45.0f, 
           (float) width/(float) height, 
           0.1f, 100.0f); 

    gl.glMatrixMode(GL10.GL_MODELVIEW); 

    gl.glLoadIdentity(); 

} 

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

    // Set the background color to white 
    gl.glClearColor(1f, 1f, 1f, 0.5f); 

    gl.glShadeModel(GL10.GL_SMOOTH); 

    gl.glClearDepthf(1.0f); 

    gl.glEnable(GL10.GL_DEPTH_TEST); 

    gl.glDepthFunc(GL10.GL_LEQUAL); 

    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, 
         GL10.GL_NICEST); 

} 

} 

答えて

3

古い質問ですが、いくつかの投票がありますレンダラにスレッド間でやりとりするために使用するトリックは、レンダラオブジェクトをアクティビティの変数として保持し、レンダラに描画するときにレンダラにキューを入れることです。次のフレーム(onDrawの次の呼び出し):

public class Basic extends Activity { 
/** Called when the activity is first created. */ 

BasicRenderer myRenderer; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    myRenderer = new BasicRenderer(this); 

    GLSurfaceView glView = (GLSurfaceView)findViewById(R.id.GLView); 
    glView.setRenderer(myRenderer); 


    myRenderer.queueDoSomethingNextTick(BasicRenderer.DO_THIS); 

    findViewById(R.id.Button1).setOnTocuhListener(new OnTouchListener(){ 

     public void onTouch(MotionEvent event){ 
      myRenderer.queueDoSomethingNextTick(BasicRenderer.DO_THAT); 
      // Compiler might complain about myRenderer not being final 
     } 


    }); 


} 
} 


public class BasicRenderer implements Renderer { 

    private Context mContext; 
    private float mWidth, mHeight; 


    private int command; 
    public static final DO_THIS = 1; 
    public static final DO_THAT = 2; 

public BasicRenderer(Context context){ 
    mContext=context; 
} 

@Override 
public void onDrawFrame(GL10 gl) { 

    if(command==DO_THIS){ 
     //doThis(); 
    } else if(command==DO_THAT){ 
     //doThat(); 
    } 

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | 
     GL10.GL_DEPTH_BUFFER_BIT); 

    gl.glDrawAwesomeness(); 


} 

public void queueDoSomethingNextTick(int command){ 

    this.command = command; 

} 

} 

上記の例では、コマンドを配列に変更してループすることができます。ゲームは、それを必要とするラウンドなったとき、それはメモリに既にあるし、そこにあるように、私は実際には、モデルが描くためにどの私のレンダラを伝える、または事前に大規模なモデルやテクスチャのロードを開始するために私のレンダラを伝えるために、このアプローチを使用しますその前にそれをロードするの遅れは、あなたが)(onsurfaceCreatedに入れなければならない何

+0

が描かれていますか? – NovusMobile

関連する問題