티스토리 뷰

Programming/OpenGL ES

OpenGL ES 2.0 Practice 2 : Simple Matrix

데굴데굴여기 2016. 10. 9. 16:51

OpenGL ES 2.0 Practice 2 : Simple Matrix

Simple Triangle 에서 shader 코드에 matrix 연산을 추가하여 Triangle 을 회전하는 코드 입니다. 


기본 shader 코드에서 matrix 연산을 추가합니다.

                   // no matrix transform

                    "precision mediump float;\n" +

                    "attribute vec3 aPosition;\n" +


                    "void main() {\n" +

                    " gl_Position = vec4(aPosition, 1.0);\n" +

                    "}\n",


                    // matrix transform 

                    "precision mediump float;\n" +

                    "attribute vec3 aPosition;\n" +

                    "uniform mat4 uMatrix;\n" +

                    "void main() {\n" +

                    " gl_Position = uMatrix * vec4(aPosition, 1.0);\n" +

                    "}\n",


그리고, 4x4 matrix 처리를 위해 float mMatrix[] = new float[16]; 를 멤버 변수로 추가합니다.

// 4x4 matrix

    private float mMatrix[] = new float[16];


앱 시작 시, 이 matrix 값을 identity matrix로 초기화 하고, 매 frame마다 rotation 값을 연산하도록 합니다. 

public SimpleMatrix() {

...

        // init matrix value

        Matrix.setIdentityM(mMatrix, 0);

    }


    @Override

    public void draw() {


...

        // triangle matrix

        // z-axis, 1 degree rotation every frames

        Matrix.rotateM(mMatrix, 0, 1.0f, 0.0f, 0.0f, 1.0f);

        GLES20.glUniformMatrix4fv(GLES20.glGetUniformLocation(mProgram, "uMatrix"), 1, false, mMatrix, 0);


...

    }


결과 화면 ... (Nexus 7 2nd, Android 6.0)

OpenGLES20SimpleMatrix.zip


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함