gpuray_glsl
diff vmath/vector.cc @ 0:f234630e38ff
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 09 Nov 2014 13:03:36 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/vmath/vector.cc Sun Nov 09 13:03:36 2014 +0200 1.3 @@ -0,0 +1,313 @@ 1.4 +#include "vector.h" 1.5 +#include "vmath.h" 1.6 + 1.7 +// ---------- Vector2 ----------- 1.8 + 1.9 +Vector2::Vector2(scalar_t x, scalar_t y) 1.10 +{ 1.11 + this->x = x; 1.12 + this->y = y; 1.13 +} 1.14 + 1.15 +Vector2::Vector2(const vec2_t &vec) 1.16 +{ 1.17 + x = vec.x; 1.18 + y = vec.y; 1.19 +} 1.20 + 1.21 +Vector2::Vector2(const Vector3 &vec) 1.22 +{ 1.23 + x = vec.x; 1.24 + y = vec.y; 1.25 +} 1.26 + 1.27 +Vector2::Vector2(const Vector4 &vec) 1.28 +{ 1.29 + x = vec.x; 1.30 + y = vec.y; 1.31 +} 1.32 + 1.33 +void Vector2::normalize() 1.34 +{ 1.35 + scalar_t len = length(); 1.36 + x /= len; 1.37 + y /= len; 1.38 +} 1.39 + 1.40 +Vector2 Vector2::normalized() const 1.41 +{ 1.42 + scalar_t len = length(); 1.43 + return Vector2(x / len, y / len); 1.44 +} 1.45 + 1.46 +void Vector2::transform(const Matrix3x3 &mat) 1.47 +{ 1.48 + scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2]; 1.49 + y = mat[1][0] * x + mat[1][1] * y + mat[1][2]; 1.50 + x = nx; 1.51 +} 1.52 + 1.53 +Vector2 Vector2::transformed(const Matrix3x3 &mat) const 1.54 +{ 1.55 + Vector2 vec; 1.56 + vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2]; 1.57 + vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2]; 1.58 + return vec; 1.59 +} 1.60 + 1.61 +void Vector2::rotate(scalar_t angle) 1.62 +{ 1.63 + *this = Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y); 1.64 +} 1.65 + 1.66 +Vector2 Vector2::rotated(scalar_t angle) const 1.67 +{ 1.68 + return Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y); 1.69 +} 1.70 + 1.71 +Vector2 Vector2::reflection(const Vector2 &normal) const 1.72 +{ 1.73 + return 2.0 * dot_product(*this, normal) * normal - *this; 1.74 +} 1.75 + 1.76 +Vector2 Vector2::refraction(const Vector2 &normal, scalar_t src_ior, scalar_t dst_ior) const 1.77 +{ 1.78 + // quick and dirty implementation :) 1.79 + Vector3 v3refr = Vector3(this->x, this->y, 1.0).refraction(Vector3(this->x, this->y, 1), src_ior, dst_ior); 1.80 + return Vector2(v3refr.x, v3refr.y); 1.81 +} 1.82 + 1.83 +std::ostream &operator <<(std::ostream &out, const Vector2 &vec) 1.84 +{ 1.85 + out << "[" << vec.x << " " << vec.y << "]"; 1.86 + return out; 1.87 +} 1.88 + 1.89 + 1.90 + 1.91 +// --------- Vector3 ---------- 1.92 + 1.93 +Vector3::Vector3(scalar_t x, scalar_t y, scalar_t z) 1.94 +{ 1.95 + this->x = x; 1.96 + this->y = y; 1.97 + this->z = z; 1.98 +} 1.99 + 1.100 +Vector3::Vector3(const vec3_t &vec) 1.101 +{ 1.102 + x = vec.x; 1.103 + y = vec.y; 1.104 + z = vec.z; 1.105 +} 1.106 + 1.107 +Vector3::Vector3(const Vector2 &vec) 1.108 +{ 1.109 + x = vec.x; 1.110 + y = vec.y; 1.111 + z = 1; 1.112 +} 1.113 + 1.114 +Vector3::Vector3(const Vector4 &vec) 1.115 +{ 1.116 + x = vec.x; 1.117 + y = vec.y; 1.118 + z = vec.z; 1.119 +} 1.120 + 1.121 +void Vector3::normalize() 1.122 +{ 1.123 + scalar_t len = length(); 1.124 + x /= len; 1.125 + y /= len; 1.126 + z /= len; 1.127 +} 1.128 + 1.129 +Vector3 Vector3::normalized() const 1.130 +{ 1.131 + scalar_t len = length(); 1.132 + return Vector3(x / len, y / len, z / len); 1.133 +} 1.134 + 1.135 +Vector3 Vector3::reflection(const Vector3 &normal) const 1.136 +{ 1.137 + return 2.0 * dot_product(*this, normal) * normal - *this; 1.138 +} 1.139 + 1.140 +Vector3 Vector3::refraction(const Vector3 &normal, scalar_t src_ior, scalar_t dst_ior) const 1.141 +{ 1.142 + return refraction(normal, src_ior / dst_ior); 1.143 +} 1.144 + 1.145 +Vector3 Vector3::refraction(const Vector3 &normal, scalar_t ior) const 1.146 +{ 1.147 + scalar_t cos_inc = dot_product(*this, -normal); 1.148 + 1.149 + scalar_t radical = 1.0 + SQ(ior) * (SQ(cos_inc) - 1.0); 1.150 + 1.151 + if(radical < 0.0) { // total internal reflection 1.152 + return -reflection(normal); 1.153 + } 1.154 + 1.155 + scalar_t beta = ior * cos_inc - sqrt(radical); 1.156 + 1.157 + return *this * ior + normal * beta; 1.158 +} 1.159 + 1.160 +void Vector3::transform(const Matrix3x3 &mat) 1.161 +{ 1.162 + scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z; 1.163 + scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z; 1.164 + z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z; 1.165 + x = nx; 1.166 + y = ny; 1.167 +} 1.168 + 1.169 +Vector3 Vector3::transformed(const Matrix3x3 &mat) const 1.170 +{ 1.171 + Vector3 vec; 1.172 + vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z; 1.173 + vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z; 1.174 + vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z; 1.175 + return vec; 1.176 +} 1.177 + 1.178 +void Vector3::transform(const Matrix4x4 &mat) 1.179 +{ 1.180 + scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3]; 1.181 + scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3]; 1.182 + z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3]; 1.183 + x = nx; 1.184 + y = ny; 1.185 +} 1.186 + 1.187 +Vector3 Vector3::transformed(const Matrix4x4 &mat) const 1.188 +{ 1.189 + Vector3 vec; 1.190 + vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3]; 1.191 + vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3]; 1.192 + vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3]; 1.193 + return vec; 1.194 +} 1.195 + 1.196 +void Vector3::transform(const Quaternion &quat) 1.197 +{ 1.198 + Quaternion vq(0.0f, *this); 1.199 + vq = quat * vq * quat.inverse(); 1.200 + *this = vq.v; 1.201 +} 1.202 + 1.203 +Vector3 Vector3::transformed(const Quaternion &quat) const 1.204 +{ 1.205 + Quaternion vq(0.0f, *this); 1.206 + vq = quat * vq * quat.inverse(); 1.207 + return vq.v; 1.208 +} 1.209 + 1.210 +void Vector3::rotate(const Vector3 &euler) 1.211 +{ 1.212 + Matrix4x4 rot; 1.213 + rot.set_rotation(euler); 1.214 + transform(rot); 1.215 +} 1.216 + 1.217 +Vector3 Vector3::rotated(const Vector3 &euler) const 1.218 +{ 1.219 + Matrix4x4 rot; 1.220 + rot.set_rotation(euler); 1.221 + return transformed(rot); 1.222 +} 1.223 + 1.224 +std::ostream &operator <<(std::ostream &out, const Vector3 &vec) 1.225 +{ 1.226 + out << "[" << vec.x << " " << vec.y << " " << vec.z << "]"; 1.227 + return out; 1.228 +} 1.229 + 1.230 + 1.231 +// -------------- Vector4 -------------- 1.232 +Vector4::Vector4(scalar_t x, scalar_t y, scalar_t z, scalar_t w) 1.233 +{ 1.234 + this->x = x; 1.235 + this->y = y; 1.236 + this->z = z; 1.237 + this->w = w; 1.238 +} 1.239 + 1.240 +Vector4::Vector4(const vec4_t &vec) 1.241 +{ 1.242 + x = vec.x; 1.243 + y = vec.y; 1.244 + z = vec.z; 1.245 + w = vec.w; 1.246 +} 1.247 + 1.248 +Vector4::Vector4(const Vector2 &vec) 1.249 +{ 1.250 + x = vec.x; 1.251 + y = vec.y; 1.252 + z = 1; 1.253 + w = 1; 1.254 +} 1.255 + 1.256 +Vector4::Vector4(const Vector3 &vec) 1.257 +{ 1.258 + x = vec.x; 1.259 + y = vec.y; 1.260 + z = vec.z; 1.261 + w = 1; 1.262 +} 1.263 + 1.264 +void Vector4::normalize() 1.265 +{ 1.266 + scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w); 1.267 + x /= len; 1.268 + y /= len; 1.269 + z /= len; 1.270 + w /= len; 1.271 +} 1.272 + 1.273 +Vector4 Vector4::normalized() const 1.274 +{ 1.275 + scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w); 1.276 + return Vector4(x / len, y / len, z / len, w / len); 1.277 +} 1.278 + 1.279 +void Vector4::transform(const Matrix4x4 &mat) 1.280 +{ 1.281 + scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w; 1.282 + scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w; 1.283 + scalar_t nz = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w; 1.284 + w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w; 1.285 + x = nx; 1.286 + y = ny; 1.287 + z = nz; 1.288 +} 1.289 + 1.290 +Vector4 Vector4::transformed(const Matrix4x4 &mat) const 1.291 +{ 1.292 + Vector4 vec; 1.293 + vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w; 1.294 + vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w; 1.295 + vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w; 1.296 + vec.w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w; 1.297 + return vec; 1.298 +} 1.299 + 1.300 +// TODO: implement 4D vector reflection 1.301 +Vector4 Vector4::reflection(const Vector4 &normal) const 1.302 +{ 1.303 + return *this; 1.304 +} 1.305 + 1.306 +// TODO: implement 4D vector refraction 1.307 +Vector4 Vector4::refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const 1.308 +{ 1.309 + return *this; 1.310 +} 1.311 + 1.312 +std::ostream &operator <<(std::ostream &out, const Vector4 &vec) 1.313 +{ 1.314 + out << "[" << vec.x << " " << vec.y << " " << vec.z << " " << vec.w << "]"; 1.315 + return out; 1.316 +}