gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:03:23 26 4
gpt4 key购买 nike

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

我应该如何进行?谢谢

最佳答案

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

关于java - 如何通过触摸屏输入修改 Opengl 形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11314592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com