| 1 | 1 |
deleted file mode 100755 |
| ... | ... |
@@ -1,376 +0,0 @@ |
| 1 |
-// VectorUtils3 header |
|
| 2 |
-// See source for more information |
|
| 3 |
- |
|
| 4 |
-#ifndef VECTORUTILS3 |
|
| 5 |
-#define VECTORUTILS3 |
|
| 6 |
- |
|
| 7 |
-#ifdef __APPLE__ |
|
| 8 |
- #define GL_SILENCE_DEPRECATION |
|
| 9 |
- #include <OpenGL/gl3.h> |
|
| 10 |
-#else |
|
| 11 |
- #if defined(_WIN32) |
|
| 12 |
- #include "glew.h" |
|
| 13 |
- #endif |
|
| 14 |
- #include <GL/gl.h> |
|
| 15 |
-#endif |
|
| 16 |
-#include <math.h> |
|
| 17 |
-#include <stdio.h> |
|
| 18 |
- |
|
| 19 |
- |
|
| 20 |
-#ifndef M_PI |
|
| 21 |
-#define M_PI 3.14159265358979323846 |
|
| 22 |
-#endif |
|
| 23 |
- |
|
| 24 |
-// Really old type names |
|
| 25 |
-#define Vector3f Point3D |
|
| 26 |
-#define Matrix3f Matrix3D |
|
| 27 |
-#define Matrix4f Matrix4D |
|
| 28 |
- |
|
| 29 |
-// GLSL-style |
|
| 30 |
-// These are already changed, here I define the intermediate type names that I use in some demos. |
|
| 31 |
-#define Point3D vec3 |
|
| 32 |
-#define Matrix3D mat3 |
|
| 33 |
-#define Matrix4D mat4 |
|
| 34 |
-// These are not changed below yet |
|
| 35 |
-#define dot DotProduct |
|
| 36 |
-#define cross CrossProduct |
|
| 37 |
-#define normalize Normalize |
|
| 38 |
-// Furthermore, SplitVector should be revised to conform with reflect() |
|
| 39 |
- |
|
| 40 |
- |
|
| 41 |
-// Note 210515: I have removed the constructors from my structs below in order to please |
|
| 42 |
-// some compilers. They did work but the compilers were upset about C using these |
|
| 43 |
-// without knowing about the constructors, while the C++ code happily used them. |
|
| 44 |
-// However, we do not need them; you can initialize with SetVec* instead of |
|
| 45 |
-// vec*() and it will work. |
|
| 46 |
- |
|
| 47 |
- // vec3 is very useful |
|
| 48 |
- typedef struct vec3 |
|
| 49 |
- {
|
|
| 50 |
- GLfloat x, y, z; |
|
| 51 |
-// #ifdef __cplusplus |
|
| 52 |
-// vec3() {}
|
|
| 53 |
-// vec3(GLfloat x2, GLfloat y2, GLfloat z2) : x(x2), y(y2), z(z2) {}
|
|
| 54 |
- |
|
| 55 |
-//// vec3(vec4 v) : x(v.x), y(v.y), z(v.z) {}
|
|
| 56 |
-// #endif |
|
| 57 |
- } vec3, *vec3Ptr; |
|
| 58 |
- |
|
| 59 |
- // vec4 is not as useful. Can be a color with alpha, or a quaternion, but IMHO you |
|
| 60 |
- // rarely need homogenous coordinate vectors on the CPU. |
|
| 61 |
- typedef struct vec4 |
|
| 62 |
- {
|
|
| 63 |
- GLfloat x, y, z, w; // w or h |
|
| 64 |
-// #ifdef __cplusplus |
|
| 65 |
-// vec4() {}
|
|
| 66 |
-// vec4(GLfloat x2, GLfloat y2, GLfloat z2, GLfloat w2) : x(x2), y(y2), z(z2), w(w2) {}
|
|
| 67 |
-// vec4(GLfloat xyz, GLfloat w2) : x(xyz), y(xyz), z(xyz), w(w2) {}
|
|
| 68 |
-// vec4(vec3 v, GLfloat w2) : x(v.x), y(v.y), z(v.z), w(w2) {}
|
|
| 69 |
-// |
|
| 70 |
-// vec4(vec3 v) : x(v.x), y(v.y), z(v.z), w(1) {}
|
|
| 71 |
-// #endif |
|
| 72 |
- } vec4, *vec4Ptr; |
|
| 73 |
- |
|
| 74 |
-// vec2 is mostly used for texture cordinates, so I havn't bothered defining any operations for it |
|
| 75 |
- typedef struct vec2 |
|
| 76 |
- {
|
|
| 77 |
- GLfloat x, y; |
|
| 78 |
-// #ifdef __cplusplus |
|
| 79 |
-// vec2() {}
|
|
| 80 |
-// vec2(GLfloat x2, GLfloat y2) : x(x2), y(y2) {}
|
|
| 81 |
-// #endif |
|
| 82 |
- } vec2, *vec2Ptr; |
|
| 83 |
- |
|
| 84 |
- |
|
| 85 |
- typedef struct mat4 |
|
| 86 |
- {
|
|
| 87 |
- GLfloat m[16]; |
|
| 88 |
- } mat4; |
|
| 89 |
- typedef struct mat3 |
|
| 90 |
- {
|
|
| 91 |
- GLfloat m[9]; |
|
| 92 |
- } mat3; |
|
| 93 |
- |
|
| 94 |
-#ifdef __cplusplus |
|
| 95 |
-extern "C" {
|
|
| 96 |
-#endif |
|
| 97 |
- |
|
| 98 |
-// New better name for SetVector and replacements for constructors |
|
| 99 |
- vec2 SetVec2(GLfloat x, GLfloat y); |
|
| 100 |
- vec3 SetVec3(GLfloat x, GLfloat y, GLfloat z); |
|
| 101 |
- vec4 SetVec4(GLfloat x, GLfloat y, GLfloat z, GLfloat w); |
|
| 102 |
- |
|
| 103 |
-// void CopyVector(vec3 *v, vec3 *dest); // Will probably be removed |
|
| 104 |
- vec3 SetVector(GLfloat x, GLfloat y, GLfloat z); |
|
| 105 |
- mat3 SetMat3(GLfloat p0, GLfloat p1, GLfloat p2, GLfloat p3, GLfloat p4, GLfloat p5, GLfloat p6, GLfloat p7, GLfloat p8); |
|
| 106 |
- mat4 SetMat4(GLfloat p0, GLfloat p1, GLfloat p2, GLfloat p3, |
|
| 107 |
- GLfloat p4, GLfloat p5, GLfloat p6, GLfloat p7, |
|
| 108 |
- GLfloat p8, GLfloat p9, GLfloat p10, GLfloat p11, |
|
| 109 |
- GLfloat p12, GLfloat p13, GLfloat p14, GLfloat p15 |
|
| 110 |
- ); |
|
| 111 |
-// Basic vector operations on vec3's. (vec4 not included since I never need them.) |
|
| 112 |
- vec3 VectorSub(vec3 a, vec3 b); |
|
| 113 |
- vec3 VectorAdd(vec3 a, vec3 b); |
|
| 114 |
- vec3 CrossProduct(vec3 a, vec3 b); |
|
| 115 |
- GLfloat DotProduct(vec3 a, vec3 b); |
|
| 116 |
- vec3 ScalarMult(vec3 a, GLfloat s); |
|
| 117 |
- GLfloat Norm(vec3 a); |
|
| 118 |
- vec3 Normalize(vec3 a); |
|
| 119 |
- vec3 CalcNormalVector(vec3 a, vec3 b, vec3 c); |
|
| 120 |
- void SplitVector(vec3 v, vec3 n, vec3 *vn, vec3 *vp); |
|
| 121 |
- |
|
| 122 |
-// Matrix operations primarily on 4x4 matrixes! |
|
| 123 |
-// Row-wise by default but can be configured to column-wise (see SetTransposed) |
|
| 124 |
- |
|
| 125 |
- mat4 IdentityMatrix(); |
|
| 126 |
- mat4 Rx(GLfloat a); |
|
| 127 |
- mat4 Ry(GLfloat a); |
|
| 128 |
- mat4 Rz(GLfloat a); |
|
| 129 |
- mat4 T(GLfloat tx, GLfloat ty, GLfloat tz); |
|
| 130 |
- mat4 S(GLfloat sx, GLfloat sy, GLfloat sz); |
|
| 131 |
- mat4 Mult(mat4 a, mat4 b); // dest = a * b - rename to MultMat4 considered but I don't like to make the name of the most common operation longer |
|
| 132 |
- // but for symmetry, MultMat4 is made a synonym: |
|
| 133 |
- #define MultMat4 Mult |
|
| 134 |
- |
|
| 135 |
- // Was MatrixMultPoint3D |
|
| 136 |
- vec3 MultVec3(mat4 a, vec3 b); // result = a * b |
|
| 137 |
- vec4 MultVec4(mat4 a, vec4 b); |
|
| 138 |
-// void CopyMatrix(GLfloat *src, GLfloat *dest); // Will probably be removed |
|
| 139 |
- |
|
| 140 |
-// Mat3 operations (new) |
|
| 141 |
- mat3 MultMat3(mat3 a, mat3 b); // m = a * b |
|
| 142 |
- vec3 MultMat3Vec3(mat3 a, vec3 b); // result = a * b |
|
| 143 |
- |
|
| 144 |
- void OrthoNormalizeMatrix(mat4 *R); |
|
| 145 |
- mat4 transpose(mat4 m); |
|
| 146 |
-// mat4 TransposeRotation(mat4 m); // Will probably be removed |
|
| 147 |
- mat3 TransposeMat3(mat3 m); |
|
| 148 |
- mat4 ArbRotate(vec3 axis, GLfloat fi); |
|
| 149 |
- mat4 CrossMatrix(vec3 a); |
|
| 150 |
- mat4 MatrixAdd(mat4 a, mat4 b); |
|
| 151 |
- |
|
| 152 |
-// Configure, i.e. if you want matrices to be column-wise |
|
| 153 |
- void SetTransposed(char t); |
|
| 154 |
- |
|
| 155 |
-// GLU replacement functions |
|
| 156 |
- mat4 lookAtv(vec3 p, vec3 l, vec3 v); |
|
| 157 |
- mat4 lookAt(GLfloat px, GLfloat py, GLfloat pz, |
|
| 158 |
- GLfloat lx, GLfloat ly, GLfloat lz, |
|
| 159 |
- GLfloat vx, GLfloat vy, GLfloat vz); |
|
| 160 |
- mat4 perspective(float fovyInDegrees, float aspectRatio, |
|
| 161 |
- float znear, float zfar); |
|
| 162 |
- mat4 frustum(float left, float right, float bottom, float top, |
|
| 163 |
- float znear, float zfar); |
|
| 164 |
- mat4 ortho(GLfloat left, GLfloat right, GLfloat bottom, |
|
| 165 |
- GLfloat top, GLfloat near, GLfloat far); |
|
| 166 |
- |
|
| 167 |
-// For creating a normal matrix |
|
| 168 |
- mat3 InvertMat3(mat3 in); |
|
| 169 |
- mat3 InverseTranspose(mat4 in); |
|
| 170 |
- mat4 InvertMat4(mat4 a); |
|
| 171 |
- |
|
| 172 |
-// Simple conversions |
|
| 173 |
- mat3 mat4tomat3(mat4 m); |
|
| 174 |
- mat4 mat3tomat4(mat3 m); |
|
| 175 |
- vec3 vec4tovec3(vec4 v); |
|
| 176 |
- vec4 vec3tovec4(vec3 v); |
|
| 177 |
- |
|
| 178 |
-// Convenient printing calls |
|
| 179 |
- void printMat4(mat4 m); |
|
| 180 |
- void printVec3(vec3 in); |
|
| 181 |
- |
|
| 182 |
-/* Utility functions for easier uploads to shaders with error messages. */ |
|
| 183 |
-// NEW as prototype 2022, added to VU 2023 |
|
| 184 |
- void uploadMat4ToShader(GLuint shader, char *nameInShader, mat4 m); |
|
| 185 |
- void uploadUniformIntToShader(GLuint shader, char *nameInShader, GLint i); |
|
| 186 |
- void uploadUniformFloatToShader(GLuint shader, char *nameInShader, GLfloat f); |
|
| 187 |
- void uploadUniformFloatArrayToShader(GLuint shader, char *nameInShader, GLfloat *f, int arrayLength); |
|
| 188 |
- void uploadUniformVec3ToShader(GLuint shader, char *nameInShader, vec3 v); |
|
| 189 |
- void uploadUniformVec3ArrayToShader(GLuint shader, char *nameInShader, vec3 *a, int arrayLength); |
|
| 190 |
- void bindTextureToTextureUnit(GLuint tex, int unit); |
|
| 191 |
- |
|
| 192 |
- |
|
| 193 |
- |
|
| 194 |
-#ifdef __cplusplus |
|
| 195 |
-} |
|
| 196 |
-#endif |
|
| 197 |
- |
|
| 198 |
-#ifdef __cplusplus |
|
| 199 |
-// Some C++ operator overloads |
|
| 200 |
-// Non-member C++ operators! |
|
| 201 |
-// New version 2021-05-2x: Constructiors for vec3 etc replaced in order to avoid |
|
| 202 |
-// problems with some C++ compilers. |
|
| 203 |
- |
|
| 204 |
-// --- vec3 operations --- |
|
| 205 |
-inline |
|
| 206 |
-vec3 operator+(const vec3 &a, const vec3 &b) // vec3+vec3 |
|
| 207 |
-{
|
|
| 208 |
- return SetVector(a.x+b.x, a.y+b.y, a.z+b.z); |
|
| 209 |
-} |
|
| 210 |
- |
|
| 211 |
-inline |
|
| 212 |
-vec3 operator-(const vec3 &a, const vec3 &b) // vec3-vec3 |
|
| 213 |
-{
|
|
| 214 |
- return SetVector(a.x-b.x, a.y-b.y, a.z-b.z); |
|
| 215 |
-} |
|
| 216 |
- |
|
| 217 |
-inline |
|
| 218 |
-vec3 operator-(const vec3 &a) |
|
| 219 |
-{
|
|
| 220 |
- return SetVector(-a.x, -a.y, -a.z); |
|
| 221 |
-} |
|
| 222 |
- |
|
| 223 |
- // Questionable, not like GLSL |
|
| 224 |
-inline |
|
| 225 |
-float operator*(const vec3 &a, const vec3 &b) // vec3 dot vec3 |
|
| 226 |
-{
|
|
| 227 |
- return (a.x*b.x+ a.y*b.y+ a.z*b.z); |
|
| 228 |
-} |
|
| 229 |
- |
|
| 230 |
-inline |
|
| 231 |
-vec3 operator*(const vec3 &b, double a) // vec3 * scalar |
|
| 232 |
-{
|
|
| 233 |
- return SetVector(a*b.x, a*b.y, a*b.z); |
|
| 234 |
-} |
|
| 235 |
- |
|
| 236 |
-inline |
|
| 237 |
-vec3 operator*(double a, const vec3 &b) // scalar * vec3 |
|
| 238 |
-{
|
|
| 239 |
- return SetVector(a*b.x, a*b.y, a*b.z); |
|
| 240 |
-} |
|
| 241 |
- |
|
| 242 |
-inline |
|
| 243 |
-vec3 operator/(const vec3 &b, double a) // vec3 / scalar |
|
| 244 |
-{
|
|
| 245 |
- return SetVector(b.x/a, b.y/a, b.z/a); |
|
| 246 |
-} |
|
| 247 |
- |
|
| 248 |
-inline |
|
| 249 |
-void operator+=(vec3 &a, const vec3 &b) // vec3+=vec3 |
|
| 250 |
-{
|
|
| 251 |
- a = a + b; |
|
| 252 |
-} |
|
| 253 |
- |
|
| 254 |
-inline |
|
| 255 |
-void operator-=(vec3 &a, const vec3 &b) // vec3-=vec3 |
|
| 256 |
-{
|
|
| 257 |
- a = a - b; |
|
| 258 |
-} |
|
| 259 |
- |
|
| 260 |
-inline |
|
| 261 |
-void operator*=(vec3 &a, const float &b) // vec3*=scalar |
|
| 262 |
-{
|
|
| 263 |
- a = a * b; |
|
| 264 |
-} |
|
| 265 |
- |
|
| 266 |
-inline |
|
| 267 |
-void operator/=(vec3 &a, const float &b) // vec3/=scalar |
|
| 268 |
-{
|
|
| 269 |
- a = a / b; |
|
| 270 |
-} |
|
| 271 |
- |
|
| 272 |
-// --- vec4 operations --- |
|
| 273 |
- |
|
| 274 |
-inline |
|
| 275 |
-vec4 operator+(const vec4 &a, const vec4 &b) // vec4+vec4 |
|
| 276 |
-{
|
|
| 277 |
- return SetVec4(a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w); |
|
| 278 |
-} |
|
| 279 |
- |
|
| 280 |
-inline |
|
| 281 |
-vec4 operator-(const vec4 &a, const vec4 &b) // vec4-vec4 |
|
| 282 |
-{
|
|
| 283 |
- return SetVec4(a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w); |
|
| 284 |
-} |
|
| 285 |
- |
|
| 286 |
- // Questionable, not like GLSL |
|
| 287 |
-inline |
|
| 288 |
-float operator*(const vec4 &a, const vec4 &b) // vec4 dot vec4 |
|
| 289 |
-{
|
|
| 290 |
- return (a.x*b.x+ a.y*b.y+ a.z*b.z+ a.w*b.w); |
|
| 291 |
-} |
|
| 292 |
- |
|
| 293 |
-inline |
|
| 294 |
-vec4 operator*(const vec4 &b, double a) // vec4 * scalar |
|
| 295 |
-{
|
|
| 296 |
- return SetVec4(a*b.x, a*b.y, a*b.z, a*b.w); |
|
| 297 |
-} |
|
| 298 |
- |
|
| 299 |
-inline |
|
| 300 |
-vec4 operator*(double a, const vec4 &b) // scalar * vec4 |
|
| 301 |
-{
|
|
| 302 |
- return SetVec4(a*b.x, a*b.y, a*b.z, a*b.w); |
|
| 303 |
-} |
|
| 304 |
- |
|
| 305 |
-inline |
|
| 306 |
-vec4 operator/(const vec4 &b, double a) // vec4 / scalar |
|
| 307 |
-{
|
|
| 308 |
- return SetVec4(b.x/a, b.y/a, b.z/a, b.w/a); |
|
| 309 |
-} |
|
| 310 |
- |
|
| 311 |
- |
|
| 312 |
-inline |
|
| 313 |
-void operator+=(vec4 &a, const vec4 &b) // vec4+=vec4 |
|
| 314 |
-{
|
|
| 315 |
- a = a + b; |
|
| 316 |
-} |
|
| 317 |
- |
|
| 318 |
-inline |
|
| 319 |
-void operator-=(vec4 &a, const vec4 &b) // vec4-=vec4 |
|
| 320 |
-{
|
|
| 321 |
- a = a - b; |
|
| 322 |
-} |
|
| 323 |
- |
|
| 324 |
-inline |
|
| 325 |
-void operator*=(vec4 &a, const float &b) // vec4 *= scalar |
|
| 326 |
-{
|
|
| 327 |
- a = a * b; |
|
| 328 |
-} |
|
| 329 |
- |
|
| 330 |
-inline |
|
| 331 |
-void operator/=(vec4 &a, const float &b) // vec4 /= scalar |
|
| 332 |
-{
|
|
| 333 |
- a = a / b; |
|
| 334 |
-} |
|
| 335 |
- |
|
| 336 |
-// --- Matrix multiplication --- |
|
| 337 |
- |
|
| 338 |
-// mat4 * mat4 |
|
| 339 |
-inline |
|
| 340 |
-mat4 operator*(const mat4 &a, const mat4 &b) |
|
| 341 |
-{
|
|
| 342 |
- return (Mult(a, b)); |
|
| 343 |
-} |
|
| 344 |
- |
|
| 345 |
-// mat3 * mat3 |
|
| 346 |
-inline |
|
| 347 |
-mat3 operator*(const mat3 &a, const mat3 &b) |
|
| 348 |
-{
|
|
| 349 |
- return (MultMat3(a, b)); |
|
| 350 |
-} |
|
| 351 |
- |
|
| 352 |
-// mat4 * vec3 |
|
| 353 |
-inline |
|
| 354 |
-vec3 operator*(const mat4 &a, const vec3 &b) |
|
| 355 |
-{
|
|
| 356 |
- return MultVec3(a, b); // result = a * b |
|
| 357 |
-} |
|
| 358 |
- |
|
| 359 |
-// mat4 * vec4 |
|
| 360 |
-inline |
|
| 361 |
-vec4 operator*(const mat4 &a, const vec4 &b) |
|
| 362 |
-{
|
|
| 363 |
- return MultVec4(a, b); // result = a * b |
|
| 364 |
-} |
|
| 365 |
- |
|
| 366 |
-// mat3 * vec3 |
|
| 367 |
-inline |
|
| 368 |
-vec3 operator*(const mat3 &a, const vec3 &b) |
|
| 369 |
-{
|
|
| 370 |
- return MultMat3Vec3(a, b); // result = a * b |
|
| 371 |
-} |
|
| 372 |
- |
|
| 373 |
-#endif |
|
| 374 |
- |
|
| 375 |
- |
|
| 376 |
-#endif |
| 1 | 1 |
new file mode 100755 |
| ... | ... |
@@ -0,0 +1,376 @@ |
| 1 |
+// VectorUtils3 header |
|
| 2 |
+// See source for more information |
|
| 3 |
+ |
|
| 4 |
+#ifndef VECTORUTILS3 |
|
| 5 |
+#define VECTORUTILS3 |
|
| 6 |
+ |
|
| 7 |
+#ifdef __APPLE__ |
|
| 8 |
+ #define GL_SILENCE_DEPRECATION |
|
| 9 |
+ #include <OpenGL/gl3.h> |
|
| 10 |
+#else |
|
| 11 |
+ #if defined(_WIN32) |
|
| 12 |
+ #include "glew.h" |
|
| 13 |
+ #endif |
|
| 14 |
+ #include <GL/gl.h> |
|
| 15 |
+#endif |
|
| 16 |
+#include <math.h> |
|
| 17 |
+#include <stdio.h> |
|
| 18 |
+ |
|
| 19 |
+ |
|
| 20 |
+#ifndef M_PI |
|
| 21 |
+#define M_PI 3.14159265358979323846 |
|
| 22 |
+#endif |
|
| 23 |
+ |
|
| 24 |
+// Really old type names |
|
| 25 |
+#define Vector3f Point3D |
|
| 26 |
+#define Matrix3f Matrix3D |
|
| 27 |
+#define Matrix4f Matrix4D |
|
| 28 |
+ |
|
| 29 |
+// GLSL-style |
|
| 30 |
+// These are already changed, here I define the intermediate type names that I use in some demos. |
|
| 31 |
+#define Point3D vec3 |
|
| 32 |
+#define Matrix3D mat3 |
|
| 33 |
+#define Matrix4D mat4 |
|
| 34 |
+// These are not changed below yet |
|
| 35 |
+#define dot DotProduct |
|
| 36 |
+#define cross CrossProduct |
|
| 37 |
+#define normalize Normalize |
|
| 38 |
+// Furthermore, SplitVector should be revised to conform with reflect() |
|
| 39 |
+ |
|
| 40 |
+ |
|
| 41 |
+// Note 210515: I have removed the constructors from my structs below in order to please |
|
| 42 |
+// some compilers. They did work but the compilers were upset about C using these |
|
| 43 |
+// without knowing about the constructors, while the C++ code happily used them. |
|
| 44 |
+// However, we do not need them; you can initialize with SetVec* instead of |
|
| 45 |
+// vec*() and it will work. |
|
| 46 |
+ |
|
| 47 |
+ // vec3 is very useful |
|
| 48 |
+ typedef struct vec3 |
|
| 49 |
+ {
|
|
| 50 |
+ GLfloat x, y, z; |
|
| 51 |
+// #ifdef __cplusplus |
|
| 52 |
+// vec3() {}
|
|
| 53 |
+// vec3(GLfloat x2, GLfloat y2, GLfloat z2) : x(x2), y(y2), z(z2) {}
|
|
| 54 |
+ |
|
| 55 |
+//// vec3(vec4 v) : x(v.x), y(v.y), z(v.z) {}
|
|
| 56 |
+// #endif |
|
| 57 |
+ } vec3, *vec3Ptr; |
|
| 58 |
+ |
|
| 59 |
+ // vec4 is not as useful. Can be a color with alpha, or a quaternion, but IMHO you |
|
| 60 |
+ // rarely need homogenous coordinate vectors on the CPU. |
|
| 61 |
+ typedef struct vec4 |
|
| 62 |
+ {
|
|
| 63 |
+ GLfloat x, y, z, w; // w or h |
|
| 64 |
+// #ifdef __cplusplus |
|
| 65 |
+// vec4() {}
|
|
| 66 |
+// vec4(GLfloat x2, GLfloat y2, GLfloat z2, GLfloat w2) : x(x2), y(y2), z(z2), w(w2) {}
|
|
| 67 |
+// vec4(GLfloat xyz, GLfloat w2) : x(xyz), y(xyz), z(xyz), w(w2) {}
|
|
| 68 |
+// vec4(vec3 v, GLfloat w2) : x(v.x), y(v.y), z(v.z), w(w2) {}
|
|
| 69 |
+// |
|
| 70 |
+// vec4(vec3 v) : x(v.x), y(v.y), z(v.z), w(1) {}
|
|
| 71 |
+// #endif |
|
| 72 |
+ } vec4, *vec4Ptr; |
|
| 73 |
+ |
|
| 74 |
+// vec2 is mostly used for texture cordinates, so I havn't bothered defining any operations for it |
|
| 75 |
+ typedef struct vec2 |
|
| 76 |
+ {
|
|
| 77 |
+ GLfloat x, y; |
|
| 78 |
+// #ifdef __cplusplus |
|
| 79 |
+// vec2() {}
|
|
| 80 |
+// vec2(GLfloat x2, GLfloat y2) : x(x2), y(y2) {}
|
|
| 81 |
+// #endif |
|
| 82 |
+ } vec2, *vec2Ptr; |
|
| 83 |
+ |
|
| 84 |
+ |
|
| 85 |
+ typedef struct mat4 |
|
| 86 |
+ {
|
|
| 87 |
+ GLfloat m[16]; |
|
| 88 |
+ } mat4; |
|
| 89 |
+ typedef struct mat3 |
|
| 90 |
+ {
|
|
| 91 |
+ GLfloat m[9]; |
|
| 92 |
+ } mat3; |
|
| 93 |
+ |
|
| 94 |
+#ifdef __cplusplus |
|
| 95 |
+extern "C" {
|
|
| 96 |
+#endif |
|
| 97 |
+ |
|
| 98 |
+// New better name for SetVector and replacements for constructors |
|
| 99 |
+ vec2 SetVec2(GLfloat x, GLfloat y); |
|
| 100 |
+ vec3 SetVec3(GLfloat x, GLfloat y, GLfloat z); |
|
| 101 |
+ vec4 SetVec4(GLfloat x, GLfloat y, GLfloat z, GLfloat w); |
|
| 102 |
+ |
|
| 103 |
+// void CopyVector(vec3 *v, vec3 *dest); // Will probably be removed |
|
| 104 |
+ vec3 SetVector(GLfloat x, GLfloat y, GLfloat z); |
|
| 105 |
+ mat3 SetMat3(GLfloat p0, GLfloat p1, GLfloat p2, GLfloat p3, GLfloat p4, GLfloat p5, GLfloat p6, GLfloat p7, GLfloat p8); |
|
| 106 |
+ mat4 SetMat4(GLfloat p0, GLfloat p1, GLfloat p2, GLfloat p3, |
|
| 107 |
+ GLfloat p4, GLfloat p5, GLfloat p6, GLfloat p7, |
|
| 108 |
+ GLfloat p8, GLfloat p9, GLfloat p10, GLfloat p11, |
|
| 109 |
+ GLfloat p12, GLfloat p13, GLfloat p14, GLfloat p15 |
|
| 110 |
+ ); |
|
| 111 |
+// Basic vector operations on vec3's. (vec4 not included since I never need them.) |
|
| 112 |
+ vec3 VectorSub(vec3 a, vec3 b); |
|
| 113 |
+ vec3 VectorAdd(vec3 a, vec3 b); |
|
| 114 |
+ vec3 CrossProduct(vec3 a, vec3 b); |
|
| 115 |
+ GLfloat DotProduct(vec3 a, vec3 b); |
|
| 116 |
+ vec3 ScalarMult(vec3 a, GLfloat s); |
|
| 117 |
+ GLfloat Norm(vec3 a); |
|
| 118 |
+ vec3 Normalize(vec3 a); |
|
| 119 |
+ vec3 CalcNormalVector(vec3 a, vec3 b, vec3 c); |
|
| 120 |
+ void SplitVector(vec3 v, vec3 n, vec3 *vn, vec3 *vp); |
|
| 121 |
+ |
|
| 122 |
+// Matrix operations primarily on 4x4 matrixes! |
|
| 123 |
+// Row-wise by default but can be configured to column-wise (see SetTransposed) |
|
| 124 |
+ |
|
| 125 |
+ mat4 IdentityMatrix(); |
|
| 126 |
+ mat4 Rx(GLfloat a); |
|
| 127 |
+ mat4 Ry(GLfloat a); |
|
| 128 |
+ mat4 Rz(GLfloat a); |
|
| 129 |
+ mat4 T(GLfloat tx, GLfloat ty, GLfloat tz); |
|
| 130 |
+ mat4 S(GLfloat sx, GLfloat sy, GLfloat sz); |
|
| 131 |
+ mat4 Mult(mat4 a, mat4 b); // dest = a * b - rename to MultMat4 considered but I don't like to make the name of the most common operation longer |
|
| 132 |
+ // but for symmetry, MultMat4 is made a synonym: |
|
| 133 |
+ #define MultMat4 Mult |
|
| 134 |
+ |
|
| 135 |
+ // Was MatrixMultPoint3D |
|
| 136 |
+ vec3 MultVec3(mat4 a, vec3 b); // result = a * b |
|
| 137 |
+ vec4 MultVec4(mat4 a, vec4 b); |
|
| 138 |
+// void CopyMatrix(GLfloat *src, GLfloat *dest); // Will probably be removed |
|
| 139 |
+ |
|
| 140 |
+// Mat3 operations (new) |
|
| 141 |
+ mat3 MultMat3(mat3 a, mat3 b); // m = a * b |
|
| 142 |
+ vec3 MultMat3Vec3(mat3 a, vec3 b); // result = a * b |
|
| 143 |
+ |
|
| 144 |
+ void OrthoNormalizeMatrix(mat4 *R); |
|
| 145 |
+ mat4 transpose(mat4 m); |
|
| 146 |
+// mat4 TransposeRotation(mat4 m); // Will probably be removed |
|
| 147 |
+ mat3 TransposeMat3(mat3 m); |
|
| 148 |
+ mat4 ArbRotate(vec3 axis, GLfloat fi); |
|
| 149 |
+ mat4 CrossMatrix(vec3 a); |
|
| 150 |
+ mat4 MatrixAdd(mat4 a, mat4 b); |
|
| 151 |
+ |
|
| 152 |
+// Configure, i.e. if you want matrices to be column-wise |
|
| 153 |
+ void SetTransposed(char t); |
|
| 154 |
+ |
|
| 155 |
+// GLU replacement functions |
|
| 156 |
+ mat4 lookAtv(vec3 p, vec3 l, vec3 v); |
|
| 157 |
+ mat4 lookAt(GLfloat px, GLfloat py, GLfloat pz, |
|
| 158 |
+ GLfloat lx, GLfloat ly, GLfloat lz, |
|
| 159 |
+ GLfloat vx, GLfloat vy, GLfloat vz); |
|
| 160 |
+ mat4 perspective(float fovyInDegrees, float aspectRatio, |
|
| 161 |
+ float znear, float zfar); |
|
| 162 |
+ mat4 frustum(float left, float right, float bottom, float top, |
|
| 163 |
+ float znear, float zfar); |
|
| 164 |
+ mat4 ortho(GLfloat left, GLfloat right, GLfloat bottom, |
|
| 165 |
+ GLfloat top, GLfloat near, GLfloat far); |
|
| 166 |
+ |
|
| 167 |
+// For creating a normal matrix |
|
| 168 |
+ mat3 InvertMat3(mat3 in); |
|
| 169 |
+ mat3 InverseTranspose(mat4 in); |
|
| 170 |
+ mat4 InvertMat4(mat4 a); |
|
| 171 |
+ |
|
| 172 |
+// Simple conversions |
|
| 173 |
+ mat3 mat4tomat3(mat4 m); |
|
| 174 |
+ mat4 mat3tomat4(mat3 m); |
|
| 175 |
+ vec3 vec4tovec3(vec4 v); |
|
| 176 |
+ vec4 vec3tovec4(vec3 v); |
|
| 177 |
+ |
|
| 178 |
+// Convenient printing calls |
|
| 179 |
+ void printMat4(mat4 m); |
|
| 180 |
+ void printVec3(vec3 in); |
|
| 181 |
+ |
|
| 182 |
+/* Utility functions for easier uploads to shaders with error messages. */ |
|
| 183 |
+// NEW as prototype 2022, added to VU 2023 |
|
| 184 |
+ void uploadMat4ToShader(GLuint shader, char *nameInShader, mat4 m); |
|
| 185 |
+ void uploadUniformIntToShader(GLuint shader, char *nameInShader, GLint i); |
|
| 186 |
+ void uploadUniformFloatToShader(GLuint shader, char *nameInShader, GLfloat f); |
|
| 187 |
+ void uploadUniformFloatArrayToShader(GLuint shader, char *nameInShader, GLfloat *f, int arrayLength); |
|
| 188 |
+ void uploadUniformVec3ToShader(GLuint shader, char *nameInShader, vec3 v); |
|
| 189 |
+ void uploadUniformVec3ArrayToShader(GLuint shader, char *nameInShader, vec3 *a, int arrayLength); |
|
| 190 |
+ void bindTextureToTextureUnit(GLuint tex, int unit); |
|
| 191 |
+ |
|
| 192 |
+ |
|
| 193 |
+ |
|
| 194 |
+#ifdef __cplusplus |
|
| 195 |
+} |
|
| 196 |
+#endif |
|
| 197 |
+ |
|
| 198 |
+#ifdef __cplusplus |
|
| 199 |
+// Some C++ operator overloads |
|
| 200 |
+// Non-member C++ operators! |
|
| 201 |
+// New version 2021-05-2x: Constructiors for vec3 etc replaced in order to avoid |
|
| 202 |
+// problems with some C++ compilers. |
|
| 203 |
+ |
|
| 204 |
+// --- vec3 operations --- |
|
| 205 |
+inline |
|
| 206 |
+vec3 operator+(const vec3 &a, const vec3 &b) // vec3+vec3 |
|
| 207 |
+{
|
|
| 208 |
+ return SetVector(a.x+b.x, a.y+b.y, a.z+b.z); |
|
| 209 |
+} |
|
| 210 |
+ |
|
| 211 |
+inline |
|
| 212 |
+vec3 operator-(const vec3 &a, const vec3 &b) // vec3-vec3 |
|
| 213 |
+{
|
|
| 214 |
+ return SetVector(a.x-b.x, a.y-b.y, a.z-b.z); |
|
| 215 |
+} |
|
| 216 |
+ |
|
| 217 |
+inline |
|
| 218 |
+vec3 operator-(const vec3 &a) |
|
| 219 |
+{
|
|
| 220 |
+ return SetVector(-a.x, -a.y, -a.z); |
|
| 221 |
+} |
|
| 222 |
+ |
|
| 223 |
+ // Questionable, not like GLSL |
|
| 224 |
+inline |
|
| 225 |
+float operator*(const vec3 &a, const vec3 &b) // vec3 dot vec3 |
|
| 226 |
+{
|
|
| 227 |
+ return (a.x*b.x+ a.y*b.y+ a.z*b.z); |
|
| 228 |
+} |
|
| 229 |
+ |
|
| 230 |
+inline |
|
| 231 |
+vec3 operator*(const vec3 &b, double a) // vec3 * scalar |
|
| 232 |
+{
|
|
| 233 |
+ return SetVector(a*b.x, a*b.y, a*b.z); |
|
| 234 |
+} |
|
| 235 |
+ |
|
| 236 |
+inline |
|
| 237 |
+vec3 operator*(double a, const vec3 &b) // scalar * vec3 |
|
| 238 |
+{
|
|
| 239 |
+ return SetVector(a*b.x, a*b.y, a*b.z); |
|
| 240 |
+} |
|
| 241 |
+ |
|
| 242 |
+inline |
|
| 243 |
+vec3 operator/(const vec3 &b, double a) // vec3 / scalar |
|
| 244 |
+{
|
|
| 245 |
+ return SetVector(b.x/a, b.y/a, b.z/a); |
|
| 246 |
+} |
|
| 247 |
+ |
|
| 248 |
+inline |
|
| 249 |
+void operator+=(vec3 &a, const vec3 &b) // vec3+=vec3 |
|
| 250 |
+{
|
|
| 251 |
+ a = a + b; |
|
| 252 |
+} |
|
| 253 |
+ |
|
| 254 |
+inline |
|
| 255 |
+void operator-=(vec3 &a, const vec3 &b) // vec3-=vec3 |
|
| 256 |
+{
|
|
| 257 |
+ a = a - b; |
|
| 258 |
+} |
|
| 259 |
+ |
|
| 260 |
+inline |
|
| 261 |
+void operator*=(vec3 &a, const float &b) // vec3*=scalar |
|
| 262 |
+{
|
|
| 263 |
+ a = a * b; |
|
| 264 |
+} |
|
| 265 |
+ |
|
| 266 |
+inline |
|
| 267 |
+void operator/=(vec3 &a, const float &b) // vec3/=scalar |
|
| 268 |
+{
|
|
| 269 |
+ a = a / b; |
|
| 270 |
+} |
|
| 271 |
+ |
|
| 272 |
+// --- vec4 operations --- |
|
| 273 |
+ |
|
| 274 |
+inline |
|
| 275 |
+vec4 operator+(const vec4 &a, const vec4 &b) // vec4+vec4 |
|
| 276 |
+{
|
|
| 277 |
+ return SetVec4(a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w); |
|
| 278 |
+} |
|
| 279 |
+ |
|
| 280 |
+inline |
|
| 281 |
+vec4 operator-(const vec4 &a, const vec4 &b) // vec4-vec4 |
|
| 282 |
+{
|
|
| 283 |
+ return SetVec4(a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w); |
|
| 284 |
+} |
|
| 285 |
+ |
|
| 286 |
+ // Questionable, not like GLSL |
|
| 287 |
+inline |
|
| 288 |
+float operator*(const vec4 &a, const vec4 &b) // vec4 dot vec4 |
|
| 289 |
+{
|
|
| 290 |
+ return (a.x*b.x+ a.y*b.y+ a.z*b.z+ a.w*b.w); |
|
| 291 |
+} |
|
| 292 |
+ |
|
| 293 |
+inline |
|
| 294 |
+vec4 operator*(const vec4 &b, double a) // vec4 * scalar |
|
| 295 |
+{
|
|
| 296 |
+ return SetVec4(a*b.x, a*b.y, a*b.z, a*b.w); |
|
| 297 |
+} |
|
| 298 |
+ |
|
| 299 |
+inline |
|
| 300 |
+vec4 operator*(double a, const vec4 &b) // scalar * vec4 |
|
| 301 |
+{
|
|
| 302 |
+ return SetVec4(a*b.x, a*b.y, a*b.z, a*b.w); |
|
| 303 |
+} |
|
| 304 |
+ |
|
| 305 |
+inline |
|
| 306 |
+vec4 operator/(const vec4 &b, double a) // vec4 / scalar |
|
| 307 |
+{
|
|
| 308 |
+ return SetVec4(b.x/a, b.y/a, b.z/a, b.w/a); |
|
| 309 |
+} |
|
| 310 |
+ |
|
| 311 |
+ |
|
| 312 |
+inline |
|
| 313 |
+void operator+=(vec4 &a, const vec4 &b) // vec4+=vec4 |
|
| 314 |
+{
|
|
| 315 |
+ a = a + b; |
|
| 316 |
+} |
|
| 317 |
+ |
|
| 318 |
+inline |
|
| 319 |
+void operator-=(vec4 &a, const vec4 &b) // vec4-=vec4 |
|
| 320 |
+{
|
|
| 321 |
+ a = a - b; |
|
| 322 |
+} |
|
| 323 |
+ |
|
| 324 |
+inline |
|
| 325 |
+void operator*=(vec4 &a, const float &b) // vec4 *= scalar |
|
| 326 |
+{
|
|
| 327 |
+ a = a * b; |
|
| 328 |
+} |
|
| 329 |
+ |
|
| 330 |
+inline |
|
| 331 |
+void operator/=(vec4 &a, const float &b) // vec4 /= scalar |
|
| 332 |
+{
|
|
| 333 |
+ a = a / b; |
|
| 334 |
+} |
|
| 335 |
+ |
|
| 336 |
+// --- Matrix multiplication --- |
|
| 337 |
+ |
|
| 338 |
+// mat4 * mat4 |
|
| 339 |
+inline |
|
| 340 |
+mat4 operator*(const mat4 &a, const mat4 &b) |
|
| 341 |
+{
|
|
| 342 |
+ return (Mult(a, b)); |
|
| 343 |
+} |
|
| 344 |
+ |
|
| 345 |
+// mat3 * mat3 |
|
| 346 |
+inline |
|
| 347 |
+mat3 operator*(const mat3 &a, const mat3 &b) |
|
| 348 |
+{
|
|
| 349 |
+ return (MultMat3(a, b)); |
|
| 350 |
+} |
|
| 351 |
+ |
|
| 352 |
+// mat4 * vec3 |
|
| 353 |
+inline |
|
| 354 |
+vec3 operator*(const mat4 &a, const vec3 &b) |
|
| 355 |
+{
|
|
| 356 |
+ return MultVec3(a, b); // result = a * b |
|
| 357 |
+} |
|
| 358 |
+ |
|
| 359 |
+// mat4 * vec4 |
|
| 360 |
+inline |
|
| 361 |
+vec4 operator*(const mat4 &a, const vec4 &b) |
|
| 362 |
+{
|
|
| 363 |
+ return MultVec4(a, b); // result = a * b |
|
| 364 |
+} |
|
| 365 |
+ |
|
| 366 |
+// mat3 * vec3 |
|
| 367 |
+inline |
|
| 368 |
+vec3 operator*(const mat3 &a, const vec3 &b) |
|
| 369 |
+{
|
|
| 370 |
+ return MultMat3Vec3(a, b); // result = a * b |
|
| 371 |
+} |
|
| 372 |
+ |
|
| 373 |
+#endif |
|
| 374 |
+ |
|
| 375 |
+ |
|
| 376 |
+#endif |