티스토리 뷰

OpenGL ES 2.0 Practice 3 : Simple Projection

지금까지 Projection matrix 없이 기본 영역에서 Triangle 그리기를 하였는데, 여기에 Projection matrix를 추가하여 명시적으로 3차원 영역을 지정하도록 합니다. 

최종 position = Projection matrix * Model View matrix * vertex(position) 순으로 연산되도록 합니다. Vertex(Position) 와 연관된 연산으로 Vertex Shader Code 만 수정합니다. 

                    "precision mediump float;\n" +

                    "attribute vec3 aPosition;\n" +

                    "uniform mat4 uMatrix;\n" +

                    "void main() {\n" +

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

                    "}\n",


                    "precision mediump float;\n" +

                    "attribute vec3 aPosition;\n" +

                    "uniform mat4 uProjMatrix;\n" +

                    "uniform mat4 uMVMatrix;\n" +

                    "void main() {\n" +

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

                    "}\n"


Projection matrix 도 4x4 matrix 로 float array 를 추가 합니다. 

// 4x4 matrix

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

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


이제 View의 width, height 에 따라 Projection matrix 를 지정해야 하는데, 다음과 같이 지정합니다.

FOVy 는 45도, aspect ratio 는 width / height, near 1, far 10 으로 지정합니다.

    @Override

    public void resize(int width, int height) {


        GLES20.glViewport(0, 0, width, height);


        // perspective projection

        Matrix.perspectiveM(mProjMatrix, 0, 45.f, (float)width/(float)height, 1.0f, 10.0f);

    }


마지막으로 렌더링 시 Projection matrix와 Model View matrix 를 shader 에 설정합니다.

@Override

    public void draw() {

       ...


        // triangle matrix

        // y-axis, 1 degree rotation every frames

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

        GLES20.glUniformMatrix4fv(GLES20.glGetUniformLocation(mProgram, "uMVMatrix"), 1, false, mMVMatrix, 0);


        // projection matrix

        GLES20.glUniformMatrix4fv(GLES20.glGetUniformLocation(mProgram, "uProjMatrix"), 1, false, mProjMatrix, 0);


        ...

    }


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

OpenGLES20SimpleProjection.zip



댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
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
글 보관함