CG 3D Basics


3D 그래픽스 기초 (Basics of 3D Graphics)

3D 좌표계와 변환 (3D Coordinate System and Transformations)

소개 (Introduction)
3D 그래픽스는 3차원 공간에서 객체를 표현하고 조작하는 기술입니다. 3D 좌표계와 변환은 이러한 객체들을 정의하고 이동시키는 기본 요소입니다.

개념 (Concept)

  • 3D 좌표계 (3D Coordinate System): x, y, z 세 축으로 구성된 좌표계로, 3D 공간에서 객체의 위치를 표현합니다.
  • 변환 (Transformations): 객체의 위치, 방향, 크기를 변경하는 연산입니다. 주요 변환에는 이동, 회전, 스케일링이 포함됩니다.

원리 (Principle)

  • 이동 (Translation): 객체를 특정 방향으로 이동시킵니다. 행렬로 표현되며, 이동 행렬은 다음과 같습니다:

    \begin{bmatrix}
    1 & 0 & 0 & tx \\
    0 & 1 & 0 & ty \\
    0 & 0 & 1 & tz \\
    0 & 0 & 0 & 1 \\
    \end{bmatrix}

    여기서 ( tx, ty, tz )는 각각 x, y, z 축의 이동 거리입니다.
  • 회전 (Rotation): 객체를 특정 축을 기준으로 회전시킵니다. 예를 들어, z 축을 기준으로 회전하는 행렬은 다음과 같습니다:

    \begin{bmatrix}
    \cos \theta & -\sin \theta & 0 & 0 \\
    \sin \theta & \cos \theta & 0 & 0 \\
    0 & 0 & 1 & 0 \\
    0 & 0 & 0 & 1 \\
    \end{bmatrix}

    여기서 ( \theta )는 회전 각도입니다.
  • 스케일링 (Scaling): 객체의 크기를 조정합니다. 스케일링 행렬은 다음과 같습니다:

    \begin{bmatrix}
    sx & 0 & 0 & 0 \\
    0 & sy & 0 & 0 \\
    0 & 0 & sz & 0 \\
    0 & 0 & 0 & 1 \\
    \end{bmatrix}

    여기서 ( sx, sy, sz )는 각각 x, y, z 축의 스케일링 인자입니다.

예제 (Example)

import numpy as np

# 이동 행렬
tx, ty, tz = 1, 2, 3
translation_matrix = np.array([
    [1, 0, 0, tx],
    [0, 1, 0, ty],
    [0, 0, 1, tz],
    [0, 0, 0, 1]
])

# 회전 행렬 (z 축 기준)
theta = np.radians(45)
rotation_matrix = np.array([
    [np.cos(theta), -np.sin(theta), 0, 0],
    [np.sin(theta), np.cos(theta), 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 1]
])

# 스케일링 행렬
sx, sy, sz = 2, 2, 2
scaling_matrix = np.array([
    [sx, 0, 0, 0],
    [0, sy, 0, 0],
    [0, 0, sz, 0],
    [0, 0, 0, 1]
])

3D 모델링 기초 (정점, 에지, 페이스) (Basics of 3D Modeling: Vertices, Edges, Faces)

소개 (Introduction)
3D 모델링은 정점, 에지, 페이스를 사용하여 3D 객체를 만드는 과정입니다. 이러한 기본 요소들은 복잡한 3D 구조를 형성하는 기초입니다.

개념 (Concept)

  • 정점 (Vertex): 3D 공간에서의 한 점으로, 모델의 기본 단위입니다.
  • 에지 (Edge): 두 정점을 연결하는 선분으로, 모델의 골격을 형성합니다.
  • 페이스 (Face): 여러 에지가 연결되어 만들어진 면으로, 모델의 표면을 형성합니다.

원리 (Principle)

  • 다각형 메쉬 (Polygonal Mesh): 정점, 에지, 페이스의 집합으로 이루어진 3D 모델입니다. 주로 삼각형이나 사각형 페이스로 구성됩니다.
  • 네트워크 데이터 구조 (Data Structure for Meshes): 정점 목록, 에지 목록, 페이스 목록으로 구성됩니다. 이들은 서로 연결되어 모델의 형태를 정의합니다.

예제 (Example)

class Vertex:
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

class Edge:
    def __init__(self, v1, v2):
        self.v1 = v1
        self.v2 = v2

class Face:
    def __init__(self, vertices):
        self.vertices = vertices

# 정점 생성
v1 = Vertex(0, 0, 0)
v2 = Vertex(1, 0, 0)
v3 = Vertex(1, 1, 0)
v4 = Vertex(0, 1, 0)

# 에지 생성
e1 = Edge(v1, v2)
e2 = Edge(v2, v3)
e3 = Edge(v3, v4)
e4 = Edge(v4, v1)

# 페이스 생성
f1 = Face([v1, v2, v3, v4])

메시 생성 및 편집 (Mesh Creation and Editing)

소개 (Introduction)
메시는 3D 모델의 기본 구조로, 다양한 방법으로 생성 및 편집할 수 있습니다. 기본 도형을 이용한 메시 생성과 에지, 페이스의 조작을 통한 편집이 포함됩니다.

