void tree(GLfloat x, GLfloat y, GLfloat z, GLfloat width, GLfloat height, GLfloat theight)
{
    /* function tree()
    중심 x, y, z 좌표를 받아 사각뿔의 밑면 길이가 width이고 사각뿔 높이가 height이고 줄기 부분 높이가 theight인 나무 생성
    x : x 좌표
    y : y 좌표
    z : z 좌표
    width : 나무 잎 부분 사각뿔 밑면 길이
    eight : 나무 잎 부분 사각뿔 높이 길이
    theight : 나무 줄기 부분 원기둥 높이
    */
    float v[3][3];
    float out[3];

    v[0][0]= x; v[0][1]=y; v[0][2]=z;
    v[1][0] = x-width; v[1][1] = y-height; v[1][2] = z-width;
    v[2][0] = x-width; v[2][1] = y-height; v[2][2] = z+width;

    glPushMatrix();
    glTranslated(x, y+height+theight, z);
    glColor3ub(34, 139, 34);

    glBegin(GL_TRIANGLE_FAN);   //나무 윗부분 사각뿔
    calcNormal(v, out);
    glNormal3f(0.0f, 1.0f, 0.0f);
    glVertex3f(x, y, z);
    glNormal3fv(out);
    glVertex3f(x-width, y-height, z-width);
    glNormal3fv(out);
    glVertex3f(x-width, y-height, z+width);
    v[1][0] = x-width; v[1][1] = y-height; v[1][2] = z+width;
    v[2][0] = x+width; v[2][1] = y-height; v[2][2] = z-width;
    calcNormal(v, out);

    glNormal3fv(out);
    glVertex3f(x+width, y-height, z+width);
    v[1][0] = x+width; v[1][1] = y-height; v[1][2] = z+width;
    v[2][0] = x+width; v[2][1] = y-height; v[2][2] = z-width;
    calcNormal(v, out);

    glNormal3fv(out);
    glVertex3f(x+width, y-height, z-width);
    v[1][0] = x+width; v[1][1] = y-height; v[1][2] = z-width;
    v[2][0] = x-width; v[2][1] = y-height; v[2][2] = z-width;
    calcNormal(v, out);

    glNormal3fv(out);
    glVertex3f(x-width, y-height, z-width);
    glEnd();

    glBegin(GL_QUADS);       //사각뿔 밑면
    glNormal3f(0.0f, -1.0f, 0.0f);
    glVertex3f(x-width, y-height, z-width);
    glVertex3f(x+width, y-height, z-width);
    glVertex3f(x+width, y-height, z+width);
    glVertex3f(x-width, y-height, z+width);
    glEnd();
 
    glTranslatef(x, y-height, z);
    glRotated(90.0f, 1.0f, 0.0f, 0.0f);
    treebottom(0.0f, 0.0f, 0.0f, 10.0f, theight);  //나무 원기둥 함수
    glPopMatrix();
}

'Graphics > OpenGL' 카테고리의 다른 글

