mirror of
https://github.com/XProger/OpenLara.git
synced 2025-08-06 13:16:52 +02:00
minor fixes, tab to spaces
This commit is contained in:
BIN
bin/OpenLara.exe
BIN
bin/OpenLara.exe
Binary file not shown.
@@ -225,7 +225,19 @@ struct Controller {
|
|||||||
do {
|
do {
|
||||||
trigCmd = (*fd++).triggerCmd; // trigger action
|
trigCmd = (*fd++).triggerCmd; // trigger action
|
||||||
switch (trigCmd.func) {
|
switch (trigCmd.func) {
|
||||||
case 0 : break; // activate item
|
case 0 :
|
||||||
|
/*
|
||||||
|
if (getEntity().id == 0 && cmd.sub == 2) {
|
||||||
|
TR::Entity &e = level->entities[trigCmd.args];
|
||||||
|
for (int i = 0; i < level->modelsCount; i++)
|
||||||
|
if (e.id == level->models[i].id) {
|
||||||
|
TR::Animation *anim = &level->anims[level->models[i].animation];
|
||||||
|
for (int j = 0; j < anim->scCount; j++)
|
||||||
|
LOG("state: %d\n", anim->state);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
break; // activate item
|
||||||
case 1 : break; // switch to camera
|
case 1 : break; // switch to camera
|
||||||
case 2 : break; // camera delay
|
case 2 : break; // camera delay
|
||||||
case 3 : break; // flip map
|
case 3 : break; // flip map
|
||||||
|
118
src/shader.glsl
118
src/shader.glsl
@@ -1,85 +1,85 @@
|
|||||||
R"====(
|
R"====(
|
||||||
#ifndef SPRITE
|
#ifndef SPRITE
|
||||||
varying vec4 vNormal;
|
varying vec4 vNormal;
|
||||||
varying vec3 vLightVec;
|
varying vec3 vLightVec;
|
||||||
varying vec3 vViewVec;
|
varying vec3 vViewVec;
|
||||||
#endif
|
#endif
|
||||||
varying vec2 vTexCoord;
|
varying vec2 vTexCoord;
|
||||||
varying vec4 vColor;
|
varying vec4 vColor;
|
||||||
|
|
||||||
#ifdef VERTEX
|
#ifdef VERTEX
|
||||||
uniform mat4 uViewProj;
|
uniform mat4 uViewProj;
|
||||||
uniform mat4 uModel;
|
uniform mat4 uModel;
|
||||||
uniform vec4 uColor;
|
uniform vec4 uColor;
|
||||||
|
|
||||||
#ifndef SPRITE
|
#ifndef SPRITE
|
||||||
uniform vec3 uViewPos;
|
uniform vec3 uViewPos;
|
||||||
uniform vec3 uLightPos;
|
uniform vec3 uLightPos;
|
||||||
uniform vec2 uAnimTexRanges[MAX_RANGES];
|
uniform vec2 uAnimTexRanges[MAX_RANGES];
|
||||||
uniform vec2 uAnimTexOffsets[MAX_OFFSETS];
|
uniform vec2 uAnimTexOffsets[MAX_OFFSETS];
|
||||||
#else
|
#else
|
||||||
uniform mat4 uViewInv;
|
uniform mat4 uViewInv;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
uniform vec4 uParam; // x - time
|
uniform vec4 uParam; // x - time
|
||||||
|
|
||||||
attribute vec4 aCoord;
|
attribute vec4 aCoord;
|
||||||
attribute vec4 aTexCoord;
|
attribute vec4 aTexCoord;
|
||||||
attribute vec4 aNormal;
|
attribute vec4 aNormal;
|
||||||
attribute vec4 aColor;
|
attribute vec4 aColor;
|
||||||
|
|
||||||
#define TEXCOORD_SCALE (1.0 / 32767.0)
|
#define TEXCOORD_SCALE (1.0 / 32767.0)
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec4 coord = uModel * vec4(aCoord.xyz, 1.0);
|
vec4 coord = uModel * vec4(aCoord.xyz, 1.0);
|
||||||
vColor = aColor * uColor;
|
vColor = aColor * uColor;
|
||||||
|
|
||||||
#ifdef CAUSTICS
|
#ifdef CAUSTICS
|
||||||
float sum = coord.x + coord.y + coord.z;
|
float sum = coord.x + coord.y + coord.z;
|
||||||
vColor.xyz *= abs(sin(sum / 512.0 + uParam.x)) * 0.75 + 0.25;
|
vColor.xyz *= abs(sin(sum / 512.0 + uParam.x)) * 0.75 + 0.25;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef SPRITE
|
#ifndef SPRITE
|
||||||
// animated texture coordinates
|
// animated texture coordinates
|
||||||
vec2 range = uAnimTexRanges[int(aTexCoord.z)]; // x - start index, y - count
|
vec2 range = uAnimTexRanges[int(aTexCoord.z)]; // x - start index, y - count
|
||||||
|
|
||||||
float f = fract((aTexCoord.w + uParam.x * 4.0 - range.x) / range.y) * range.y;
|
float f = fract((aTexCoord.w + uParam.x * 4.0 - range.x) / range.y) * range.y;
|
||||||
vec2 offset = uAnimTexOffsets[int(range.x + f)]; // texCoord offset from first frame
|
vec2 offset = uAnimTexOffsets[int(range.x + f)]; // texCoord offset from first frame
|
||||||
|
|
||||||
vTexCoord = (aTexCoord.xy + offset) * TEXCOORD_SCALE; // first frame + offset * isAnimated
|
vTexCoord = (aTexCoord.xy + offset) * TEXCOORD_SCALE; // first frame + offset * isAnimated
|
||||||
|
|
||||||
vViewVec = uViewPos - coord.xyz;
|
vViewVec = uViewPos - coord.xyz;
|
||||||
vLightVec = uLightPos - coord.xyz;
|
vLightVec = uLightPos - coord.xyz;
|
||||||
vNormal = uModel * aNormal;
|
vNormal = uModel * aNormal;
|
||||||
#else
|
#else
|
||||||
vTexCoord = aTexCoord.xy * TEXCOORD_SCALE;
|
vTexCoord = aTexCoord.xy * TEXCOORD_SCALE;
|
||||||
coord.xyz -= uViewInv[0].xyz * aTexCoord.z + uViewInv[1].xyz * aTexCoord.w;
|
coord.xyz -= uViewInv[0].xyz * aTexCoord.z + uViewInv[1].xyz * aTexCoord.w;
|
||||||
#endif
|
#endif
|
||||||
gl_Position = uViewProj * coord;
|
gl_Position = uViewProj * coord;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
uniform sampler2D sDiffuse;
|
uniform sampler2D sDiffuse;
|
||||||
|
|
||||||
#ifndef SPRITE
|
#ifndef SPRITE
|
||||||
uniform vec3 uAmbient;
|
uniform vec3 uAmbient;
|
||||||
uniform vec4 uLightColor;
|
uniform vec4 uLightColor;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec4 color = texture2D(sDiffuse, vTexCoord);
|
vec4 color = texture2D(sDiffuse, vTexCoord);
|
||||||
if (color.w < 0.6)
|
if (color.w < 0.6)
|
||||||
discard;
|
discard;
|
||||||
|
|
||||||
color *= vColor;
|
color *= vColor;
|
||||||
#ifndef SPRITE
|
#ifndef SPRITE
|
||||||
color.xyz = pow(abs(color.xyz), vec3(2.2));
|
color.xyz = pow(abs(color.xyz), vec3(2.2));
|
||||||
float lum = dot(normalize(vNormal.xyz), normalize(vLightVec));
|
float lum = dot(normalize(vNormal.xyz), normalize(vLightVec));
|
||||||
float att = max(0.0, 1.0 - dot(vLightVec, vLightVec) / uLightColor.w);
|
float att = max(0.0, 1.0 - dot(vLightVec, vLightVec) / uLightColor.w);
|
||||||
vec3 light = uLightColor.xyz * max(vNormal.w, lum * att) + uAmbient;
|
vec3 light = uLightColor.xyz * max(vNormal.w, lum * att) + uAmbient;
|
||||||
color.xyz *= light;
|
color.xyz *= light;
|
||||||
color.xyz = pow(abs(color.xyz), vec3(1.0/2.2));
|
color.xyz = pow(abs(color.xyz), vec3(1.0/2.2));
|
||||||
#endif
|
#endif
|
||||||
gl_FragColor = color;
|
gl_FragColor = color;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
)===="
|
)===="
|
@@ -19,7 +19,7 @@ struct Shader {
|
|||||||
#ifdef MOBILE
|
#ifdef MOBILE
|
||||||
#define GLSL_DEFINE "precision highp float;\n" "#define MOBILE\n"
|
#define GLSL_DEFINE "precision highp float;\n" "#define MOBILE\n"
|
||||||
#else
|
#else
|
||||||
#define GLSL_DEFINE "#version 120\n"
|
#define GLSL_DEFINE "#version 120\n"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const int type[2] = { GL_VERTEX_SHADER, GL_FRAGMENT_SHADER };
|
const int type[2] = { GL_VERTEX_SHADER, GL_FRAGMENT_SHADER };
|
||||||
|
772
src/utils.h
772
src/utils.h
@@ -7,514 +7,514 @@
|
|||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
#define debugBreak() _asm { int 3 }
|
#define debugBreak() _asm { int 3 }
|
||||||
#define ASSERT(expr) if (expr) {} else { LOG("ASSERT %s in %s:%d\n", #expr, __FILE__, __LINE__); debugBreak(); }
|
#define ASSERT(expr) if (expr) {} else { LOG("ASSERT %s in %s:%d\n", #expr, __FILE__, __LINE__); debugBreak(); }
|
||||||
|
|
||||||
#ifndef ANDROID
|
#ifndef ANDROID
|
||||||
#define LOG(...) printf(__VA_ARGS__)
|
#define LOG(...) printf(__VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
#include <android/log.h>
|
#include <android/log.h>
|
||||||
#define LOG(...) __android_log_print(ANDROID_LOG_INFO,"X5",__VA_ARGS__)
|
#define LOG(...) __android_log_print(ANDROID_LOG_INFO,"X5",__VA_ARGS__)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#define ASSERT(expr)
|
#define ASSERT(expr)
|
||||||
// #define LOG(...) ((void)0)
|
// #define LOG(...) ((void)0)
|
||||||
#define LOG(...) printf(__VA_ARGS__)
|
#define LOG(...) printf(__VA_ARGS__)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define PI 3.14159265358979323846f
|
#define PI 3.14159265358979323846f
|
||||||
#define DEG2RAD (PI / 180.0f)
|
#define DEG2RAD (PI / 180.0f)
|
||||||
#define RAD2DEG (180.0f / PI)
|
#define RAD2DEG (180.0f / PI)
|
||||||
#define EPS FLT_EPSILON
|
#define EPS FLT_EPSILON
|
||||||
|
|
||||||
typedef char int8;
|
typedef char int8;
|
||||||
typedef short int16;
|
typedef short int16;
|
||||||
typedef int int32;
|
typedef int int32;
|
||||||
typedef unsigned char uint8;
|
typedef unsigned char uint8;
|
||||||
typedef unsigned short uint16;
|
typedef unsigned short uint16;
|
||||||
typedef unsigned int uint32;
|
typedef unsigned int uint32;
|
||||||
|
|
||||||
struct ubyte4 {
|
struct ubyte4 {
|
||||||
uint8 x, y, z, w;
|
uint8 x, y, z, w;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct short2 {
|
struct short2 {
|
||||||
int16 x, y;
|
int16 x, y;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct short3 {
|
struct short3 {
|
||||||
int16 x, y, z;
|
int16 x, y, z;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct short4 {
|
struct short4 {
|
||||||
int16 x, y, z, w;
|
int16 x, y, z, w;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline const T& min(const T &a, const T &b) {
|
inline const T& min(const T &a, const T &b) {
|
||||||
return a < b ? a : b;
|
return a < b ? a : b;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
inline const T& max(const T &a, const T &b) {
|
inline const T& max(const T &a, const T &b) {
|
||||||
return a > b ? a : b;
|
return a > b ? a : b;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
inline const T& clamp(const T &x, const T &a, const T &b) {
|
inline const T& clamp(const T &x, const T &a, const T &b) {
|
||||||
return x < a ? a : (x > b ? b : x);
|
return x < a ? a : (x > b ? b : x);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
inline const int sign(const T &x) {
|
inline const int sign(const T &x) {
|
||||||
return x > 0 ? 1 : (x < 0 ? -1 : 0);
|
return x > 0 ? 1 : (x < 0 ? -1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct vec2 {
|
struct vec2 {
|
||||||
float x, y;
|
float x, y;
|
||||||
vec2() {}
|
vec2() {}
|
||||||
vec2(float s) : x(s), y(s) {}
|
vec2(float s) : x(s), y(s) {}
|
||||||
vec2(float x, float y) : x(x), y(y) {}
|
vec2(float x, float y) : x(x), y(y) {}
|
||||||
|
|
||||||
vec2 operator + (const vec2 &v) const { return vec2(x+v.x, y+v.y); }
|
vec2 operator + (const vec2 &v) const { return vec2(x+v.x, y+v.y); }
|
||||||
vec2 operator - (const vec2 &v) const { return vec2(x-v.x, y-v.y); }
|
vec2 operator - (const vec2 &v) const { return vec2(x-v.x, y-v.y); }
|
||||||
vec2 operator * (float s) const { return vec2(x*s, y*s); }
|
vec2 operator * (float s) const { return vec2(x*s, y*s); }
|
||||||
float dot(const vec2 &v) const { return x*v.x + y*v.y; }
|
float dot(const vec2 &v) const { return x*v.x + y*v.y; }
|
||||||
float cross(const vec2 &v) const { return x*v.y - y*v.x; }
|
float cross(const vec2 &v) const { return x*v.y - y*v.x; }
|
||||||
float length2() const { return dot(*this); }
|
float length2() const { return dot(*this); }
|
||||||
float length() const { return sqrtf(length2()); }
|
float length() const { return sqrtf(length2()); }
|
||||||
vec2 normal() const { float s = length(); return s == 0.0 ? (*this) : (*this)*(1.0f/s); }
|
vec2 normal() const { float s = length(); return s == 0.0 ? (*this) : (*this)*(1.0f/s); }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct vec3 {
|
struct vec3 {
|
||||||
float x, y, z;
|
float x, y, z;
|
||||||
vec3() {}
|
vec3() {}
|
||||||
vec3(float s) : x(s), y(s), z(s) {}
|
vec3(float s) : x(s), y(s), z(s) {}
|
||||||
vec3(float x, float y, float z) : x(x), y(y), z(z) {}
|
vec3(float x, float y, float z) : x(x), y(y), z(z) {}
|
||||||
vec3(const vec2 &xy, float z = 0.0f) : x(xy.x), y(xy.y), z(z) {}
|
vec3(const vec2 &xy, float z = 0.0f) : x(xy.x), y(xy.y), z(z) {}
|
||||||
vec3(float lng, float lat) : x(sinf(lat) * cosf(lng)), y(-sinf(lng)), z(cosf(lat) * cosf(lng)) {}
|
vec3(float lng, float lat) : x(sinf(lat) * cosf(lng)), y(-sinf(lng)), z(cosf(lat) * cosf(lng)) {}
|
||||||
|
|
||||||
float& operator [] (int index) const { return ((float*)this)[index]; }
|
float& operator [] (int index) const { return ((float*)this)[index]; }
|
||||||
|
|
||||||
vec3 operator + (const vec3 &v) const { return vec3(x+v.x, y+v.y, z+v.z); }
|
vec3 operator + (const vec3 &v) const { return vec3(x+v.x, y+v.y, z+v.z); }
|
||||||
vec3 operator - (const vec3 &v) const { return vec3(x-v.x, y-v.y, z-v.z); }
|
vec3 operator - (const vec3 &v) const { return vec3(x-v.x, y-v.y, z-v.z); }
|
||||||
vec3 operator * (const vec3 &v) const { return vec3(x*v.x, y*v.y, z*v.z); }
|
vec3 operator * (const vec3 &v) const { return vec3(x*v.x, y*v.y, z*v.z); }
|
||||||
vec3 operator / (const vec3 &v) const { return vec3(x/v.x, y/v.y, z/v.z); }
|
vec3 operator / (const vec3 &v) const { return vec3(x/v.x, y/v.y, z/v.z); }
|
||||||
vec3 operator * (float s) const { return vec3(x*s, y*s, z*s); }
|
vec3 operator * (float s) const { return vec3(x*s, y*s, z*s); }
|
||||||
|
|
||||||
float dot(const vec3 &v) const { return x*v.x + y*v.y + z*v.z; }
|
float dot(const vec3 &v) const { return x*v.x + y*v.y + z*v.z; }
|
||||||
vec3 cross(const vec3 &v) const { return vec3(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x); }
|
vec3 cross(const vec3 &v) const { return vec3(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x); }
|
||||||
float length2() const { return dot(*this); }
|
float length2() const { return dot(*this); }
|
||||||
float length() const { return sqrtf(length2()); }
|
float length() const { return sqrtf(length2()); }
|
||||||
vec3 normal() const { float s = length(); return s == 0.0 ? (*this) : (*this)*(1.0f/s); }
|
vec3 normal() const { float s = length(); return s == 0.0 ? (*this) : (*this)*(1.0f/s); }
|
||||||
|
|
||||||
vec3 lerp(const vec3 &v, const float t) const {
|
vec3 lerp(const vec3 &v, const float t) const {
|
||||||
return *this + (v - *this) * t;
|
return *this + (v - *this) * t;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 rotateY(float angle) const {
|
vec3 rotateY(float angle) const {
|
||||||
float s = sinf(angle), c = cosf(angle);
|
float s = sinf(angle), c = cosf(angle);
|
||||||
return vec3(x*c - z*s, y, x*s + z*c);
|
return vec3(x*c - z*s, y, x*s + z*c);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct vec4 {
|
struct vec4 {
|
||||||
union {
|
union {
|
||||||
struct { vec3 xyz; };
|
struct { vec3 xyz; };
|
||||||
struct { float x, y, z, w; };
|
struct { float x, y, z, w; };
|
||||||
};
|
};
|
||||||
|
|
||||||
vec4() {}
|
vec4() {}
|
||||||
vec4(float s) : x(s), y(s), z(s), w(s) {}
|
vec4(float s) : x(s), y(s), z(s), w(s) {}
|
||||||
vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
|
vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
|
||||||
vec4(const vec3 &xyz, float w) : x(xyz.x), y(xyz.y), z(xyz.z), w(w) {}
|
vec4(const vec3 &xyz, float w) : x(xyz.x), y(xyz.y), z(xyz.z), w(w) {}
|
||||||
|
|
||||||
vec4& operator *= (const vec4 &v) { x*=v.x; y*=v.y; z*=v.z; w*=v.w; return *this; }
|
vec4& operator *= (const vec4 &v) { x*=v.x; y*=v.y; z*=v.z; w*=v.w; return *this; }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct quat {
|
struct quat {
|
||||||
float x, y, z, w;
|
float x, y, z, w;
|
||||||
|
|
||||||
quat() {}
|
quat() {}
|
||||||
quat(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
|
quat(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
|
||||||
|
|
||||||
quat(const vec3 &axis, float angle) {
|
quat(const vec3 &axis, float angle) {
|
||||||
angle *= 0.5f;
|
angle *= 0.5f;
|
||||||
float s = sinf(angle);
|
float s = sinf(angle);
|
||||||
x = axis.x * s;
|
x = axis.x * s;
|
||||||
y = axis.y * s;
|
y = axis.y * s;
|
||||||
z = axis.z * s;
|
z = axis.z * s;
|
||||||
w = cosf(angle);
|
w = cosf(angle);
|
||||||
}
|
}
|
||||||
|
|
||||||
quat operator - () const {
|
quat operator - () const {
|
||||||
return quat(-x, -y, -z, -w);
|
return quat(-x, -y, -z, -w);
|
||||||
}
|
}
|
||||||
|
|
||||||
quat operator + (const quat &q) const {
|
quat operator + (const quat &q) const {
|
||||||
return quat(x + q.x, y + q.y, z + q.z, w + q.w);
|
return quat(x + q.x, y + q.y, z + q.z, w + q.w);
|
||||||
}
|
}
|
||||||
|
|
||||||
quat operator - (const quat &q) const {
|
quat operator - (const quat &q) const {
|
||||||
return quat(x - q.x, y - q.y, z - q.z, w - q.w);
|
return quat(x - q.x, y - q.y, z - q.z, w - q.w);
|
||||||
}
|
}
|
||||||
|
|
||||||
quat operator * (const float s) const {
|
quat operator * (const float s) const {
|
||||||
return quat(x * s, y * s, z * s, w * s);
|
return quat(x * s, y * s, z * s, w * s);
|
||||||
}
|
}
|
||||||
|
|
||||||
quat operator * (const quat &q) const {
|
quat operator * (const quat &q) const {
|
||||||
return quat(w * q.x + x * q.w + y * q.z - z * q.y,
|
return quat(w * q.x + x * q.w + y * q.z - z * q.y,
|
||||||
w * q.y + y * q.w + z * q.x - x * q.z,
|
w * q.y + y * q.w + z * q.x - x * q.z,
|
||||||
w * q.z + z * q.w + x * q.y - y * q.x,
|
w * q.z + z * q.w + x * q.y - y * q.x,
|
||||||
w * q.w - x * q.x - y * q.y - z * q.z);
|
w * q.w - x * q.x - y * q.y - z * q.z);
|
||||||
}
|
}
|
||||||
|
|
||||||
float dot(const quat &q) const {
|
float dot(const quat &q) const {
|
||||||
return x * q.x + y * q.y + z * q.z + w * q.w;
|
return x * q.x + y * q.y + z * q.z + w * q.w;
|
||||||
}
|
}
|
||||||
|
|
||||||
float length2() const {
|
float length2() const {
|
||||||
return dot(*this);
|
return dot(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
float length() const {
|
float length() const {
|
||||||
return sqrtf(length2());
|
return sqrtf(length2());
|
||||||
}
|
}
|
||||||
|
|
||||||
void normalize() {
|
void normalize() {
|
||||||
*this = normal();
|
*this = normal();
|
||||||
}
|
}
|
||||||
|
|
||||||
quat normal() const {
|
quat normal() const {
|
||||||
return *this * (1.0f / length());
|
return *this * (1.0f / length());
|
||||||
}
|
}
|
||||||
|
|
||||||
quat conjugate() const {
|
quat conjugate() const {
|
||||||
return quat(-x, -y, -z, w);
|
return quat(-x, -y, -z, w);
|
||||||
}
|
}
|
||||||
|
|
||||||
quat inverse() const {
|
quat inverse() const {
|
||||||
return conjugate() * (1.0f / length2());
|
return conjugate() * (1.0f / length2());
|
||||||
}
|
}
|
||||||
|
|
||||||
quat lerp(const quat &q, float t) const {
|
quat lerp(const quat &q, float t) const {
|
||||||
if (t <= 0.0f) return *this;
|
if (t <= 0.0f) return *this;
|
||||||
if (t >= 1.0f) return q;
|
if (t >= 1.0f) return q;
|
||||||
|
|
||||||
return dot(q) < 0 ? (*this - (q + *this) * t) :
|
return dot(q) < 0 ? (*this - (q + *this) * t) :
|
||||||
(*this + (q - *this) * t);
|
(*this + (q - *this) * t);
|
||||||
}
|
}
|
||||||
|
|
||||||
quat slerp(const quat &q, float t) const {
|
quat slerp(const quat &q, float t) const {
|
||||||
if (t <= 0.0f) return *this;
|
if (t <= 0.0f) return *this;
|
||||||
if (t >= 1.0f) return q;
|
if (t >= 1.0f) return q;
|
||||||
|
|
||||||
quat temp;
|
quat temp;
|
||||||
float omega, cosom, sinom, scale0, scale1;
|
float omega, cosom, sinom, scale0, scale1;
|
||||||
|
|
||||||
cosom = dot(q);
|
cosom = dot(q);
|
||||||
if (cosom < 0.0f) {
|
if (cosom < 0.0f) {
|
||||||
temp = -q;
|
temp = -q;
|
||||||
cosom = -cosom;
|
cosom = -cosom;
|
||||||
} else
|
} else
|
||||||
temp = q;
|
temp = q;
|
||||||
|
|
||||||
if (1.0f - cosom > EPS) {
|
if (1.0f - cosom > EPS) {
|
||||||
omega = acos(cosom);
|
omega = acos(cosom);
|
||||||
sinom = 1.0f / sin(omega);
|
sinom = 1.0f / sin(omega);
|
||||||
scale0 = sin((1.0f - t) * omega) * sinom;
|
scale0 = sin((1.0f - t) * omega) * sinom;
|
||||||
scale1 = sin(t * omega) * sinom;
|
scale1 = sin(t * omega) * sinom;
|
||||||
} else {
|
} else {
|
||||||
scale0 = 1.0f - t;
|
scale0 = 1.0f - t;
|
||||||
scale1 = t;
|
scale1 = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
return *this * scale0 + temp * scale1;
|
return *this * scale0 + temp * scale1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct mat4 {
|
struct mat4 {
|
||||||
|
|
||||||
union {
|
union {
|
||||||
struct {
|
struct {
|
||||||
float e00, e10, e20, e30,
|
float e00, e10, e20, e30,
|
||||||
e01, e11, e21, e31,
|
e01, e11, e21, e31,
|
||||||
e02, e12, e22, e32,
|
e02, e12, e22, e32,
|
||||||
e03, e13, e23, e33;
|
e03, e13, e23, e33;
|
||||||
};
|
};
|
||||||
struct { vec4 right, up, dir, offset; };
|
struct { vec4 right, up, dir, offset; };
|
||||||
};
|
};
|
||||||
|
|
||||||
mat4() {}
|
mat4() {}
|
||||||
|
|
||||||
mat4(const quat &rot, const vec3 &pos) {
|
mat4(const quat &rot, const vec3 &pos) {
|
||||||
setRot(rot);
|
setRot(rot);
|
||||||
setPos(pos);
|
setPos(pos);
|
||||||
e30 = e31 = e32 = 0.0f;
|
e30 = e31 = e32 = 0.0f;
|
||||||
e33 = 1.0f;
|
e33 = 1.0f;
|
||||||
}
|
|
||||||
|
|
||||||
mat4(float l, float r, float b, float t, float znear, float zfar) {
|
|
||||||
identity();
|
|
||||||
e00 = 2.0f / (r - l);
|
|
||||||
e11 = 2.0f / (t - b);
|
|
||||||
e22 = 2.0f / (znear - zfar);
|
|
||||||
e03 = (l + r) / (l - r);
|
|
||||||
e13 = (b + t) / (b - t);
|
|
||||||
e23 = (zfar + znear) / (znear - zfar);
|
|
||||||
}
|
|
||||||
|
|
||||||
mat4(float fov, float aspect, float znear, float zfar) {
|
|
||||||
float k = 1.0f / tanf(fov * 0.5f * DEG2RAD);
|
|
||||||
identity();
|
|
||||||
e00 = k / aspect;
|
|
||||||
e11 = k;
|
|
||||||
e22 = (znear + zfar) / (znear - zfar);
|
|
||||||
e33 = 0.0f;
|
|
||||||
e32 = -1.0f;
|
|
||||||
e23 = 2.0f * zfar * znear / (znear - zfar);
|
|
||||||
}
|
|
||||||
|
|
||||||
mat4(const vec3 &from, const vec3 &at, const vec3 &up) {
|
|
||||||
vec3 r, u, d;
|
|
||||||
d = (from - at).normal();
|
|
||||||
r = up.cross(d).normal();
|
|
||||||
u = d.cross(r);
|
|
||||||
|
|
||||||
this->right = vec4(r, 0.0f);
|
|
||||||
this->up = vec4(u, 0.0f);
|
|
||||||
this->dir = vec4(d, 0.0f);
|
|
||||||
this->offset = vec4(from, 1.0f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void identity() {
|
mat4(float l, float r, float b, float t, float znear, float zfar) {
|
||||||
e10 = e20 = e30 = e01 = e21 = e31 = e02 = e12 = e32 = e03 = e13 = e23 = 0.0f;
|
identity();
|
||||||
e00 = e11 = e22 = e33 = 1.0f;
|
e00 = 2.0f / (r - l);
|
||||||
}
|
e11 = 2.0f / (t - b);
|
||||||
|
e22 = 2.0f / (znear - zfar);
|
||||||
|
e03 = (l + r) / (l - r);
|
||||||
|
e13 = (b + t) / (b - t);
|
||||||
|
e23 = (zfar + znear) / (znear - zfar);
|
||||||
|
}
|
||||||
|
|
||||||
mat4 operator * (const mat4 &m) const {
|
mat4(float fov, float aspect, float znear, float zfar) {
|
||||||
mat4 r;
|
float k = 1.0f / tanf(fov * 0.5f * DEG2RAD);
|
||||||
r.e00 = e00 * m.e00 + e01 * m.e10 + e02 * m.e20 + e03 * m.e30;
|
identity();
|
||||||
r.e10 = e10 * m.e00 + e11 * m.e10 + e12 * m.e20 + e13 * m.e30;
|
e00 = k / aspect;
|
||||||
r.e20 = e20 * m.e00 + e21 * m.e10 + e22 * m.e20 + e23 * m.e30;
|
e11 = k;
|
||||||
r.e30 = e30 * m.e00 + e31 * m.e10 + e32 * m.e20 + e33 * m.e30;
|
e22 = (znear + zfar) / (znear - zfar);
|
||||||
r.e01 = e00 * m.e01 + e01 * m.e11 + e02 * m.e21 + e03 * m.e31;
|
e33 = 0.0f;
|
||||||
r.e11 = e10 * m.e01 + e11 * m.e11 + e12 * m.e21 + e13 * m.e31;
|
e32 = -1.0f;
|
||||||
r.e21 = e20 * m.e01 + e21 * m.e11 + e22 * m.e21 + e23 * m.e31;
|
e23 = 2.0f * zfar * znear / (znear - zfar);
|
||||||
r.e31 = e30 * m.e01 + e31 * m.e11 + e32 * m.e21 + e33 * m.e31;
|
}
|
||||||
r.e02 = e00 * m.e02 + e01 * m.e12 + e02 * m.e22 + e03 * m.e32;
|
|
||||||
r.e12 = e10 * m.e02 + e11 * m.e12 + e12 * m.e22 + e13 * m.e32;
|
|
||||||
r.e22 = e20 * m.e02 + e21 * m.e12 + e22 * m.e22 + e23 * m.e32;
|
|
||||||
r.e32 = e30 * m.e02 + e31 * m.e12 + e32 * m.e22 + e33 * m.e32;
|
|
||||||
r.e03 = e00 * m.e03 + e01 * m.e13 + e02 * m.e23 + e03 * m.e33;
|
|
||||||
r.e13 = e10 * m.e03 + e11 * m.e13 + e12 * m.e23 + e13 * m.e33;
|
|
||||||
r.e23 = e20 * m.e03 + e21 * m.e13 + e22 * m.e23 + e23 * m.e33;
|
|
||||||
r.e33 = e30 * m.e03 + e31 * m.e13 + e32 * m.e23 + e33 * m.e33;
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
vec3 operator * (const vec3 &v) const {
|
mat4(const vec3 &from, const vec3 &at, const vec3 &up) {
|
||||||
return vec3(
|
vec3 r, u, d;
|
||||||
e00 * v.x + e01 * v.y + e02 * v.z + e03,
|
d = (from - at).normal();
|
||||||
e10 * v.x + e11 * v.y + e12 * v.z + e13,
|
r = up.cross(d).normal();
|
||||||
e20 * v.x + e21 * v.y + e22 * v.z + e23);
|
u = d.cross(r);
|
||||||
}
|
|
||||||
|
|
||||||
vec4 operator * (const vec4 &v) const {
|
this->right = vec4(r, 0.0f);
|
||||||
return vec4(
|
this->up = vec4(u, 0.0f);
|
||||||
e00 * v.x + e01 * v.y + e02 * v.z + e03 * v.w,
|
this->dir = vec4(d, 0.0f);
|
||||||
e10 * v.x + e11 * v.y + e12 * v.z + e13 * v.w,
|
this->offset = vec4(from, 1.0f);
|
||||||
e20 * v.x + e21 * v.y + e22 * v.z + e23 * v.w,
|
}
|
||||||
e30 * v.x + e31 * v.y + e32 * v.z + e33 * v.w);
|
|
||||||
}
|
|
||||||
|
|
||||||
void translate(const vec3 &offset) {
|
void identity() {
|
||||||
mat4 m;
|
e10 = e20 = e30 = e01 = e21 = e31 = e02 = e12 = e32 = e03 = e13 = e23 = 0.0f;
|
||||||
m.identity();
|
e00 = e11 = e22 = e33 = 1.0f;
|
||||||
m.setPos(offset);
|
}
|
||||||
*this = *this * m;
|
|
||||||
};
|
|
||||||
|
|
||||||
void scale(const vec3 &factor) {
|
mat4 operator * (const mat4 &m) const {
|
||||||
mat4 m;
|
mat4 r;
|
||||||
m.identity();
|
r.e00 = e00 * m.e00 + e01 * m.e10 + e02 * m.e20 + e03 * m.e30;
|
||||||
m.e00 = factor.x;
|
r.e10 = e10 * m.e00 + e11 * m.e10 + e12 * m.e20 + e13 * m.e30;
|
||||||
m.e11 = factor.y;
|
r.e20 = e20 * m.e00 + e21 * m.e10 + e22 * m.e20 + e23 * m.e30;
|
||||||
m.e22 = factor.z;
|
r.e30 = e30 * m.e00 + e31 * m.e10 + e32 * m.e20 + e33 * m.e30;
|
||||||
*this = *this * m;
|
r.e01 = e00 * m.e01 + e01 * m.e11 + e02 * m.e21 + e03 * m.e31;
|
||||||
}
|
r.e11 = e10 * m.e01 + e11 * m.e11 + e12 * m.e21 + e13 * m.e31;
|
||||||
|
r.e21 = e20 * m.e01 + e21 * m.e11 + e22 * m.e21 + e23 * m.e31;
|
||||||
|
r.e31 = e30 * m.e01 + e31 * m.e11 + e32 * m.e21 + e33 * m.e31;
|
||||||
|
r.e02 = e00 * m.e02 + e01 * m.e12 + e02 * m.e22 + e03 * m.e32;
|
||||||
|
r.e12 = e10 * m.e02 + e11 * m.e12 + e12 * m.e22 + e13 * m.e32;
|
||||||
|
r.e22 = e20 * m.e02 + e21 * m.e12 + e22 * m.e22 + e23 * m.e32;
|
||||||
|
r.e32 = e30 * m.e02 + e31 * m.e12 + e32 * m.e22 + e33 * m.e32;
|
||||||
|
r.e03 = e00 * m.e03 + e01 * m.e13 + e02 * m.e23 + e03 * m.e33;
|
||||||
|
r.e13 = e10 * m.e03 + e11 * m.e13 + e12 * m.e23 + e13 * m.e33;
|
||||||
|
r.e23 = e20 * m.e03 + e21 * m.e13 + e22 * m.e23 + e23 * m.e33;
|
||||||
|
r.e33 = e30 * m.e03 + e31 * m.e13 + e32 * m.e23 + e33 * m.e33;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
void rotateX(float angle) {
|
vec3 operator * (const vec3 &v) const {
|
||||||
mat4 m;
|
return vec3(
|
||||||
m.identity();
|
e00 * v.x + e01 * v.y + e02 * v.z + e03,
|
||||||
float s = sinf(angle), c = cosf(angle);
|
e10 * v.x + e11 * v.y + e12 * v.z + e13,
|
||||||
m.e11 = c; m.e21 = s;
|
e20 * v.x + e21 * v.y + e22 * v.z + e23);
|
||||||
m.e12 = -s; m.e22 = c;
|
}
|
||||||
*this = *this * m;
|
|
||||||
}
|
|
||||||
|
|
||||||
void rotateY(float angle) {
|
vec4 operator * (const vec4 &v) const {
|
||||||
mat4 m;
|
return vec4(
|
||||||
m.identity();
|
e00 * v.x + e01 * v.y + e02 * v.z + e03 * v.w,
|
||||||
float s = sinf(angle), c = cosf(angle);
|
e10 * v.x + e11 * v.y + e12 * v.z + e13 * v.w,
|
||||||
m.e00 = c; m.e20 = -s;
|
e20 * v.x + e21 * v.y + e22 * v.z + e23 * v.w,
|
||||||
m.e02 = s; m.e22 = c;
|
e30 * v.x + e31 * v.y + e32 * v.z + e33 * v.w);
|
||||||
*this = *this * m;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void rotateZ(float angle) {
|
void translate(const vec3 &offset) {
|
||||||
mat4 m;
|
mat4 m;
|
||||||
m.identity();
|
m.identity();
|
||||||
float s = sinf(angle), c = cosf(angle);
|
m.setPos(offset);
|
||||||
m.e00 = c; m.e01 = -s;
|
*this = *this * m;
|
||||||
m.e10 = s; m.e11 = c;
|
};
|
||||||
*this = *this * m;
|
|
||||||
}
|
|
||||||
|
|
||||||
float det() const {
|
void scale(const vec3 &factor) {
|
||||||
return e00 * (e11 * (e22 * e33 - e32 * e23) - e21 * (e12 * e33 - e32 * e13) + e31 * (e12 * e23 - e22 * e13)) -
|
mat4 m;
|
||||||
e10 * (e01 * (e22 * e33 - e32 * e23) - e21 * (e02 * e33 - e32 * e03) + e31 * (e02 * e23 - e22 * e03)) +
|
m.identity();
|
||||||
e20 * (e01 * (e12 * e33 - e32 * e13) - e11 * (e02 * e33 - e32 * e03) + e31 * (e02 * e13 - e12 * e03)) -
|
m.e00 = factor.x;
|
||||||
e30 * (e01 * (e12 * e23 - e22 * e13) - e11 * (e02 * e23 - e22 * e03) + e21 * (e02 * e13 - e12 * e03));
|
m.e11 = factor.y;
|
||||||
}
|
m.e22 = factor.z;
|
||||||
|
*this = *this * m;
|
||||||
|
}
|
||||||
|
|
||||||
mat4 inverse() const {
|
void rotateX(float angle) {
|
||||||
float idet = 1.0f / det();
|
mat4 m;
|
||||||
mat4 r;
|
m.identity();
|
||||||
r.e00 = (e11 * (e22 * e33 - e32 * e23) - e21 * (e12 * e33 - e32 * e13) + e31 * (e12 * e23 - e22 * e13)) * idet;
|
float s = sinf(angle), c = cosf(angle);
|
||||||
r.e01 = -(e01 * (e22 * e33 - e32 * e23) - e21 * (e02 * e33 - e32 * e03) + e31 * (e02 * e23 - e22 * e03)) * idet;
|
m.e11 = c; m.e21 = s;
|
||||||
r.e02 = (e01 * (e12 * e33 - e32 * e13) - e11 * (e02 * e33 - e32 * e03) + e31 * (e02 * e13 - e12 * e03)) * idet;
|
m.e12 = -s; m.e22 = c;
|
||||||
r.e03 = -(e01 * (e12 * e23 - e22 * e13) - e11 * (e02 * e23 - e22 * e03) + e21 * (e02 * e13 - e12 * e03)) * idet;
|
*this = *this * m;
|
||||||
r.e10 = -(e10 * (e22 * e33 - e32 * e23) - e20 * (e12 * e33 - e32 * e13) + e30 * (e12 * e23 - e22 * e13)) * idet;
|
}
|
||||||
r.e11 = (e00 * (e22 * e33 - e32 * e23) - e20 * (e02 * e33 - e32 * e03) + e30 * (e02 * e23 - e22 * e03)) * idet;
|
|
||||||
r.e12 = -(e00 * (e12 * e33 - e32 * e13) - e10 * (e02 * e33 - e32 * e03) + e30 * (e02 * e13 - e12 * e03)) * idet;
|
|
||||||
r.e13 = (e00 * (e12 * e23 - e22 * e13) - e10 * (e02 * e23 - e22 * e03) + e20 * (e02 * e13 - e12 * e03)) * idet;
|
|
||||||
r.e20 = (e10 * (e21 * e33 - e31 * e23) - e20 * (e11 * e33 - e31 * e13) + e30 * (e11 * e23 - e21 * e13)) * idet;
|
|
||||||
r.e21 = -(e00 * (e21 * e33 - e31 * e23) - e20 * (e01 * e33 - e31 * e03) + e30 * (e01 * e23 - e21 * e03)) * idet;
|
|
||||||
r.e22 = (e00 * (e11 * e33 - e31 * e13) - e10 * (e01 * e33 - e31 * e03) + e30 * (e01 * e13 - e11 * e03)) * idet;
|
|
||||||
r.e23 = -(e00 * (e11 * e23 - e21 * e13) - e10 * (e01 * e23 - e21 * e03) + e20 * (e01 * e13 - e11 * e03)) * idet;
|
|
||||||
r.e30 = -(e10 * (e21 * e32 - e31 * e22) - e20 * (e11 * e32 - e31 * e12) + e30 * (e11 * e22 - e21 * e12)) * idet;
|
|
||||||
r.e31 = (e00 * (e21 * e32 - e31 * e22) - e20 * (e01 * e32 - e31 * e02) + e30 * (e01 * e22 - e21 * e02)) * idet;
|
|
||||||
r.e32 = -(e00 * (e11 * e32 - e31 * e12) - e10 * (e01 * e32 - e31 * e02) + e30 * (e01 * e12 - e11 * e02)) * idet;
|
|
||||||
r.e33 = (e00 * (e11 * e22 - e21 * e12) - e10 * (e01 * e22 - e21 * e02) + e20 * (e01 * e12 - e11 * e02)) * idet;
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
|
|
||||||
mat4 transpose() const {
|
void rotateY(float angle) {
|
||||||
mat4 r;
|
mat4 m;
|
||||||
r.e00 = e00; r.e10 = e01; r.e20 = e02; r.e30 = e03;
|
m.identity();
|
||||||
r.e01 = e10; r.e11 = e11; r.e21 = e12; r.e31 = e13;
|
float s = sinf(angle), c = cosf(angle);
|
||||||
r.e02 = e20; r.e12 = e21; r.e22 = e22; r.e32 = e23;
|
m.e00 = c; m.e20 = -s;
|
||||||
r.e03 = e30; r.e13 = e31; r.e23 = e32; r.e33 = e33;
|
m.e02 = s; m.e22 = c;
|
||||||
return r;
|
*this = *this * m;
|
||||||
}
|
}
|
||||||
|
|
||||||
quat getRot() const {
|
void rotateZ(float angle) {
|
||||||
float t, s;
|
mat4 m;
|
||||||
t = 1.0f + e00 + e11 + e22;
|
m.identity();
|
||||||
if (t > EPS) {
|
float s = sinf(angle), c = cosf(angle);
|
||||||
s = 0.5f / sqrtf(t);
|
m.e00 = c; m.e01 = -s;
|
||||||
return quat((e21 - e12) * s, (e02 - e20) * s, (e10 - e01) * s, 0.25f / s);
|
m.e10 = s; m.e11 = c;
|
||||||
} else
|
*this = *this * m;
|
||||||
if (e00 > e11 && e00 > e22) {
|
}
|
||||||
s = 0.5f / sqrtf(1.0f + e00 - e11 - e22);
|
|
||||||
return quat(0.25f / s, (e01 + e10) * s, (e02 + e20) * s, (e21 - e12) * s);
|
|
||||||
} else
|
|
||||||
if (e11 > e22) {
|
|
||||||
s = 0.5f / sqrtf(1.0f - e00 + e11 - e22);
|
|
||||||
return quat((e01 + e10) * s, 0.25f / s, (e12 + e21) * s, (e02 - e20) * s);
|
|
||||||
} else {
|
|
||||||
s = 0.5f / sqrtf(1.0f - e00 - e11 + e22);
|
|
||||||
return quat((e02 + e20) * s, (e12 + e21) * s, 0.25f / s, (e10 - e01) * s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void setRot(const quat &rot) {
|
float det() const {
|
||||||
float sx = rot.x * rot.x,
|
return e00 * (e11 * (e22 * e33 - e32 * e23) - e21 * (e12 * e33 - e32 * e13) + e31 * (e12 * e23 - e22 * e13)) -
|
||||||
sy = rot.y * rot.y,
|
e10 * (e01 * (e22 * e33 - e32 * e23) - e21 * (e02 * e33 - e32 * e03) + e31 * (e02 * e23 - e22 * e03)) +
|
||||||
sz = rot.z * rot.z,
|
e20 * (e01 * (e12 * e33 - e32 * e13) - e11 * (e02 * e33 - e32 * e03) + e31 * (e02 * e13 - e12 * e03)) -
|
||||||
sw = rot.w * rot.w,
|
e30 * (e01 * (e12 * e23 - e22 * e13) - e11 * (e02 * e23 - e22 * e03) + e21 * (e02 * e13 - e12 * e03));
|
||||||
inv = 1.0f / (sx + sy + sz + sw);
|
}
|
||||||
|
|
||||||
e00 = ( sx - sy - sz + sw) * inv;
|
mat4 inverse() const {
|
||||||
e11 = (-sx + sy - sz + sw) * inv;
|
float idet = 1.0f / det();
|
||||||
e22 = (-sx - sy + sz + sw) * inv;
|
mat4 r;
|
||||||
inv *= 2.0f;
|
r.e00 = (e11 * (e22 * e33 - e32 * e23) - e21 * (e12 * e33 - e32 * e13) + e31 * (e12 * e23 - e22 * e13)) * idet;
|
||||||
|
r.e01 = -(e01 * (e22 * e33 - e32 * e23) - e21 * (e02 * e33 - e32 * e03) + e31 * (e02 * e23 - e22 * e03)) * idet;
|
||||||
|
r.e02 = (e01 * (e12 * e33 - e32 * e13) - e11 * (e02 * e33 - e32 * e03) + e31 * (e02 * e13 - e12 * e03)) * idet;
|
||||||
|
r.e03 = -(e01 * (e12 * e23 - e22 * e13) - e11 * (e02 * e23 - e22 * e03) + e21 * (e02 * e13 - e12 * e03)) * idet;
|
||||||
|
r.e10 = -(e10 * (e22 * e33 - e32 * e23) - e20 * (e12 * e33 - e32 * e13) + e30 * (e12 * e23 - e22 * e13)) * idet;
|
||||||
|
r.e11 = (e00 * (e22 * e33 - e32 * e23) - e20 * (e02 * e33 - e32 * e03) + e30 * (e02 * e23 - e22 * e03)) * idet;
|
||||||
|
r.e12 = -(e00 * (e12 * e33 - e32 * e13) - e10 * (e02 * e33 - e32 * e03) + e30 * (e02 * e13 - e12 * e03)) * idet;
|
||||||
|
r.e13 = (e00 * (e12 * e23 - e22 * e13) - e10 * (e02 * e23 - e22 * e03) + e20 * (e02 * e13 - e12 * e03)) * idet;
|
||||||
|
r.e20 = (e10 * (e21 * e33 - e31 * e23) - e20 * (e11 * e33 - e31 * e13) + e30 * (e11 * e23 - e21 * e13)) * idet;
|
||||||
|
r.e21 = -(e00 * (e21 * e33 - e31 * e23) - e20 * (e01 * e33 - e31 * e03) + e30 * (e01 * e23 - e21 * e03)) * idet;
|
||||||
|
r.e22 = (e00 * (e11 * e33 - e31 * e13) - e10 * (e01 * e33 - e31 * e03) + e30 * (e01 * e13 - e11 * e03)) * idet;
|
||||||
|
r.e23 = -(e00 * (e11 * e23 - e21 * e13) - e10 * (e01 * e23 - e21 * e03) + e20 * (e01 * e13 - e11 * e03)) * idet;
|
||||||
|
r.e30 = -(e10 * (e21 * e32 - e31 * e22) - e20 * (e11 * e32 - e31 * e12) + e30 * (e11 * e22 - e21 * e12)) * idet;
|
||||||
|
r.e31 = (e00 * (e21 * e32 - e31 * e22) - e20 * (e01 * e32 - e31 * e02) + e30 * (e01 * e22 - e21 * e02)) * idet;
|
||||||
|
r.e32 = -(e00 * (e11 * e32 - e31 * e12) - e10 * (e01 * e32 - e31 * e02) + e30 * (e01 * e12 - e11 * e02)) * idet;
|
||||||
|
r.e33 = (e00 * (e11 * e22 - e21 * e12) - e10 * (e01 * e22 - e21 * e02) + e20 * (e01 * e12 - e11 * e02)) * idet;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
float t1 = rot.x * rot.y;
|
mat4 transpose() const {
|
||||||
float t2 = rot.z * rot.w;
|
mat4 r;
|
||||||
e10 = (t1 + t2) * inv;
|
r.e00 = e00; r.e10 = e01; r.e20 = e02; r.e30 = e03;
|
||||||
e01 = (t1 - t2) * inv;
|
r.e01 = e10; r.e11 = e11; r.e21 = e12; r.e31 = e13;
|
||||||
|
r.e02 = e20; r.e12 = e21; r.e22 = e22; r.e32 = e23;
|
||||||
|
r.e03 = e30; r.e13 = e31; r.e23 = e32; r.e33 = e33;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
t1 = rot.x * rot.z;
|
quat getRot() const {
|
||||||
t2 = rot.y * rot.w;
|
float t, s;
|
||||||
e20 = (t1 - t2) * inv;
|
t = 1.0f + e00 + e11 + e22;
|
||||||
e02 = (t1 + t2) * inv;
|
if (t > EPS) {
|
||||||
|
s = 0.5f / sqrtf(t);
|
||||||
|
return quat((e21 - e12) * s, (e02 - e20) * s, (e10 - e01) * s, 0.25f / s);
|
||||||
|
} else
|
||||||
|
if (e00 > e11 && e00 > e22) {
|
||||||
|
s = 0.5f / sqrtf(1.0f + e00 - e11 - e22);
|
||||||
|
return quat(0.25f / s, (e01 + e10) * s, (e02 + e20) * s, (e21 - e12) * s);
|
||||||
|
} else
|
||||||
|
if (e11 > e22) {
|
||||||
|
s = 0.5f / sqrtf(1.0f - e00 + e11 - e22);
|
||||||
|
return quat((e01 + e10) * s, 0.25f / s, (e12 + e21) * s, (e02 - e20) * s);
|
||||||
|
} else {
|
||||||
|
s = 0.5f / sqrtf(1.0f - e00 - e11 + e22);
|
||||||
|
return quat((e02 + e20) * s, (e12 + e21) * s, 0.25f / s, (e10 - e01) * s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
t1 = rot.y * rot.z;
|
void setRot(const quat &rot) {
|
||||||
t2 = rot.x * rot.w;
|
float sx = rot.x * rot.x,
|
||||||
e21 = (t1 + t2) * inv;
|
sy = rot.y * rot.y,
|
||||||
e12 = (t1 - t2) * inv;
|
sz = rot.z * rot.z,
|
||||||
}
|
sw = rot.w * rot.w,
|
||||||
|
inv = 1.0f / (sx + sy + sz + sw);
|
||||||
|
|
||||||
vec3 getPos() const {
|
e00 = ( sx - sy - sz + sw) * inv;
|
||||||
return offset.xyz;
|
e11 = (-sx + sy - sz + sw) * inv;
|
||||||
}
|
e22 = (-sx - sy + sz + sw) * inv;
|
||||||
|
inv *= 2.0f;
|
||||||
|
|
||||||
void setPos(const vec3 &pos) {
|
float t1 = rot.x * rot.y;
|
||||||
offset.xyz = pos;
|
float t2 = rot.z * rot.w;
|
||||||
}
|
e10 = (t1 + t2) * inv;
|
||||||
|
e01 = (t1 - t2) * inv;
|
||||||
|
|
||||||
|
t1 = rot.x * rot.z;
|
||||||
|
t2 = rot.y * rot.w;
|
||||||
|
e20 = (t1 - t2) * inv;
|
||||||
|
e02 = (t1 + t2) * inv;
|
||||||
|
|
||||||
|
t1 = rot.y * rot.z;
|
||||||
|
t2 = rot.x * rot.w;
|
||||||
|
e21 = (t1 + t2) * inv;
|
||||||
|
e12 = (t1 - t2) * inv;
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 getPos() const {
|
||||||
|
return offset.xyz;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setPos(const vec3 &pos) {
|
||||||
|
offset.xyz = pos;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Stream {
|
struct Stream {
|
||||||
FILE *f;
|
FILE *f;
|
||||||
int size, pos;
|
int size, pos;
|
||||||
|
|
||||||
Stream(const char *name) : pos(0) {
|
Stream(const char *name) : pos(0) {
|
||||||
f = fopen(name, "rb");
|
f = fopen(name, "rb");
|
||||||
if (!f) LOG("error loading file\n");
|
if (!f) LOG("error loading file\n");
|
||||||
fseek(f, 0, SEEK_END);
|
fseek(f, 0, SEEK_END);
|
||||||
size = ftell(f);
|
size = ftell(f);
|
||||||
fseek(f, 0, SEEK_SET);
|
fseek(f, 0, SEEK_SET);
|
||||||
}
|
}
|
||||||
|
|
||||||
~Stream() {
|
~Stream() {
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setPos(int pos) {
|
void setPos(int pos) {
|
||||||
this->pos = pos;
|
this->pos = pos;
|
||||||
fseek(f, pos, SEEK_SET);
|
fseek(f, pos, SEEK_SET);
|
||||||
}
|
}
|
||||||
|
|
||||||
void seek(int offset) {
|
void seek(int offset) {
|
||||||
fseek(f, offset, SEEK_CUR);
|
fseek(f, offset, SEEK_CUR);
|
||||||
pos += offset;
|
pos += offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
int raw(void *data, int size) {
|
int raw(void *data, int size) {
|
||||||
pos += size;
|
pos += size;
|
||||||
return fread(data, 1, size, f);
|
return fread(data, 1, size, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T& read(T &x) {
|
T& read(T &x) {
|
||||||
raw(&x, sizeof(x));
|
raw(&x, sizeof(x));
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T* read(T *&a, int count) {
|
T* read(T *&a, int count) {
|
||||||
if (count) {
|
if (count) {
|
||||||
a = new T[count];
|
a = new T[count];
|
||||||
raw(a, count * sizeof(T));
|
raw(a, count * sizeof(T));
|
||||||
} else
|
} else
|
||||||
a = NULL;
|
a = NULL;
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
Reference in New Issue
Block a user