2016-02-14 43 views
0

首先我是真正的新的OpenGL上androidand我還在尋找,雖然網上課程Android的OpenGL的移動對象Ontouch

我試圖做一個簡單的appthat在屏幕 中間的sequre和觸摸它移動到觸摸事件的座標 這是我的看法sureface

public class MyGLSurfaceView extends GLSurfaceView { 

private final MyGLRenderer mRenderer; 

public MyGLSurfaceView(Context context) { 
    super(context); 

    // Set the Renderer for drawing on the GLSurfaceView 
    mRenderer = new MyGLRenderer(); 
    setRenderer(mRenderer); 

    // Render the view only when there is a change in the drawing data 
    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); 
} 


@Override 
public boolean onTouchEvent(MotionEvent e) 
{ 
    float x =e.getX(); 
    float y =e.getY(); 

    if(e.getAction()==MotionEvent.ACTION_MOVE) 
    { 
     mRenderer.setLoc(x,y); 
     requestRender(); 
    } 
    return true; 
} 

}

,這是我的渲染器類

public class MyGLRenderer implements GLSurfaceView.Renderer { 

private float mAngle; 

public PVector pos; 
public Rectangle r; 
public Square sq; 

@Override 
public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
    // Set the background frame color 
    gl.glClearColor(1f, 1f, 1f, 1.0f); 

    pos=new PVector(); 
    r=new Rectangle(0.5f,0.4f); 
    sq=new Square(0.3f); 
} 

@Override 
public void onDrawFrame(GL10 gl) { 

    // Draw background color 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

    // Set GL_MODELVIEW transformation mode 
    gl.glMatrixMode(GL10.GL_MODELVIEW); 
    gl.glLoadIdentity(); // reset the matrix to its default state 

    // When using GL_MODELVIEW, you must set the view point 
    GLU.gluLookAt(gl, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); 

    gl.glTranslatef(pos.x,pos.y,0f); 

    //r.draw(gl); 
    sq.draw(gl); 

}//rend 

@Override 
public void onSurfaceChanged(GL10 gl, int width, int height) { 
    // Adjust the viewport based on geometry changes 
    // such as screen rotations 
    gl.glViewport(0, 0, width, height); 

    // make adjustments for screen ratio 
    float ratio = (float) width/height; 
    gl.glMatrixMode(GL10.GL_PROJECTION);  // set matrix to projection mode 
    gl.glLoadIdentity();      // reset the matrix to its default state 
    gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7); // apply the projection matrix 
} 

/** 
* Returns the rotation angle of the triangle shape (mTriangle). 
* 
* @return - A float representing the rotation angle. 
*/ 
public float getAngle() { 
    return mAngle; 
} 

/** 
* Sets the rotation angle of the triangle shape (mTriangle). 
*/ 
public void setAngle(float angle) { 
    mAngle = angle; 
} 

public void setLoc(float x,float y) 
{ 
    pos.x=x; pos.y=y; 
} 

}

回答

0

當問一個問題,你應該添加什麼是您目前的結果,什麼是你期望的結果。

從簡短的一瞥你的代碼,我會期待廣場被正確繪製,直到你觸摸屏幕後,它完全消失(除非你實際上按下屏幕的左上角部分)。

如果是這種情況,您的問題只是您沒有將觸摸座標轉換爲openGL座標系。您在所有座標軸中默認打開GL座標系的範圍爲[-1,1],但您可以使用矩陣更改它(與您一樣)。最常見的兩種是glFrustumglOrtho,它們都接受左邊,右邊,底部和頂部的4個邊界座標,這些邊界座標表示在視圖對應邊界處的值。

所以要計算x從觸摸比如你首先正常化它來看看你的relativeX = touch.x/view.size.width壓制,那麼這將出現在OpenGL的座標,這樣你做glX = left + (right-left)*relativeX屏幕的一部分。類似的是垂直座標。

但在你的情況下,使用2D會更好,並使用glOrtho,同時使用視圖座標。這意味着用這個替換平頭電話並且設置left=.0,right=viewWidth,top=.0,bottom=viewHeight。現在,您將在OpenGL中擁有與視圖中相同的座標系。您需要增加方形尺寸才能看到它,因爲此時它非常小。此外,你應該刪除lookAt,只使用身份+翻譯。

+0

對於不寫我的結果感到抱歉,沒有noticic –