1. OpenGLグラフィックスプログラミング入門

OpenGLチュートリアルシリーズは、他のコースを先に受講せずに進める事が出来ますが、GLSLコースを先に受講する事をお勧めします。

1.1 OpenGLとは?

OpenGLは、1990年代にSilicon Graphicsによって作成された非常に古いグラフィックスAPIで、2D及び3Dレンダリングをプラットフォーム間で標準化し、普及させる為に開発されました。 レガシーAPIであるにも関わらず、今日でもホビーストのプロジェクトだけでなく、プロフェッショナルなゲームやレンダリングを必要とする他のソフトウェアで使用されています。 其の理由は、OpenGLがシンプルなAPIであり、学習が非常に簡単である一方で、多くの事を抽象化している為です。

OpenGLは現在積極的に開発されてはいませんが、拡張を通じてサポートされており、多くのプロジェクトで依然として使用されています。 Vulkan、DirectX 12、Metal等の最新のAPIと比較すると、OpenGLは高レベルな抽象化とドライバーのオーバーヘッドが大きい為、パフォーマンスが劣りますが、最新のハードウェアをサポートする為のアップデートも行われています。 然し、最新のAPIを学びたいと考えている場合、先ずOpenGLから始める事を強くお勧めします。 OpenGLで学んだ多くの知識は、Vulkan、DirectX 12、Metalに転用出来る事が分かるでしょう。

AmigaOSやIRIX等のニッチ又はレガシーなオペレーティングシステムでは、OpenGLが唯一実際的に利用可能なグラフィックスAPIである場合がありますが、サポートはシステムによって異なります。 OpenGL 4.6(コアプロファイル)を学習します。

1.2 Flow of the course

This course assumes you know nothing about graphics programming, but you're already familiar with C++.We will go over GLSL shading as that's part of OpenGL, but for much deeper contents and understanding, please follow the GLSL course first.We will go over the basics such as rendering a triangle on the screen, transformations, textures, shaders, cameras, lighting.Then we'll transition to more advanced topics such as loading glTF models (specifically GLB file format), advanced graphics concepts such as depth and stencil testing, bldening, culling, framebuffers, cubemapping, instancing, anti aliasing, physically based rendering (PBR), and so on.At the end of the course we will take everything we have learned, and make 4 simple games out of it.

The first game will be a Space Invader clone, which will teach you the concepts of collision detection, scene reloading, instancing, and basic controls.The second game will be a 2D fighting game to make you familiar with concepts like level scrolling, AI controlled enemies, 2D physics, and scene graphs.The third game will be a top-down 2D RPG to get you familiar with 2D movements and swapping between scenes.The forth game will be a 3D third person shooter to also make you familiar with a 3D environment, 3D math, and 3D physics.

So by the end of this course, you will already have 4 games added to your portfolio.

1.2 始め方

Unlike with the GLSL tutorial, I won't prepare a project in advance.Instead, we'll set one up by ourselves.This tutorial will assume you're targetting Windows, Linux, and FreeBSD, and so we will set up the project accordingly.Just like back then, you only need to install GLFW and CMake in advance.For Windows, it's recommended to install Visual Studio as well.For Linux and FreeBSD, use whichever text editor you want to use.

Windows

Download GLFW, and unzip it in C:\.Next, download and install CMake.

Linux and FreeBSD

Linux又はFreeBSDを使用する場合、OSのパッケージマネージャーからOpenGLドライバーとGLFWをインストールして下さい。

FreeBSDの場合:

pkg install glfw cmake

Devuan又はDebianの場合:

apt install libglfw3-dev libglfw3 cmake

他のディストリビューションの場合は、各自で調べて下さい。
此のスクリプトはClangコンパイラを使用している事を前提としています。

「ccが見つからない」というエラーが出た場合、build.shファイルを編集し、ccをgccに変更して下さい。

tar zxfv glsltester.tar.gz
cd glsltester
chmod +x build.sh

コンパイルして実行するには、./build.shを実行して下さい。

