Android opengl将对象移动到触摸屏上
首先,我对Android上的opengl非常陌生,而且我还在网上浏览课程 我试图在屏幕中间做一个简单的应用程序。 触摸时,它会移动到触摸事件坐标 这是我确定的观点Android opengl将对象移动到触摸屏上,android,opengl-es,touch-event,Android,Opengl Es,Touch Event,首先,我对Android上的opengl非常陌生,而且我还在网上浏览课程 我试图在屏幕中间做一个简单的应用程序。 触摸时,它会移动到触摸事件坐标 这是我确定的观点 public class MyGLSurfaceView extends GLSurfaceView { private final MyGLRenderer mRenderer; public MyGLSurfaceView(Context context) { super(context); // Set t
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;
}
}当问问题时,你应该加上你当前的结果和你预期的结果 只要看一眼你的代码,我就知道正方形是正确绘制的,直到你触摸屏幕,然后它就完全消失了(除非你按下屏幕左上角的部分) 如果是这种情况,您的问题只是没有将触摸坐标转换为openGL坐标系。默认情况下,openGL坐标系在所有轴的范围
[-1,1]
,但您可以使用矩阵对其进行更改。最常见的两种是glFrustum
和glOrtho
这两种都接受4个边界坐标,分别为左、右、下和上,表示视图对应边界处的值
例如,要通过触摸计算x,您首先要对其进行规格化,以查看您通过relativeX=touch.x/view.size.width
按下屏幕的哪个部分,然后这将在openGL中显示一个坐标,这样您就可以执行glX=left+(right-left)*relativeX
。垂直坐标也类似
但在您的情况下,在使用视图坐标时,最好使用2D并使用
glOrtho
。这意味着用这个调用替换平截头体调用,并设置左=0.0
,右=视图宽度
,顶部=0.0
,底部=视图高度
。现在,openGL中的坐标系与视图中的坐标系相同。您需要增加正方形的大小才能看到它,因为此时它非常小。另外,您应该删除lookAt
,只使用identity+translate。很抱歉没有写我的结果,我没有注意到