diff --git a/CMakeLists.txt b/CMakeLists.txt index 3947347..449e203 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,12 @@ endif() add_library(glad STATIC external/glad/src/glad.c) target_include_directories(glad PUBLIC external/glad/include) +# stb_image:実装は src/stb_image_impl.cc だけ。他人のコードなので警告フラグなしの別ライブラリにする +# stb_image: the implementation lives in src/stb_image_impl.cc only. It is someone else's code, +# so it is a separate library built without our warning flags +add_library(stb_image STATIC src/stb_image_impl.cc) +target_include_directories(stb_image PUBLIC external/stb) + add_executable(game src/main.cc src/window.cc @@ -26,9 +32,10 @@ add_executable(game src/buffer.cc src/mesh.cc src/file_util.cc - src/shader.cc) + src/shader.cc + src/texture.cc) target_include_directories(game PRIVATE src external/stb external/linalg) -target_link_libraries(game PRIVATE glad glfw) +target_link_libraries(game PRIVATE glad stb_image glfw) # 警告を有効にする(自分のコードだけ) / enable warnings (our own code only) if(MSVC) diff --git a/assets/LICENSE.txt b/assets/LICENSE.txt new file mode 100644 index 0000000..cfad344 --- /dev/null +++ b/assets/LICENSE.txt @@ -0,0 +1,7 @@ +assets/ のファイルはこのチュートリアルのために作者が自作したもの(作者のPythonのスクリプトで生成。スクリプトは +このプロジェクトには含まれない)。自由に使ってよい。 +The files in assets/ were made for this tutorial by the author (generated with the author's own Python scripts, +which are not part of this project). Free to use for any purpose. + +textures/crate.png 木箱 / wooden crate +textures/emblem.png 桜の紋章 / sakura emblem diff --git a/assets/textures/crate.png b/assets/textures/crate.png new file mode 100644 index 0000000..911f36c Binary files /dev/null and b/assets/textures/crate.png differ diff --git a/assets/textures/emblem.png b/assets/textures/emblem.png new file mode 100644 index 0000000..328f5b6 Binary files /dev/null and b/assets/textures/emblem.png differ diff --git a/shaders/shapes.frag b/shaders/shapes.frag deleted file mode 100644 index 571e5fd..0000000 --- a/shaders/shapes.frag +++ /dev/null @@ -1,13 +0,0 @@ -#version 460 core -in vec3 vColor; - -uniform float uTime; // 秒 / seconds -uniform vec3 uTint; // 図形ごとの色 / per-shape colour - -layout(location = 0) out vec4 out_color; - -void main() { - // 画面を左から右へ流れる明るさの波 / a wave of brightness moving across the screen - float wave = 0.75 + 0.25 * sin(uTime * 3.0 - gl_FragCoord.x * 0.02); - out_color = vec4(vColor * uTint * wave, 1.0); -} diff --git a/shaders/shapes.vert b/shaders/shapes.vert deleted file mode 100644 index 1d98468..0000000 --- a/shaders/shapes.vert +++ /dev/null @@ -1,12 +0,0 @@ -#version 460 core -layout(location = 0) in vec3 aPosition; -layout(location = 1) in vec3 aColor; - -uniform vec2 uOffset; // NDCでの移動量 / offset in NDC - -out vec3 vColor; - -void main() { - vColor = aColor; - gl_Position = vec4(aPosition.xy + uOffset, aPosition.z, 1.0); -} diff --git a/shaders/textured.frag b/shaders/textured.frag new file mode 100644 index 0000000..5cda206 --- /dev/null +++ b/shaders/textured.frag @@ -0,0 +1,22 @@ +#version 460 core +in vec2 vUv; + +// テクスチャユニット0と1から読む(C++側はTexture2D::bind(0)、bind(1)) +// read from texture units 0 and 1 (C++ side: Texture2D::bind(0), bind(1)) +layout(binding = 0) uniform sampler2D uTexture0; +layout(binding = 1) uniform sampler2D uTexture1; + +uniform float uMix; // 二枚目をどれだけ混ぜるか / how much of the second texture to mix in +uniform float uEmblemScale; // 二枚目のUVの拡大率(中心から) / UV scale for the second texture (around the centre) + +layout(location = 0) out vec4 out_color; + +void main() { + vec4 base = texture(uTexture0, vUv); + // 拡大したUVは0〜1をはみ出す:はみ出した部分はラップモードで決まる + // the scaled UVs leave 0..1: what appears there is decided by the wrap mode + vec2 emblemUv = (vUv - 0.5) * uEmblemScale + 0.5; + vec4 emblem = texture(uTexture1, emblemUv); + // 二枚目のアルファで混ぜる / blend by the second texture's alpha + out_color = vec4(mix(base.rgb, emblem.rgb, emblem.a * uMix), 1.0); +} diff --git a/shaders/textured.vert b/shaders/textured.vert new file mode 100644 index 0000000..9c8b6ef --- /dev/null +++ b/shaders/textured.vert @@ -0,0 +1,10 @@ +#version 460 core +layout(location = 0) in vec3 aPosition; +layout(location = 2) in vec2 aUv; // 1番は第10章の法線のために空けておく / location 1 is kept free for the normal (chapter 10) + +out vec2 vUv; + +void main() { + vUv = aUv; + gl_Position = vec4(aPosition, 1.0); +} diff --git a/src/main.cc b/src/main.cc index 57a8330..3627ff4 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,186 +1,116 @@ -// 第5章:シェーダークラスとユニフォーム -// Chapter 5: a shader class and uniforms +// 第6章:テクスチャ +// Chapter 6: textures // // 操作 / 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 -// P ワイヤーフレーム切り替え / toggle wireframe // Esc 終了 / quit #include "mesh.hh" #include "shader.hh" +#include "texture.hh" #include "window.hh" #include -#include +#include #include #include #include #include -// 位置と色を交互に並べた(インターリーブ)頂点 / a vertex with position and colour interleaved -struct ColorVertex { +// 位置とテクスチャ座標(UV) / position and texture coordinates (UV) +struct TexturedVertex { Vec3 position; - Vec3 color; + Vec2 uv; }; -// この形式をVAOに教える / tells the VAO about this format -constexpr VertexAttrib kColorVertexLayout[] = { - {0, 3, offsetof(ColorVertex, position)}, // layout(location = 0) in vec3 aPosition - {1, 3, offsetof(ColorVertex, color)}, // layout(location = 1) in vec3 aColor +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 }; -// NDCは横に引き伸ばされる(1280×720)ので、計算で作る図形はxを縮める。第8章で投影行列が解決する。 -// NDC is stretched horizontally (1280×720), so generated shapes shrink x. Chapter 8's projection fixes this. -constexpr float kAspectFix = 720.0f / 1280.0f; - -// 色相(0〜1)から鮮やかな色 / a saturated colour from a hue in 0..1 -static Vec3 hueColor(float h) { - auto channel = [h](float offset) { - return 0.5f + 0.5f * std::cos(2.0f * PI * (h + offset)); - }; - return {channel(0.0f), channel(2.0f / 3.0f), channel(1.0f / 3.0f)}; -} - -// 中心+周りの頂点で正多角形。インデックスで中心を共有する。 -// A regular polygon from a centre plus a ring of vertices. The indices share the centre vertex. -static Mesh makePolygon(Vec2 center, float radius, int sides) { - std::vector vertices = {{{center.x, center.y, 0.0f}, {1.0f, 1.0f, 1.0f}}}; - std::vector indices; - for (int i = 0; i < sides; ++i) { - float a = 2.0f * PI * static_cast(i) / static_cast(sides); - vertices.push_back({{center.x + radius * kAspectFix * std::cos(a), center.y + radius * std::sin(a), 0.0f}, - hueColor(static_cast(i) / static_cast(sides))}); - std::uint32_t current = static_cast(i + 1); - std::uint32_t next = static_cast((i + 1) % sides + 1); - indices.insert(indices.end(), {0, current, next}); // 三角形一つ / one triangle - } - return Mesh(vertices, indices, kColorVertexLayout); -} - -// 5×3の点 / a 5×3 grid of points -static Mesh makePoints(Vec2 center) { - std::vector vertices; - for (int y = 0; y < 3; ++y) - for (int x = 0; x < 5; ++x) - vertices.push_back({{center.x + (static_cast(x) - 2.0f) * 0.07f, center.y + (static_cast(y) - 1.0f) * 0.12f, 0.0f}, - hueColor(static_cast(x + 5 * y) / 15.0f)}); - return Mesh(vertices, {}, kColorVertexLayout, GL_POINTS); -} - -// 星形の線(GL_LINE_LOOPは最後の頂点と最初を繋ぐ) / a star outline (GL_LINE_LOOP joins the last vertex to the first) -static Mesh makeStar(Vec2 center, float radius) { - std::vector vertices; - for (int i = 0; i < 10; ++i) { - float a = PI * 0.5f + PI * static_cast(i) / 5.0f; - float r = (i % 2 == 0) ? radius : radius * 0.4f; - vertices.push_back({{center.x + r * kAspectFix * std::cos(a), center.y + r * std::sin(a), 0.0f}, - hueColor(static_cast(i) / 10.0f)}); - } - return Mesh(vertices, {}, kColorVertexLayout, GL_LINE_LOOP); -} - -// 波打つリボン(GL_TRIANGLE_STRIPは頂点を一つ足すごとに三角形が一つ増える) -// A wavy ribbon (GL_TRIANGLE_STRIP: every extra vertex adds one triangle) -static Mesh makeRibbon(Vec2 center) { - std::vector vertices; - for (int i = 0; i < 10; ++i) { - float t = static_cast(i) / 9.0f; - float x = center.x + (t - 0.5f) * 0.45f; - float wave = 0.08f * std::sin(t * 2.0f * PI); - Vec3 color = hueColor(t * 0.5f); - vertices.push_back({{x, center.y + wave - 0.08f, 0.0f}, color}); - vertices.push_back({{x, center.y + wave + 0.08f, 0.0f}, 0.6f * color}); - } - return Mesh(vertices, {}, kColorVertexLayout, GL_TRIANGLE_STRIP); -} +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"}, +}; int main() { Window window; - if (!window.create(1280, 720, "OpenGL Tutorial - Chapter 5")) return 1; + if (!window.create(1280, 720, "OpenGL Tutorial - Chapter 6")) return 1; - // シェーダーはファイルから読む(作業ディレクトリ=プロジェクトのルート) - // shaders are read from files (working directory = project root) - std::optional shader = Shader::load("shaders/shapes.vert", "shaders/shapes.frag"); - if (!shader) 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 wireframe = 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_R) { - // 失敗しても古いシェーダーで描き続ける / on failure we keep drawing with the old shader - if (shader->reload()) printf("シェーダーを読み直した / shaders reloaded\n"); + 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_P) { - wireframe = !wireframe; - // 三角形を線で描く(デバッグに便利) / draw triangles as lines (handy for debugging) - glPolygonMode(GL_FRONT_AND_BACK, wireframe ? GL_LINE : GL_FILL); + if (key == GLFW_KEY_W) { + wrapIndex = (wrapIndex + 1) % 4; + emblem->setWrap(kWrapModes[wrapIndex].mode, kWrapModes[wrapIndex].mode); + printf("wrap: %s\n", kWrapModes[wrapIndex].name); } }; - // 1. 色付きの三角形:色は三角形の中で補間される / a coloured triangle: colours are interpolated inside it - std::vector triangleVertices = { - {{-0.85f, 0.20f, 0.0f}, {1.0f, 0.0f, 0.0f}}, // 赤 / red - {{-0.35f, 0.20f, 0.0f}, {0.0f, 1.0f, 0.0f}}, // 緑 / green - {{-0.60f, 0.75f, 0.0f}, {0.0f, 0.0f, 1.0f}}, // 青 / blue - }; - Mesh triangle(triangleVertices, {}, kColorVertexLayout); - - // 2. 四角形:頂点4つ+インデックス6つ(三角形2つが2頂点を共有) - // A quad: 4 vertices + 6 indices (the two triangles share 2 vertices) - std::vector quadVertices = { - {{-0.14f, 0.20f, 0.0f}, {1.0f, 0.8f, 0.2f}}, // 0 左下 / bottom left - {{ 0.14f, 0.20f, 0.0f}, {1.0f, 0.3f, 0.3f}}, // 1 右下 / bottom right - {{ 0.14f, 0.70f, 0.0f}, {0.6f, 0.2f, 0.8f}}, // 2 右上 / top right - {{-0.14f, 0.70f, 0.0f}, {0.2f, 0.6f, 1.0f}}, // 3 左上 / top left + // 画面の中央の正方形(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; + 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 }; - std::vector quadIndices = { - 0, 1, 2, // 一つ目の三角形 / first triangle - 2, 3, 0, // 二つ目の三角形 / second triangle - }; - Mesh quad(quadVertices, quadIndices, kColorVertexLayout); - - // 3〜6. 計算で作る図形(VAOはそれぞれ別) / generated shapes (each has its own VAO) - Mesh hexagon = makePolygon({0.6f, 0.45f}, 0.27f, 6); - Mesh points = makePoints({-0.6f, -0.45f}); - Mesh star = makeStar({0.0f, -0.45f}, 0.3f); - Mesh ribbon = makeRibbon({0.6f, -0.45f}); - - glPointSize(10.0f); // GL_POINTSの大きさ(ピクセル) / size of GL_POINTS in pixels + Mesh quad(vertices, {0, 1, 2, 2, 3, 0}, kTexturedVertexLayout); + 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); + glClearColor(0.10f, 0.15f, 0.25f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - float t = static_cast(window.time()); - const Vec3 white = {1.0f, 1.0f, 1.0f}; - + crate->bind(0); // ユニット0 → uTexture0 / unit 0 → uTexture0 + emblem->bind(1); // ユニット1 → uTexture1 / unit 1 → uTexture1 shader->use(); - // フレームに一回:全ての描画で同じ値 / once per frame: the same value for every draw - shader->set("uTime", t); - - // 描画ごと:ユニフォームは次に変えるまでプログラムに残る - // per draw: a uniform keeps its value in the program until we change it - shader->set("uOffset", Vec2{0.0f, 0.0f}); - shader->set("uTint", white); - triangle.draw(); - points.draw(); - star.draw(); - ribbon.draw(); - - // 四角形:色が白とオレンジの間で変わる / the quad: colour moves between white and orange - float pulse = 0.5f + 0.5f * std::sin(t * 2.0f); - shader->set("uTint", lerp(white, Vec3{1.0f, 0.5f, 0.1f}, pulse)); + shader->set("uMix", mixAmount); + shader->set("uEmblemScale", 2.0f); quad.draw(); - // 六角形:上下に揺れる / the hexagon bobs up and down - shader->set("uTint", white); - shader->set("uOffset", Vec2{0.0f, 0.1f * std::sin(t * 3.0f)}); - hexagon.draw(); - window.swapBuffers(); } - return 0; // シェーダーとメッシュはここで自動的に削除される / the shader and meshes are deleted automatically here + return 0; } diff --git a/src/stb_image_impl.cc b/src/stb_image_impl.cc new file mode 100644 index 0000000..b979d69 --- /dev/null +++ b/src/stb_image_impl.cc @@ -0,0 +1,6 @@ +// stb_imageの実装はプログラム全体で一つの.ccだけに入れる。 +// 他人のコードなので、CMakeでは別のライブラリにして自分の警告フラグを付けない。 +// stb_image's implementation goes into exactly one .cc file in the whole program. +// It is someone else's code, so CMake builds it as a separate library without our warning flags. +#define STB_IMAGE_IMPLEMENTATION +#include diff --git a/src/texture.cc b/src/texture.cc new file mode 100644 index 0000000..cc9cc98 --- /dev/null +++ b/src/texture.cc @@ -0,0 +1,84 @@ +#include "texture.hh" + +#include + +#include +#include + +// 1×1になるまで半分にした回数+1 / number of halvings until 1×1, plus one +static int mipLevelCount(int width, int height) { + int levels = 1; + while ((width >> levels) > 0 || (height >> levels) > 0) ++levels; + return levels; +} + +Texture2D::Texture2D(int width, int height, GLenum internalFormat, int levels) + : w(width), h(height), mipLevels(levels) { + glCreateTextures(GL_TEXTURE_2D, 1, &handle); + // 不変ストレージ:サイズと形式は後から変えられない / immutable storage: size and format are fixed + glTextureStorage2D(handle, levels, internalFormat, width, height); + setWrap(GL_REPEAT, GL_REPEAT); + setFilter(levels > 1 ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR, GL_LINEAR); +} + +Texture2D::~Texture2D() { + if (handle) glDeleteTextures(1, &handle); +} + +Texture2D::Texture2D(Texture2D &&other) noexcept + : handle(std::exchange(other.handle, 0)), w(other.w), h(other.h), mipLevels(other.mipLevels) {} + +Texture2D &Texture2D::operator=(Texture2D &&other) noexcept { + if (this != &other) { + if (handle) glDeleteTextures(1, &handle); + handle = std::exchange(other.handle, 0); + w = other.w; + h = other.h; + mipLevels = other.mipLevels; + } + return *this; +} + +Texture2D Texture2D::fromPixels(const unsigned char *rgba, int width, int height, const TextureOptions &options) { + int levels = options.mipmaps ? mipLevelCount(width, height) : 1; + Texture2D texture(width, height, options.srgb ? GL_SRGB8_ALPHA8 : GL_RGBA8, levels); + texture.upload(rgba); + if (options.mipmaps) texture.generateMipmaps(); + return texture; +} + +std::optional Texture2D::load(const std::string &path, const TextureOptions &options) { + stbi_set_flip_vertically_on_load(options.flipY ? 1 : 0); + int width = 0, height = 0, channels = 0; + // 元が何チャンネルでも4(RGBA)で受け取る / always ask for 4 channels (RGBA), whatever the file has + unsigned char *pixels = stbi_load(path.c_str(), &width, &height, &channels, 4); + if (!pixels) { + fprintf(stderr, "%s: 画像を読めない / cannot load image: %s (プロジェクトのルートから実行した? / running from the project root?)\n", + path.c_str(), stbi_failure_reason()); + return std::nullopt; + } + Texture2D texture = fromPixels(pixels, width, height, options); + stbi_image_free(pixels); + return texture; +} + +void Texture2D::upload(const void *pixels, GLenum format, GLenum type) { + // 行の幅が4バイトの倍数でなくても良いように / rows need not be a multiple of 4 bytes + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTextureSubImage2D(handle, 0, 0, 0, w, h, format, type, pixels); + glPixelStorei(GL_UNPACK_ALIGNMENT, 4); +} + +void Texture2D::generateMipmaps() { + if (mipLevels > 1) glGenerateTextureMipmap(handle); +} + +void Texture2D::setFilter(GLenum minFilter, GLenum magFilter) { + glTextureParameteri(handle, GL_TEXTURE_MIN_FILTER, static_cast(minFilter)); // 縮小 / minification + glTextureParameteri(handle, GL_TEXTURE_MAG_FILTER, static_cast(magFilter)); // 拡大 / magnification +} + +void Texture2D::setWrap(GLenum wrapS, GLenum wrapT) { + glTextureParameteri(handle, GL_TEXTURE_WRAP_S, static_cast(wrapS)); // 横 / horizontal (u) + glTextureParameteri(handle, GL_TEXTURE_WRAP_T, static_cast(wrapT)); // 縦 / vertical (v) +} diff --git a/src/texture.hh b/src/texture.hh new file mode 100644 index 0000000..83dd1f4 --- /dev/null +++ b/src/texture.hh @@ -0,0 +1,55 @@ +// 2Dテクスチャ / 2D textures +#pragma once + +#include + +#include +#include + +struct TextureOptions { + bool flipY = true; // 画像は上から、OpenGLのUVは下から / images start at the top, OpenGL UVs at the bottom + bool mipmaps = true; // ミップマップを作る / build mipmaps + bool srgb = false; // 色の画像はsRGB(正しい扱いは第21章) / colour images are sRGB (handled properly in chapter 21) +}; + +// 2Dテクスチャ(RAII、コピー不可・移動可)。DSAで作る。 +// A 2D texture (RAII, non-copyable, movable), created with DSA. +class Texture2D { +public: + Texture2D() = default; + // 中身のないストレージを作る(levels = ミップマップの段数) / allocates empty storage (levels = mipmap levels) + Texture2D(int width, int height, GLenum internalFormat, int levels = 1); + ~Texture2D(); + Texture2D(const Texture2D &) = delete; + Texture2D &operator=(const Texture2D &) = delete; + Texture2D(Texture2D &&other) noexcept; + Texture2D &operator=(Texture2D &&other) noexcept; + + // 画像ファイル(PNG、JPEG…)から読む。失敗したらエラーを表示してstd::nullopt。 + // Loads an image file (PNG, JPEG, ...). On failure prints the error and returns std::nullopt. + static std::optional load(const std::string &path, const TextureOptions &options = {}); + // RGBA8のピクセルから作る。渡された順に行をアップロードするので、最初の行がv = 0(テクスチャの下端)になる。 + // options.flipYは使わない(反転するのはloadの中のstb_imageだけ)。 + // Builds from RGBA8 pixels. Rows are uploaded in the order given, so the first row becomes v = 0 (the bottom of + // the texture). options.flipY is ignored here (only load flips, inside stb_image). + static Texture2D fromPixels(const unsigned char *rgba, int width, int height, const TextureOptions &options = {}); + + // ミップマップ0段目を全部書き換える / replaces all of mipmap level 0 + void upload(const void *pixels, GLenum format = GL_RGBA, GLenum type = GL_UNSIGNED_BYTE); + void generateMipmaps(); + + void setFilter(GLenum minFilter, GLenum magFilter); + void setWrap(GLenum wrapS, GLenum wrapT); + + // テクスチャユニットunitに繋ぐ(シェーダーの layout(binding = unit)) / attach to texture unit `unit` + void bind(GLuint unit) const { glBindTextureUnit(unit, handle); } + + GLuint id() const { return handle; } + int width() const { return w; } + int height() const { return h; } + +private: + GLuint handle = 0; + int w = 0, h = 0; + int mipLevels = 0; +};