[OpenGL] 놀이동산 만들기  (2) 2016.12.05
[OpenGL]반구 만들기 함수  (0) 2014.07.05
[OpenGL]간단한 의자 만들기 함수  (0) 2014.07.05
[OpenGL]간단한 링 만드는 함수  (0) 2014.07.05
[OpenGL]간단한 배 만들기 함수  (5) 2014.07.05
void createHalfSphere(GLfloat x, GLfloat y, GLfloat z, GLfloat radius)
{
    /* function createHalfSphere()
    구의 중심 x, y, z 좌표를 받아 반구를 만드는 함수
    x : 반구의 중심 x 좌표
    y : 반구의 중심 y 좌표
    z : 반구의 중심 z 좌표
    raidus : 반구의 반지름
    */
    GLfloat angley;  //y축 값을 구하기 위한 각도
    GLfloat nexty;  //다음 y축 값을 구하기 위한 각도
    GLfloat anglex;  //x, y축 값을 구하기 위한 각도

    glColor3ub(136, 206, 250);  //반구의 색 지정
    glBegin(GL_QUAD_STRIP);
    for(angley = 0.0f; angley <= (0.5f*GL_PI); angley += ((0.5f*GL_PI)/8.0f))  //반구만 그려야 하므로 0.5곱함
    {
        y = radius*sin(angley);     //y축 값 계산
        nexty = angley+((0.5f*GL_PI)/8.0f);  //다음 angley값 저장
        for(anglex = 0.0f; anglex < (2.0f*GL_PI); anglex += (GL_PI/8.0f))
        {
            x = radius*cos(angley)*sin(anglex);
            z = radius*cos(angley)*cos(anglex);
            glNormal3f(-cos(angley)*sin(anglex), -sin(angley), -cos(angley)*cos(anglex)); //반구의 안쪽으로 normal 벡터 생성
            glVertex3f(x, y, z);

            x = radius*cos(nexty)*sin(anglex);
            z = radius*cos(nexty)*cos(anglex);
            glNormal3f(-cos(nexty)*sin(anglex), -sin(nexty), -cos(nexty)*cos(anglex));
            glVertex3f(x, radius*sin(nexty), z);
        }
    }
    glEnd();
}
void createChair(GLfloat x, GLfloat y, GLfloat z, GLfloat thick, GLfloat width, GLfloat height)
{
    /* function createChair()
    의자 왼쪽 위 끝 x,y,z좌표, 등받이 높이, 앉는부분 길이를 받아 의자를 생성하는 함수
    x : 의자 왼쪽 위 끝 x좌표
    y : 의자 왼쪽 위 끝 y좌표
    z : 의자 오른쪽 위 끝 z좌표
    thick : 의자 두께
    width : 의자 가로길이
    height : 의자 세로길이
    */
    glColor3ub(0, 0, 0);
    glBegin(GL_QUADS);
    glNormal3f(-1.0f, 0.0f, 0.0f);   //등받이 왼쪽 옆면
    glVertex3f(x, y, z);
    glVertex3f(x+thick, y, z);
    glVertex3f(x+thick, y+height, z);
    glVertex3f(x, y+height, z);
 
    glNormal3f(0.0f, 0.0f, 1.0f);   //등받이 앞면
    glVertex3f(x+thick, y, z);
    glVertex3f(x+thick, y, z+width);
    glVertex3f(x+thick, y+height, z+width);
    glVertex3f(x+thick, y+height, z);

    glNormal3f(1.0f, 0.0f, 0.0f);   //등받이 오른쪽 옆면
    glVertex3f(x+thick, y, z+width);
    glVertex3f(x, y, z+width);
    glVertex3f(x, y+height, z+width);
    glVertex3f(x+thick, y+height, z+width);

    glNormal3f(0.0f, 0.0f, -1.0f);   //등받이 뒷면
    glVertex3f(x, y, z+width);
    glVertex3f(x, y, z);
    glVertex3f(x, y+height+thick, z);
    glVertex3f(x, y+height+thick, z+width);
 
    glNormal3f(0.0f, 1.0f, 0.0f);   //등받이 윗면
    glVertex3f(x, y, z);
    glVertex3f(x, y, z+width);
    glVertex3f(x+thick, y, z+width);
    glVertex3f(x+thick, y, z);

    glNormal3f(0.0f, 1.0f, 0.0f);   //앉는 부분 윗면
    glVertex3f(x, y+height, z);
    glVertex3f(x, y+height, z+width);
    glVertex3f(x+height, y+height, z+width);
    glVertex3f(x+height, y+height, z);

    glNormal3f(-1.0f, 0.0f, 0.0f);   //앉는 부분 왼쪽 옆면
    glVertex3f(x, y+height, z);
    glVertex3f(x+height, y+height, z);
    glVertex3f(x+height, y+height+thick, z);
    glVertex3f(x, y+height+thick, z);
    glNormal3f(0.0f, 0.0f, -1.0f);   //앉는 부분 앞쪽
    glVertex3f(x+height, y+height, z);
    glVertex3f(x+height, y+height, z+width);
    glVertex3f(x+height, y+height+thick, z+width);
    glVertex3f(x+height, y+height+thick, z);

    glNormal3f(1.0f, 0.0f, 0.0f);   //앉는 부분 오른쪽 옆면
    glVertex3f(x, y+height, z+width);
    glVertex3f(x, y+height+thick, z+width);
    glVertex3f(x+height, y+height+thick, z+width);
    glVertex3f(x+height, y+height, z+width);

    glNormal3f(0.0f, -1.0f, 0.0f);   //앉는 부분 밑면
    glVertex3f(x, y+height+thick, z);
    glVertex3f(x+height, y+height+thick, z);
    glVertex3f(x+height, y+height+thick, z+width);
    glVertex3f(x, y+height+thick, z+width);
    glEnd();
}

'Graphics > OpenGL' 카테고리의 다른 글

[OpenGL]간단한 나무 만들기 함수  (2) 2014.07.05
[OpenGL]반구 만들기 함수  (0) 2014.07.05
[OpenGL]간단한 링 만드는 함수  (0) 2014.07.05
[OpenGL]간단한 배 만들기 함수  (5) 2014.07.05
[OpenGL]glColor() 색상모음  (1) 2014.07.05

+ Recent posts