개발/이슈 해결 모음

[Computer Graphics / M1 MAC] M1 Mac 에서 freeglut 사용하기

Junhyung-Choi 2022. 9. 16. 13:35
반응형

작업 환경 : M1 Mac (MacOS Monteray) / VSCode / iTerm

스택 : CMake, Homebrew

설치 필요 프로그램 : https://www.xquartz.org/

 

XQuartz

The XQuartz project is an open-source effort to develop a version of the X.Org X Window System that runs on macOS. Together with supporting libraries and applications, it forms the X11.app that Apple shipped with OS X versions 10.5 through 10.7. Quick Down

www.xquartz.org

 

brew install cmake freeglut

이후 프로젝트 파일이 있는 곳에 CMakeLists.txt 생성

아래와 같이 작성

cmake_minimum_required(VERSION 3.24)
PROJECT ( "test" )

SET ( OUTPUT_ELF
        "test.out"
        )
SET ( SRC_FILES
        "./main.cpp"
        )

# 공통 컴파일러
SET ( CMAKE_CXX_COMPILER "g++" )

find_package(OpenGL REQUIRED)


set(GLUT_LIBRARIES libglut.3.dylib)
set(GLUT_INCLUDE_DIRS /opt/homebrew/include)
set(GLUT_LIBRARY_DIRS /opt/homebrew/lib)

INCLUDE_DIRECTORIES ( ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} )
ADD_COMPILE_OPTIONS ( "-g" )

LINK_LIBRARIES ( ${OPENGL_LIBRRARIES} ${GLUT_LIBRARIES})

LINK_DIRECTORIES ( ${OPENGL_LIBRARY_DIRS} ${GLUT_LIBRARY_DIRS})

ADD_EXECUTABLE( ${OUTPUT_ELF} ${SRC_FILES} )
target_link_libraries( ${OUTPUT_ELF} ${OPENGL_LIBRARIES} )

 

아래와 같이 main.cpp 작성

 

#include <iostream>
#include <GL/freeglut.h>

void display() {
    glClear(GL_COLOR_BUFFER_BIT); 
    glColor3f(1.0, 0.0, 0.0); 
    glBegin(GL_POLYGON); 
    glVertex2f(-0.5, -0.5); 
    glVertex2f(0.5, -0.5); 
    glVertex2f(0.5, 0.5); 
    glVertex2f(-0.5, 0.5); 
    glEnd();
    glFlush();
}

/* Main function: GLUT runs as a console application starting at main()  */
int main(int argc, char** argv) {
   glutInit(&argc, argv);                 // Initialize GLUT
   glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
   glutInitWindowSize(320, 320);   // Set the window's initial width & height
   glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
   glutDisplayFunc(display); // Register display callback handler for window re-paint
   glutMainLoop();           // Enter the infinitely event-processing loop
   return 0;
}

 

만일 

#include <GL/freeglut.h>

에서 빨간 줄이 나온다면

 

.vscode 안의 c_cpp_properties.json 의 includepath에 아까 homebrew로 설치한 freeglut의 include 폴더를 추가

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "/opt/homebrew/include",
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-clang-arm64",
            "configurationProvider": "ms-vscode.makefile-tools"
        }
    ],
    "version": 4
}

 

이후 

cmake ./
make
./test.out

위 3가지 명령어를 통해 프로그램 동작 확인

728x90