我正在寻找一个 OpenGL 视图(GLSurfaceView),它使用渲染器以及相对布局中的一些按钮,以便这些按钮中的每一个都可以与渲染视图交互。
假设在这个 RelativeLayout 中,我有一个 GLSurfaceView 和它下面的一个按钮,按下时会将 GLSurfaceView 更改为随机纯色。我了解如何在 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);
}
}