0

如何通过触摸屏输入修改/重新绘制 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)

我应该如何进行?谢谢

4

2 回答 2

3

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

于 2012-07-03T15:52:36.597 回答
1

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

于 2012-10-10T00:09:43.093 回答