void calcNormal(float v[3][3], float out[3])
{
    /* function calcNormal()
     : 점 3개를 받아 해당 삼각형의 normal vector를 구하는 함수
    v[3][3] : (x, y, z) 점 좌표 3개
    out[3] : normal 벡터를 반환받을 벡터 3개
    ※ glut함수를 쓰면 느리므로 직접 제작
    */
    float v1[3], v2[3], length;
    static const int x = 0;
    static const int y = 1;
    static const int z = 2;

    v1[x] = v[0][x]-v[1][x]; v1[y] = v[0][y]-v[1][y]; v1[z] = v[0][z]-v[1][z];
    v2[x] = v[2][x]-v[1][x]; v2[y] = v[2][y]-v[1][y]; v2[z] = v[2][z]-v[1][z];
 
    out[x] = v1[y]*v2[z] - v1[z]*v2[y];
    out[y] = v1[z]*v2[x] - v1[x]*v2[z];
    out[z] = v1[x]*v2[y] - v1[y]*v2[x];

    length = (float)sqrt(out[x]*out[x] + out[y]*out[y]+out[z]*out[z]);
    if(length == 0.0f)
        length = 1.0f;

    out[x] /= length;
    out[y] /= length;
    out[z] /= length;
}

void createShip(GLfloat x, GLfloat y, GLfloat z, GLfloat size)
{
    /* function createShip()
    중심 (x,y,z)로 부터 z축으로 길게 배 생성, 옆면 길이는 size * 2, 총 길이는 size * 4
    x : x 좌표
    y : y 좌표
    z : z 좌표
    size : 배 크기의 기본단위
    */
    float v[3][3];
    float out[3];

    glBegin(GL_TRIANGLES);
    glColor3f(0.65f, 0.16f, 0.16f);   //바이킹 뒷 왼쪽 삼각형
    v[0][0] = x-size; v[0][1] = y; v[0][2] = z-size;
    v[1][0] = x; v[1][1] = y; v[1][2] = z-2*size;
    v[2][0] = x; v[2][1] = y-size; v[2][2] = z-size;
    calcNormal(v, out);
    glNormal3fv(out);
    glVertex3f(x-size, y, z-size);
    glVertex3f(x, y, z-2*size);
    glVertex3f(x, y-size, z-size);
 
    v[0][0] = x; v[0][1] = y-size; v[0][2] = z+size;
    v[1][0] = x; v[1][1] = y; v[1][2] = z-2*size;
    v[2][0] = x-size; v[2][1] = y; v[2][2] = z+size;
    calcNormal(v, out);
    glNormal3fv(out);
    glVertex3f(x, y-size, z+size);  //바이킹 앞 왼쪽 삼각형
    glVertex3f(x, y, z+2*size);
    glVertex3f(x-size, y, z+size);
 
    glColor3f(0.65f, 0.16f, 0.16f);   //바이킹 왼쪽 몸통
    v[0][0] = x; v[0][1] = y-size; v[0][2] = z+size;
    v[1][0] = x-size; v[1][1] = y; v[1][2] = z-size;
    v[2][0] = x; v[2][1] = y-size; v[2][2] = z-size;
    calcNormal(v, out);
    glNormal3fv(out);
    glVertex3f(x, y-size, z+size);
    glVertex3f(x-size, y, z-size);
    glVertex3f(x, y-size, z-size);
 
    v[0][0] = x-size; v[0][1] = y; v[0][2] = z-size;
    v[1][0] = x; v[1][1] = y-size; v[1][2] = z+size;
    v[2][0] = x-size; v[2][1] = y; v[2][2] = z+size;
    calcNormal(v, out);
    glNormal3fv(out);
    glVertex3f(x-size, y, z-size);
    glVertex3f(x, y-size, z+size);
    glVertex3f(x-size, y, z+size);

    glColor3f(0.65f, 0.16f, 0.16f);   //바이킹 뒷 오른쪽 삼각형
    v[0][0] = x; v[0][1] = y; v[0][2] = z-2*size;
    v[1][0] = x+size; v[1][1] = y; v[1][2] = z-size;
    v[2][0] = x; v[2][1] = y-size; v[2][2] = z-size;
    calcNormal(v, out);
    glNormal3fv(out);
    glVertex3f(x, y, z-2*size);
    glVertex3f(x+size, y, z-size);
    glVertex3f(x, y-size, z-size);
 
    v[0][0] = x; v[0][1] = y-size; v[0][2] = z+size;
    v[1][0] = x+size; v[1][1] = y; v[1][2] = z+size;
    v[2][0] = x; v[2][1] = y; v[2][2] = z+2*size;
    calcNormal(v, out);
    glNormal3fv(out);
    glVertex3f(x, y-size, z+size);  //바이킹 앞 오른쪽 삼각형
    glVertex3f(x+size, y, z+size);
    glVertex3f(x, y, z+2*size);
 
    glColor3f(0.65f, 0.16f, 0.16f);   //바이킹 오른쪽 몸통
    v[0][0] = x+size; v[0][1] = y; v[0][2] = z-size;
    v[1][0] = x; v[1][1] = y-size; v[1][2] = z+size;
    v[2][0] = x; v[2][1] = y-size; v[2][2] = z-size;
    calcNormal(v, out);
    glNormal3fv(out);
    glVertex3f(x+size, y, z-size);
    glVertex3f(x, y-size, z+size);
    glVertex3f(x, y-size, z-size);
 
    v[0][0] = x+size; v[0][1] = y; v[0][2] = z-size;
    v[1][0] = x+size; v[1][1] = y; v[1][2] = z+size;
    v[2][0] = x; v[2][1] = y-size; v[2][2] = z+size;
    calcNormal(v, out);
    glNormal3fv(out);
    glVertex3f(x+size, y, z-size);
    glVertex3f(x+size, y, z+size);
    glVertex3f(x, y-size, z+size);
    glEnd();

    glBegin(GL_POLYGON);     //바이킹 갑판
    glColor3f(0.65f, 0.16f, 0.16f);
    glNormal3f(0.0f, 1.0f, 0.0f);
    glVertex3f(x+size, y, z-size);
    glNormal3f(0.0f, 1.0f, 0.0f);
    glVertex3f(x, y, z-2*size);
    glNormal3f(0.0f, 1.0f, 0.0f);
    glVertex3f(x-size, y, z-size);
    glNormal3f(0.0f, 1.0f, 0.0f);
    glVertex3f(x-size, y, z+size);
    glNormal3f(0.0f, 1.0f, 0.0f);
    glVertex3f(x, y, z+2*size);
    glNormal3f(0.0f, 1.0f, 0.0f);
    glVertex3f(x+size, y, z+size);
    glEnd();
}

+ Recent posts