개념 (Concept)

  • 기본 메시 (Primitive Mesh): 구, 큐브, 원기둥 등 기본적인 3D 도형으로 구성된 메시입니다.
  • 메시 편집 (Mesh Editing): 기존 메시에 변형을 가하여 새로운 형태를 만드는 과정입니다.

원리 (Principle)

  • 구성요소 추가 및 삭제 (Adding and Removing Elements): 정점, 에지, 페이스를 추가하거나 삭제하여 메시의 형태를 변경합니다.
  • 메시 변형 (Mesh Deformation): 정점의 위치를 변경하거나 에지의 길이를 조정하여 모델의 형태를 변경합니다.

예제 (Example)

# 큐브 메시 생성
def create_cube():
    vertices = [
        Vertex(-1, -1, -1), Vertex(1, -1, -1),
        Vertex(1, 1, -1), Vertex(-1, 1, -1),
        Vertex(-1, -1, 1), Vertex(1, -1, 1),
        Vertex(1, 1, 1), Vertex(-1, 1, 1)
    ]
    edges = [
        Edge(vertices[0], vertices[1]), Edge(vertices[1], vertices[2]),
        Edge(vertices[2], vertices[3]), Edge(vertices[3], vertices[0]),
        Edge(vertices[4], vertices[5]), Edge(vertices[5], vertices[6]),
        Edge(vertices[6], vertices[7]), Edge(vertices[7], vertices[4]),
        Edge(vertices[0], vertices[4]), Edge(vertices[1], vertices[5]),
        Edge(vertices[2], vertices[6]), Edge(vertices[3], vertices[7])
    ]
    faces = [
        Face([vertices[0], vertices[1], vertices[2], vertices[3]]),
        Face([vertices[4], vertices[5], vertices[6], vertices[7]]),
        Face([vertices[0], vertices[1], vertices[5], vertices[4]]),
        Face([vertices[2], vertices[3], vertices[7], vertices[6]]),
        Face([vertices[0], vertices[3], vertices[7], vertices[4]]),
        Face([vertices[1], vertices[2], vertices[6], vertices[5]])
    ]
    return vertices, edges, faces

vertices, edges, faces = create_cube()

기초 3D 변환 (이동, 회전, 스케일링) (Basic 3D Transformations: Translation, Rotation, Scaling)

소개 (Introduction)
3D 변환은 객체의 위치, 방향, 크기를 변경하는 작업입니다. 이동, 회전, 스케일링은 이러한 변환의 기본 요소입니다.

개념 (Concept)

  • 이동 (Translation): 객체를 특정 방향으로 이동시키는 변환.
  • 회전 (Rotation): 객체를 특정 축을 기준으로 회전시키는 변환.
  • **스케

일링 (Scaling):** 객체의 크기를 조정하는 변환.

원리 (Principle)

  • 변환 행렬 (Transformation Matrix): 3D 변환은 행렬 연산으로 표현됩니다. 각 변환은 고유한 행렬을 가지며, 이들을 조합하여 복잡한 변환을 수행할 수 있습니다.

함수들 (Functions)

  • Translation Matrix: 위의 이동 행렬을 참조.
  • Rotation Matrix: 특정 축을 기준으로 한 회전 행렬.
  • Scaling Matrix: 위의 스케일링 행렬을 참조.

예제 (Example)

import numpy as np

# 이동 변환
def translate(vertex, tx, ty, tz):
    translation_matrix = np.array([
        [1, 0, 0, tx],
        [0, 1, 0, ty],
        [0, 0, 1, tz],
        [0, 0, 0, 1]
    ])
    vertex_homogeneous = np.array([vertex.x, vertex.y, vertex.z, 1])
    transformed_vertex = translation_matrix.dot(vertex_homogeneous)
    return Vertex(transformed_vertex[0], transformed_vertex[1], transformed_vertex[2])

# 회전 변환 (z 축 기준)
def rotate_z(vertex, theta):
    rotation_matrix = np.array([
        [np.cos(theta), -np.sin(theta), 0, 0],
        [np.sin(theta), np.cos(theta), 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1]
    ])
    vertex_homogeneous = np.array([vertex.x, vertex.y, vertex.z, 1])
    transformed_vertex = rotation_matrix.dot(vertex_homogeneous)
    return Vertex(transformed_vertex[0], transformed_vertex[1], transformed_vertex[2])

# 스케일링 변환
def scale(vertex, sx, sy, sz):
    scaling_matrix = np.array([
        [sx, 0, 0, 0],
        [0, sy, 0, 0],
        [0, 0, sz, 0],
        [0, 0, 0, 1]
    ])
    vertex_homogeneous = np.array([vertex.x, vertex.y, vertex.z, 1])
    transformed_vertex = scaling_matrix.dot(vertex_homogeneous)
    return Vertex(transformed_vertex[0], transformed_vertex[1], transformed_vertex[2])

# 예제 사용법
v = Vertex(1, 1, 1)
v_translated = translate(v, 2, 3, 4)
v_rotated = rotate_z(v, np.radians(45))
v_scaled = scale(v, 2, 2, 2)

이처럼 3D 그래픽스의 기초를 이해하기 위해서는 3D 좌표계와 변환, 3D 모델링 기초, 메시 생성 및 편집, 기초 3D 변환에 대한 전반적인 이해가 필요합니다.


Posted in CG

Leave a Reply

Your email address will not be published. Required fields are marked *