화면에 객체 그리기는 OpenGL의 매우 기본적인 기능이지만 Canvas
및 Drawable
객체를 비롯한 다른 Android 그래픽 프레임워크 클래스를 사용하여 이 작업을 할 수 있습니다. OpenGL ES는 3차원에서 그린 객체를 이동하고 변환하는 추가 기능 또는 매력적인 사용자 환경을 만들기 위한 다른 고유한 기능을 제공합니다.
이 강의에서는 회전을 통해 도형에 모션을 추가하는 방법을 알아보고 OpenGL ES 사용을 한 단계 더 진행합니다.
도형 회전
OpenGL ES 2.0으로 그림 객체를 회전하는 것은 비교적 간단합니다. 렌더기에서 다른 변환 매트릭스 (회전 매트릭스)를 만든 다음 투영 및 카메라 뷰 변환 매트릭스와 결합합니다.
Kotlin
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) }
Java
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이 도형을 한 증분만 회전하고 GLSurfaceView
컨테이너에서 requestRender()
호출을 기다립니다.
Kotlin
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 } }
Java
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); }
사용자 상호작용 없이 객체를 변경하지 않는 한 일반적으로 이 플래그를 사용 설정하는 것이 좋습니다. 다음 과정에서 이 호출을 다시 적용할 수 있으므로 이 코드의 주석 처리를 삭제합니다.