如何通过触摸屏输入修改/重新绘制 Opengl 形状?
我当前的尝试监听触摸事件,获取 x 和 y 值,我想用它们来根据 XY 平面中的触摸屏输入重写形状矩阵中的顶点:
编辑:我已经能够读取触摸事件并更新形状矩阵的条目verticies
但是,每当我运行程序时,它都会启动,但是一旦我输入触摸事件就会崩溃;
这是我更新的代码:
public class MainActivity extends Activity{
float x;
float y;
public float verticies[]={3,4,0,3,-4,0,-3,-4,0,-3,4,0};
public FloatBuffer vertBuff;
GLSurfaceView mysurface;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mysurface = new GLSurfaceView(this);
mysurface.setRenderer(new MyRenderer());
setContentView(mysurface);
mysurface.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
@Override
public boolean onTouchEvent(MotionEvent e) {
// MotionEvent reports input details from the touch screen
// and other input controls. In this case, you are only
// interested in events where the touch position changed.
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
x = e.getX()/100;
y = e.getY()/100;
verticies[0]=x;
verticies[1]=y;
vertBuff.put(verticies);
mysurface.requestRender();
break;
}
我猜它要么 OnDrawframe 不喜欢我更新顶点输入,要么缓冲区重新刷新存在一些不规则,无论哪种方式我都会收到空指针异常错误:
07-05 19:52:12.067: W/dalvikvm(633): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
07-05 19:52:12.087: E/AndroidRuntime(633): FATAL EXCEPTION: main
07-05 19:52:12.087: E/AndroidRuntime(633): java.lang.NullPointerException
07-05 19:52:12.087: E/AndroidRuntime(633): at com.example.touch_test.MainActivity.onTouchEvent(MainActivity.java:57)
07-05 19:52:12.087: E/AndroidRuntime(633): at android.app.Activity.dispatchTouchEvent(Activity.java:2367)
07-05 19:52:12.087: E/AndroidRuntime(633): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1840)
07-05 19:52:12.087: E/AndroidRuntime(633): at android.view.View.dispatchPointerEvent(View.java:5662)
07-05 19:52:12.087: E/AndroidRuntime(633): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2863)
07-05 19:52:12.087: E/AndroidRuntime(633): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-05 19:52:12.087: E/AndroidRuntime(633): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 19:52:12.087: E/AndroidRuntime(633): at android.os.Looper.loop(Looper.java:137)
07-05 19:52:12.087: E/AndroidRuntime(633): at android.app.ActivityThread.main(ActivityThread.java:4340)
07-05 19:52:12.087: E/AndroidRuntime(633): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 19:52:12.087: E/AndroidRuntime(633): at java.lang.reflect.Method.invoke(Method.java:511)
07-05 19:52:12.087: E/AndroidRuntime(633): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-05 19:52:12.087: E/AndroidRuntime(633): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-05 19:52:12.087: E/AndroidRuntime(633): at dalvik.system.NativeStart.main(Native Method)
我应该如何进行?谢谢