어쨌든, Android 2.2 이상에서 OpenGL ES 2.0을 이용하는 방법(순서?)를 ApiDemo를 기준으로 정리하면 다음과 같다.
[[[ Activity ]]]
GLES20Activity.java
1.
GLSurfaceView instance를 생성
2. ActivityManager service에서 device configuration info(
android.content.pm.ConfigurationInfo)를 획득하여 OpenGL ES 버전을 확인
3. (1)에서 생성한 GLSurfaceView가 사용할
EGL의 Version을 2(아마도 2.0?)로 설정4. OpenGL ES 2.0으로 작성 된 Renderer instance를 (1)에서 생성한
GLSurfaceView의 renderer로 설정5. 선택적으로 rendering overhead를 줄이기 위해, (1)에서 생성한 GLSurfaceView의
setRenderMode method를 호출하여 렌더링 모드를 RENDERMODE_WHEN_DIRTY로 설정할 수 있다. (기본은 RENDERMODE_CONTINUOSLY)
[[[ GLSurfaceView.Renderer ]]]
GLES20TriangleRenderer.java
(onSurfaceCreated)1. Vertex Shader를 생성하고, Vertext Shader 코드를 로드 & 컴파일
glCreateShader(), glShaderSource(), glCompileShader()2. Fragment Shader를 생성하고, Fragment Shader 코드를 로드 & 컴파일
glCreateShader(), glShaderSource(), glCompileShader()3. Program을 생성 (Vertext Shader 및 Fragment Shader를 묶는 단위)
3-1. Vertext Shader를 Program에 attach
glCreateProgram()3-2. Fragment Shader를 Program에 attach
glAttachShader()3-3. Vertext Shader와 Fragment Shader가 추가된 Program을 링크
glLinkProgram()4. Program으로부터 attribute와 uniform을 참조할 수 있는 handle을 획득
glGetAttribLocation(), glGetUniformLocation()5. Rendering에 사용 될 Texture를 준비
5-1. Texture object 생성
5-2. 생성된 Texture의 ID를 GL_TEXTURE_2D로 바인딩
5-3. GL_TEXTURE_2D의 옵션(속성)을 설정
5-4. Bitmap data를 GL_TEXTURE_2D로 전송
6. Matrix class를 이용해 View Matrix를 초기화 (Camera 설정이라고 생각하면 될 듯)
Matrix.setLootAtM()
(onSurfaceChanged)7. View 영역(View port)를 설정
glViewport()8. Matrix class를 이용해 Projection Matrix를 초기화
Matrix.frustumM()
(onDrawFrame)
9. Clear 색상 설정
glClearColor()10. Depth buffer와 Color buffer를 초기화
glClear()11. 사용할 Program을 지정
glUseProgram()12. Texture를 활성화 하고, (5-4)에서 전송 해 둔 Texture ID와 연결
13. Vertex 좌표가 저장된 buffer의 포인터를 attribute로 설정
glVertexAttribPointer() - glVertexAttribPointer() 사용 시 주의할 점은, 5
th argument인 stride는 byte 기준이라는 것!
- 즉, float 형 x, y, z 세 개씩 건너 뛰어야 할 경우, stride 값은
3 * float 변수의 byte 크기(4) = 12 (stride 값을 3으로 주고, 안보여서 한참 삽질 했네-_-;)
14. Vertex attribute array를 활성화
glEnableVertexAttribArray()15. Texture 좌표가 저장된 buffer의 포인터를 attribute로 설정
16. Model Matrix 설정(여기서는 회전)
Matrix.setRotateM()17. MVP Matrix를 계산(MVPMatrix = mProjMatrix * VMatrix * MMatrix)
Matrix.multiplyMM()18. MVP Matrix를 uniform으로 설정
glUniformMatrix4fv()19. 삼각형을 출력
glDrawArrays()