如何修改通过触摸屏输入的Opengl形状? [英] How do I modify Opengl shapes via touchscreen input?

查看:141
本文介绍了如何修改通过触摸屏输入的Opengl形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样通过触摸屏输入修改/重画Opengl的形状?

How do I modify/re-draw Opengl shapes via touchscreen input?

我目前的尝试侦听触摸事件,得到x和y的值,我想用重新编写基于在XY平面触摸屏输入的形状矩阵顶点:

My current attempt listens for touch events, gets the x and y values, which I want to use to re-write a vertex in the shape matrix based on touchscreen input in XY plane:

编辑:
我已经能够同时读取触摸事件,并更新形状矩阵的输入 verticies

然而,每当我运行程序,它启动,但一旦崩溃,因为我输入的触摸事件;

however, whenever I run the program, it starts up, but crashes as soon as I input a touch event;

这是我更新code:

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输入还是存在一些违规行为与缓冲区再新鲜无论哪种方式,我得到一个空指针异常错误:

I'm guessing its either that OnDrawframe does not like me updating the verticies input or there are some irregularities with the buffer re-fresh either way I get a null pointer exception error:

    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)

我应该如何进行?
谢谢

How should I proceed? thanks

推荐答案

由于它似乎的 verticies 的获取其* _x *和* _y *在初始化但之后不更新。如果设置的 verticies 的再次的TouchEvent,它可能会作为工作设想。

As it seems, verticies gets its *_x* and *_y* at initialization but doesn't update after that. If you set verticies again in touchEvent, it will probably work as assumed.

这篇关于如何修改通过触摸屏输入的Opengl形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