삼각형 움직이기
Android에서 OpenGL ES 2.0으로 객체를 터치한 만큼 옮겨본다.
Last updated
public class MyGLSurfaceView extends GLSurfaceView {
...
private float mPreviousX;
private float mPreviousY;
@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.
float x = e.getX();
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
float dx = mPreviousX - x;
float dy = mPreviousY - y;
mRenderer.translate(dx, dy, 0f); // translate
requestRender();
}
mPreviousX = x;
mPreviousY = y;
return true;
}
}public class MyGLRenderer implements GLSurfaceView.Renderer {
...
private final float[] mTranslateMatrix = new float[16];
private int screenHeight;
private int screenWidth;
@Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
...
Matrix.setIdentityM(mTranslateMatrix, 0);
}
@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
this.screenWidth = width;
this.screenHeight = height;
...
}
@Override
public void onDrawFrame(GL10 unused) {
float[] scratch = new float[16];
...
// Combine the translated matrix with the projection and camera view
Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mTranslateMatrix, 0);
// Draw triangle
mTriangle.draw(scratch);
}
public void translate(float dx, float dy, float dz) {
Matrix.translateM(mTranslateMatrix, 0,
dx * 2f / screenHeight,
dy * 2f / screenHeight,
dz * 2f / screenHeight);
}
}