Android opengl 移动对象 Ontouch

Android opengl moving an object Ontouch

首先,我对 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;
}

}

这是我的渲染器class

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

}

在提问时,您应该添加您当前的结果是什么以及您期望的结果是什么。

只要看一眼你的代码,我就会认为正方形是正确绘制的,直到你触摸屏幕,然后它才完全消失(除非你实际上按下了屏幕的左上角)。

如果是这种情况,您的问题只是您没有将触摸坐标转换为 openGL 坐标系。默认情况下,您的 openGL 坐标系在所有轴的 [-1,1] 范围内,但您可以使用矩阵更改它(如您所做的那样)。最常见的 2 个是 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.