Android opengl移动对象Ontouch

时间:2016-02-14 19:32:37

标签: android opengl-es ontouchevent

首先,我对Android上的opengl真的很新,我仍在寻找在线课程

我正在尝试制作一个简单的应用程序,它在屏幕中间有一个sequre 并且在触摸时它移动到触摸事件坐标 这是我肯定的观点

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;
}

}

1 个答案:

答案 0 :(得分:0)

在提出问题时,您应该添加您当前的结果以及您的预期结果。

从简短的一瞥你的代码我会期望正确绘制正方形,直到你触摸屏幕,之后它完全消失(除非你实际按下屏幕的左上角部分)。

如果是这种情况,则问题仅在于您没有将触摸坐标转换为openGL坐标系。默认情况下,openGL坐标系在所有轴的[-1,1]范围内,但您可以使用矩阵更改它(就像您一样)。最常见的两个是glFrustumglOrtho这两个接受4个边框坐标,分别是左边,右边,下边和顶边,它们代表视图对应边框的值。

因此,为了从触摸计算x,您首先要对其进行标准化以查看您按relativeX = touch.x / view.size.width按下的屏幕的哪个部分,然后这将在openGL中显示一个坐标,以便您执行glX = left + (right-left)*relativeX。类似于垂直坐标。

但在你的情况下,最好使用2D并在使用视图坐标时使用glOrtho。这意味着用此版本取代平截头体调用并设置left=.0right=viewWidthtop=.0bottom=viewHeight。现在,您将在openGL中使用与在视图上相同的坐标系。您需要增加方块大小才能看到它,因为此时它非常小。此外,您应该删除lookAt并使用identity + translate。

相关问题