OpenGL 로 원하는 도형 만들기

  android로 OpenGL ES 를 이용하는 예제는

  Dev site 에서 찾을 수 있습니다.

  예제는 Cube.java 와 CubeRenderer.java 입니다.

  그런데 여기서 Cube 만 다루다 보니 

  다른 도형을 어떻게 해야 할지 좀 막막하더군요.

  Web 상의 OpenGL 문서를 뒤져보고 해도 기초만 나오기 때문에

  좀처럼 이해하기 어려웠습니다.

  원하는 도형을 한번에 만들어서 Renderer 에서 그리면 좋을텐데,

  꼭지점을 이용하여 그리는 방식을 이해하기 어렵더군요.

  그간 고민한게 효과가 있었던지, 드디어 알게 되었습니다.

  일단 Cube.java 파일을 봅시다.

class Cube

{

    public Cube()

    {

        int one = 0x10000;

        int vertices[] = {

               //x,    y,    z,

                -one, -one, -one, // index #0

                one, -one, -one,  // index #1

                one,  one, -one,  // index #2

                -one,  one, -one, // index #3

                -one, -one,  one, // index #4

                one, -one,  one,  // index #5

                one,  one,  one,  // index #6

                -one,  one,  one, // index #7

        };


        int colors[] = {

              //R, G, B, A

                0,    0,    0,  one,

                one,    0,    0,  one,

                one,  one,    0,  one,

                0,  one,    0,  one,

                0,    0,  one,  one,

                one,    0,  one,  one,

                one,  one,  one,  one,

                0,  one,  one,  one,

       

        };


        byte indices[] = {

                // Triangle 로 쪼개는 인덱스 번호들

                0, 4, 5,    0, 5, 1,

                1, 5, 6,    1, 6, 2,

                2, 6, 7,    2, 7, 3,

                3, 7, 4,    3, 4, 0,

                4, 7, 6,    4, 6, 5,

                3, 0, 1,    3, 1, 2,

        

        };


        // Buffers to be passed to gl*Pointer() functions

        // must be direct, i.e., they must be placed on the

        // native heap where the garbage collector cannot

        // move them.

        //

        // Buffers with multi-byte datatypes (e.g., short, int, float)

        // must have their byte order set to native order


        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);

        vbb.order(ByteOrder.nativeOrder());

        mVertexBuffer = vbb.asIntBuffer();

        mVertexBuffer.put(vertices);

        mVertexBuffer.position(0);


        ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);

        cbb.order(ByteOrder.nativeOrder());

        mColorBuffer = cbb.asIntBuffer();

        mColorBuffer.put(colors);

        mColorBuffer.position(0);


        mIndexBuffer = ByteBuffer.allocateDirect(indices.length);

        mIndexBuffer.put(indices);

        mIndexBuffer.position(0);

        

    }


    public void draw(GL10 gl)

    {

        gl.glFrontFace(gl.GL_CW);

        gl.glVertexPointer(3, gl.GL_FIXED, 0, mVertexBuffer);

        gl.glColorPointer(4, gl.GL_FIXED, 0, mColorBuffer);

        gl.glDrawElements(gl.GL_TRIANGLES, 36, gl.GL_UNSIGNED_BYTE, mIndexBuffer);

        

    }

    

    private IntBuffer   mVertexBuffer;

    private IntBuffer   mColorBuffer;

    private ByteBuffer  mIndexBuffer;

}


그리고 anddev.org 사이트에서 찾아보면 나오는 설명에서 아래의 그림처럼
꼭지점들(vertices)을 정하고, 색을 정하고, 그림을 그리기 위한 조각들을 결정합니다.

 

위의 그림처럼 꼭지점이 인덱스가 "0" 이면 (-1,-1,-1) 이고,  "1"이면 (1,-1,-1)  의 순으로 지정됩니다.
vertices[] 내의 좌표 순서가 바로 인덱스 순서라고 보면 되지요.
그리고 glDrawElements 함수로 그리기 위해 GL_TRIANGLES를 이용했는데 아래 그림처럼 삼각형으로 잘라지지요.

 

삼각형의 번호는 인덱스의 번호로 3개씩 짝을 짓고 있고 glFrontFace 함수에서 시계방향을 선택했으므로
인덱스 번호의 순서를 결정할 수 있습니다.

그러면 다른 도형의 형태를 어떻게 할 것인가? 감이 오시는지...

안오신다면 아래의 그림을 다시 한번 보면 이해가 될 것 같네요.


만약 집모양을 만든다고 하면 좌표값의 크기를 정하고, 좌표값에 맞는 인덱스를 정한 후 

도형의 면을 그릴 수 있도록 삼각형, Loop 등의 방법을 결정해 주면 됩니다.