如何通過觸摸屏輸入修改/重繪Opengl形狀?如何通過觸摸屏輸入修改Opengl形狀?
我現在嘗試偵聽觸摸事件,得到x和y的值,我想用重新寫在外形矩陣頂點基於在XY平面觸摸屏輸入:
編輯: 我但是,無論何時我運行該程序,它都會啓動,但一旦輸入觸摸事件就會崩潰;但是,只要我運行該程序,它就會啓動,但一旦輸入觸摸事件,就會崩潰。
這裏是我更新的代碼:
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)
我該如何繼續? 謝謝
另外,不要忘記像RobotRock所說的那樣在更新之後再次注意'vertBuff.put(verticies);'。 – Geobits
當我嘗試將矩陣放入觸摸事件中時,我不斷收到語法錯誤,我很快就知道程序從不使用頂點信息......任何提示? – tavjas
不要在函數中聲明頂點,而是改變函數中的值。所以verticies [0] = _x和verticies [1] = _y。我猜這就是你做錯了什麼,但如果你用錯誤和變化更新你的問題,我可以更有幫助。 – RobotRock