diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..eb822bf --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,38 @@ +cmake_minimum_required(VERSION 3.20) +project(opengl_tutorial C CXX) +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# GLFW:Windowsは公式のビルド済みバイナリ、Linux/FreeBSDはシステムのパッケージ +# GLFW: prebuilt binaries on Windows, the system package on Linux/FreeBSD +if(WIN32) + set(GLFW_ROOT "C:/glfw-3.4.bin.WIN64" CACHE PATH "GLFW prebuilt binaries") + add_library(glfw STATIC IMPORTED) + set_target_properties(glfw PROPERTIES + IMPORTED_LOCATION "${GLFW_ROOT}/lib-vc2022/glfw3.lib" + INTERFACE_INCLUDE_DIRECTORIES "${GLFW_ROOT}/include") +else() + find_package(glfw3 3.3 REQUIRED) +endif() + +# GLAD:生成したOpenGLローダー / the generated OpenGL loader +add_library(glad STATIC external/glad/src/glad.c) +target_include_directories(glad PUBLIC external/glad/include) + +add_executable(game src/main.cc) +target_include_directories(game PRIVATE src external/stb external/linalg) +target_link_libraries(game PRIVATE glad glfw) + +# 警告を有効にする(自分のコードだけ) / enable warnings (our own code only) +if(MSVC) + target_compile_options(game PRIVATE /W4 /utf-8) +else() + target_compile_options(game PRIVATE -Wall -Wextra) +endif() + +# シェーダーとアセットは作業ディレクトリからの相対パスで読む。 +# Visual Studioではプロジェクトのルートを作業ディレクトリにする。Unixではルートから ./build/game を実行。 +# Shaders and assets are loaded relative to the working directory. +# Visual Studio: use the project root as the working directory. Unix: run ./build/game from the root. +set_property(TARGET game PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") +set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT game) diff --git a/README.md b/README.md new file mode 100644 index 0000000..68cf1a1 --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +# OpenGL Tutorial + +OpenGLチュートリアルシリーズのサンプルコード。 / Sample code for the OpenGL tutorial series. + +## 依存ライブラリ / Dependencies + +`external/` に自分で置く(第1章を参照)。 / Put these in `external/` yourself (see chapter 1). + +``` +external/ + glad/include/glad/glad.h + glad/include/KHR/khrplatform.h + glad/src/glad.c ← https://glad.dav1d.de/ (C/C++, OpenGL, gl 4.6, Core, Generate a loader) + stb/stb_image.h ← https://github.com/nothings/stb + linalg/linalg.hh ← 線型代数シリーズ / Linear Algebra series +``` + +GLFW 3.4: Windowsは `C:\glfw-3.4.bin.WIN64` にビルド済みバイナリを展開(CMakeの `GLFW_ROOT` で変更可)。 +Linux/FreeBSDはパッケージ(Debian: `libglfw3-dev`、FreeBSD: `pkg install glfw`)。 +GLFW 3.4: on Windows unzip the prebuilt binaries to `C:\glfw-3.4.bin.WIN64` (change with the CMake variable `GLFW_ROOT`). +On Linux/FreeBSD use the package (Debian: `libglfw3-dev`, FreeBSD: `pkg install glfw`). + +## ビルドと実行 / Build and run + +Linux / FreeBSD(プロジェクトのルートから / from the project root): + +```sh +cmake -S . -B build +cmake --build build -j8 +./build/game +``` + +Windows (Visual Studio 2022): + +```bat +cmake -S . -B build -G "Visual Studio 17 2022" +cmake --build build --config Debug +build\Debug\game.exe +``` + +または `build\opengl_tutorial.sln` を開いてF5(作業ディレクトリはプロジェクトのルートに設定済み)。 +Or open `build\opengl_tutorial.sln` and press F5 (the working directory is already set to the project root). + +シェーダーとアセットは作業ディレクトリからの相対パスで読み込むので、必ずプロジェクトのルートから実行すること。 +Shaders and assets are loaded relative to the working directory, so always run from the project root. diff --git a/src/main.cc b/src/main.cc new file mode 100644 index 0000000..d28637d --- /dev/null +++ b/src/main.cc @@ -0,0 +1,63 @@ +// 第1章:ウィンドウを開いて色で塗りつぶす +// Chapter 1: open a window and clear it to a colour + +#ifdef _WIN32 +// SetConsoleOutputCPの為。GLADより先にインクルードする(APIENTRYの二重定義を防ぐ) +// For SetConsoleOutputCP. Include it before GLAD (so APIENTRY is not defined twice) +#define WIN32_LEAN_AND_MEAN +#define NOMINMAX +#include +#endif +// GLADはGLFWより先にインクルードする / include GLAD before GLFW +#include +#include + +#include + +int main() { +#ifdef _WIN32 + // コンソールの出力をUTF-8にする(日本語のメッセージが文字化けしない様に) + // switch the console output to UTF-8 (so the Japanese messages are not garbled) + SetConsoleOutputCP(CP_UTF8); +#endif + // GLFWを初期化 / initialise GLFW + if (!glfwInit()) { + fprintf(stderr, "GLFWの初期化に失敗 / failed to initialise GLFW\n"); + return 1; + } + + // OpenGL 4.6 コアプロファイルを要求 / ask for an OpenGL 4.6 core profile context + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + GLFWwindow *window = glfwCreateWindow(1280, 720, "OpenGL Tutorial - Chapter 1", nullptr, nullptr); + if (!window) { + fprintf(stderr, "ウィンドウの作成に失敗 / failed to create the window (OpenGL 4.6 not supported?)\n"); + glfwTerminate(); + return 1; + } + glfwMakeContextCurrent(window); + + // GLADでOpenGLの関数を読み込む / load the OpenGL functions with GLAD + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { + fprintf(stderr, "GLADの初期化に失敗 / failed to initialise GLAD\n"); + glfwTerminate(); + return 1; + } + printf("OpenGL %s\n", (const char *)glGetString(GL_VERSION)); + printf("GPU: %s\n", (const char *)glGetString(GL_RENDERER)); + + // メインループ / main loop + while (!glfwWindowShouldClose(window)) { + glClearColor(0.10f, 0.15f, 0.25f, 1.0f); // 紺色 / dark blue + glClear(GL_COLOR_BUFFER_BIT); + + glfwSwapBuffers(window); // 描いた画像を表示 / show what we drew + glfwPollEvents(); // 入力やウィンドウのイベントを処理 / handle input and window events + } + + glfwDestroyWindow(window); + glfwTerminate(); + return 0; +}