diff --git a/model.h b/model.h index 434e6f3..b889925 100755 --- a/model.h +++ b/model.h @@ -20,6 +20,7 @@ namespace vb01{ Model(){} Model(std::string); ~Model(); + inline Material* getMaterial(){return material;} void update(); void setMaterial(Material*); void setCastShadow(bool); diff --git a/quaternion.h b/quaternion.h index 8c2d322..52eabb7 100755 --- a/quaternion.h +++ b/quaternion.h @@ -20,7 +20,9 @@ namespace vb01{ Quaternion(){} inline bool operator==(Quaternion q){return w == q.w && x == q.x && y == q.y && z == q.z;} inline bool operator!=(Quaternion q){return w != q.w || x != q.x || y != q.y || z != q.z;} - inline Quaternion operator- (){return Quaternion(w, -x, -y, -z);} + inline friend Quaternion operator-(Quaternion q){return Quaternion(q.w, -q.x, -q.y, -q.z);} + inline friend Quaternion operator+(Quaternion q1, Quaternion q2){return Quaternion(q1.w + q2.w, q1.x + q2.x, q1.y + q2.y, q1.z + q2.z);} + inline friend Quaternion operator-(Quaternion q1, Quaternion q2){return Quaternion(q1.w - q2.w, q1.x - q2.x, q1.y - q2.y, q1.z - q2.z);} inline Quaternion operator* (Quaternion q){ float a1 = this->w, a2 = q.w, b1 = this->x, b2 = q.x, @@ -36,15 +38,35 @@ namespace vb01{ Quaternion vQuat = (*this) * Quaternion(0, v.x, v.y, v.z) * recip(); return Vector3(vQuat.x, vQuat.y, vQuat.z); } + inline float getAngle(){return fabs(w) > 1 ? 0 : 2 * acos(w);} + inline Vector3 getAxis(){return x == 0 && y == 0 && z == 0 ? Vector3(1, 0, 0) : Vector3(x, y, z).norm();} + template friend Quaternion operator+(Quaternion q, T s){return Quaternion(q.w + s, q.x + s, q.y + s, q.z + s);} + template friend Quaternion operator-(Quaternion q, T s){return Quaternion(q.w - s, q.x - s, q.y - s, q.z - s);} + template friend Quaternion operator*(Quaternion q, T s){return Quaternion(q.w * s, q.x * s, q.y * s, q.z * s);} + template friend Quaternion operator/(Quaternion q, T s){return Quaternion(q.w / s, q.x / s, q.y / s, q.z / s);} + template friend Quaternion operator+(T s, Quaternion q){return Quaternion(q.w + s, q.x + s, q.y + s, q.z + s);} + template friend Quaternion operator-(T s, Quaternion q){return Quaternion(q.w - s, q.x - s, q.y - s, q.z - s);} + template friend Quaternion operator*(T s, Quaternion q){return Quaternion(q.w * s, q.x * s, q.y * s, q.z * s);} + template friend Quaternion operator/(T s, Quaternion q){return Quaternion(q.w / s, q.x / s, q.y / s, q.z / s);} + void operator+=(Quaternion q){w += q.w, x += q.x, y += q.y, z += q.z;} + void operator-=(Quaternion q){w -= q.w, x -= q.x, y -= q.y, z -= q.z;} + void operator*=(Quaternion q){ + float a1 = this->w, a2 = q.w, + b1 = this->x, b2 = q.x, + c1 = this->y, c2 = q.y, + d1 = this->z, d2 = q.z; + w = a1 * a2 - b1 * b2 - c1 * c2 - d1 * d2; + x = a1 * b2 + b1 * a2 + c1 * d2 - d1 * c2; + y = a1 * c2 - b1 * d2 + c1 * a2 + d1 * b2; + z = a1 * d2 + b1 * c2 - c1 * b2 + d1 * a2; + } + template void operator+=(T s){w += s, x += s, y += s, z += s;} + template void operator-=(T s){w -= s, x -= s, y -= s, z -= s;} + template void operator*=(T s){w *= s, x *= s, y *= s, z *= s;} + template void operator/=(T s){w /= s, x /= s, y /= s, z /= s;} inline Quaternion fromAngle(float angle, Vector3 axis){ return Quaternion(cos(angle / 2), axis.x * sin(angle / 2), axis.y * sin(angle / 2), axis.z * sin(angle / 2)); } - inline float getAngle(){return w > 1 ? 0 : 2 * acos(w);} - inline Vector3 getAxis(){return x == 0 && y == 0 && z == 0 ? Vector3(1, 0, 0) : Vector3(x, y, z).norm();} - inline Quaternion operator+(Quaternion q){return Quaternion(w + q.w, x + q.x, y + q.y, z + q.z);} - inline Quaternion operator-(Quaternion q){return Quaternion(w - q.w, x - q.x, y - q.y, z - q.z);} - template inline Quaternion operator*(T s){return Quaternion(w * s, x * s, y * s, z * s);} - template inline Quaternion operator/(T s){return Quaternion(w / s, x / s, y / s, z / s);} inline Quaternion conj(){ return (*this + Quaternion(0, 1, 0, 0) *(*this) * Quaternion(0, 1, 0, 0) + Quaternion(0, 0, 1, 0) diff --git a/ray.cpp b/ray.cpp index dbb9048..5030b5d 100644 --- a/ray.cpp +++ b/ray.cpp @@ -26,8 +26,8 @@ namespace vb01{ for(int i = 0; i < numVerts / 3; i++){ Vector3 pointA = pos + rot * vertices[indices[i * 3]].pos, pointB = pos + rot * vertices[indices[i * 3 + 1]].pos, pointC = pos + rot * vertices[indices[i * 3 + 2]].pos; Vector3 hypVec = pointA - rayPos; - Vector3 perpVec = (pointB - pointA).cross(pointC - pointA); - float a1 = hypVec.norm().getAngleBetween(perpVec.norm()); + Vector3 perpVec = (pointB - pointA).cross(pointC - pointA).norm(); + float a1 = hypVec.norm().getAngleBetween(perpVec); if(a1 > PI / 2){ a1 = PI - a1; @@ -35,7 +35,7 @@ namespace vb01{ } float perpLine = hypVec.getLength() * cos(a1); - float a2 = perpVec.norm().getAngleBetween(rayDir.norm()); + float a2 = perpVec.getAngleBetween(rayDir.norm()); if(a2 <= PI / 2){ float distance = perpLine / cos(a2); @@ -55,6 +55,7 @@ namespace vb01{ if(withinBisecA && withinBisecB && withinBisecC){ CollisionResult result; result.pos = contactPoint; + result.norm = -perpVec; result.distance = distance; result.mesh = m; results.push_back(result); diff --git a/ray.h b/ray.h index 49877a8..5c7d40b 100644 --- a/ray.h +++ b/ray.h @@ -10,7 +10,7 @@ namespace vb01{ class Ray{ public: struct CollisionResult{ - Vector3 pos; + Vector3 pos, norm; float distance; Mesh *mesh = nullptr; }; diff --git a/vector.h b/vector.h index a852ede..7fadbad 100755 --- a/vector.h +++ b/vector.h @@ -13,12 +13,23 @@ namespace vb01{ this->z = z; this->w = w; } - Vector4 operator-(const Vector4 &v){return Vector4(x - v.x, y - v.y, z - v.z, w - v.w);} - Vector4 operator+(const Vector4 &v){return Vector4(x + v.x, y + v.y, z + v.z, w + v.w);} - templateVector4 operator+(T s){return Vector4(x + s, y + s, z + s, w + s);} - templateVector4 operator-(T s){return Vector4(x - s, y - s, z - s, w - s);} - templateVector4 operator*(T s){return Vector4(x * s, y * s, z * s, w * s);} - templateVector4 operator/(T s){return Vector4(x / s, y / s, z / s, w / s);} + Vector4 friend operator-(Vector4 v){return Vector4(-v.x, -v.y, -v.z, -v.w);} + Vector4 friend operator-(Vector4 v1, Vector4 v2){return Vector4(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);} + Vector4 friend operator+(Vector4 v1, Vector4 v2){return Vector4(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);} + template friend Vector4 operator+(Vector4 v, T s){return Vector4(v.x + s, v.y + s, v.z + s, v.w + s);} + template friend Vector4 operator-(Vector4 v, T s){return Vector4(v.x - s, v.y - s, v.z - s, v.w - s);} + template friend Vector4 operator*(Vector4 v, T s){return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);} + template friend Vector4 operator/(Vector4 v, T s){return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);} + template friend Vector4 operator+(T s, Vector4 v){return Vector4(v.x + s, v.y + s, v.z + s, v.w + s);} + template friend Vector4 operator-(T s, Vector4 v){return Vector4(v.x - s, v.y - s, v.z - s, v.w - s);} + template friend Vector4 operator*(T s, Vector4 v){return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);} + template friend Vector4 operator/(T s, Vector4 v){return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);} + void operator+=(Vector4 v){x += v.x, y +=v.y, z += v.z, w += v.w;} + void operator-=(Vector4 v){x -= v.x, y -=v.y, z -= v.z, w -= v.w;} + template void operator+=(T s){x += s, y +=s, z += s, w += s;} + template void operator-=(T s){x -= s, y -=s, z -= s, w -= s;} + template void operator*=(T s){x *= s, y *=s, z *= s, w *= s;} + template void operator/=(T s){x /= s, y /=s, z /= s, w /= s;} float x, y, z, w; static const Vector4 VEC_IJKL, VEC_I, VEC_J, VEC_K, VEC_L, VEC_ZERO; @@ -33,13 +44,23 @@ namespace vb01{ } bool operator!=(const Vector3 &v){return x != v.x || y != v.y || z != v.z;} bool operator==(const Vector3 &v){return x == v.x && y == v.y && z == v.z;} - Vector3 operator-(){return Vector3(-x, -y, -z);} - Vector3 operator-(const Vector3 &v){return Vector3(x - v.x, y - v.y, z - v.z);} - Vector3 operator+(const Vector3 &v){return Vector3(x + v.x, y + v.y, z + v.z);} - templateVector3 operator+(T s){return Vector3(x + s, y + s, z + s);} - templateVector3 operator-(T s){return Vector3(x - s, y - s, z - s);} - templateVector3 operator*(T s){return Vector3(x * s, y * s, z * s);} - templateVector3 operator/(T s){return Vector3(x / s, y / s, z / s);} + Vector3 friend operator-(Vector3 v){return Vector3(-v.x, -v.y, -v.z);} + Vector3 friend operator-(Vector3 v1, Vector3 v2){return Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);} + Vector3 friend operator+(Vector3 v1, Vector3 v2){return Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);} + template friend Vector3 operator+(Vector3 v, T s){return Vector3(v.x + s, v.y + s, v.z + s);} + template friend Vector3 operator-(Vector3 v, T s){return Vector3(v.x - s, v.y - s, v.z - s);} + template friend Vector3 operator*(Vector3 v, T s){return Vector3(v.x * s, v.y * s, v.z * s);} + template friend Vector3 operator/(Vector3 v, T s){return Vector3(v.x / s, v.y / s, v.z / s);} + template friend Vector3 operator+(T s, Vector3 v){return Vector3(v.x + s, v.y + s, v.z + s);} + template friend Vector3 operator-(T s, Vector3 v){return Vector3(v.x - s, v.y - s, v.z - s);} + template friend Vector3 operator*(T s, Vector3 v){return Vector3(v.x * s, v.y * s, v.z * s);} + template friend Vector3 operator/(T s, Vector3 v){return Vector3(v.x / s, v.y / s, v.z / s);} + void operator+=(Vector3 v){x += v.x, y += v.y, z += v.z;} + void operator-=(Vector3 v){x -= v.x, y -= v.y, z -= v.z;} + template void operator+=(T s){x += s, y += s, z += s;} + template void operator-=(T s){x -= s, y -= s, z -= s;} + template void operator*=(T s){x *= s, y *= s, z *= s;} + template void operator/=(T s){x /= s, y /= s, z /= s;} float getLengthSq(){return x * x + y * y + z * z;} float getLength(){return std::sqrt(getLengthSq());} float dot(Vector3 v){return x * v.x + y * v.y + z * v.z;} @@ -67,12 +88,23 @@ namespace vb01{ } bool operator!=(const Vector2 &v){return x != v.x || y != v.y;} bool operator==(const Vector2 &v){return x == v.x && y == v.y;} - Vector2 operator-(const Vector2 &v){return Vector2(x - v.x, y - v.y);} - Vector2 operator+(const Vector2 &v){return Vector2(x + v.x, y + v.y);} - templateVector2 operator+(T s){return Vector2(x + s, y + s);} - templateVector2 operator-(T s){return Vector2(x - s, y - s);} - templateVector2 operator*(T s){return Vector2(x * s, y * s);} - templateVector2 operator/(T s){return Vector2(x / s, y / s);} + Vector2 friend operator-(Vector2 v){return Vector2(-v.x, -v.y);} + Vector2 friend operator+(Vector2 v1, Vector2 v2){return Vector2(v1.x + v2.x, v1.y + v2.y);} + Vector2 friend operator-(Vector2 v1, Vector2 v2){return Vector2(v1.x - v2.x, v1.y - v2.y);} + template friend Vector2 operator+(Vector2 v, T s){return Vector2(v.x + s, v.y + s);} + template friend Vector2 operator-(Vector2 v, T s){return Vector2(v.x - s, v.y - s);} + template friend Vector2 operator*(Vector2 v, T s){return Vector2(v.x * s, v.y * s);} + template friend Vector2 operator/(Vector2 v, T s){return Vector2(v.x / s, v.y / s);} + template friend Vector2 operator+(T s, Vector2 v){return Vector2(v.x + s, v.y + s);} + template friend Vector2 operator-(T s, Vector2 v){return Vector2(v.x - s, v.y - s);} + template friend Vector2 operator*(T s, Vector2 v){return Vector2(v.x * s, v.y * s);} + template friend Vector2 operator/(T s, Vector2 v){return Vector2(v.x / s, v.y / s);} + void operator+=(Vector2 v){x += v.x, y += v.y;} + void operator-=(Vector2 v){x -= v.x, y -= v.y;} + template void operator+=(T s){x += s, y += s;} + template void operator-=(T s){x -= s, y -= s;} + template void operator*=(T s){x *= s, y *= s;} + template void operator/=(T s){x /= s, y /= s;} float getLengthSq(){return x * x + y * y;} float getLength(){return std::sqrt(getLengthSq());} float dot(Vector2 v){return x * v.x + y * v.y;}