blobt
5 years ago
21 changed files with 772 additions and 1971 deletions
-
2CMakeLists.txt
-
2src/6threed/Image.h
-
555src/6threed/Raster.cc
-
434src/6threed/Raster.h
-
159src/6threed/struct/common.h
-
175src/6threed/struct/plane.h
-
87src/6threed/struct/tfrustum.h
-
174src/6threed/struct/tmat3x3.h
-
387src/6threed/struct/tmat4x4.h
-
57src/6threed/struct/tvec2.h
-
172src/6threed/struct/tvec3.h
-
183src/6threed/struct/tvec4.h
-
77src/6threed/threed.cc
-
7src/7camera/CMakeLists.txt
-
0src/7camera/Image.cc
-
0src/7camera/Image.h
-
0src/7camera/Raster.cc
-
0src/7camera/Raster.h
-
73src/7camera/camera.cc
-
0src/7camera/common.h
-
7src/7threed/CMakeLists.txt
@ -1,159 +0,0 @@ |
|||
/* |
|||
* To change this license header, choose License Headers in Project Properties. |
|||
* To change this template file, choose Tools | Templates |
|||
* and open the template in the editor. |
|||
*/ |
|||
|
|||
/* |
|||
* File: common.h |
|||
* Author: Blobt |
|||
* |
|||
* Created on October 5, 2019, 4:55 PM |
|||
*/ |
|||
|
|||
#ifndef COMMON_H |
|||
#define COMMON_H |
|||
|
|||
#include <assert.h> |
|||
#include <cstddef> |
|||
#include <stdlib.h> |
|||
#include <cmath> |
|||
#include "Rgba.h" |
|||
#include "struct/tvec2.h" |
|||
#include "struct/tvec3.h" |
|||
#include "struct/tmat3x3.h" |
|||
#include "struct/tvec4.h" |
|||
#include "struct/tmat4x4.h" |
|||
#include "struct/tfrustum.h" |
|||
|
|||
#define die(m) do { perror(m); exit(EXIT_FAILURE); } while(0) |
|||
#define def2rad(theta) (0.01745329251994329 * (theta)) //每个角度所对应的弧度 |
|||
|
|||
typedef tvec2<float> float2; |
|||
typedef tvec2<int> int2; |
|||
typedef tvec3<float> float3; |
|||
typedef tvec4<float> float4; |
|||
typedef unsigned char byte; |
|||
typedef tmat3x3<float> matrix3; |
|||
typedef tmat4x4<float> matrix4; |
|||
typedef tfrustum<float> Frustum; |
|||
|
|||
template<class T> inline T tmin(T a, T b) { |
|||
return a < b ? a : b; |
|||
} |
|||
|
|||
template<class T> inline T tmax(T a, T b) { |
|||
return a > b ? a : b; |
|||
} |
|||
|
|||
inline Rgba colorLerp(const Rgba& color1, const Rgba& color2, float step) { |
|||
Rgba ret; |
|||
|
|||
ret._r = (unsigned char) color1._r + step * (color2._r - color1._r); |
|||
ret._g = (unsigned char) color1._g + step * (color2._g - color1._g); |
|||
ret._b = (unsigned char) color1._b + step * (color2._b - color1._b); |
|||
ret._a = (unsigned char) color1._a + step * (color2._a - color1._a); |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
inline float2 uvLerp(const float2& uv1, const float2& uv2, float step) { |
|||
|
|||
if (step < 0 || step > 1) { |
|||
printf("step : %f\n", step); |
|||
die("step must more than zero and less than 1"); |
|||
} |
|||
|
|||
float2 ret; |
|||
|
|||
ret.x = (float) uv1.x + (uv2.x - uv1.x) * step; |
|||
ret.y = (float) uv1.y + (uv2.y - uv1.y) * step; |
|||
|
|||
return ret; |
|||
} |
|||
|
|||
template <typename T> |
|||
tvec3<T> cross(tvec3<T> const & x, tvec3<T> const & y) { |
|||
return tvec3<T> |
|||
( |
|||
x.y * y.z - y.y * x.z, |
|||
x.z * y.x - y.z * x.x, |
|||
x.x * y.y - y.x * x.y |
|||
); |
|||
} |
|||
|
|||
template <typename T> |
|||
typename tvec3<T>::value_type dot(tvec3<T> const & x, tvec3<T> const & y) { |
|||
return x.x * y.x + x.y * y.y + x.z * y.z; |
|||
} |
|||
|
|||
template <typename T> |
|||
T inversesqrt(T x) { |
|||
return T(1) / sqrt(x); |
|||
} |
|||
|
|||
template <typename T> |
|||
tvec2<T> normalize(tvec2<T> const & x) { |
|||
typename tvec2<T>::value_type sqr = x.x * x.x + x.y * x.y; |
|||
return x * inversesqrt(sqr); |
|||
} |
|||
|
|||
template <typename T> |
|||
tvec3<T> normalize(tvec3<T> const & x) { |
|||
typename tvec3<T>::value_type sqr = x.x * x.x + x.y * x.y + x.z * x.z; |
|||
return x * inversesqrt(sqr); |
|||
} |
|||
|
|||
template <typename T> |
|||
tvec4<T> normalize(tvec4<T> const & x) { |
|||
typename tvec4<T>::value_type sqr = x.x * x.x + x.y * x.y + x.z * x.z + x.w * x.w; |
|||
return x * inversesqrt(sqr); |
|||
} |
|||
|
|||
template <typename valType> |
|||
tmat4x4<valType> perspective(valType fovy, valType aspect, valType zNear, valType zFar) { |
|||
valType range = tan(fovy * valType(DEG2RAD(0.5))) * zNear; |
|||
valType left = -range * aspect; |
|||
valType right = range * aspect; |
|||
valType bottom = -range; |
|||
valType top = range; |
|||
|
|||
tmat4x4<valType> res(valType(0)); |
|||
res[0][0] = (valType(2) * zNear) / (right - left); |
|||
res[1][1] = (valType(2) * zNear) / (top - bottom); |
|||
res[2][2] = -(zFar + zNear) / (zFar - zNear); |
|||
res[2][3] = -valType(1); |
|||
res[3][2] = -(valType(2) * zFar * zNear) / (zFar - zNear); |
|||
return res; |
|||
} |
|||
|
|||
template <typename T> |
|||
tmat4x4<T> lookAt |
|||
( |
|||
tvec3<T> const & eye, |
|||
tvec3<T> const & center, |
|||
tvec3<T> const & up |
|||
) { |
|||
tvec3<T> f = normalize(center - eye); |
|||
tvec3<T> u = normalize(up); |
|||
tvec3<T> s = normalize(cross(f, u)); |
|||
u = cross(s, f); |
|||
|
|||
tmat4x4<T> res(1); |
|||
res[0][0] = s.x; |
|||
res[1][0] = s.y; |
|||
res[2][0] = s.z; |
|||
res[0][1] = u.x; |
|||
res[1][1] = u.y; |
|||
res[2][1] = u.z; |
|||
res[0][2] = -f.x; |
|||
res[1][2] = -f.y; |
|||
res[2][2] = -f.z; |
|||
res[3][0] = -dot(s, eye); |
|||
res[3][1] = -dot(u, eye); |
|||
res[3][2] = dot(f, eye); |
|||
return res; |
|||
} |
|||
|
|||
#endif /* COMMON_H */ |
|||
|
@ -1,175 +0,0 @@ |
|||
/* |
|||
* To change this license header, choose License Headers in Project Properties. |
|||
* To change this template file, choose Tools | Templates |
|||
* and open the template in the editor. |
|||
*/ |
|||
|
|||
/* |
|||
* File: plane.h |
|||
* Author: Blobt |
|||
* |
|||
* Created on February 23, 2020, 11:33 AM |
|||
*/ |
|||
|
|||
#ifndef PLANE_H |
|||
#define PLANE_H |
|||
#include "../common.h" |
|||
|
|||
template<class T> |
|||
class Plane { |
|||
public: |
|||
tvec3<T> _normal; |
|||
T _distance; |
|||
public: |
|||
|
|||
Plane() { |
|||
_normal = tvec3<T>(0, 0, 0); |
|||
_distance = 0.0f; |
|||
} |
|||
|
|||
Plane(const Plane& right) { |
|||
_normal = right._normal; |
|||
_distance = right._distance; |
|||
} |
|||
|
|||
/** Construct a plane through a normal, and a distance to move the plane along the normal.*/ |
|||
Plane(const tvec3<T>& rkNormal, T fConstant) { |
|||
_normal = rkNormal; |
|||
_distance = -fConstant; |
|||
} |
|||
|
|||
/** Construct a plane using the 4 constants directly **/ |
|||
Plane(T x, T y, T z, T o) { |
|||
_normal = tvec3<T>(x, y, z); |
|||
T invLen = 1.0f / (_normal).length(); |
|||
_normal *= invLen; |
|||
_distance = o * invLen; |
|||
} |
|||
|
|||
Plane(const tvec3<T>& rkNormal, const tvec3<T>& rkPoint) { |
|||
redefine(rkNormal, rkPoint); |
|||
} |
|||
|
|||
Plane(const tvec3<T>& rkPoint0, const tvec3<T>& rkPoint1, const tvec3<T>& rkPoint2) { |
|||
redefine(rkPoint0, rkPoint1, rkPoint2); |
|||
} |
|||
|
|||
/** |
|||
* ����ľ��� |
|||
*/ |
|||
float distance(const tvec3<T> &pos) const { |
|||
return dot(_normal, pos) + _distance; |
|||
} |
|||
|
|||
/** The "positive side" of the plane is the half space to which the |
|||
plane normal points. The "negative side" is the other half |
|||
space. The flag "no side" indicates the plane itself. |
|||
*/ |
|||
enum Side { |
|||
NO_SIDE, |
|||
POSITIVE_SIDE, |
|||
NEGATIVE_SIDE, |
|||
BOTH_SIDE |
|||
}; |
|||
|
|||
Side getSide(const tvec3<T>& rkPoint) const { |
|||
float fDistance = getDistance(rkPoint); |
|||
|
|||
if (fDistance < 0.0) |
|||
return Plane::NEGATIVE_SIDE; |
|||
|
|||
if (fDistance > 0.0) |
|||
return Plane::POSITIVE_SIDE; |
|||
|
|||
return Plane::NO_SIDE; |
|||
} |
|||
|
|||
Side getSide(const tvec3<T>& centre, const tvec3<T>& halfSize) const { |
|||
// Calculate the distance between box centre and the plane |
|||
float dist = getDistance(centre); |
|||
|
|||
// Calculate the maximise allows absolute distance for |
|||
// the distance between box centre and plane |
|||
float maxAbsDist = _normal.absDot(halfSize); |
|||
|
|||
if (dist < -maxAbsDist) |
|||
return Plane::NEGATIVE_SIDE; |
|||
|
|||
if (dist > +maxAbsDist) |
|||
return Plane::POSITIVE_SIDE; |
|||
|
|||
return Plane::BOTH_SIDE; |
|||
} |
|||
|
|||
float getDistance(const tvec3<T>& rkPoint) const { |
|||
return _normal.dot(rkPoint) + _distance; |
|||
} |
|||
|
|||
void redefine(const tvec3<T>& rkPoint0, const tvec3<T>& rkPoint1, |
|||
const tvec3<T>& rkPoint2) { |
|||
tvec3<T> kEdge1 = rkPoint1 - rkPoint0; |
|||
tvec3<T> kEdge2 = rkPoint2 - rkPoint0; |
|||
_normal = cross(kEdge1, kEdge2); |
|||
_normal.normalise(); |
|||
_distance = -dot(_normal, rkPoint0); |
|||
} |
|||
|
|||
/** Redefine this plane based on a normal and a point. */ |
|||
void redefine(const tvec3<T>& rkNormal, const tvec3<T>& rkPoint) { |
|||
_normal = rkNormal; |
|||
_distance = -dot(rkNormal, rkPoint); |
|||
} |
|||
|
|||
|
|||
// tvec3<T> projectVector(const tvec3<T>& p) const |
|||
// { |
|||
// matrix3 xform; |
|||
// xform[0][0] = 1.0f - _normal.x * _normal.x; |
|||
// xform[0][1] = -_normal.x * _normal.y; |
|||
// xform[0][2] = -_normal.x * _normal.z; |
|||
// xform[1][0] = -_normal.y * _normal.x; |
|||
// xform[1][1] = 1.0f - _normal.y * _normal.y; |
|||
// xform[1][2] = -_normal.y * _normal.z; |
|||
// xform[2][0] = -_normal.z * _normal.x; |
|||
// xform[2][1] = -_normal.z * _normal.y; |
|||
// xform[2][2] = 1.0f - _normal.z * _normal.z; |
|||
// return xform * p; |
|||
// } |
|||
|
|||
/** Normalises the plane. |
|||
@remarks |
|||
This method normalises the plane's normal and the length scale of d |
|||
is as well. |
|||
@note |
|||
This function will not crash for zero-sized vectors, but there |
|||
will be no changes made to their components. |
|||
@returns The previous length of the plane's normal. |
|||
*/ |
|||
float normalise(void) { |
|||
float fLength = _normal.length(); |
|||
|
|||
// Will also work for zero-sized vectors, but will change nothing |
|||
if (fLength > 1e-08f) { |
|||
float fInvLength = 1.0f / fLength; |
|||
_normal *= fInvLength; |
|||
_distance *= fInvLength; |
|||
} |
|||
|
|||
return fLength; |
|||
} |
|||
|
|||
|
|||
|
|||
/// Comparison operator |
|||
|
|||
bool operator==(const Plane& right) const { |
|||
return (right._distance == _distance && right._normal == _normal); |
|||
} |
|||
|
|||
bool operator!=(const Plane& right) const { |
|||
return (right._distance != _distance && right._normal != _normal); |
|||
} |
|||
}; |
|||
|
|||
#endif /* PLANE_H */ |
|||
|
@ -1,87 +0,0 @@ |
|||
/* |
|||
* To change this license header, choose License Headers in Project Properties. |
|||
* To change this template file, choose Tools | Templates |
|||
* and open the template in the editor. |
|||
*/ |
|||
|
|||
/* |
|||
* File: tfrustum.h |
|||
* Author: Blobt |
|||
* |
|||
* Created on February 23, 2020, 11:22 AM |
|||
*/ |
|||
|
|||
#ifndef TFRUSTUM_H |
|||
#define TFRUSTUM_H |
|||
#include "../common.h" |
|||
#include "plane.h" |
|||
|
|||
template<class T> |
|||
class tfrustum { |
|||
public: |
|||
|
|||
enum { |
|||
FRUSTUM_LEFT = 0, |
|||
FRUSTUM_RIGHT = 1, |
|||
FRUSTUM_TOP = 2, |
|||
FRUSTUM_BOTTOM = 3, |
|||
FRUSTUM_FAR = 4, |
|||
FRUSTUM_NEAR = 5, |
|||
}; |
|||
public: |
|||
|
|||
/** |
|||
* project * view |
|||
*/ |
|||
void loadFrustum(const tmat4x4<T> &mvp) { |
|||
const T* dataPtr = mvp.data(); |
|||
_planes[FRUSTUM_LEFT ] = Plane<T>(dataPtr[12] - dataPtr[0], dataPtr[13] - dataPtr[1], dataPtr[14] - dataPtr[2], dataPtr[15] - dataPtr[3]); |
|||
_planes[FRUSTUM_RIGHT ] = Plane<T>(dataPtr[12] + dataPtr[0], dataPtr[13] + dataPtr[1], dataPtr[14] + dataPtr[2], dataPtr[15] + dataPtr[3]); |
|||
|
|||
_planes[FRUSTUM_TOP ] = Plane<T>(dataPtr[12] - dataPtr[4], dataPtr[13] - dataPtr[5], dataPtr[14] - dataPtr[6], dataPtr[15] - dataPtr[7]); |
|||
_planes[FRUSTUM_BOTTOM] = Plane<T>(dataPtr[12] + dataPtr[4], dataPtr[13] + dataPtr[5], dataPtr[14] + dataPtr[6], dataPtr[15] + dataPtr[7]); |
|||
|
|||
_planes[FRUSTUM_FAR ] = Plane<T>(dataPtr[12] - dataPtr[8], dataPtr[13] - dataPtr[9], dataPtr[14] - dataPtr[10], dataPtr[15] - dataPtr[11]); |
|||
_planes[FRUSTUM_NEAR ] = Plane<T>(dataPtr[12] + dataPtr[8], dataPtr[13] + dataPtr[9], dataPtr[14] + dataPtr[10], dataPtr[15] + dataPtr[11]); |
|||
} |
|||
|
|||
bool pointInFrustum(const tvec3<T> &pos) const { |
|||
for (int i = 0; i < 6; i++) { |
|||
if (_planes[i].distance(pos) <= 0) |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
bool sphereInFrustum(const tvec3<T> &pos, const float radius) const { |
|||
for (int i = 0; i < 6; i++) { |
|||
if (_planes[i].distance(pos) <= -radius) |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
bool cubeInFrustum(T minX, T maxX, T minY, T maxY, T minZ, T maxZ) const { |
|||
for (int i = 0; i < 6; i++) { |
|||
if (_planes[i].distance(tvec3<T>(minX, minY, minZ)) > 0) continue; |
|||
if (_planes[i].distance(tvec3<T>(minX, minY, maxZ)) > 0) continue; |
|||
if (_planes[i].distance(tvec3<T>(minX, maxY, minZ)) > 0) continue; |
|||
if (_planes[i].distance(tvec3<T>(minX, maxY, maxZ)) > 0) continue; |
|||
if (_planes[i].distance(tvec3<T>(maxX, minY, minZ)) > 0) continue; |
|||
if (_planes[i].distance(tvec3<T>(maxX, minY, maxZ)) > 0) continue; |
|||
if (_planes[i].distance(tvec3<T>(maxX, maxY, minZ)) > 0) continue; |
|||
if (_planes[i].distance(tvec3<T>(maxX, maxY, maxZ)) > 0) continue; |
|||
return false; |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
const Plane<T> &getPlane(const int plane) const { |
|||
return _planes[plane]; |
|||
} |
|||
protected: |
|||
Plane<T> _planes[6]; |
|||
}; |
|||
|
|||
#endif /* TFRUSTUM_H */ |
|||
|
@ -1,174 +0,0 @@ |
|||
/* |
|||
* To change this license header, choose License Headers in Project Properties. |
|||
* To change this template file, choose Tools | Templates |
|||
* and open the template in the editor. |
|||
*/ |
|||
|
|||
/* |
|||
* File: tmat3x3.h |
|||
* Author: Blobt |
|||
* |
|||
* Created on February 19, 2020, 2:45 PM |
|||
*/ |
|||
|
|||
#ifndef TMAT3X3_H |
|||
#define TMAT3X3_H |
|||
|
|||
#include "../common.h" |
|||
#define def2rad(theta) (0.01745329251994329 * (theta)) |
|||
|
|||
template <typename T> |
|||
struct tmat3x3 { |
|||
typedef T value_type; |
|||
typedef std::size_t size_type; |
|||
typedef tvec3<T> col_type; |
|||
typedef tvec3<T> row_type; |
|||
typedef tmat3x3<T> type; |
|||
typedef tmat3x3<T> transpose_type; |
|||
|
|||
private: |
|||
col_type value[3]; |
|||
public: |
|||
|
|||
size_type length() const { |
|||
return 3; |
|||
} |
|||
|
|||
size_type col_size() { |
|||
return 3; |
|||
} |
|||
|
|||
size_type row_size() { |
|||
return 3; |
|||
} |
|||
|
|||
tmat3x3() { |
|||
value_type const zero(0); |
|||
value_type const one(1); |
|||
value[0] = col_type(one, zero, zero); |
|||
value[1] = col_type(zero, one, zero); |
|||
value[2] = col_type(zero, zero, one); |
|||
} |
|||
|
|||
tmat3x3(tmat3x3<T> const &m) { |
|||
value[0] = m.value[0]; |
|||
value[1] = m.value[1]; |
|||
value[2] = m.value[2]; |
|||
} |
|||
|
|||
tmat3x3(value_type const &s) { |
|||
value_type const zero(0); |
|||
value[0] = col_type(s, zero, zero); |
|||
value[1] = col_type(zero, s, zero); |
|||
value[2] = col_type(zero, zero, s); |
|||
} |
|||
|
|||
|
|||
tmat3x3( |
|||
value_type const &x0, value_type const &y0, value_type const &z0, |
|||
value_type const &x1, value_type const &y1, value_type const &z1, |
|||
value_type const &x2, value_type const &y2, value_type const &z2 |
|||
) { |
|||
value[0] = col_type(x0, y0, z0); |
|||
value[1] = col_type(x1, y1, z1); |
|||
value[2] = col_type(x2, y2, z2); |
|||
} |
|||
|
|||
tmat3x3(col_type const &v0, col_type const &v1, col_type const &v2) { |
|||
value[0] = v0; |
|||
value[1] = v1; |
|||
value[2] = v2; |
|||
} |
|||
|
|||
col_type& operator[](size_type i) { |
|||
assert(i < this->length()); |
|||
return value[i]; |
|||
} |
|||
|
|||
col_type const & operator[](size_type i) const { |
|||
assert(i < this->length()); |
|||
return this->value[i]; |
|||
} |
|||
|
|||
type &operator=(type & s) { |
|||
value[0] = s[0]; |
|||
value[1] = s[1]; |
|||
value[2] = s[2]; |
|||
return *this; |
|||
} |
|||
|
|||
type const &operator=(type const & s) { |
|||
value[0] = s[0]; |
|||
value[1] = s[1]; |
|||
value[2] = s[2]; |
|||
return *this; |
|||
} |
|||
|
|||
tvec3<T> operator*(const tvec3<T> &s) const { |
|||
tvec3<T> ret( |
|||
value[0][0] * s[0] + value[1][0] * s[1] + value[2][0] * s[2], |
|||
value[0][1] * s[0] + value[1][1] * s[1] + value[2][1] * s[2], |
|||
value[0][2] * s[0] + value[1][2] * s[1] + value[2][2] * s[2] |
|||
); |
|||
return ret; |
|||
} |
|||
|
|||
type operator*(const type &s) { |
|||
|
|||
T const srcA00 = value[0][0]; |
|||
T const srcA01 = value[0][1]; |
|||
T const srcA02 = value[0][2]; |
|||
T const srcA10 = value[1][0]; |
|||
T const srcA11 = value[1][1]; |
|||
T const srcA12 = value[1][2]; |
|||
T const srcA20 = value[2][0]; |
|||
T const srcA21 = value[2][1]; |
|||
T const srcA22 = value[2][2]; |
|||
|
|||
T const srcB00 = s[0][0]; |
|||
T const srcB01 = s[0][1]; |
|||
T const srcB02 = s[0][2]; |
|||
T const srcB10 = s[1][0]; |
|||
T const srcB11 = s[1][1]; |
|||
T const srcB12 = s[1][2]; |
|||
T const srcB20 = s[2][0]; |
|||
T const srcB21 = s[2][1]; |
|||
T const srcB22 = s[2][2]; |
|||
|
|||
tmat3x3<T> res; |
|||
res[0][0] = srcA00 * srcB00 + srcA10 * srcB01 + srcA20 * srcB02; |
|||
res[0][1] = srcA01 * srcB00 + srcA11 * srcB01 + srcA21 * srcB02; |
|||
res[0][2] = srcA02 * srcB00 + srcA12 * srcB01 + srcA22 * srcB02; |
|||
res[1][0] = srcA00 * srcB10 + srcA10 * srcB11 + srcA20 * srcB12; |
|||
res[1][1] = srcA01 * srcB10 + srcA11 * srcB11 + srcA21 * srcB12; |
|||
res[1][2] = srcA02 * srcB10 + srcA12 * srcB11 + srcA22 * srcB12; |
|||
res[2][0] = srcA00 * srcB20 + srcA10 * srcB21 + srcA20 * srcB22; |
|||
res[2][1] = srcA01 * srcB20 + srcA11 * srcB21 + srcA21 * srcB22; |
|||
res[2][2] = srcA02 * srcB20 + srcA12 * srcB21 + srcA22 * srcB22; |
|||
return res; |
|||
} |
|||
|
|||
void translate(T x, T y) { |
|||
value[0] = col_type(value_type(1), value_type(0), value_type(0)); |
|||
value[1] = col_type(value_type(0), value_type(1), value_type(0)); |
|||
value[2] = col_type(value_type(x), value_type(y), value_type(1)); |
|||
} |
|||
|
|||
void scale(T x, T y) { |
|||
value[0] = col_type(value_type(x), value_type(0), value_type(0)); |
|||
value[1] = col_type(value_type(0), value_type(y), value_type(0)); |
|||
value[2] = col_type(value_type(0), value_type(0), value_type(1)); |
|||
} |
|||
|
|||
void rotate(T angle) { |
|||
T rad = def2rad(angle); |
|||
T c = cos(rad); |
|||
T s = sin(rad); |
|||
value[0] = col_type(value_type(c), value_type(-s), value_type(0)); |
|||
value[1] = col_type(value_type(s), value_type(c), value_type(0)); |
|||
value[2] = col_type(value_type(0), value_type(0), value_type(1)); |
|||
} |
|||
}; |
|||
|
|||
#endif /* TMAT3X3_H */ |
|||
|
@ -1,387 +0,0 @@ |
|||
/* |
|||
* To change this license header, choose License Headers in Project Properties. |
|||
* To change this template file, choose Tools | Templates |
|||
* and open the template in the editor. |
|||
*/ |
|||
|
|||
/* |
|||
* File: tmat4x4.h |
|||
* Author: Blobt |
|||
* |
|||
* Created on February 22, 2020, 10:11 AM |
|||
*/ |
|||
|
|||
#ifndef TMAT4X4_H |
|||
#define TMAT4X4_H |
|||
|
|||
#include "../common.h" |
|||
#define DEG2RAD(theta) (0.01745329251994329 * (theta)) |
|||
|
|||
template <typename T> |
|||
struct tmat4x4 { |
|||
typedef T value_type; |
|||
typedef std::size_t size_type; |
|||
typedef tvec4<T> col_type; |
|||
typedef tvec4<T> row_type; |
|||
typedef tmat4x4<T> type; |
|||
typedef tmat4x4<T> transpose_type; |
|||
|
|||
public: |
|||
|
|||
size_type length() const { |
|||
return 4; |
|||
} |
|||
|
|||
size_type col_size() { |
|||
return 4; |
|||
} |
|||
|
|||
size_type row_size() { |
|||
return 4; |
|||
} |
|||
|
|||
void identify() { |
|||
this->value[0] = col_type(1, 0, 0, 0); |
|||
this->value[1] = col_type(0, 1, 0, 0); |
|||
this->value[2] = col_type(0, 0, 1, 0); |
|||
this->value[3] = col_type(0, 0, 0, 1); |
|||
} |
|||
|
|||
col_type & operator[](size_type i) { |
|||
assert(i < this->length()); |
|||
return this->value[i]; |
|||
} |
|||
|
|||
col_type const & operator[](size_type i) const { |
|||
assert(i < this->length()); |
|||
return this->value[i]; |
|||
} |
|||
|
|||
tmat4x4() { |
|||
} |
|||
|
|||
tmat4x4(tmat4x4<T> const & m) { |
|||
this->value[0] = m.value[0]; |
|||
this->value[1] = m.value[1]; |
|||
this->value[2] = m.value[2]; |
|||
this->value[3] = m.value[3]; |
|||
} |
|||
|
|||
tmat4x4(value_type s) { |
|||
value_type const Zero(0); |
|||
this->value[0] = col_type(s, Zero, Zero, Zero); |
|||
this->value[1] = col_type(Zero, s, Zero, Zero); |
|||
this->value[2] = col_type(Zero, Zero, s, Zero); |
|||
this->value[3] = col_type(Zero, Zero, Zero, s); |
|||
} |
|||
|
|||
tmat4x4 |
|||
( |
|||
value_type const & x0, value_type const & y0, value_type const & z0, value_type const & w0, |
|||
value_type const & x1, value_type const & y1, value_type const & z1, value_type const & w1, |
|||
value_type const & x2, value_type const & y2, value_type const & z2, value_type const & w2, |
|||
value_type const & x3, value_type const & y3, value_type const & z3, value_type const & w3 |
|||
) { |
|||
this->value[0] = col_type(x0, y0, z0, w0); |
|||
this->value[1] = col_type(x1, y1, z1, w1); |
|||
this->value[2] = col_type(x2, y2, z2, w2); |
|||
this->value[3] = col_type(x3, y3, z3, w3); |
|||
} |
|||
|
|||
tmat4x4 |
|||
( |
|||
col_type const & v0, |
|||
col_type const & v1, |
|||
col_type const & v2, |
|||
col_type const & v3 |
|||
) { |
|||
this->value[0] = v0; |
|||
this->value[1] = v1; |
|||
this->value[2] = v2; |
|||
this->value[3] = v3; |
|||
} |
|||
|
|||
template <typename U> |
|||
tmat4x4(tmat4x4<U> const & m) { |
|||
this->value[0] = col_type(m[0]); |
|||
this->value[1] = col_type(m[1]); |
|||
this->value[2] = col_type(m[2]); |
|||
this->value[3] = col_type(m[3]); |
|||
} |
|||
|
|||
T const * data() const { |
|||
return &this->value[0][0]; |
|||
} |
|||
|
|||
tmat4x4<T>& translate(value_type x, value_type y, value_type z) { |
|||
this->value[0] = col_type(1, 0, 0, 0); |
|||
this->value[1] = col_type(0, 1, 0, 0); |
|||
this->value[2] = col_type(0, 0, 1, 0); |
|||
this->value[3] = col_type(x, y, z, 1); |
|||
return *this; |
|||
} |
|||
|
|||
template<typename U> |
|||
tmat4x4<T>& translate(U x, U y, U z) { |
|||
this->value[0] = col_type(1, 0, 0, 0); |
|||
this->value[1] = col_type(0, 1, 0, 0); |
|||
this->value[2] = col_type(0, 0, 1, 0); |
|||
this->value[3] = col_type(T(x), T(y), T(z), 1); |
|||
return *this; |
|||
} |
|||
|
|||
tmat4x4<T>& translate(tvec3<T> const& pos) { |
|||
this->value[0] = col_type(1, 0, 0, 0); |
|||
this->value[1] = col_type(0, 1, 0, 0); |
|||
this->value[2] = col_type(0, 0, 1, 0); |
|||
this->value[3] = col_type(pos.x, pos.y, pos.z, 1); |
|||
return *this; |
|||
} |
|||
|
|||
template<typename U> |
|||
tmat4x4<T>& translate(tvec3<U> const& pos) { |
|||
this->value[0] = col_type(1, 0, 0, 0); |
|||
this->value[1] = col_type(0, 1, 0, 0); |
|||
this->value[2] = col_type(0, 0, 1, 0); |
|||
this->value[3] = col_type(T(pos.x), T(pos.y), T(pos.z), 1); |
|||
return *this; |
|||
} |
|||
|
|||
tmat4x4<T>& rotate(value_type angle, tvec3<T> const & v) { |
|||
T a = DEG2RAD(angle); |
|||
T c = cos(a); |
|||
T s = sin(a); |
|||
|
|||
tvec3<T> axis = normalize(v); |
|||
|
|||
tvec3<T> temp = (T(1) - c) * axis; |
|||
|
|||
tmat4x4<T> res; |
|||
this->value[0][0] = c + temp[0] * axis[0]; |
|||
this->value[0][1] = 0 + temp[0] * axis[1] + s * axis[2]; |
|||
this->value[0][2] = 0 + temp[0] * axis[2] - s * axis[1]; |
|||
this->value[0][3] = 0; |
|||
|
|||
this->value[1][0] = 0 + temp[1] * axis[0] - s * axis[2]; |
|||
this->value[1][1] = c + temp[1] * axis[1]; |
|||
this->value[1][2] = 0 + temp[1] * axis[2] + s * axis[0]; |
|||
this->value[1][3] = 0; |
|||
|
|||
this->value[2][0] = 0 + temp[2] * axis[0] + s * axis[1]; |
|||
this->value[2][1] = 0 + temp[2] * axis[1] - s * axis[0]; |
|||
this->value[2][2] = c + temp[2] * axis[2]; |
|||
this->value[2][3] = 0; |
|||
|
|||
this->value[3][0] = 0; |
|||
this->value[3][1] = 0; |
|||
this->value[3][2] = 0; |
|||
this->value[3][3] = 1; |
|||
return *this; |
|||
} |
|||
|
|||
tmat4x4<T>& rotateX(value_type angle) { |
|||
T a = DEG2RAD(angle); |
|||
T c = cos(a); |
|||
T s = sin(a); |
|||
|
|||
this->value[0] = col_type(1, 0, 0, 0); |
|||
this->value[1] = col_type(0, c, s, 0); |
|||
this->value[2] = col_type(0, -s, c, 0); |
|||
this->value[3] = col_type(0, 0, 0, 1); |
|||
|
|||
return *this; |
|||
} |
|||
|
|||
template<typename U> |
|||
tmat4x4<T>& rotateX(U angle) { |
|||
T a = DEG2RAD(angle); |
|||
T c = cos(a); |
|||
T s = sin(a); |
|||
|
|||
this->value[0] = col_type(1, 0, 0, 0); |
|||
this->value[1] = col_type(0, c, s, 0); |
|||
this->value[2] = col_type(0, -s, c, 0); |
|||
this->value[3] = col_type(0, 0, 0, 1); |
|||
|
|||
return *this; |
|||
} |
|||
|
|||
tmat4x4<T>& rotateY(value_type angle) { |
|||
T a = DEG2RAD(angle); |
|||
T c = cos(a); |
|||
T s = sin(a); |
|||
|
|||
this->value[0] = col_type(c, 0, -s, 0); |
|||
this->value[1] = col_type(0, 1, 0, 0); |
|||
this->value[2] = col_type(s, 0, c, 0); |
|||
this->value[3] = col_type(0, 0, 0, 1); |
|||
return *this; |
|||
|
|||
} |
|||
|
|||
template<typename U> |
|||
tmat4x4<T>& rotateY(U angle) { |
|||
T a = DEG2RAD(angle); |
|||
T c = cos(a); |
|||
T s = sin(a); |
|||
|
|||
this->value[0] = col_type(c, 0, -s, 0); |
|||
this->value[1] = col_type(0, 1, 0, 0); |
|||
this->value[2] = col_type(s, 0, c, 0); |
|||
this->value[3] = col_type(0, 0, 0, 1); |
|||
return *this; |
|||
|
|||
} |
|||
|
|||
tmat4x4<T>& rotateZ(value_type angle) { |
|||
T a = T(DEG2RAD(angle)); |
|||
T c = cos(a); |
|||
T s = sin(a); |
|||
|
|||
this->value[0] = col_type(c, s, 0, 0); |
|||
this->value[1] = col_type(-s, c, 0, 0); |
|||
this->value[2] = col_type(0, 0, 1, 0); |
|||
this->value[3] = col_type(0, 0, 0, 1); |
|||
return *this; |
|||
} |
|||
|
|||
template<typename U> |
|||
tmat4x4<T>& rotateZ(U angle) { |
|||
T a = DEG2RAD(angle); |
|||
|
|||
T c = cos(a); |
|||
T s = sin(a); |
|||
|
|||
this->value[0] = col_type(c, s, 0, 0); |
|||
this->value[1] = col_type(-s, c, 0, 0); |
|||
this->value[2] = col_type(0, 0, 1, 0); |
|||
this->value[3] = col_type(0, 0, 0, 1); |
|||
return *this; |
|||
} |
|||
|
|||
tmat4x4<T>& scale(tvec3<T> const& s) { |
|||
this->value[0] = col_type(s[0], 0, 0, 0); |
|||
this->value[1] = col_type(0, s[1], 0, 0); |
|||
this->value[2] = col_type(0, 0, s[2], 0); |
|||
this->value[3] = col_type(0, 0, 0, 1); |
|||
|
|||
return *this; |
|||
} |
|||
|
|||
tmat4x4<T>& scale(value_type x, value_type y, value_type z) { |
|||
this->value[0] = col_type(x, 0, 0, 0); |
|||
this->value[1] = col_type(0, y, 0, 0); |
|||
this->value[2] = col_type(0, 0, z, 0); |
|||
this->value[3] = col_type(0, 0, 0, 1); |
|||
|
|||
return *this; |
|||
} |
|||
|
|||
template<typename U> |
|||
tmat4x4<T>& scale(U x, U y, U z) { |
|||
this->value[0] = col_type(value_type(x), 0, 0, 0); |
|||
this->value[1] = col_type(0, value_type(y), 0, 0); |
|||
this->value[2] = col_type(0, 0, value_type(z), 0); |
|||
this->value[3] = col_type(0, 0, 0, 1); |
|||
|
|||
return *this; |
|||
} |
|||
|
|||
template<typename U, typename V, typename W> |
|||
tmat4x4<T>& scale(U x, V y, W z) { |
|||
this->value[0] = col_type(value_type(x), 0, 0, 0); |
|||
this->value[1] = col_type(0, value_type(y), 0, 0); |
|||
this->value[2] = col_type(0, 0, value_type(z), 0); |
|||
this->value[3] = col_type(0, 0, 0, 1); |
|||
return *this; |
|||
} |
|||
|
|||
tmat4x4<T> transpose() const { |
|||
return tmat4x4<T>( |
|||
this->value[0][0], this->value[1][0], this->value[2][0], this->value[3][0], |
|||
this->value[0][1], this->value[1][1], this->value[2][1], this->value[3][1], |
|||
this->value[0][2], this->value[1][2], this->value[2][2], this->value[3][2], |
|||
this->value[0][3], this->value[1][3], this->value[2][3], this->value[3][3] |
|||
); |
|||
} |
|||
|
|||
tmat4x4<T> extractMatrixRotation() const { |
|||
return tmat4x4<T>( |
|||
this->value[0][0], this->value[0][1], this->value[0][2], 0.0, |
|||
this->value[1][0], this->value[1][1], this->value[1][2], 0.0, |
|||
this->value[2][0], this->value[2][1], this->value[2][2], 0.0, |
|||
0.0, 0.0, 0.0, 1.0 |
|||
); |
|||
} |
|||
|
|||
private: |
|||
col_type value[4]; |
|||
}; |
|||
|
|||
template <typename T> |
|||
tvec3<T> operator*(tvec3<T> const& v, tmat4x4<T> const& mat) { |
|||
return tvec3<T> |
|||
( |
|||
v.x * mat[0][0] + v.y * mat[1][0] + v.z * mat[2][0] + 1 * mat[3][0], |
|||
v.x * mat[0][1] + v.y * mat[1][1] + v.z * mat[2][1] + 1 * mat[3][1], |
|||
v.x * mat[0][2] + v.y * mat[1][2] + v.z * mat[2][2] + 1 * mat[3][2] |
|||
); |
|||
} |
|||
|
|||
template <typename T> |
|||
tmat4x4<T> operator*(tmat4x4<T> const & m, typename tmat4x4<T>::value_type s) { |
|||
return tmat4x4<T>( |
|||
m[0] * s, |
|||
m[1] * s, |
|||
m[2] * s, |
|||
m[3] * s); |
|||
} |
|||
|
|||
template <typename T> |
|||
tmat4x4<T> operator*(typename tmat4x4<T>::value_type s, tmat4x4<T> const & m) { |
|||
return tmat4x4<T>( |
|||
m[0] * s, |
|||
m[1] * s, |
|||
m[2] * s, |
|||
m[3] * s); |
|||
} |
|||
|
|||
template <typename T> |
|||
typename tmat4x4<T>::col_type operator*(tmat4x4<T> const & m, typename tmat4x4<T>::row_type const & v) { |
|||
return typename tmat4x4<T>::col_type( |
|||
m[0][0] * v.x + m[1][0] * v.y + m[2][0] * v.z + m[3][0] * v.w, |
|||
m[0][1] * v.x + m[1][1] * v.y + m[2][1] * v.z + m[3][1] * v.w, |
|||
m[0][2] * v.x + m[1][2] * v.y + m[2][2] * v.z + m[3][2] * v.w, |
|||
m[0][3] * v.x + m[1][3] * v.y + m[2][3] * v.z + m[3][3] * v.w); |
|||
} |
|||
|
|||
template <typename T> |
|||
typename tmat4x4<T>::row_type operator*(typename tmat4x4<T>::col_type const & v, tmat4x4<T> const & m) { |
|||
return typename tmat4x4<T>::row_type( |
|||
m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w, |
|||
m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w, |
|||
m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w, |
|||
m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w); |
|||
} |
|||
|
|||
template <typename T> |
|||
tmat4x4<T> operator*(tmat4x4<T> const & m1, tmat4x4<T> const & m2) { |
|||
typename tmat4x4<T>::col_type const srcA0 = m1[0]; |
|||
typename tmat4x4<T>::col_type const srcA1 = m1[1]; |
|||
typename tmat4x4<T>::col_type const srcA2 = m1[2]; |
|||
typename tmat4x4<T>::col_type const srcA3 = m1[3]; |
|||
|
|||
typename tmat4x4<T>::col_type const srcB0 = m2[0]; |
|||
typename tmat4x4<T>::col_type const srcB1 = m2[1]; |
|||
typename tmat4x4<T>::col_type const srcB2 = m2[2]; |
|||
typename tmat4x4<T>::col_type const srcB3 = m2[3]; |
|||
|
|||
tmat4x4<T> res; |
|||
res[0] = srcA0 * srcB0[0] + srcA1 * srcB0[1] + srcA2 * srcB0[2] + srcA3 * srcB0[3]; |
|||
res[1] = srcA0 * srcB1[0] + srcA1 * srcB1[1] + srcA2 * srcB1[2] + srcA3 * srcB1[3]; |
|||
res[2] = srcA0 * srcB2[0] + srcA1 * srcB2[1] + srcA2 * srcB2[2] + srcA3 * srcB2[3]; |
|||
res[3] = srcA0 * srcB3[0] + srcA1 * srcB3[1] + srcA2 * srcB3[2] + srcA3 * srcB3[3]; |
|||
return res; |
|||
} |
|||
#endif /* TMAT4X4_H */ |
|||
|
@ -1,57 +0,0 @@ |
|||
/* |
|||
* To change this license header, choose License Headers in Project Properties. |
|||
* To change this template file, choose Tools | Templates |
|||
* and open the template in the editor. |
|||
*/ |
|||
|
|||
/* |
|||
* File: tvec2.h |
|||
* Author: Blobt |
|||
* |
|||
* Created on February 19, 2020, 9:32 AM |
|||
*/ |
|||
|
|||
#ifndef TVEC2_H |
|||
#define TVEC2_H |
|||
|
|||
template<typename T> |
|||
struct tvec2 { |
|||
typedef T value_type; |
|||
typedef std::size_t size_type; |
|||
typedef tvec2<T> type; |
|||
|
|||
value_type x; |
|||
value_type y; |
|||
|
|||
size_type length() const { |
|||
return 2; |
|||
} |
|||
|
|||
value_type operator[](size_type i) { |
|||
assert(i < this->length()); |
|||
return (&x)[i]; |
|||
} |
|||
|
|||
value_type const operator[](size_type i) const { |
|||
assert(i < this->length()); |
|||
return (&x)[i]; |
|||
} |
|||
|
|||
tvec2() { |
|||
x = 0; |
|||
y = 0; |
|||
} |
|||
|
|||
tvec2(const value_type &s) { |
|||
x = s; |
|||
y = s; |
|||
} |
|||
|
|||
tvec2(const value_type &s1, const value_type &s2) { |
|||
x = s1; |
|||
y = s2; |
|||
} |
|||
}; |
|||
|
|||
#endif /* TVEC2_H */ |
|||
|
@ -1,172 +0,0 @@ |
|||
/* |
|||
* To change this license header, choose License Headers in Project Properties. |
|||
* To change this template file, choose Tools | Templates |
|||
* and open the template in the editor. |
|||
*/ |
|||
|
|||
/* |
|||
* File: tvec3.h |
|||
* Author: Blobt |
|||
* |
|||
* Created on February 19, 2020, 9:34 AM |
|||
*/ |
|||
|
|||
#ifndef TVEC3_H |
|||
#define TVEC3_H |
|||
|
|||
#include <cstdio> |
|||
|
|||
template<typename T> |
|||
struct tvec3 { |
|||
typedef T value_type; |
|||
typedef std::size_t size_type; |
|||
typedef tvec3<T> type; |
|||
|
|||
value_type x; |
|||
value_type y; |
|||
value_type z; |
|||
|
|||
size_type length()const { |
|||
return 3; |
|||
} |
|||
|
|||
value_type &operator[](size_type i) { |
|||
assert(i < this->length()); |
|||
return (&x)[i]; |
|||
} |
|||
|
|||
value_type const &operator[](size_type i) const { |
|||
assert(i < this->length()); |
|||
return (&x)[i]; |
|||
} |
|||
|
|||
tvec3() { |
|||
x = 0; |
|||
y = 0; |
|||
z = 0; |
|||
} |
|||
|
|||
inline tvec3(tvec3<T> const & v) : |
|||
x(v.x), |
|||
y(v.y), |
|||
z(v.z) { |
|||
} |
|||
|
|||
tvec3(type &v) { |
|||
x = v.x; |
|||
y = v.y; |
|||
z = v.z; |
|||
} |
|||
|
|||
tvec3(value_type s0, value_type s1, value_type s2) { |
|||
x = s0; |
|||
y = s1; |
|||
z = s2; |
|||
} |
|||
|
|||
tvec3(value_type s) { |
|||
x = s; |
|||
y = s; |
|||
z = s; |
|||
} |
|||
|
|||
/** |
|||
* 这个template <typename U>起到用来配备其它类型做参数的情况 |
|||
* 例如tvec3<int> n(3.1f); |
|||
*/ |
|||
template <typename U> |
|||
tvec3(U s) { |
|||
x = value_type(s); |
|||
y = value_type(s); |
|||
z = value_type(s); |
|||
} |
|||
|
|||
template <typename A, typename B, typename C> |
|||
tvec3(A s1, B s2, C s3) { |
|||
x = value_type(s1); |
|||
y = value_type(s2); |
|||
z = value_type(s3); |
|||
} |
|||
|
|||
tvec3<T>& operator=(tvec3<T> &s) { |
|||
x = s.x; |
|||
y = s.y; |
|||
z = s.z; |
|||
|
|||
return *this; |
|||
} |
|||
|
|||
tvec3<T> const & operator=(tvec3<T> const &s) { |
|||
x = s.x; |
|||
y = s.y; |
|||
z = s.z; |
|||
|
|||
return *this; |
|||
} |
|||
|
|||
template <typename U> |
|||
tvec3<T>& operator=(tvec3<U> const & v) { |
|||
this->x = T(v.x); |
|||
this->y = T(v.y); |
|||
this->z = T(v.z); |
|||
return *this; |
|||
} |
|||
|
|||
tvec3<T> & operator++() { |
|||
++x; |
|||
++y; |
|||
++z; |
|||
return *this; |
|||
} |
|||
|
|||
template <typename U> |
|||
tvec3<T> & operator*=(U const & s) { |
|||
this->x *= T(s); |
|||
this->y *= T(s); |
|||
this->z *= T(s); |
|||
return *this; |
|||
} |
|||
}; |
|||
|
|||
template <typename T> |
|||
tvec3<T> operator-(tvec3<T> const & v, T const & s) { |
|||
return tvec3<T>( |
|||
v.x - T(s), |
|||
v.y - T(s), |
|||
v.z - T(s)); |
|||
} |
|||
|
|||
template <typename T> |
|||
tvec3<T> operator-(T const & s, tvec3<T> const & v) { |
|||
return tvec3<T>( |
|||
T(s) - v.x, |
|||
T(s) - v.y, |
|||
T(s) - v.z); |
|||
} |
|||
|
|||
template <typename T> |
|||
tvec3<T> operator-(tvec3<T> const & v1, tvec3<T> const & v2) { |
|||
return tvec3<T>( |
|||
v1.x - T(v2.x), |
|||
v1.y - T(v2.y), |
|||
v1.z - T(v2.z)); |
|||
} |
|||
|
|||
template <typename T> |
|||
tvec3<T> operator*(tvec3<T> const & v, T const & s) { |
|||
return tvec3<T>( |
|||
v.x * T(s), |
|||
v.y * T(s), |
|||
v.z * T(s)); |
|||
} |
|||
|
|||
template <typename T> |
|||
tvec3<T> operator*(T const & s, tvec3<T> const & v) { |
|||
return tvec3<T>( |
|||
T(s) * v.x, |
|||
T(s) * v.y, |
|||
T(s) * v.z); |
|||
} |
|||
|
|||
#endif /* TVEC3_H */ |
|||
|
@ -1,183 +0,0 @@ |
|||
/* |
|||
* To change this license header, choose License Headers in Project Properties. |
|||
* To change this template file, choose Tools | Templates |
|||
* and open the template in the editor. |
|||
*/ |
|||
|
|||
/* |
|||
* File: tvec4.h |
|||
* Author: Blobt |
|||
* |
|||
* Created on February 22, 2020, 9:12 AM |
|||
*/ |
|||
|
|||
#ifndef TVEC4_H |
|||
#define TVEC4_H |
|||
|
|||
#include "../common.h" |
|||
|
|||
template <typename T> |
|||
struct tvec4 { |
|||
typedef T value_type; |
|||
typedef std::size_t size_type; |
|||
typedef tvec4<T> type; |
|||
|
|||
|
|||
|
|||
value_type x, y, z, w; |
|||
|
|||
size_type length() const { |
|||
return 4; |
|||
} |
|||
|
|||
value_type & operator[](size_type i) { |
|||
assert(i < this->length()); |
|||
return (&x)[i]; |
|||
} |
|||
|
|||
value_type const & operator[](size_type i) const { |
|||
assert(i < this->length()); |
|||
return (&x)[i]; |
|||
} |
|||
|
|||
tvec4() : |
|||
x(value_type(0)), |
|||
y(value_type(0)), |
|||
z(value_type(0)), |
|||
w(value_type(0)) { |
|||
} |
|||
|
|||
tvec4(T s) : |
|||
x(s), |
|||
y(s), |
|||
z(s), |
|||
w(s) { |
|||
} |
|||
|
|||
tvec4(tvec4<T> const & v) : |
|||
x(v.x), |
|||
y(v.y), |
|||
z(v.z), |
|||
w(v.w) { |
|||
} |
|||
|
|||
template<typename U> |
|||
tvec4(tvec4<U> const & v) : |
|||
x(value_type(v.x)), |
|||
y(value_type(v.y)), |
|||
z(value_type(v.z)), |
|||
w(value_type(v.w)) { |
|||
} |
|||
|
|||
tvec4 |
|||
( |
|||
value_type s1, |
|||
value_type s2, |
|||
value_type s3, |
|||
value_type s4 |
|||
) : |
|||
x(s1), |
|||
y(s2), |
|||
z(s3), |
|||
w(s4) { |
|||
} |
|||
|
|||
tvec4<T> & operator=(tvec4<T> const & v) { |
|||
this->x = v.x; |
|||
this->y = v.y; |
|||
this->z = v.z; |
|||
this->w = v.w; |
|||
return *this; |
|||
} |
|||
|
|||
template <typename U> |
|||
tvec4<T> & operator=(tvec4<U> const & v) { |
|||
this->x = T(v.x); |
|||
this->y = T(v.y); |
|||
this->z = T(v.z); |
|||
this->w = T(v.w); |
|||
return *this; |
|||
} |
|||
|
|||
template <typename U> |
|||
tvec4<T> & operator*=(U const & s) { |
|||
this->x *= T(s); |
|||
this->y *= T(s); |
|||
this->z *= T(s); |
|||
this->w *= T(s); |
|||
return *this; |
|||
} |
|||
|
|||
template <typename U> |
|||
tvec4<T> & operator*=(tvec4<U> const & v) { |
|||
this->x *= T(v.x); |
|||
this->y *= T(v.y); |
|||
this->z *= T(v.z); |
|||
this->w *= T(v.w); |
|||
return *this; |
|||
} |
|||
|
|||
tvec4<T> & operator++() { |
|||
++this->x; |
|||
++this->y; |
|||
++this->z; |
|||
++this->w; |
|||
return *this; |
|||
} |
|||
}; |
|||
|
|||
template <typename T> |
|||
tvec4<T> operator+(tvec4<T> const & v, T const & s) { |
|||
return tvec4<T>( |
|||
v.x + s, |
|||
v.y + s, |
|||
v.z + s, |
|||
v.w + s); |
|||
} |
|||
|
|||
template <typename T> |
|||
tvec4<T> operator+(T const & s, tvec4<T> const & v) { |
|||
return tvec4<T>( |
|||
s + v.x, |
|||
s + v.y, |
|||
s + v.z, |
|||
s + v.w); |
|||
} |
|||
|
|||
template <typename T> |
|||
tvec4<T> operator+(tvec4<T> const & v1, tvec4<T> const & v2) { |
|||
return tvec4<T>( |
|||
v1.x + v2.x, |
|||
v1.y + v2.y, |
|||
v1.z + v2.z, |
|||
v1.w + v2.w); |
|||
} |
|||
|
|||
template <typename T> |
|||
tvec4<T> operator*(tvec4<T> const & v, T const & s) { |
|||
return tvec4<T>( |
|||
v.x * s, |
|||
v.y * s, |
|||
v.z * s, |
|||
v.w * s); |
|||
} |
|||
|
|||
template <typename T> |
|||
tvec4<T> operator*(T const & s, tvec4<T> const & v) { |
|||
return tvec4<T>( |
|||
s * v.x, |
|||
s * v.y, |
|||
s * v.z, |
|||
s * v.w); |
|||
} |
|||
|
|||
template <typename T> |
|||
tvec4<T> operator*(tvec4<T> const & v1, tvec4<T> const & v2) { |
|||
return tvec4<T>( |
|||
v1.x * v2.x, |
|||
v1.y * v2.y, |
|||
v1.z * v2.z, |
|||
v1.w * v2.w); |
|||
} |
|||
#endif /* TVEC4_H */ |
|||
|
@ -0,0 +1,7 @@ |
|||
set(commom_src |
|||
Raster.cc |
|||
Image.cc |
|||
) |
|||
|
|||
add_executable(camera camera.cc ${commom_src}) |
|||
target_link_libraries (camera ${FC_DEP_LIBS}) |
@ -1,7 +0,0 @@ |
|||
set(commom_src |
|||
Raster.cc |
|||
Image.cc |
|||
) |
|||
|
|||
add_executable(threed threed.cc ${commom_src}) |
|||
target_link_libraries (threed ${FC_DEP_LIBS}) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue