1. Modern OpenGL 이란
OpenGL은 3차원 그래픽스 API 중 하나이며, 플랫폼 독립적으로 설계되어 있어 다양한 OS에서 수정없이 돌아간다는 장점이 있습니다.
OpenGL 1.x로 대표되는 기존 Legacy OpenGL은 그래픽스 하드웨어의 주어진 기능을 활용하는데 초점이 맞춰져 있었습니다. 즉 고정된 기능만 사용할 수 있는 고정 렌더링 파이프라인(Fixed rendering pipeline)만을 지원하였습니다.
OpenGL 2.x 이후를 Modern OpenGL이라 칭하며, 프로그래머가 원하는 기능을 shader를 통해 프로그래밍할 수 있는 렌더링 파이프라인(Programmable rendering pipeline)을 지원하기 시작했습니다.
본 포스팅에서는 Ubuntu LTS 18.04+에서 Modern OpenGL의 개발환경 구축 방법을 소개합니다.
(필자는 MAC M2에서 UTM 가상머신을 사용하여 Ubuntu 22.04를 설치하였습니다.)
2. OpenGL 개발환경 설치
apt를 사용하여 C++ 개발환경, GLEW, GLFW 등을 설치합니다.
sudo apt update # apt 패키지 정보 업데이트
sudo apt install build-essential # C/C++ 개발환경 설치
sudo apt install git curl # git, curl 설치
sudo apt install libglew-dev glew-utils # GLEW 라이브러리 및 유틸리티 설치
sudo apt install libglfw3-dev # GLFW 라이브러리 설치
GLEW와 GLFW를 설치하여 생기는 헤더 파일과 라이브러리 파일은 이후 g++ 컴파일러를 이용해서 모던 OpenGL 기반 프로그램 작성 시 활용됩니다.
최근 출시된 대부분의 pc나 노트북은 OpenGL 2.x 이상을 지원하지만, 혹시 모르니 내 컴퓨터의 그래픽카드가 지원하는 OpenGL 버전을 확인해봅시다.
glewinfo > info.txt # GLEW 유틸리티인 glewinfo의 실행 결과를 info.txt로 리다이렉션
vi info.txt # info.txt 내용 확인
제 노트북의 그래픽카드는 4.5까지 지원하네요!
3. 간단한 OpenGL 기반 프로그램 작성하기
3.1 소스코드 작성
main.cpp 파일을 작성합니다.
///// main.cpp
///// OpenGL 3+, GLEW, GLFW3
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
int main(void)
{
GLFWwindow* window;
// GLFW 라이브러리 초기화
if (!glfwInit())
return -1;
// GLFW 윈도우 생성 (OpenGL context 포함한)
window = glfwCreateWindow(500, 500, "Hello OpenGL!!!", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
// 현재 OpenGL context를 윈도우에서 하나로 만들기
glfwMakeContextCurrent(window);
// GLEW 라이브러리 초기화
if (glewInit() != GLEW_OK)
std::cout << "GLEW Init Error!" << std::endl;
// 내 pc 그래픽 카드에서 지원하는 OpenGL 버전 프린트
std::cout << glGetString(GL_VERSION) << std::endl;
// OpenGL 초기
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glClearColor(0.0, 0.0, 1.0, 0.0); // 윈도우 내부색 파란색으로 (RGB)
// 유저가 윈도우 닫을 때 까지 루프 돌리기
while (!glfwWindowShouldClose(window))
{
// 렌더
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 앞 버퍼랑 뒷 버퍼 스와프
glfwSwapBuffers(window);
// 이벤트 처리
glfwPollEvents();
}
glfwTerminate();
return 0;
}
3.2 컴파일 및 실행
터미널에서 g++ 컴파일러를 이용해서 작성한 main.cpp 파일을 컴파일 합니다.
g++ main.cpp -o hello -lGL -lGLEW -lglfw
컴파일이 성공하면 현재 디렉토리에 실행 가능한 파일인 hello가 생성됩니다.
만들어진 실행파일을 다음과 같이 실행시킵니다.
./hello &
위 화면처럼 파란색 윈도우가 떴다면 성공입니다!! 정상적으로 Modern OpenGL 개발 환경 구축을 완료했습니다.
3.3 컴파일 과정 간소화하기
그런데 컴파일 옵션이 너무 길어서 아무래도 타이핑하기 귀찮습니다. 소스코드가 수정되면 매번 컴파일 과정을 거쳐 실행파일을 만들어줘야 하거든요.
동일한 디렉토리에 다음과 같이 Makefile을 작성해서 g++ 컴파일 과정을 간단하게 만들어봅시다.
g++ -std=c+11 main.cpp -o hello -lGL -lGLEW -lglfw
이제 터미널에서 make라고 입력하기만 하면 컴파일이 진행됩니다. 편하죠.
make
출처 및 참고자료 : 국민대학교 소프트웨어융합대학 컴퓨터그래픽스 교과목
본 포스팅은 필자가 2023.07.23 github blog에 업로드한 글을 가져왔습니다.
'CS > Computer Graphics(C++)' 카테고리의 다른 글
[컴퓨터그래픽스] 1. Overview (0) | 2023.10.30 |
---|