diff --git a/CMakeLists.txt b/CMakeLists.txt index 449e203..8e20e56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,8 @@ add_executable(game src/mesh.cc src/file_util.cc src/shader.cc - src/texture.cc) + src/texture.cc + src/shapes.cc) target_include_directories(game PRIVATE src external/stb external/linalg) target_link_libraries(game PRIVATE glad stb_image glfw) diff --git a/shaders/textured.vert b/shaders/textured.vert index b3698e3..f7065b1 100644 --- a/shaders/textured.vert +++ b/shaders/textured.vert @@ -3,12 +3,13 @@ layout(location = 0) in vec3 aPosition; layout(location = 2) in vec2 aUv; // 1番は第10章の法線のために空けておく / location 1 is kept free for the normal (chapter 10) uniform mat4 uModel; // モデル座標 → ワールド座標 / model space → world space -uniform mat4 uProj; // 投影(この章ではアスペクト比の補正だけ) / projection (only the aspect-ratio fix in this chapter) +uniform mat4 uView; // ワールド座標 → ビュー座標(カメラから見た座標) / world space → view space (seen from the camera) +uniform mat4 uProj; // ビュー座標 → クリップ座標(透視投影) / view space → clip space (perspective) out vec2 vUv; void main() { vUv = aUv; - // 行列は右から順に効く:先にuModel、次にuProj / matrices apply right to left: uModel first, then uProj - gl_Position = uProj * uModel * vec4(aPosition, 1.0); + // 行列は右から順に効く:モデル → ビュー → 投影 / matrices apply right to left: model → view → projection + gl_Position = uProj * uView * uModel * vec4(aPosition, 1.0); } diff --git a/src/main.cc b/src/main.cc index b4e0ee7..b85710d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,67 +1,74 @@ -// 第7章:変換 -// Chapter 7: transformations +// 第8章:3Dへ:座標系と深度 +// Chapter 8: going 3D: coordinate systems and depth // // 操作 / Controls: +// Z 深度テストの切り替え / toggle the depth test // スペース 一時停止 / pause // R シェーダーを読み直す / reload the shaders // Esc 終了 / quit #include "mesh.hh" #include "shader.hh" +#include "shapes.hh" #include "texture.hh" #include "window.hh" #include -#include -#include #include #include -#include -struct TexturedVertex { +// 立方体の位置と回転軸 / where each cube is and the axis it spins around +struct CubeInfo { Vec3 position; - Vec2 uv; + Vec3 axis; + float speed; // ラジアン毎秒 / radians per second }; -constexpr VertexAttrib kTexturedVertexLayout[] = { - {0, 3, offsetof(TexturedVertex, position)}, - {2, 2, offsetof(TexturedVertex, uv)}, +static const CubeInfo kCubes[] = { + {{0.0f, 0.0f, 0.0f}, {0.3f, 1.0f, 0.2f}, 0.6f}, + {{2.4f, 1.2f, -3.5f}, {1.0f, 0.3f, 0.0f}, 0.9f}, + {{-2.2f, -1.0f, -2.5f}, {0.0f, 1.0f, 0.6f}, -0.7f}, + {{-3.8f, 1.6f, -7.0f}, {0.5f, 0.5f, 0.1f}, 0.5f}, + {{2.6f, -1.3f, -1.2f}, {0.2f, 0.4f, 1.0f}, -1.1f}, + {{-1.3f, 2.0f, -4.0f}, {1.0f, 0.0f, 0.4f}, 0.8f}, + {{1.0f, -0.6f, -6.5f}, {0.7f, 1.0f, 0.3f}, -0.5f}, + {{4.8f, 0.4f, -8.5f}, {0.1f, 1.0f, 0.9f}, 1.2f}, + {{-4.4f, -1.5f, -5.5f}, {0.9f, 0.2f, 0.6f}, -0.9f}, + {{0.4f, 2.6f, -9.5f}, {0.4f, 0.8f, 1.0f}, 0.7f}, }; -// TRS:拡大縮小 → 回転 → 平行移動の順に効く(行列は右から掛かる) -// TRS: scale, then rotate, then translate (matrices apply from the right) -static Mat4 trs(Vec3 translation, float angleZ, Vec3 scaling) { - return translate(translation) * rotateZ(angleZ) * scale(scaling); -} - int main() { Window window; - if (!window.create(1280, 720, "OpenGL Tutorial - Chapter 7")) return 1; + if (!window.create(1280, 720, "OpenGL Tutorial - Chapter 8")) return 1; std::optional shader = Shader::load("shaders/textured.vert", "shaders/textured.frag"); std::optional crate = Texture2D::load("assets/textures/crate.png"); std::optional emblem = Texture2D::load("assets/textures/emblem.png"); if (!shader || !crate || !emblem) return 1; + // 近いものが遠いものを隠すようにする / make near things hide far things + bool depthTest = true; + glEnable(GL_DEPTH_TEST); + bool paused = false; window.onKey = [&](int key, int action, int /*mods*/) { if (action != GLFW_PRESS) return; if (key == GLFW_KEY_ESCAPE) window.close(); if (key == GLFW_KEY_SPACE) paused = !paused; if (key == GLFW_KEY_R && shader->reload()) printf("シェーダーを読み直した / shaders reloaded\n"); + if (key == GLFW_KEY_Z) { + depthTest = !depthTest; + if (depthTest) + glEnable(GL_DEPTH_TEST); + else + glDisable(GL_DEPTH_TEST); + printf("depth test %s\n", depthTest ? "on" : "off"); + } }; - // 原点を中心とした1×1の正方形(モデル座標)。大きさと位置は行列で決める。 - // A 1×1 square centred on the origin (model space). Size and position now come from matrices. - std::vector vertices = { - {{-0.5f, -0.5f, 0.0f}, {0.0f, 0.0f}}, - {{ 0.5f, -0.5f, 0.0f}, {1.0f, 0.0f}}, - {{ 0.5f, 0.5f, 0.0f}, {1.0f, 1.0f}}, - {{-0.5f, 0.5f, 0.0f}, {0.0f, 1.0f}}, - }; - Mesh quad(vertices, {0, 1, 2, 2, 3, 0}, kTexturedVertexLayout); + Mesh cube = makeCube(); - float t = 0.0f; // アニメーションの時間 / animation time + float t = 0.0f; double lastTime = window.time(); while (!window.shouldClose()) { double now = window.time(); @@ -71,27 +78,27 @@ int main() { if (!paused) t += dt; glClearColor(0.10f, 0.15f, 0.25f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); + // 色と深度の両方を毎フレーム消す / clear both colour and depth every frame + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // ビュー:カメラは(0, 1.5, 6)から少し奥を見る / view: the camera at (0, 1.5, 6) looks a little into the scene + Mat4 view = lookAt({0.0f, 1.5f, 6.0f}, {0.0f, 0.0f, -2.0f}, {0.0f, 1.0f, 0.0f}); + // 投影:縦の視野角45度。アスペクト比は毎フレーム読むので、ウィンドウを変えても歪まない。 + // projection: 45° vertical field of view. The aspect ratio is read every frame, so resizing does not distort. + Mat4 proj = perspectiveGl(radians(45.0f), window.aspect(), 0.1f, 100.0f); crate->bind(0); emblem->bind(1); shader->use(); shader->set("uMix", 0.8f); shader->set("uEmblemScale", 1.0f); + shader->set("uView", view); + shader->set("uProj", proj); - // 一番簡単な「投影」:xをアスペクト比で割って、正方形を正方形に見せる(第8章で本物の投影行列) - // The simplest "projection": divide x by the aspect ratio so squares look square (real projection in chapter 8) - shader->set("uProj", scale({1.0f / window.aspect(), 1.0f, 1.0f})); - - // 同じ四角形を違う行列で二回描く / draw the same quad twice with different matrices - // 1. 右下:回り続ける / bottom right: keeps rotating - shader->set("uModel", trs({0.6f, -0.3f, 0.0f}, t, {0.8f, 0.8f, 1.0f})); - quad.draw(); - - // 2. 左上:大きさが脈打つ / top left: its size pulses - float s = 0.5f + 0.15f * std::sin(t * 3.0f); - shader->set("uModel", trs({-0.6f, 0.35f, 0.0f}, 0.0f, {s, s, 1.0f})); - quad.draw(); + for (const CubeInfo &c : kCubes) { + shader->set("uModel", translate(c.position) * rotate(c.axis, t * c.speed + c.position.x)); + cube.draw(); + } window.swapBuffers(); } diff --git a/src/mesh.hh b/src/mesh.hh index d00ee20..ab2daf9 100644 --- a/src/mesh.hh +++ b/src/mesh.hh @@ -4,6 +4,9 @@ #include "buffer.hh" +#include + +#include #include #include #include @@ -20,6 +23,20 @@ struct VertexAttrib { GLboolean normalized = GL_FALSE; // 整数を0〜1にするか / map integers to 0..1? }; +// 3Dの標準の頂点:位置・法線・UV。シェーダーではlocation 0, 1, 2。 +// The standard 3D vertex: position, normal, UV. Locations 0, 1, 2 in the shaders. +struct Vertex3D { + Vec3 position; + Vec3 normal; // 面の向き(第10章のライティングで使う) / which way the surface faces (used for lighting in chapter 10) + Vec2 uv; +}; + +constexpr VertexAttrib kVertex3DLayout[] = { + {0, 3, offsetof(Vertex3D, position)}, // layout(location = 0) in vec3 aPosition + {1, 3, offsetof(Vertex3D, normal)}, // layout(location = 1) in vec3 aNormal + {2, 2, offsetof(Vertex3D, uv)}, // layout(location = 2) in vec2 aUv +}; + // 一つの描画単位(RAII、コピー不可・移動可)。インデックスが空ならglDrawArraysで描く。 // One drawable unit (RAII, non-copyable, movable). With no indices it draws with glDrawArrays. class Mesh { diff --git a/src/shapes.cc b/src/shapes.cc new file mode 100644 index 0000000..1f98689 --- /dev/null +++ b/src/shapes.cc @@ -0,0 +1,30 @@ +#include "shapes.hh" + +#include + +// 一つの面(正方形)を足す。normal = 外向き、right × up = normal になるように選ぶ。 +// Adds one face (a square). normal points outwards; choose right and up so that right × up = normal. +static void addFace(std::vector &vertices, std::vector &indices, Vec3 normal, Vec3 right, + Vec3 up) { + std::uint32_t first = static_cast(vertices.size()); + Vec3 c = 0.5f * normal; // 面の中心 / centre of the face + Vec3 r = 0.5f * right, u = 0.5f * up; + vertices.push_back({c - r - u, normal, {0.0f, 0.0f}}); // 左下 / bottom left + vertices.push_back({c + r - u, normal, {1.0f, 0.0f}}); // 右下 / bottom right + vertices.push_back({c + r + u, normal, {1.0f, 1.0f}}); // 右上 / top right + vertices.push_back({c - r + u, normal, {0.0f, 1.0f}}); // 左上 / top left + // 反時計回りの三角形2つ / two counter-clockwise triangles + indices.insert(indices.end(), {first, first + 1, first + 2, first + 2, first + 3, first}); +} + +Mesh makeCube() { + std::vector vertices; + std::vector indices; + addFace(vertices, indices, {0, 0, 1}, {1, 0, 0}, {0, 1, 0}); // 前 +Z / front + addFace(vertices, indices, {0, 0, -1}, {-1, 0, 0}, {0, 1, 0}); // 後 -Z / back + addFace(vertices, indices, {1, 0, 0}, {0, 0, -1}, {0, 1, 0}); // 右 +X / right + addFace(vertices, indices, {-1, 0, 0}, {0, 0, 1}, {0, 1, 0}); // 左 -X / left + addFace(vertices, indices, {0, 1, 0}, {1, 0, 0}, {0, 0, -1}); // 上 +Y / top + addFace(vertices, indices, {0, -1, 0}, {1, 0, 0}, {0, 0, 1}); // 下 -Y / bottom + return Mesh(vertices, indices, kVertex3DLayout); +} diff --git a/src/shapes.hh b/src/shapes.hh new file mode 100644 index 0000000..9ec5c68 --- /dev/null +++ b/src/shapes.hh @@ -0,0 +1,10 @@ +// よく使う形のメッシュを作る / builds meshes for common shapes +#pragma once + +#include "mesh.hh" + +// 原点を中心とした1×1×1の立方体。面ごとに頂点4つ(UVと法線が面ごとに違うので共有できない)=24頂点、36インデックス。 +// 外から見て反時計回り(第16章のカリング用)。 +// A 1×1×1 cube centred on the origin. 4 vertices per face (UVs and normals differ per face, so corners +// cannot be shared) = 24 vertices, 36 indices. Counter-clockwise seen from outside (for culling in chapter 16). +Mesh makeCube();