Metaverse/게임 수학

Mesh와 WireframeRendering

onenewkong 2023. 8. 18. 18:46

Mesh

: 삼각형을 중심으로 물체에 관련된 정보를 기록한 데이터

  • 삼각형을 이루는 각 점의 위치를 가지고 있어야 함
  • 물체를 표현하는데 활용할 수 있는 색상이나 방향과 같은 다양한 부가 정보도 담아서 제공
  • 위치 정보와 부가 정보를 묶은 특별한 점을 정점이라고 함
  • 즉, 메시는 정점들이 모인 데이터
  • 정점 버퍼와 인덱스 버퍼에 데이터 저장

 

ex) 정사각형을 구성하는 메시 정보

기준점 (0, 0)

P0(-0.5, -0.5), P1(-0.5, 0.5), P2(0.5, 0.5), P3(0.5, -0.5)

정점 버퍼

순서 좌표
0 (-0.5, -0.5)
1 (-0.5, 0.5)
2 (0.5, 0.5)
3 (0.5, -0.5)

 

인덱스 버퍼

순서 삼각형 정점 순서
0 0 0
1 0 1
2 0 2
3 1 0
4 1 2
5 1 3

 

 

메시 정보를 와이어프레임으로 렌더링 하기

void SoftRenderer::Render2D() {
	auto& r = GetRenderer();
	const auto& g = Get2DGameEngine();

	DrawGizmo2D();

	static constexpr float squareHalfSize = 0.5f;
	static constexpr size_t vertexCount = 4;
	static constexpr size_t triangleCount = 2;

	static constexpr std::array<Vertex2D, vertexCount> rawVertices = {
		Vertex2D(Vector2(-squareHalfSize, -squareHalfSize)),
		Vertex2D(Vector2(-squareHalfSize, squareHalfSize)),
		Vertex2D(Vector2(squareHalfSize, squareHalfSize)),
		Vertex2D(Vector2(squareHalfSize, -squareHalfSize))
	};

	static constexpr std::array<size_t, triangleCount * 3> indices = {
		0, 1, 2,
		0, 2, 3
	};


	static std::vector<Vertex2D> vertices(vertexCount);
	
	for (size_t vi = 0; vi < vertexCount; ++vi) {
		vertices[vi].Position = finalMatrix * rawVertices[vi].Position;
	}

	for (size_t ti = 0; ti < triangleCount; ++ti) {
		size_t bi = ti * 3;
		r.DrawLine(vertices[indices[bi]].Position, vertices[indices[bi + 1].Position, WireframeColor);
		r.DrawLine(vertices[indices[bi]].Position, vertices[indices[bi + 2].Position, WireframeColor);
		r.DrawLine(vertices[indices[bi + 1]].Position, vertices[indices[bi + 2].Position, WireframeColor);
	}

	r.PushStatisticText(std::string("Position : ")) + currentPosition.ToString());
	r.PushStatisticText(std::string("Scale : ")) + std::to_string(currentScale));
	r.PushStatisticText(std::string("Degree : ")) + std::to_string(currentDegree));
}

 

'Metaverse > 게임 수학' 카테고리의 다른 글

오일러 각 문제 해결  (0) 2023.03.29
오일러 각과 짐벌락  (0) 2023.03.29