如何通过触摸屏输入修改Opengl形状?

时间:2012-07-03 15:48:08

标签: java android opengl-es

如何通过触摸屏输入修改/重新绘制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不喜欢我更新verticies输入或者有一些不规则的缓冲区重新刷新我得到一个空指针异常错误:

    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)

我该怎么办? 感谢

2 个答案:

答案 0 :(得分:3)

看起来, verticies 在初始化时获取其* _x *和* _y *但在此之后不会更新。如果您在touchEvent中再次设置 verticies ,它可能会按照假设工作。

答案 1 :(得分:1)

事实证明,为了做到这一点,我必须重新设置并清除缓冲区 - 这样做只需在循环中使用clear buffer命令。这将允许延迟更新构成形状的顶点