티스토리 툴바

달력

052012  이전 다음

  •  
  •  
  • 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
  •  
  •  
Android 나 iOS 에서는 openGL ES 를 사용한다. openGL ES 에서는 사각형을 지원하지 않기 때문에 glut 에서 사용하는 것과 같은 GL_QUADS 따위의 도형은 그릴 수 없다. 때문에 GL_ANGLES 등을 이용한 구를 그려야 하는데, 간단하게 freeglut 에 있는 것을 가져오기로 하였다.

freeglut 전체를 사용하겠다면 모르겠지만, 일부만 사용하기 때문에 관련된 함수만 같이 가져왔다. 아래는 구를 그리기 위한 소스이다. 소스의 출처는 이곳.


static void fghCircleTable(double **sint,double **cost,const int n)
{
    int i;

    /* Table size, the sign of n flips the circle direction */

    const int size = abs(n);

    /* Determine the angle between samples */

    const double angle = 2*M_PI/(double)( ( n == 0 ) ? 1 : n );

    /* Allocate memory for n samples, plus duplicate of first entry at the end */

    *sint = (double *) calloc(sizeof(double), size+1);
    *cost = (double *) calloc(sizeof(double), size+1);

    /* Bail out if memory allocation fails, fgError never returns */

    if (!(*sint) || !(*cost))
    {
        free(*sint);
        free(*cost);
    }

    /* Compute cos and sin around the circle */

    (*sint)[0] = 0.0;
    (*cost)[0] = 1.0;

    for (i=1; i<size; i++)
    {
        (*sint)[i] = sin(angle*i);
        (*cost)[i] = cos(angle*i);
    }

    /* Last sample is duplicate of the first */

    (*sint)[size] = (*sint)[0];
    (*cost)[size] = (*cost)[0];
}

void solidSphere(GLdouble radius, GLint slices, GLint stacks)
{
    int i,j;

    /* Adjust z and radius as stacks are drawn. */

    double z0,z1;
    double r0,r1;

    /* Pre-computed circle */

    double *sint1,*cost1;
    double *sint2,*cost2;

    fghCircleTable(&sint1,&cost1,-slices);
    fghCircleTable(&sint2,&cost2,stacks*2);

    /* The top stack is covered with a triangle fan */

    z0 = 1.0;
    z1 = cost2[(stacks>0)?1:0];
    r0 = 0.0;
    r1 = sint2[(stacks>0)?1:0];

    glBegin(GL_TRIANGLE_FAN);

        glNormal3d(0,0,1);
        glVertex3d(0,0,radius);

        for (j=slices; j>=0; j--)
        {
            glNormal3d(cost1[j]*r1,        sint1[j]*r1,        z1       );
            glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius);
        }

    glEnd();

    /* Cover each stack with a quad strip, except the top and bottom stacks */

    for( i=1; i<stacks-1; i++ )
    {
        z0 = z1; z1 = cost2[i+1];
        r0 = r1; r1 = sint2[i+1];

        glBegin(GL_QUAD_STRIP);

            for(j=0; j<=slices; j++)
            {
                glNormal3d(cost1[j]*r1,        sint1[j]*r1,        z1       );
                glVertex3d(cost1[j]*r1*radius, sint1[j]*r1*radius, z1*radius);
                glNormal3d(cost1[j]*r0,        sint1[j]*r0,        z0       );
                glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius);
            }

        glEnd();
    }

    /* The bottom stack is covered with a triangle fan */

    z0 = z1;
    r0 = r1;

    glBegin(GL_TRIANGLE_FAN);

        glNormal3d(0,0,-1);
        glVertex3d(0,0,-radius);

        for (j=0; j<=slices; j++)
        {
            glNormal3d(cost1[j]*r0,        sint1[j]*r0,        z0       );
            glVertex3d(cost1[j]*r0*radius, sint1[j]*r0*radius, z0*radius);
        }

    glEnd();

    /* Release sin and cos tables */

    free(sint1);
    free(cost1);
    free(sint2);
    free(cost2);
}