コード内では、メイン関数、抽象化レイヤー、及び幾つかのヘルパーツールを準備しています。

ファイル: /dev/null | b/.gitignore

前新
@@ -0,0 +1 @@
    1 build/

ファイル: /dev/null | b/CMakeLists.txt

前新
@@ -0,0 +1,38 @@
    1 cmake_minimum_required(VERSION 3.20)
    2 project(opengl_tutorial C CXX)
    3 set(CMAKE_CXX_STANDARD 20)
    4 set(CMAKE_CXX_STANDARD_REQUIRED ON)
    5  
    6 # GLFW:Windowsは公式のビルド済みバイナリ、Linux/FreeBSDはシステムのパッケージ
    7 # GLFW: prebuilt binaries on Windows, the system package on Linux/FreeBSD
    8 if(WIN32)
    9 set(GLFW_ROOT "C:/glfw-3.4.bin.WIN64" CACHE PATH "GLFW prebuilt binaries")
    10 add_library(glfw STATIC IMPORTED)
    11 set_target_properties(glfw PROPERTIES
    12 IMPORTED_LOCATION "${GLFW_ROOT}/lib-vc2022/glfw3.lib"
    13 INTERFACE_INCLUDE_DIRECTORIES "${GLFW_ROOT}/include")
    14 else()
    15 find_package(glfw3 3.3 REQUIRED)
    16 endif()
    17  
    18 # GLAD:生成したOpenGLローダー / the generated OpenGL loader
    19 add_library(glad STATIC external/glad/src/glad.c)
    20 target_include_directories(glad PUBLIC external/glad/include)
    21  
    22 add_executable(game src/main.cc)
    23 target_include_directories(game PRIVATE src external/stb external/linalg)
    24 target_link_libraries(game PRIVATE glad glfw)
    25  
    26 # 警告を有効にする(自分のコードだけ) / enable warnings (our own code only)
    27 if(MSVC)
    28 target_compile_options(game PRIVATE /W4 /utf-8)
    29 else()
    30 target_compile_options(game PRIVATE -Wall -Wextra)
    31 endif()
    32  
    33 # シェーダーとアセットは作業ディレクトリからの相対パスで読む。
    34 # Visual Studioではプロジェクトのルートを作業ディレクトリにする。Unixではルートから ./build/game を実行。
    35 # Shaders and assets are loaded relative to the working directory.
    36 # Visual Studio: use the project root as the working directory. Unix: run ./build/game from the root.
    37 set_property(TARGET game PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
    38 set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT game)

ファイル: /dev/null | b/README.md

前新
@@ -0,0 +1,45 @@
    1 # OpenGL Tutorial
    2  
    3 OpenGLチュートリアルシリーズのサンプルコード。 / Sample code for the OpenGL tutorial series.
    4  
    5 ## 依存ライブラリ / Dependencies
    6  
    7 `external/` に自分で置く(第1章を参照)。 / Put these in `external/` yourself (see chapter 1).
    8  
    9 ```
    10 external/
    11 glad/include/glad/glad.h
    12 glad/include/KHR/khrplatform.h
    13 glad/src/glad.c ← https://glad.dav1d.de/ (C/C++, OpenGL, gl 4.6, Core, Generate a loader)
    14 stb/stb_image.h ← https://github.com/nothings/stb
    15 linalg/linalg.hh ← 線型代数シリーズ / Linear Algebra series
    16 ```
    17  
    18 GLFW 3.4: Windowsは `C:\glfw-3.4.bin.WIN64` にビルド済みバイナリを展開(CMakeの `GLFW_ROOT` で変更可)。
    19 Linux/FreeBSDはパッケージ(Debian: `libglfw3-dev`、FreeBSD: `pkg install glfw`)。
    20 GLFW 3.4: on Windows unzip the prebuilt binaries to `C:\glfw-3.4.bin.WIN64` (change with the CMake variable `GLFW_ROOT`).
    21 On Linux/FreeBSD use the package (Debian: `libglfw3-dev`, FreeBSD: `pkg install glfw`).
    22  
    23 ## ビルドと実行 / Build and run
    24  
    25 Linux / FreeBSD(プロジェクトのルートから / from the project root):
    26  
    27 ```sh
    28 cmake -S . -B build
    29 cmake --build build -j8
    30 ./build/game
    31 ```
    32  
    33 Windows (Visual Studio 2022):
    34  
    35 ```bat
    36 cmake -S . -B build -G "Visual Studio 17 2022"
    37 cmake --build build --config Debug
    38 build\Debug\game.exe
    39 ```
    40  
    41 または `build\opengl_tutorial.sln` を開いてF5(作業ディレクトリはプロジェクトのルートに設定済み)。
    42 Or open `build\opengl_tutorial.sln` and press F5 (the working directory is already set to the project root).
    43  
    44 シェーダーとアセットは作業ディレクトリからの相対パスで読み込むので、必ずプロジェクトのルートから実行すること。
    45 Shaders and assets are loaded relative to the working directory, so always run from the project root.

