OpenGL

[OpenGL] OpenGL와 GLM(OpenGL Mathematics)

usingsystem 2024. 4. 16. 14:58
728x90

GLM이란 OpenGL을 위한 수학 라이브러리로 대부분 vector와 matrix를 계산하는 기하학으로 이루어져 있다.

 

OpenGL에서 사용하기 위한 수학 라이브러리입니다. 이 라이브러리는 벡터, 행렬, 쿼터니언 등을 다루는 함수와 클래스를 제공하여 OpenGL 애플리케이션에서 수학적 연산을 수행할 때 편리하게 사용할 수 있도록 돕습니다. GLM은 C++ 템플릿 라이브러리로 구현되어 있으며, OpenGL에서 자주 사용되는 데이터 형식과 연산을 지원합니다. 주로 변환, 투영, 회전 등의 그래픽 관련 수학적 계산에 사용됩니다.

 

또한 DLSL에서는 Vertex shader나 Fragment shader의 vector연산과 matrix연산을 c++에서도 사용가능하게 해준다 하지만 차이점으론 DLSL에서는 Vertex shader나 Fragment shader는 gpu를 사용하여 vector와 matrix를 병렬적으로 처리해 주는데 GLM에서 작성한 vector와 matrix연산은 CPU에서 순차적으로 처리한다. 하지만 CPU에서도 병렬로 처리하는 방법을 제공하는 CUDA나 OpenCL 및 SYCL이 있다.

GLM 설치

다운로드 URL :  https://github.com/g-truc/glm

 

GitHub - g-truc/glm: OpenGL Mathematics (GLM)

OpenGL Mathematics (GLM). Contribute to g-truc/glm development by creating an account on GitHub.

github.com

 

glm 복사

다운받은 glm폴더의 glm폴더를

C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\xx.yy.zzzzz\include 안에 복사해준다.

테스트

#ifndef __cplusplus
#error This file works only with C++
#endif

#pragma warning(disable:4711 4710 4100 4514 4626 4774 4365 4625 4464 4571 4201 5026 5027 5039)

#define GLM_FORCE_SWIZZLE//.xxyy와 같은 수의질 오퍼레이터가 glm에서도 사용가능하게
#include<glm/glm.hpp>
#include<glm/gtx/string_cast.hpp>//to_string()사용가능
#include<glm/gtc/type_ptr.hpp>//make_mat4() 어떤 포인터를 자료형으로 변환할수 있다.

#include<iostream>
using namespace std;

int main(void) {
	// glm test
	glm::vec4 a(1.0f, 2.0f, 3.0f, 1.0f);
	glm::vec4 b = glm::vec4(2.0f, 3.0f, 1.0f, 1.0f);
	glm::vec4 c = a + b;
	float d = glm::dot(a, b);
	std::cout << glm::to_string(a) << " + " << std::endl;
	std::cout << glm::to_string(b) << " --> " << std::endl;
	std::cout << glm::to_string(c) << std::endl;
	std::cout << "a . b = " << d << std::endl;
	// glm test
	glm::vec4 pos = glm::vec4(1.0f, 2.0f, 3.0f, 1.0f);
	std::cout << "pos = " << glm::to_string(pos) << std::endl;
	const float mat_val[] = { // CAUTION: column major
		2.0f, 0.0f, 0.0f, 1.0f,
		0.0f, 3.0f, 0.0f, 2.0f,
		0.0f, 0.0f, 1.0f, 3.0f,
		0.0f, 0.0f, 0.0f, 1.0f,
	};
	glm::mat4 mat = glm::make_mat4(mat_val);
	std::cout << "mat = " << glm::to_string(mat) << std::endl;
	std::cout << "mat * pos = " << glm::to_string(mat * pos) << std::endl;
	std::cout << "pos * mat = " << glm::to_string(pos * mat) << std::endl;
	// done
	return 0;
}

 

 

 

728x90