void wireSphere(GLdouble radius, GLint slices, GLint stacks)
{
    int i,j;

    /* Adjust z and radius as stacks and slices are drawn. */

    double r;
    double x,y,z;

    /* Pre-computed circle */

    double *sint1,*cost1;
    double *sint2,*cost2;


    fghCircleTable(&sint1,&cost1,-slices  );
    fghCircleTable(&sint2,&cost2, stacks*2);

    /* Draw a line loop for each stack */

    for (i=1; i<stacks; i++)
    {
        z = cost2[i];
        r = sint2[i];

        glBegin(GL_LINE_LOOP);

            for(j=0; j<=slices; j++)
            {
                x = cost1[j];
                y = sint1[j];

                glNormal3d(x,y,z);
                glVertex3d(x*r*radius,y*r*radius,z*radius);
            }

        glEnd();
    }

    /* Draw a line loop for each slice */

    for (i=0; i<slices; i++)
    {
        glBegin(GL_LINE_STRIP);

            for(j=0; j<=stacks; j++)
            {
                x = cost1[i]*sint2[j];
                y = sint1[i]*sint2[j];
                z = cost2[j];

                glNormal3d(x,y,z);
                glVertex3d(x*radius,y*radius,z*radius);
            }

        glEnd();
    }

    /* Release sin and cos tables */

    free(sint1);
    free(cost1);
    free(sint2);
    free(cost2);
}



이외에도 구를 그리는 다양한 방법들이 있다. 관련된 링크를 아래 첨부한다.

http://paulbourke.net/miscellaneous/sphere_cylinder/ 
http://marc.blog.atpurpose.com/2009/10/31/creating-sphere-from-a-rectangular-mesh/
http://ozark.hendrix.edu/~burch/cs/490/sched/feb8/

저작자 표시 비영리 동일 조건 변경 허락
크리에이티브 커먼즈 라이선스
Creative Commons License

'openGL' 카테고리의 다른 글

openGL ES 에서 구 그리기  (0) 2011/07/20
깃발 휘날리기  (0) 2011/07/18
Frustum 설정 방법  (0) 2011/07/18
Posted by 삽질 즈려밟다

Java Source to UML

자바 2011/07/19 14:03
프로젝트를 진행하다 보면 UML 을 요구하는 경우가 종종 있다. 처음부터 UML 을 만들고 계획대로 프로젝트를 진행한다면 문제가 없겠지만, 진행 도중 구조가 바뀌거나 혹은 완성한 이후에 UML 을 작성해야 할 경우에는 reverse engineering 을 해야 한다. UML 툴은 많이 있지만 source 를 UML 로 고쳐야 하는 경우에는 그리 유용하지 못한 것이 태반이다.

만약 eclipse 로 java 관련 프로젝트를 진행한다면 ObjectAid plug-in 을 사용하는 것도 좋은 방법이다.

 eclipse plug-in 이 다 그렇듯 설치 방법은 간단하다.

먼저 메뉴의 help > install new software 에서 아래 경로를 url 에 넣는다.

http://www.objectaid.com/update

그러면 class diagram 과 sequence diagram 을 설치할 수 있도록 나오는데, sequence diagram 은 single-user license ( 유료) 가 필요하다.

 설치 후 메뉴의 window > preferences 에 objectaid 탭이 생기는데, 하위 메뉴의 class diagram 을 보면 여러 옵션들이 있다.

이미지 파일로 저장할 때 기본 포맷을 gif 로 하는 것이 제일 가독성이 있어 보인다.

다른 옵션은 직접 확인해 보되, Attribute 는 변수 관련 부분이고, operations 는 함수 관련 부분이라는 것만 기억하자. 
저작자 표시 비영리 동일 조건 변경 허락
크리에이티브 커먼즈 라이선스
Creative Commons License

'자바' 카테고리의 다른 글

Java Source to UML  (0) 2011/07/19
formatter 형식  (0) 2011/01/11
JAVA SWAP  (0) 2011/01/06
Posted by 삽질 즈려밟다