ファイル: /dev/null | b/src/main.cc

前新
@@ -0,0 +1,63 @@
    1 // 第1章:ウィンドウを開いて色で塗りつぶす
    2 // Chapter 1: open a window and clear it to a colour
    3  
    4 #ifdef _WIN32
    5 // SetConsoleOutputCPの為。GLADより先にインクルードする(APIENTRYの二重定義を防ぐ)
    6 // For SetConsoleOutputCP. Include it before GLAD (so APIENTRY is not defined twice)
    7 #define WIN32_LEAN_AND_MEAN
    8 #define NOMINMAX
    9 #include <windows.h>
    10 #endif
    11 // GLADはGLFWより先にインクルードする / include GLAD before GLFW
    12 #include <glad/glad.h>
    13 #include <GLFW/glfw3.h>
    14  
    15 #include <cstdio>
    16  
    17 int main() {
    18 #ifdef _WIN32
    19 // コンソールの出力をUTF-8にする(日本語のメッセージが文字化けしない様に)
    20 // switch the console output to UTF-8 (so the Japanese messages are not garbled)
    21 SetConsoleOutputCP(CP_UTF8);
    22 #endif
    23 // GLFWを初期化 / initialise GLFW
    24 if (!glfwInit()) {
    25 fprintf(stderr, "GLFWの初期化に失敗 / failed to initialise GLFW\n");
    26 return 1;
    27 }
    28  
    29 // OpenGL 4.6 コアプロファイルを要求 / ask for an OpenGL 4.6 core profile context
    30 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    31 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
    32 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    33  
    34 GLFWwindow *window = glfwCreateWindow(1280, 720, "OpenGL Tutorial - Chapter 1", nullptr, nullptr);
    35 if (!window) {
    36 fprintf(stderr, "ウィンドウの作成に失敗 / failed to create the window (OpenGL 4.6 not supported?)\n");
    37 glfwTerminate();
    38 return 1;
    39 }
    40 glfwMakeContextCurrent(window);
    41  
    42 // GLADでOpenGLの関数を読み込む / load the OpenGL functions with GLAD
    43 if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
    44 fprintf(stderr, "GLADの初期化に失敗 / failed to initialise GLAD\n");
    45 glfwTerminate();
    46 return 1;
    47 }
    48 printf("OpenGL %s\n", (const char *)glGetString(GL_VERSION));
    49 printf("GPU: %s\n", (const char *)glGetString(GL_RENDERER));
    50  
    51 // メインループ / main loop
    52 while (!glfwWindowShouldClose(window)) {
    53 glClearColor(0.10f, 0.15f, 0.25f, 1.0f); // 紺色 / dark blue
    54 glClear(GL_COLOR_BUFFER_BIT);
    55  
    56 glfwSwapBuffers(window); // 描いた画像を表示 / show what we drew
    57 glfwPollEvents(); // 入力やウィンドウのイベントを処理 / handle input and window events
    58 }
    59  
    60 glfwDestroyWindow(window);
    61 glfwTerminate();
    62 return 0;
    63 }


コメント(0)

まだコメントはありません。