diff --git a/shaders/textured.vert b/shaders/textured.vert index 9c8b6ef..b3698e3 100644 --- a/shaders/textured.vert +++ b/shaders/textured.vert @@ -2,9 +2,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) + out vec2 vUv; void main() { vUv = aUv; - gl_Position = vec4(aPosition, 1.0); + // 行列は右から順に効く:先にuModel、次にuProj / matrices apply right to left: uModel first, then uProj + gl_Position = uProj * uModel * vec4(aPosition, 1.0); } diff --git a/src/main.cc b/src/main.cc index 3627ff4..b4e0ee7 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,12 +1,10 @@ -// 第6章:テクスチャ -// Chapter 6: textures +// 第7章:変換 +// Chapter 7: transformations // // 操作 / Controls: -// ↑/↓ 二枚目のテクスチャを混ぜる量 / how much of the second texture to mix in -// F フィルタ:線型⇔最近傍 / filtering: linear ⇔ nearest -// W 二枚目のラップモードを切り替え / cycle the second texture's wrap mode -// R シェーダーを読み直す / reload the shaders -// Esc 終了 / quit +// スペース 一時停止 / pause +// R シェーダーを読み直す / reload the shaders +// Esc 終了 / quit #include "mesh.hh" #include "shader.hh" #include "texture.hh" @@ -14,100 +12,85 @@ #include -#include +#include #include #include #include #include -// 位置とテクスチャ座標(UV) / position and texture coordinates (UV) struct TexturedVertex { Vec3 position; Vec2 uv; }; constexpr VertexAttrib kTexturedVertexLayout[] = { - {0, 3, offsetof(TexturedVertex, position)}, // layout(location = 0) in vec3 aPosition - {2, 2, offsetof(TexturedVertex, uv)}, // layout(location = 2) in vec2 aUv + {0, 3, offsetof(TexturedVertex, position)}, + {2, 2, offsetof(TexturedVertex, uv)}, }; -struct WrapMode { - GLenum mode; - const char *name; -}; -static const WrapMode kWrapModes[] = { - {GL_REPEAT, "GL_REPEAT"}, - {GL_MIRRORED_REPEAT, "GL_MIRRORED_REPEAT"}, - {GL_CLAMP_TO_EDGE, "GL_CLAMP_TO_EDGE"}, - {GL_CLAMP_TO_BORDER, "GL_CLAMP_TO_BORDER"}, -}; +// 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 6")) return 1; + if (!window.create(1280, 720, "OpenGL Tutorial - Chapter 7")) 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; - // GL_CLAMP_TO_BORDERの時、UVが0〜1の外ならこの色 / with GL_CLAMP_TO_BORDER, UVs outside 0..1 get this colour - const float borderColor[4] = {0.95f, 0.75f, 0.85f, 1.0f}; - glTextureParameterfv(emblem->id(), GL_TEXTURE_BORDER_COLOR, borderColor); - - float mixAmount = 0.8f; - bool nearest = false; - int wrapIndex = 0; - + 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_F) { - nearest = !nearest; - for (Texture2D *t : {&*crate, &*emblem}) { - if (nearest) - t->setFilter(GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST); // 一番近いテクセル / the nearest texel - else - t->setFilter(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR); // 周りの4テクセルを補間 / blend the 4 around - } - printf("filter: %s\n", nearest ? "nearest" : "linear"); - } - if (key == GLFW_KEY_W) { - wrapIndex = (wrapIndex + 1) % 4; - emblem->setWrap(kWrapModes[wrapIndex].mode, kWrapModes[wrapIndex].mode); - printf("wrap: %s\n", kWrapModes[wrapIndex].name); - } }; - // 画面の中央の正方形(xは16:9のために縮める) / a square in the middle of the screen (x shrunk for 16:9) - const float hx = 0.8f * 720.0f / 1280.0f, hy = 0.8f; + // 原点を中心とした1×1の正方形(モデル座標)。大きさと位置は行列で決める。 + // A 1×1 square centred on the origin (model space). Size and position now come from matrices. std::vector vertices = { - {{-hx, -hy, 0.0f}, {0.0f, 0.0f}}, // 左下 = UV (0, 0) / bottom left = UV (0, 0) - {{ hx, -hy, 0.0f}, {1.0f, 0.0f}}, // 右下 / bottom right - {{ hx, hy, 0.0f}, {1.0f, 1.0f}}, // 右上 / top right - {{-hx, hy, 0.0f}, {0.0f, 1.0f}}, // 左上 / top left + {{-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); + float t = 0.0f; // アニメーションの時間 / animation time double lastTime = window.time(); while (!window.shouldClose()) { double now = window.time(); float dt = static_cast(now - lastTime); lastTime = now; window.pollEvents(); - - if (window.keyDown(GLFW_KEY_UP)) mixAmount = std::clamp(mixAmount + dt, 0.0f, 1.0f); - if (window.keyDown(GLFW_KEY_DOWN)) mixAmount = std::clamp(mixAmount - dt, 0.0f, 1.0f); + if (!paused) t += dt; glClearColor(0.10f, 0.15f, 0.25f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - crate->bind(0); // ユニット0 → uTexture0 / unit 0 → uTexture0 - emblem->bind(1); // ユニット1 → uTexture1 / unit 1 → uTexture1 + crate->bind(0); + emblem->bind(1); shader->use(); - shader->set("uMix", mixAmount); - shader->set("uEmblemScale", 2.0f); + shader->set("uMix", 0.8f); + shader->set("uEmblemScale", 1.0f); + + // 一番簡単な「投影」: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(); window.swapBuffers();