3dphotoshoot

diff libs/vmath/vector.cc @ 10:c71c477521ca

converting to GLES2 and C++
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 31 May 2015 00:40:26 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/vmath/vector.cc	Sun May 31 00:40:26 2015 +0300
     1.3 @@ -0,0 +1,348 @@
     1.4 +/*
     1.5 +libvmath - a vector math library
     1.6 +Copyright (C) 2004-2015 John Tsiombikas <nuclear@member.fsf.org>
     1.7 +
     1.8 +This program is free software: you can redistribute it and/or modify
     1.9 +it under the terms of the GNU Lesser General Public License as published
    1.10 +by the Free Software Foundation, either version 3 of the License, or
    1.11 +(at your option) any later version.
    1.12 +
    1.13 +This program is distributed in the hope that it will be useful,
    1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +GNU Lesser General Public License for more details.
    1.17 +
    1.18 +You should have received a copy of the GNU Lesser General Public License
    1.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 +*/
    1.21 +#include "vector.h"
    1.22 +#include "vmath.h"
    1.23 +
    1.24 +// ---------- Vector2 -----------
    1.25 +
    1.26 +Vector2::Vector2(scalar_t x, scalar_t y)
    1.27 +{
    1.28 +	this->x = x;
    1.29 +	this->y = y;
    1.30 +}
    1.31 +
    1.32 +Vector2::Vector2(const vec2_t &vec)
    1.33 +{
    1.34 +	x = vec.x;
    1.35 +	y = vec.y;
    1.36 +}
    1.37 +
    1.38 +Vector2::Vector2(const Vector3 &vec)
    1.39 +{
    1.40 +	x = vec.x;
    1.41 +	y = vec.y;
    1.42 +}
    1.43 +
    1.44 +Vector2::Vector2(const Vector4 &vec)
    1.45 +{
    1.46 +	x = vec.x;
    1.47 +	y = vec.y;
    1.48 +}
    1.49 +
    1.50 +void Vector2::normalize()
    1.51 +{
    1.52 +	scalar_t len = length();
    1.53 +	x /= len;
    1.54 +	y /= len;
    1.55 +}
    1.56 +
    1.57 +Vector2 Vector2::normalized() const
    1.58 +{
    1.59 +	scalar_t len = length();
    1.60 +	return Vector2(x / len, y / len);
    1.61 +}
    1.62 +
    1.63 +void Vector2::transform(const Matrix3x3 &mat)
    1.64 +{
    1.65 +	scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2];
    1.66 +	y = mat[1][0] * x + mat[1][1] * y + mat[1][2];
    1.67 +	x = nx;
    1.68 +}
    1.69 +
    1.70 +Vector2 Vector2::transformed(const Matrix3x3 &mat) const
    1.71 +{
    1.72 +	Vector2 vec;
    1.73 +	vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2];
    1.74 +	vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2];
    1.75 +	return vec;
    1.76 +}
    1.77 +
    1.78 +void Vector2::rotate(scalar_t angle)
    1.79 +{
    1.80 +	*this = Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y);
    1.81 +}
    1.82 +
    1.83 +Vector2 Vector2::rotated(scalar_t angle) const
    1.84 +{
    1.85 +	return Vector2(cos(angle) * x - sin(angle) * y, sin(angle) * x + cos(angle) * y);
    1.86 +}
    1.87 +
    1.88 +Vector2 Vector2::reflection(const Vector2 &normal) const
    1.89 +{
    1.90 +	return 2.0 * dot_product(*this, normal) * normal - *this;
    1.91 +}
    1.92 +
    1.93 +Vector2 Vector2::refraction(const Vector2 &normal, scalar_t src_ior, scalar_t dst_ior) const
    1.94 +{
    1.95 +	// quick and dirty implementation :)
    1.96 +	Vector3 v3refr = Vector3(this->x, this->y, 1.0).refraction(Vector3(this->x, this->y, 1), src_ior, dst_ior);
    1.97 +	return Vector2(v3refr.x, v3refr.y);
    1.98 +}
    1.99 +
   1.100 +/*
   1.101 +std::ostream &operator <<(std::ostream &out, const Vector2 &vec)
   1.102 +{
   1.103 +	out << "[" << vec.x << " " << vec.y << "]";
   1.104 +	return out;
   1.105 +}
   1.106 +*/
   1.107 +
   1.108 +
   1.109 +// --------- Vector3 ----------
   1.110 +
   1.111 +Vector3::Vector3(scalar_t x, scalar_t y, scalar_t z)
   1.112 +{
   1.113 +	this->x = x;
   1.114 +	this->y = y;
   1.115 +	this->z = z;
   1.116 +}
   1.117 +
   1.118 +Vector3::Vector3(const vec3_t &vec)
   1.119 +{
   1.120 +	x = vec.x;
   1.121 +	y = vec.y;
   1.122 +	z = vec.z;
   1.123 +}
   1.124 +
   1.125 +Vector3::Vector3(const Vector2 &vec)
   1.126 +{
   1.127 +	x = vec.x;
   1.128 +	y = vec.y;
   1.129 +	z = 1;
   1.130 +}
   1.131 +
   1.132 +Vector3::Vector3(const Vector4 &vec)
   1.133 +{
   1.134 +	x = vec.x;
   1.135 +	y = vec.y;
   1.136 +	z = vec.z;
   1.137 +}
   1.138 +
   1.139 +Vector3::Vector3(const SphVector &sph)
   1.140 +{
   1.141 +	*this = sph;
   1.142 +}
   1.143 +
   1.144 +Vector3 &Vector3::operator =(const SphVector &sph)
   1.145 +{
   1.146 +	x = sph.r * cos(sph.theta) * sin(sph.phi);
   1.147 +	z = sph.r * sin(sph.theta) * sin(sph.phi);
   1.148 +	y = sph.r * cos(sph.phi);
   1.149 +	return *this;
   1.150 +}
   1.151 +
   1.152 +void Vector3::normalize()
   1.153 +{
   1.154 +	scalar_t len = length();
   1.155 +	x /= len;
   1.156 +	y /= len;
   1.157 +	z /= len;
   1.158 +}
   1.159 +
   1.160 +Vector3 Vector3::normalized() const
   1.161 +{
   1.162 +	scalar_t len = length();
   1.163 +	return Vector3(x / len, y / len, z / len);
   1.164 +}
   1.165 +
   1.166 +Vector3 Vector3::reflection(const Vector3 &normal) const
   1.167 +{
   1.168 +	return 2.0 * dot_product(*this, normal) * normal - *this;
   1.169 +}
   1.170 +
   1.171 +Vector3 Vector3::refraction(const Vector3 &normal, scalar_t src_ior, scalar_t dst_ior) const
   1.172 +{
   1.173 +	return refraction(normal, src_ior / dst_ior);
   1.174 +}
   1.175 +
   1.176 +Vector3 Vector3::refraction(const Vector3 &normal, scalar_t ior) const
   1.177 +{
   1.178 +	scalar_t cos_inc = dot_product(*this, -normal);
   1.179 +
   1.180 +	scalar_t radical = 1.0 + SQ(ior) * (SQ(cos_inc) - 1.0);
   1.181 +
   1.182 +	if(radical < 0.0) {		// total internal reflection
   1.183 +		return -reflection(normal);
   1.184 +	}
   1.185 +
   1.186 +	scalar_t beta = ior * cos_inc - sqrt(radical);
   1.187 +
   1.188 +	return *this * ior + normal * beta;
   1.189 +}
   1.190 +
   1.191 +void Vector3::transform(const Matrix3x3 &mat)
   1.192 +{
   1.193 +	scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z;
   1.194 +	scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z;
   1.195 +	z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z;
   1.196 +	x = nx;
   1.197 +	y = ny;
   1.198 +}
   1.199 +
   1.200 +Vector3 Vector3::transformed(const Matrix3x3 &mat) const
   1.201 +{
   1.202 +	Vector3 vec;
   1.203 +	vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z;
   1.204 +	vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z;
   1.205 +	vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z;
   1.206 +	return vec;
   1.207 +}
   1.208 +
   1.209 +void Vector3::transform(const Matrix4x4 &mat)
   1.210 +{
   1.211 +	scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
   1.212 +	scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
   1.213 +	z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
   1.214 +	x = nx;
   1.215 +	y = ny;
   1.216 +}
   1.217 +
   1.218 +Vector3 Vector3::transformed(const Matrix4x4 &mat) const
   1.219 +{
   1.220 +	Vector3 vec;
   1.221 +	vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3];
   1.222 +	vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3];
   1.223 +	vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3];
   1.224 +	return vec;
   1.225 +}
   1.226 +
   1.227 +void Vector3::transform(const Quaternion &quat)
   1.228 +{
   1.229 +	Quaternion vq(0.0f, *this);
   1.230 +	vq = quat * vq * quat.inverse();
   1.231 +	*this = vq.v;
   1.232 +}
   1.233 +
   1.234 +Vector3 Vector3::transformed(const Quaternion &quat) const
   1.235 +{
   1.236 +	Quaternion vq(0.0f, *this);
   1.237 +	vq = quat * vq * quat.inverse();
   1.238 +	return vq.v;
   1.239 +}
   1.240 +
   1.241 +void Vector3::rotate(const Vector3 &euler)
   1.242 +{
   1.243 +	Matrix4x4 rot;
   1.244 +	rot.set_rotation(euler);
   1.245 +	transform(rot);
   1.246 +}
   1.247 +
   1.248 +Vector3 Vector3::rotated(const Vector3 &euler) const
   1.249 +{
   1.250 +	Matrix4x4 rot;
   1.251 +	rot.set_rotation(euler);
   1.252 +	return transformed(rot);
   1.253 +}
   1.254 +
   1.255 +/*
   1.256 +std::ostream &operator <<(std::ostream &out, const Vector3 &vec)
   1.257 +{
   1.258 +	out << "[" << vec.x << " " << vec.y << " " << vec.z << "]";
   1.259 +	return out;
   1.260 +}
   1.261 +*/
   1.262 +
   1.263 +
   1.264 +// -------------- Vector4 --------------
   1.265 +Vector4::Vector4(scalar_t x, scalar_t y, scalar_t z, scalar_t w)
   1.266 +{
   1.267 +	this->x = x;
   1.268 +	this->y = y;
   1.269 +	this->z = z;
   1.270 +	this->w = w;
   1.271 +}
   1.272 +
   1.273 +Vector4::Vector4(const vec4_t &vec)
   1.274 +{
   1.275 +	x = vec.x;
   1.276 +	y = vec.y;
   1.277 +	z = vec.z;
   1.278 +	w = vec.w;
   1.279 +}
   1.280 +
   1.281 +Vector4::Vector4(const Vector2 &vec)
   1.282 +{
   1.283 +	x = vec.x;
   1.284 +	y = vec.y;
   1.285 +	z = 1;
   1.286 +	w = 1;
   1.287 +}
   1.288 +
   1.289 +Vector4::Vector4(const Vector3 &vec)
   1.290 +{
   1.291 +	x = vec.x;
   1.292 +	y = vec.y;
   1.293 +	z = vec.z;
   1.294 +	w = 1;
   1.295 +}
   1.296 +
   1.297 +void Vector4::normalize()
   1.298 +{
   1.299 +	scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
   1.300 +	x /= len;
   1.301 +	y /= len;
   1.302 +	z /= len;
   1.303 +	w /= len;
   1.304 +}
   1.305 +
   1.306 +Vector4 Vector4::normalized() const
   1.307 +{
   1.308 +	scalar_t len = (scalar_t)sqrt(x*x + y*y + z*z + w*w);
   1.309 +	return Vector4(x / len, y / len, z / len, w / len);
   1.310 +}
   1.311 +
   1.312 +void Vector4::transform(const Matrix4x4 &mat)
   1.313 +{
   1.314 +	scalar_t nx = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w;
   1.315 +	scalar_t ny = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w;
   1.316 +	scalar_t nz = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w;
   1.317 +	w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w;
   1.318 +	x = nx;
   1.319 +	y = ny;
   1.320 +	z = nz;
   1.321 +}
   1.322 +
   1.323 +Vector4 Vector4::transformed(const Matrix4x4 &mat) const
   1.324 +{
   1.325 +	Vector4 vec;
   1.326 +	vec.x = mat[0][0] * x + mat[0][1] * y + mat[0][2] * z + mat[0][3] * w;
   1.327 +	vec.y = mat[1][0] * x + mat[1][1] * y + mat[1][2] * z + mat[1][3] * w;
   1.328 +	vec.z = mat[2][0] * x + mat[2][1] * y + mat[2][2] * z + mat[2][3] * w;
   1.329 +	vec.w = mat[3][0] * x + mat[3][1] * y + mat[3][2] * z + mat[3][3] * w;
   1.330 +	return vec;
   1.331 +}
   1.332 +
   1.333 +// TODO: implement 4D vector reflection
   1.334 +Vector4 Vector4::reflection(const Vector4 &normal) const
   1.335 +{
   1.336 +	return *this;
   1.337 +}
   1.338 +
   1.339 +// TODO: implement 4D vector refraction
   1.340 +Vector4 Vector4::refraction(const Vector4 &normal, scalar_t src_ior, scalar_t dst_ior) const
   1.341 +{
   1.342 +	return *this;
   1.343 +}
   1.344 +
   1.345 +/*
   1.346 +std::ostream &operator <<(std::ostream &out, const Vector4 &vec)
   1.347 +{
   1.348 +	out << "[" << vec.x << " " << vec.y << " " << vec.z << " " << vec.w << "]";
   1.349 +	return out;
   1.350 +}
   1.351 +*/