算数ファイルの更新
諏訪子 <suwako@076.moe>、2026-06-27 07:07
コミット 1714cdfd1fb832b6a3af4a857fcf4e80a0981f2b
・親 01fccb9
・ファイルを見る
2個のファイル、+235 -1
| @@ -0,0 +1,212 @@ | ||
| 1 | +/* | |
| 2 | +# 076 Free License | |
| 3 | + | |
| 4 | +Copyright (c) テクニカル諏訪子 | |
| 5 | + | |
| 6 | +Permission is hereby granted to any person obtaining a copy of the software | |
| 7 | +C++ Utils (the "Software") to use, modify, merge, copy, publish, distribute, | |
| 8 | +sublicense, and/or sell copies of the Software, subject to the following conditions: | |
| 9 | + | |
| 10 | + 1. **Origin Attribution**: | |
| 11 | + - You must not misrepresent the origin of the Software; you must not claim | |
| 12 | + you created the original Software. | |
| 13 | + 2. **Notice Preservation**: | |
| 14 | + - This license and the above copyright notice must remain intact in all copies | |
| 15 | + of the source code. | |
| 16 | + | |
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
| 18 | +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
| 19 | +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
| 20 | +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
| 21 | +CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE | |
| 22 | +OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 23 | +*/ | |
| 24 | +#ifndef MATRIX_HH | |
| 25 | +#define MATRIX_HH | |
| 26 | + | |
| 27 | +#include <util/types.hh> | |
| 28 | +#include <util/vector.hh> | |
| 29 | + | |
| 30 | +typedef struct { | |
| 31 | + f32 m[16]; | |
| 32 | +} Matrix4; | |
| 33 | + | |
| 34 | +typedef struct { | |
| 35 | + f32 m[16]; | |
| 36 | +} Matrix4x4; | |
| 37 | + | |
| 38 | +typedef struct { | |
| 39 | + f32 m[12]; | |
| 40 | +} Matrix4x3; | |
| 41 | + | |
| 42 | +inline Matrix4 operator*(const Matrix4 &a, const Matrix4 &b) { | |
| 43 | + Matrix4 out = {}; | |
| 44 | + | |
| 45 | + for (int i = 0; i < 4; ++i) { | |
| 46 | + for (int j = 0; j < 4; ++j) { | |
| 47 | + float sum = 0.0f; | |
| 48 | + for (int k = 0; k < 4; ++k) { | |
| 49 | + sum += a.m[i + k * 4] * b.m[k + j * 4]; | |
| 50 | + } | |
| 51 | + out.m[i + j * 4] = sum; | |
| 52 | + } | |
| 53 | + } | |
| 54 | + return out; | |
| 55 | +} | |
| 56 | + | |
| 57 | +inline Matrix4 &operator*=(Matrix4 &a, const Matrix4 &b) { | |
| 58 | + a = a * b; | |
| 59 | + return a; | |
| 60 | +} | |
| 61 | + | |
| 62 | +inline Matrix4 mat4Identity() { | |
| 63 | + Matrix4 out{}; | |
| 64 | + | |
| 65 | + out.m[0] = 1.f; | |
| 66 | + out.m[5] = 1.f; | |
| 67 | + out.m[10] = 1.f; | |
| 68 | + out.m[15] = 1.f; | |
| 69 | + | |
| 70 | + return out; | |
| 71 | +} | |
| 72 | + | |
| 73 | +inline Matrix4x4 mat4x4Identity() { | |
| 74 | + Matrix4x4 out{}; | |
| 75 | + | |
| 76 | + out.m[0] = 1.f; | |
| 77 | + out.m[5] = 1.f; | |
| 78 | + out.m[10] = 1.f; | |
| 79 | + out.m[15] = 1.f; | |
| 80 | + | |
| 81 | + return out; | |
| 82 | +} | |
| 83 | + | |
| 84 | +inline Matrix4 mat4Mul(const Matrix4 &a, const Matrix4 &b) { | |
| 85 | + Matrix4 out{}; | |
| 86 | + | |
| 87 | + for (int row = 0; row < 4; ++row) { | |
| 88 | + for (int col = 0; col < 4; ++col) { | |
| 89 | + out.m[col * 4 + row] = | |
| 90 | + a.m[0 * 4 + row] * b.m[col * 4 + 0] + | |
| 91 | + a.m[1 * 4 + row] * b.m[col * 4 + 1] + | |
| 92 | + a.m[2 * 4 + row] * b.m[col * 4 + 2] + | |
| 93 | + a.m[3 * 4 + row] * b.m[col * 4 + 3]; | |
| 94 | + } | |
| 95 | + } | |
| 96 | + | |
| 97 | + return out; | |
| 98 | +} | |
| 99 | + | |
| 100 | +inline Matrix4 mat4Translate(f32 x, f32 y, f32 z) { | |
| 101 | + Matrix4 out = mat4Identity(); | |
| 102 | + | |
| 103 | + out.m[12] = x; | |
| 104 | + out.m[13] = y; | |
| 105 | + out.m[14] = z; | |
| 106 | + | |
| 107 | + return out; | |
| 108 | +} | |
| 109 | + | |
| 110 | +inline Matrix4 mat4Scale(f32 x, f32 y, f32 z) { | |
| 111 | + Matrix4 out = {}; | |
| 112 | + | |
| 113 | + out.m[0] = x; | |
| 114 | + out.m[5] = y; | |
| 115 | + out.m[10] = z; | |
| 116 | + out.m[15] = 1.f; | |
| 117 | + | |
| 118 | + return out; | |
| 119 | +} | |
| 120 | + | |
| 121 | +inline Matrix4 mat4RotateX(f32 angle) { | |
| 122 | + Matrix4 out = mat4Identity(); | |
| 123 | + | |
| 124 | + f32 c = cosf(angle); | |
| 125 | + f32 s = sinf(angle); | |
| 126 | + | |
| 127 | + out.m[5] = c; // m[1][1] | |
| 128 | + out.m[6] = s; // m[1][2] | |
| 129 | + out.m[9] = -s; // m[2][1] | |
| 130 | + out.m[10] = c; // m[2][2] | |
| 131 | + | |
| 132 | + return out; | |
| 133 | +} | |
| 134 | + | |
| 135 | +inline Matrix4 mat4RotateY(f32 angle) { | |
| 136 | + Matrix4 out = mat4Identity(); | |
| 137 | + | |
| 138 | + f32 c = cosf(angle); | |
| 139 | + f32 s = sinf(angle); | |
| 140 | + | |
| 141 | + out.m[0] = c; // m[0][0] | |
| 142 | + out.m[2] = -s; // m[0][2] | |
| 143 | + out.m[8] = s; // m[2][0] | |
| 144 | + out.m[10] = c; // m[2][2] | |
| 145 | + | |
| 146 | + return out; | |
| 147 | +} | |
| 148 | + | |
| 149 | +inline Matrix4 mat4RotateZ(f32 angle) { | |
| 150 | + Matrix4 out = mat4Identity(); | |
| 151 | + | |
| 152 | + f32 c = cosf(angle); | |
| 153 | + f32 s = sinf(angle); | |
| 154 | + | |
| 155 | + out.m[0] = c; // m[0][0] | |
| 156 | + out.m[1] = s; // m[0][1] | |
| 157 | + out.m[4] = -s; // m[1][0] | |
| 158 | + out.m[5] = c; // m[1][1] | |
| 159 | + | |
| 160 | + return out; | |
| 161 | +} | |
| 162 | + | |
| 163 | +inline Matrix4 mat4Rotate(f32 pitch, f32 yaw, f32 roll) { | |
| 164 | + return mat4RotateZ(roll) * mat4RotateY(yaw) * mat4RotateX(pitch); | |
| 165 | +} | |
| 166 | + | |
| 167 | +inline Matrix4 mat4Perspective(f32 fov, f32 aspect, f32 near, f32 far) { | |
| 168 | + Matrix4 out = {}; | |
| 169 | + fov *= (3.14159265f / 180.f); | |
| 170 | + | |
| 171 | + f32 tanHalf = tanf(fov * 0.5f); | |
| 172 | + | |
| 173 | + out.m[0] = 1.f / (aspect * tanHalf); | |
| 174 | + out.m[5] = 1.f / tanHalf; | |
| 175 | + out.m[10] = -(far + near) / (far - near); | |
| 176 | + out.m[11] = -1.f; | |
| 177 | + out.m[14] = -(2.f * far * near) / (far - near); | |
| 178 | + | |
| 179 | + return out; | |
| 180 | +} | |
| 181 | + | |
| 182 | +inline Matrix4 mat4LookAt(Vector3 eye, Vector3 target, Vector3 up) { | |
| 183 | + Vector3 f = vec3Normalize(vec3Sub(target, eye)); | |
| 184 | + Vector3 r = vec3Normalize(vec3Cross(f, up)); | |
| 185 | + Vector3 u = vec3Cross(r, f); | |
| 186 | + | |
| 187 | + Matrix4 out = {}; | |
| 188 | + | |
| 189 | + out.m[0] = r.pos.x; | |
| 190 | + out.m[1] = u.pos.x; | |
| 191 | + out.m[2] = -f.pos.x; | |
| 192 | + out.m[3] = 0.f; | |
| 193 | + | |
| 194 | + out.m[4] = r.pos.y; | |
| 195 | + out.m[5] = u.pos.y; | |
| 196 | + out.m[6] = -f.pos.y; | |
| 197 | + out.m[7] = 0.f; | |
| 198 | + | |
| 199 | + out.m[8] = r.pos.z; | |
| 200 | + out.m[9] = u.pos.z; | |
| 201 | + out.m[10] = -f.pos.z; | |
| 202 | + out.m[11] = 0.f; | |
| 203 | + | |
| 204 | + out.m[12] = -vec3Dot(r, eye); | |
| 205 | + out.m[13] = -vec3Dot(u, eye); | |
| 206 | + out.m[14] = vec3Dot(f, eye); | |
| 207 | + out.m[15] = 1.f; | |
| 208 | + | |
| 209 | + return out; | |
| 210 | +} | |
| 211 | + | |
| 212 | +#endif // MATRIX_HH |
| @@ -24,7 +24,7 @@ | ||
| 24 | 24 | #ifndef VECTOR_HH |
| 25 | 25 | #define VECTOR_HH |
| 26 | 26 | |
| 27 | -#include "types.hh" | |
| 27 | +#include <util/types.hh> | |
| 28 | 28 | |
| 29 | 29 | union Vector2 { |
| 30 | 30 | struct Position { |
| @@ -265,5 +265,27 @@ | ||
| 265 | 265 | Position pos; |
| 266 | 266 | RGBA rgba; |
| 267 | 267 | }; |
| 268 | + | |
| 269 | +inline Vector3 vec3Sub(Vector3 a, Vector3 b) { | |
| 270 | + return { a.pos.x - b.pos.x, a.pos.y - b.pos.y, a.pos.z - b.pos.z }; | |
| 271 | +} | |
| 272 | + | |
| 273 | +inline f32 vec3Dot(Vector3 a, Vector3 b) { | |
| 274 | + return a.pos.x * b.pos.x + a.pos.y * b.pos.y + a.pos.z * b.pos.z; | |
| 275 | +} | |
| 276 | + | |
| 277 | +inline Vector3 vec3Cross(Vector3 a, Vector3 b) { | |
| 278 | + return { | |
| 279 | + a.pos.y * b.pos.z - a.pos.z * b.pos.y, | |
| 280 | + a.pos.z * b.pos.x - a.pos.x * b.pos.z, | |
| 281 | + a.pos.x * b.pos.y - a.pos.y * b.pos.x, | |
| 282 | + }; | |
| 283 | +} | |
| 284 | + | |
| 285 | +inline Vector3 vec3Normalize(Vector3 v) { | |
| 286 | + f32 len = sqrtf(v.pos.x * v.pos.x + v.pos.y * v.pos.y + v.pos.z * v.pos.z); | |
| 287 | + if (len == 0.f) return { 0, 0, 0 }; | |
| 288 | + return { v.pos.x / len, v.pos.y / len, v.pos.z / len }; | |
| 289 | +} | |
| 268 | 290 | |
| 269 | 291 | #endif // VECTOR_HH |