화면에 객체 그리기는 매우 기본적인 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) }
자바
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 } }
자바
public 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); }
사용자 상호작용 없이 객체를 변경하지 않는 한, 일반적으로 이 플래그를 사용하는 것이 좋습니다. 다음 학습 과정에서는 이 호출을 다시 한번 적용할 수 있으므로 이 코드의 주석 처리를 삭제하도록 준비하세요.