diff --git a/assets/LICENSE.txt b/assets/LICENSE.txt index e723aa4..472c901 100644 --- a/assets/LICENSE.txt +++ b/assets/LICENSE.txt @@ -4,5 +4,4 @@ The files in assets/ were made for this tutorial by the author (generated with t which are not part of this project). Free to use for any purpose. textures/crate.png 木箱 / wooden crate -textures/emblem.png 桜の紋章 / sakura emblem textures/floor.png 石の床 / stone floor tiles diff --git a/assets/textures/emblem.png b/assets/textures/emblem.png deleted file mode 100644 index 328f5b6..0000000 Binary files a/assets/textures/emblem.png and /dev/null differ diff --git a/shaders/phong.frag b/shaders/phong.frag new file mode 100644 index 0000000..0ac0d6d --- /dev/null +++ b/shaders/phong.frag @@ -0,0 +1,52 @@ +#version 460 core +in vec3 vWorldPos; +in vec3 vNormal; +in vec2 vUv; + +layout(binding = 0) uniform sampler2D uTexture0; // 物体の色 / the object's colour + +uniform vec3 uObjectColor; // テクスチャに掛ける色 / colour multiplied with the texture +uniform float uSpecularStrength; // 鏡面反射の強さ / specular strength +uniform float uShininess; // 光沢度(大きいほど小さく鋭いハイライト) / shininess (bigger = smaller, sharper highlight) + +uniform vec3 uLightPos; // 光の位置(ワールド座標) / light position (world space) +uniform vec3 uLightColor; // 光の色と明るさ / light colour and brightness +uniform vec3 uAmbient; // 環境光の色と明るさ / ambient light colour and brightness +uniform vec3 uViewPos; // カメラの位置(ワールド座標) / camera position (world space) + +// 0 = 全部、1 = 環境光だけ、2 = 拡散反射だけ、3 = 鏡面反射だけ、4 = 法線を色で +// 0 = everything, 1 = ambient only, 2 = diffuse only, 3 = specular only, 4 = normals as colours +uniform int uViewMode; + +layout(location = 0) out vec4 out_color; + +void main() { + vec3 albedo = uObjectColor * texture(uTexture0, vUv).rgb; + + // 補間された法線は短くなるので正規化し直す / interpolated normals get shorter: normalize again + vec3 n = normalize(vNormal); + vec3 l = normalize(uLightPos - vWorldPos); // 面から光へ / from the surface towards the light + vec3 v = normalize(uViewPos - vWorldPos); // 面から目へ / from the surface towards the eye + + // 環境光:どこからともなく来る弱い光 / ambient: weak light coming from everywhere + vec3 ambient = uAmbient * albedo; + + // 拡散反射(ランバート):光に向いているほど明るい / diffuse (Lambert): brighter the more it faces the light + float diff = max(dot(n, l), 0.0); + vec3 diffuse = diff * albedo * uLightColor; + + // 鏡面反射(Blinn-Phong):ハーフベクトルが法線に近いほど明るい / specular (Blinn-Phong): bright where the half vector is close to n + float spec = 0.0; + if (diff > 0.0) { // 裏から照らされた面にハイライトは出ない / no highlight on surfaces lit from behind + vec3 h = normalize(l + v); + spec = pow(max(dot(n, h), 0.0), uShininess); + } + vec3 specular = uSpecularStrength * spec * uLightColor; + + vec3 color = ambient + diffuse + specular; + if (uViewMode == 1) color = ambient; + else if (uViewMode == 2) color = diffuse; + else if (uViewMode == 3) color = specular; + else if (uViewMode == 4) color = n * 0.5 + 0.5; // -1〜1を0〜1に / map -1..1 to 0..1 + out_color = vec4(color, 1.0); +} diff --git a/shaders/phong.vert b/shaders/phong.vert new file mode 100644 index 0000000..1dd79e8 --- /dev/null +++ b/shaders/phong.vert @@ -0,0 +1,22 @@ +#version 460 core +layout(location = 0) in vec3 aPosition; +layout(location = 1) in vec3 aNormal; // 面の向き(モデル座標) / which way the surface faces (model space) +layout(location = 2) in vec2 aUv; + +uniform mat4 uModel; +uniform mat4 uView; +uniform mat4 uProj; +uniform mat3 uNormalMatrix; // 法線をワールド座標へ(モデル行列の逆行列の転置) / normals to world space (inverse transpose of uModel) + +// ライティングはワールド座標で計算する / lighting is computed in world space +out vec3 vWorldPos; +out vec3 vNormal; +out vec2 vUv; + +void main() { + vec4 world = uModel * vec4(aPosition, 1.0); + vWorldPos = world.xyz; + vNormal = uNormalMatrix * aNormal; // 正規化はフラグメントシェーダーで / normalized in the fragment shader + vUv = aUv; + gl_Position = uProj * uView * world; +} diff --git a/shaders/textured.frag b/shaders/textured.frag deleted file mode 100644 index 5cda206..0000000 --- a/shaders/textured.frag +++ /dev/null @@ -1,22 +0,0 @@ -#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 deleted file mode 100644 index f7065b1..0000000 --- a/shaders/textured.vert +++ /dev/null @@ -1,15 +0,0 @@ -#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) - -uniform mat4 uModel; // モデル座標 → ワールド座標 / model space → world space -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; - // 行列は右から順に効く:モデル → ビュー → 投影 / matrices apply right to left: model → view → projection - gl_Position = uProj * uView * uModel * vec4(aPosition, 1.0); -} diff --git a/shaders/unlit.frag b/shaders/unlit.frag new file mode 100644 index 0000000..01b2f1c --- /dev/null +++ b/shaders/unlit.frag @@ -0,0 +1,9 @@ +#version 460 core +// ライティングなしの一色(光源の目印用) / one colour without lighting (for light markers) +uniform vec3 uColor; + +layout(location = 0) out vec4 out_color; + +void main() { + out_color = vec4(uColor, 1.0); +} diff --git a/shaders/unlit.vert b/shaders/unlit.vert new file mode 100644 index 0000000..43847fc --- /dev/null +++ b/shaders/unlit.vert @@ -0,0 +1,10 @@ +#version 460 core +layout(location = 0) in vec3 aPosition; + +uniform mat4 uModel; +uniform mat4 uView; +uniform mat4 uProj; + +void main() { + gl_Position = uProj * uView * uModel * vec4(aPosition, 1.0); +} diff --git a/src/main.cc b/src/main.cc index d23da29..82ba258 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,25 +1,29 @@ -// 第9章:カメラ -// Chapter 9: the camera +// 第10章:色とフォンライティング +// Chapter 10: colors and Phong lighting // // 操作 / Controls: // WASD 前後左右に移動 / move forward, back, left, right // Space / Ctrl 上昇・下降 / move up / down // Shift 速く動く / move faster // マウス / mouse 向きを変える(FPS)、左ドラッグで回る(オービット) / look around (FPS), drag with the left button to orbit -// ホイール / wheel ズーム(FPSは視野角、オービットは距離) / zoom (FPS: field of view, orbit: distance) +// ホイール / wheel ズーム / zoom // C FPS ⇔ オービットカメラ / FPS ⇔ orbit camera +// V 表示:全部 → 環境光 → 拡散 → 鏡面 → 法線 / view: all → ambient → diffuse → specular → normals +// L 光の色を切り替える / cycle the light colour +// P 光を止める・動かす / pause / resume the light +// N 法線行列のオン・オフ(オフ=間違った法線) / normal matrix on / off (off = wrong normals) // Tab カーソルのキャプチャ切り替え / toggle cursor capture // R シェーダーを読み直す / reload the shaders // Esc 終了 / quit #include "camera.hh" +#include "mathx.hh" #include "mesh.hh" #include "shader.hh" #include "shapes.hh" #include "texture.hh" #include "window.hh" -#include - +#include #include #include @@ -39,20 +43,35 @@ static const CrateInfo kCrates[] = { {{9.0f, 0.0f, -12.0f}, 2.5f, 0.7f}, {{-2.5f, 0.0f, 5.5f}, 0.6f, 0.5f}, }; +// Lキーで切り替える光の色 / light colours cycled with the L key +static const Vec3 kLightColors[] = { + {1.0f, 1.0f, 1.0f}, // 白 / white + {1.0f, 0.72f, 0.42f}, // 電球の色 / warm bulb + {0.35f, 1.0f, 0.45f}, // 緑 / green + {0.45f, 0.55f, 1.0f}, // 青 / blue +}; + +static const char *const kViewModeNames[] = {"all", "ambient", "diffuse", "specular", "normals"}; + int main() { Window window; - if (!window.create(1280, 720, "OpenGL Tutorial - Chapter 9")) return 1; + if (!window.create(1280, 720, "OpenGL Tutorial - Chapter 10")) return 1; - std::optional shader = Shader::load("shaders/textured.vert", "shaders/textured.frag"); + std::optional phong = Shader::load("shaders/phong.vert", "shaders/phong.frag"); + std::optional unlit = Shader::load("shaders/unlit.vert", "shaders/unlit.frag"); std::optional crate = Texture2D::load("assets/textures/crate.png"); - std::optional emblem = Texture2D::load("assets/textures/emblem.png"); std::optional floorTexture = Texture2D::load("assets/textures/floor.png"); - if (!shader || !crate || !emblem || !floorTexture) return 1; + if (!phong || !unlit || !crate || !floorTexture) return 1; glEnable(GL_DEPTH_TEST); + // 1×1の白いテクスチャ:テクスチャのない物はuObjectColorだけで色が決まる + // A 1×1 white texture: objects without a texture get their colour from uObjectColor alone + const unsigned char whitePixel[4] = {255, 255, 255, 255}; + Texture2D white = Texture2D::fromPixels(whitePixel, 1, 1, {.flipY = false, .mipmaps = false}); + Camera fps; - fps.position = {0.0f, 1.6f, 7.0f}; // 目の高さ / eye height - fps.pitch = radians(-8.0f); + fps.position = {0.5f, 2.4f, 8.5f}; + fps.pitch = radians(-14.0f); OrbitCamera orbit; orbit.target = {0.0f, 0.8f, 0.0f}; @@ -60,24 +79,41 @@ int main() { orbit.yaw = radians(30.0f); bool useOrbit = false; - window.setCursorCaptured(true); // FPSはカーソルを隠して固定 / FPS: hide and lock the cursor + int viewMode = 0; + int lightColorIndex = 0; + bool lightMoving = true; + bool useNormalMatrix = true; + float lightTime = 0.0f; // 光の動きの時間(止められる) / time for the light's motion (can be paused) + window.setCursorCaptured(true); 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 && shader->reload()) printf("シェーダーを読み直した / shaders reloaded\n"); + if (key == GLFW_KEY_R) { + bool ok = phong->reload(); + ok = unlit->reload() && ok; // 両方とも読み直す / reload both, even if the first one failed + if (ok) printf("シェーダーを読み直した / shaders reloaded\n"); + } if (key == GLFW_KEY_TAB) window.setCursorCaptured(!window.cursorCaptured()); if (key == GLFW_KEY_C) { useOrbit = !useOrbit; - // オービットはマウスでドラッグするのでカーソルを見せる / orbit is dragged with the mouse, so show the cursor window.setCursorCaptured(!useOrbit); printf("camera: %s\n", useOrbit ? "orbit" : "fps"); } + if (key == GLFW_KEY_V) { + viewMode = (viewMode + 1) % 5; + printf("view: %s\n", kViewModeNames[viewMode]); + } + if (key == GLFW_KEY_L) lightColorIndex = (lightColorIndex + 1) % 4; + if (key == GLFW_KEY_P) lightMoving = !lightMoving; + if (key == GLFW_KEY_N) { + useNormalMatrix = !useNormalMatrix; + printf("normal matrix: %s\n", useNormalMatrix ? "on" : "off (mat3(uModel))"); + } }; Mesh cube = makeCube(); - // 40×40の床。テクスチャにタイル4枚なので10回繰り返すと1タイル = 1単位 - // A 40 × 40 floor. The texture holds 4 tiles, so repeating it 10 times makes one tile = one unit + Mesh sphere = makeSphere(); Mesh floorMesh = makePlane(40.0f, 10.0f); double lastTime = window.time(); @@ -94,8 +130,6 @@ int main() { } else { if (window.cursorCaptured()) fps.turn(mouse.x, mouse.y); fps.zoom(window.scrollDelta()); - - // キーからローカルな移動方向を作る(x = 右、y = 上、z = 前) / build a local direction from the keys Vec3 dir; if (window.keyDown(GLFW_KEY_W)) dir.z += 1.0f; if (window.keyDown(GLFW_KEY_S)) dir.z -= 1.0f; @@ -104,37 +138,69 @@ int main() { if (window.keyDown(GLFW_KEY_SPACE)) dir.y += 1.0f; if (window.keyDown(GLFW_KEY_LEFT_CONTROL)) dir.y -= 1.0f; fps.speed = window.keyDown(GLFW_KEY_LEFT_SHIFT) ? 9.0f : 3.0f; - // dtを掛けるのでフレームレートに関係なく同じ速さ / multiplied by dt: the same speed at any frame rate fps.move(dir, dt); } + // 光は箱の山の周りを回り、少し上下する / the light circles the pile of crates, bobbing up and down a little + if (lightMoving) lightTime += dt; + Vec3 lightPos = {3.5f * std::sin(0.7f * lightTime), 2.6f + 0.6f * std::sin(1.3f * lightTime), + 3.5f * std::cos(0.7f * lightTime)}; + Vec3 lightColor = kLightColors[lightColorIndex]; + Mat4 view = useOrbit ? orbit.view() : fps.view(); Mat4 proj = useOrbit ? orbit.projection(window.aspect()) : fps.projection(window.aspect()); + Vec3 viewPos = useOrbit ? orbit.position() : fps.position; glClearColor(0.10f, 0.15f, 0.25f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - shader->use(); - shader->set("uView", view); - shader->set("uProj", proj); - shader->set("uEmblemScale", 1.0f); - - // 床:二枚目のテクスチャは混ぜない / the floor: no second texture + // 一つの物を描く:モデル行列と法線行列、物体の色、光沢 / draws one object: model and normal matrix, colour, shininess + auto drawLit = [&](const Mesh &mesh, const Mat4 &model, Vec3 color, float specular, float shininess) { + phong->set("uModel", model); + // 法線行列なしだと一様でない拡大縮小で法線が傾く / without the normal matrix, non-uniform scaling tilts the normals + phong->set("uNormalMatrix", useNormalMatrix ? normalMatrix(model) : toMat3(model)); + phong->set("uObjectColor", color); + phong->set("uSpecularStrength", specular); + phong->set("uShininess", shininess); + mesh.draw(); + }; + + phong->use(); + phong->set("uView", view); + phong->set("uProj", proj); + phong->set("uViewPos", viewPos); + phong->set("uLightPos", lightPos); + phong->set("uLightColor", lightColor); + phong->set("uAmbient", Vec3{0.15f, 0.15f, 0.15f}); + phong->set("uViewMode", viewMode); + + // 床:石は少しだけ光る / the floor: stone is only slightly shiny floorTexture->bind(0); - emblem->bind(1); - shader->set("uMix", 0.0f); - shader->set("uModel", Mat4::identity()); - floorMesh.draw(); + drawLit(floorMesh, Mat4::identity(), {1.0f, 1.0f, 1.0f}, 0.15f, 16.0f); - // 箱:立方体は原点が中心なので半分持ち上げて床に置く / crates: the cube is centred, so lift it by half its size + // 箱 / crates crate->bind(0); - shader->set("uMix", 0.8f); for (const CrateInfo &c : kCrates) { Vec3 centre = c.position + Vec3{0.0f, 0.5f * c.size, 0.0f}; - shader->set("uModel", translate(centre) * rotateY(c.angle) * scale({c.size, c.size, c.size})); - cube.draw(); + drawLit(cube, translate(centre) * rotateY(c.angle) * scale({c.size, c.size, c.size}), {1.0f, 1.0f, 1.0f}, 0.3f, + 32.0f); } + // テクスチャなしの球二つ:色はuObjectColorだけ。二つ目は一様でない拡大縮小(楕円体) + // Two untextured spheres: the colour is uObjectColor alone. The second one is scaled non-uniformly (an ellipsoid) + white.bind(0); + drawLit(sphere, translate({2.6f, 0.7f, 2.2f}) * scale({1.4f, 1.4f, 1.4f}), {1.0f, 0.35f, 0.25f}, 0.6f, 64.0f); + drawLit(sphere, translate({-1.8f, 0.45f, 2.4f}) * rotateY(radians(35.0f)) * scale({2.2f, 0.9f, 1.1f}), + {0.3f, 0.5f, 1.0f}, 0.6f, 64.0f); + + // 光源の目印:光の色そのまま、ライティングなし / the light's marker: the light colour itself, unlit + unlit->use(); + unlit->set("uView", view); + unlit->set("uProj", proj); + unlit->set("uModel", translate(lightPos) * scale({0.2f, 0.2f, 0.2f})); + unlit->set("uColor", lightColor); + cube.draw(); + window.swapBuffers(); } return 0; diff --git a/src/mathx.hh b/src/mathx.hh new file mode 100644 index 0000000..13df5a1 --- /dev/null +++ b/src/mathx.hh @@ -0,0 +1,25 @@ +// linalg.hhに無いものを足す(ダウンロードしたlinalg.hh自体は編集しない) +// Additions to linalg.hh (the downloaded linalg.hh itself is never edited) +#pragma once + +#include + +// 3×3行列。列優先:c[列]が一つの列ベクトル(Mat4と同じ)。法線行列に使う。 +// 3×3 matrix. Column-major: c[column] is one column vector (like Mat4). Used for the normal matrix. +struct Mat3 { + Vec3 c[3]; +}; + +// 4×4行列の左上3×3(回転と拡大縮小の部分、平行移動は捨てる) +// The upper-left 3×3 of a 4×4 matrix (the rotation and scale part; the translation is dropped) +inline Mat3 toMat3(const Mat4 &m) { + Mat3 r; + for (int i = 0; i < 3; ++i) r.c[i] = {m.c[i].x, m.c[i].y, m.c[i].z}; + return r; +} + +// 法線行列:モデル行列の逆行列の転置の左上3×3(線型代数 第10章)。 +// 一様でない拡大縮小があっても、変換した法線が面に垂直なままになる。 +// The normal matrix: the upper-left 3×3 of the model matrix's inverse transpose (Linear Algebra chapter 10). +// Transformed normals stay perpendicular to the surface even with non-uniform scaling. +inline Mat3 normalMatrix(const Mat4 &model) { return toMat3(transpose(inverse(model))); } diff --git a/src/shader.cc b/src/shader.cc index ec12940..1882ed1 100644 --- a/src/shader.cc +++ b/src/shader.cc @@ -175,6 +175,10 @@ void Shader::set(const char *name, Vec2 v) const { glProgramUniform2f(program, l void Shader::set(const char *name, Vec3 v) const { glProgramUniform3f(program, location(name), v.x, v.y, v.z); } void Shader::set(const char *name, Vec4 v) const { glProgramUniform4f(program, location(name), v.x, v.y, v.z, v.w); } +void Shader::set(const char *name, const Mat3 &m) const { + glProgramUniformMatrix3fv(program, location(name), 1, GL_FALSE, &m.c[0].x); +} + void Shader::set(const char *name, const Mat4 &m) const { // linalg.hhは列優先なので転置しない(GL_FALSE) / linalg.hh is column-major, so no transpose (GL_FALSE) glProgramUniformMatrix4fv(program, location(name), 1, GL_FALSE, &m.c[0].x); diff --git a/src/shader.hh b/src/shader.hh index c0e2aa5..a3d9856 100644 --- a/src/shader.hh +++ b/src/shader.hh @@ -1,8 +1,9 @@ // シェーダープログラム / a shader program #pragma once +#include "mathx.hh" + #include -#include #include #include @@ -37,6 +38,7 @@ public: void set(const char *name, Vec2 value) const; void set(const char *name, Vec3 value) const; void set(const char *name, Vec4 value) const; + void set(const char *name, const Mat3 &value) const; void set(const char *name, const Mat4 &value) const; private: diff --git a/src/shapes.cc b/src/shapes.cc index d340eff..cb622a8 100644 --- a/src/shapes.cc +++ b/src/shapes.cc @@ -1,5 +1,6 @@ #include "shapes.hh" +#include #include // 一つの面(正方形)を足す。normal = 外向き、right × up = normal になるように選ぶ。 @@ -41,3 +42,30 @@ Mesh makePlane(float size, float uvRepeat) { }; return Mesh(vertices, {0, 1, 2, 2, 3, 0}, kVertex3DLayout); } + +Mesh makeSphere(int segments, int rings) { + std::vector vertices; + std::vector indices; + for (int r = 0; r <= rings; ++r) { + float phi = PI * static_cast(r) / static_cast(rings); // 0 = 北極、π = 南極 / 0 = north pole, π = south pole + for (int s = 0; s <= segments; ++s) { + // 継ぎ目の頂点は二重(UVが0と1で違う) / the seam vertices are doubled (u = 0 and u = 1) + float theta = 2.0f * PI * static_cast(s) / static_cast(segments); + Vec3 n = {std::sin(phi) * std::sin(theta), std::cos(phi), std::sin(phi) * std::cos(theta)}; + Vec2 uv = {static_cast(s) / static_cast(segments), 1.0f - static_cast(r) / static_cast(rings)}; + vertices.push_back({0.5f * n, n, uv}); + } + } + // 隣り合う二つの輪の間を四角形(三角形2つ)で埋める。極の潰れた三角形は省く。 + // Fill between neighbouring rings with quads (two triangles). Skip the degenerate triangles at the poles. + std::uint32_t row = static_cast(segments + 1); + for (std::uint32_t r = 0; r < static_cast(rings); ++r) { + for (std::uint32_t s = 0; s < static_cast(segments); ++s) { + std::uint32_t a = r * row + s; // 左上 / top left + std::uint32_t b = a + row; // 左下 / bottom left + if (r != static_cast(rings) - 1) indices.insert(indices.end(), {a, b, b + 1}); + if (r != 0) indices.insert(indices.end(), {a, b + 1, a + 1}); + } + } + return Mesh(vertices, indices, kVertex3DLayout); +} diff --git a/src/shapes.hh b/src/shapes.hh index f9bdf1f..7281f93 100644 --- a/src/shapes.hh +++ b/src/shapes.hh @@ -12,3 +12,9 @@ Mesh makeCube(); // XZ平面上のsize×sizeの床(y = 0、法線は+Y)。UVはuvRepeat回繰り返す(GL_REPEATが必要)。 // A size × size floor on the XZ plane (y = 0, normal +Y). UVs repeat uvRepeat times (needs GL_REPEAT). Mesh makePlane(float size, float uvRepeat); + +// 原点を中心とした直径1の球(UV球)。segments = 経度方向の分割数、rings = 緯度方向の分割数。 +// 法線は中心から外向き。立方体と同じく外から見て反時計回り。 +// A sphere of diameter 1 centred on the origin (a UV sphere). segments = divisions around, rings = from pole to pole. +// The normals point outwards from the centre. Counter-clockwise seen from outside, like the cube. +Mesh makeSphere(int segments = 32, int rings = 16);