화면에 객체 그리기는 매우 기본적인 OpenGL 기능이지만 다른
Android 그래픽 프레임워크 클래스(Canvas
및
Drawable
객체. OpenGL ES는
그린 객체를 3D로 이동 및 변환하거나 다른 독특한 방법으로
몰입도 높은 사용자 경험을 제공합니다.
이 강의에서는 OpenGL ES 사용을 한 단계 더 발전시켜 모션을 추가하는 방법을 알아봅니다. 도형을 만들 수 있습니다.
도형 회전
OpenGL ES 2.0으로 그림 객체를 회전하는 것은 비교적 간단합니다. 렌더기에서 다른 변환 행렬 (회전 행렬)을 만든 다음 투영과 결합하기만 하면 됩니다. 카메라 뷰 변환 행렬:
private val rotationMatrix = FloatArray(16)
override fun onDrawFrame(gl: GL10) {
val scratch = FloatArray(16)
...
// Create a rotation transformation for the triangle
val time = SystemClock.uptimeMillis() % 4000L
val angle = 0.090f * time.toInt()
Matrix.setRotateM(rotationMatrix, 0, angle, 0f, 0f, -1.0f)
// Combine the rotation matrix with the projection and camera view
// Note that the vPMatrix factor *must be first* in order
// for the matrix multiplication product to be correct.
Matrix.multiplyMM(scratch, 0, vPMatrix, 0, rotationMatrix, 0)
// Draw triangle
mTriangle.draw(scratch)
}
private float[] rotationMatrix = new float[16];
@Override
public void onDrawFrame(GL10 gl) {
float[] scratch = new float[16];
...
// Create a rotation transformation for the triangle
long time = SystemClock.uptimeMillis() % 4000L;
float angle = 0.090f * ((int) time);
Matrix.setRotateM(rotationMatrix, 0, angle, 0, 0, -1.0f);
// Combine the rotation matrix with the projection and camera view
// Note that the vPMatrix factor *must be first* in order
// for the matrix multiplication product to be correct.
Matrix.multiplyMM(scratch, 0, vPMatrix, 0, rotationMatrix, 0);
// Draw triangle
mTriangle.draw(scratch);
}
위와 같이 변경한 후에도 삼각형이 회전하지 않으면
GLSurfaceView.RENDERMODE_WHEN_DIRTY
다음 섹션에 설명된 대로 사용하세요.
연속 렌더링 사용 설정
지금까지 이 수업의 예시 코드를 열심히 따라 했다면
더티일 때만 렌더링 모드를 설정하는 줄을 주석 처리합니다. 그렇지 않으면 OpenGL이
도형을 1씩만 회전한 다음 GLSurfaceView
컨테이너에서 requestRender()
호출을 기다립니다.
class MyGLSurfaceView(context: Context) : GLSurfaceView(context) {
init {
...
// Render the view only when there is a change in the drawing data.
// To allow the triangle to rotate automatically, this line is commented out:
// renderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY
}
}
public class MyGLSurfaceView(Context context) extends GLSurfaceView {
...
// Render the view only when there is a change in the drawing data.
// To allow the triangle to rotate automatically, this line is commented out:
//setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
사용자 상호작용 없이 객체를 변경하지 않는 한 일반적으로 플래그를 사용 설정합니다. 다음 학습 과정에서는 이 호출을 적용할 수 있으므로 이 코드의 주석 처리를 삭제할 준비를 하세요. 다시 한 번