Updated math library and added the KRQuaternion class.

--HG--
extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%4059
This commit is contained in:
kearwood
2012-06-10 06:24:04 +00:00
parent cad53fcf69
commit 9c8430368f
11 changed files with 675 additions and 123 deletions

View File

@@ -13,6 +13,10 @@
E414BAE91435585A00A668C4 /* KRScene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E414BAE81435585A00A668C4 /* KRScene.cpp */; };
E414BAEB14355E5500A668C4 /* KRBoundingVolume.h in Headers */ = {isa = PBXBuildFile; fileRef = E414BAEA14355E5500A668C4 /* KRBoundingVolume.h */; };
E414BAED14355EFF00A668C4 /* KRBoundingVolume.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E414BAEC14355EFF00A668C4 /* KRBoundingVolume.cpp */; };
E42CB1EC158446940066E0D8 /* KRQuaternion.h in Headers */ = {isa = PBXBuildFile; fileRef = E42CB1EB158446940066E0D8 /* KRQuaternion.h */; };
E42CB1ED158446940066E0D8 /* KRQuaternion.h in Headers */ = {isa = PBXBuildFile; fileRef = E42CB1EB158446940066E0D8 /* KRQuaternion.h */; };
E42CB1F0158446AB0066E0D8 /* KRQuaternion.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E42CB1EF158446AB0066E0D8 /* KRQuaternion.cpp */; };
E42CB1F1158446AB0066E0D8 /* KRQuaternion.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E42CB1EF158446AB0066E0D8 /* KRQuaternion.cpp */; };
E461A152152E54B500F2044A /* KRLight.h in Headers */ = {isa = PBXBuildFile; fileRef = E461A151152E54B500F2044A /* KRLight.h */; };
E461A153152E54B500F2044A /* KRLight.h in Headers */ = {isa = PBXBuildFile; fileRef = E461A151152E54B500F2044A /* KRLight.h */; settings = {ATTRIBUTES = (Public, ); }; };
E461A156152E54F800F2044A /* KRLight.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E461A155152E54F700F2044A /* KRLight.cpp */; };
@@ -157,6 +161,8 @@
E414BAE81435585A00A668C4 /* KRScene.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; name = KRScene.cpp; path = Classes/KRScene.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
E414BAEA14355E5500A668C4 /* KRBoundingVolume.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; name = KRBoundingVolume.h; path = Classes/KRBoundingVolume.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
E414BAEC14355EFF00A668C4 /* KRBoundingVolume.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; name = KRBoundingVolume.cpp; path = Classes/KRBoundingVolume.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
E42CB1EB158446940066E0D8 /* KRQuaternion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = KRQuaternion.h; path = Classes/KRQuaternion.h; sourceTree = "<group>"; };
E42CB1EF158446AB0066E0D8 /* KRQuaternion.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = KRQuaternion.cpp; path = Classes/KRQuaternion.cpp; sourceTree = "<group>"; };
E45772F113C9A13C0037BEEA /* ShadowShader.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; lineEnding = 0; name = ShadowShader.vsh; path = Shaders/ShadowShader.vsh; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.glsl; };
E45772F213C9A13C0037BEEA /* ShadowShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = ShadowShader.fsh; path = Shaders/ShadowShader.fsh; sourceTree = "<group>"; };
E45772F313C9A13C0037BEEA /* PostShader.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; name = PostShader.fsh; path = Shaders/PostShader.fsh; sourceTree = "<group>"; };
@@ -339,6 +345,8 @@
E491017F13C99BDC0098455B /* KRVector3.cpp */,
E497B943151BA93400D3DC67 /* KRVector2.h */,
E497B945151BA99400D3DC67 /* KRVector2.cpp */,
E42CB1EB158446940066E0D8 /* KRQuaternion.h */,
E42CB1EF158446AB0066E0D8 /* KRQuaternion.cpp */,
);
name = Math;
sourceTree = "<group>";
@@ -535,6 +543,7 @@
E48C696F15374F5B00232E28 /* KRContext.h in Headers */,
E46F4A00155DF46700CCF8B8 /* KRWorld.h in Headers */,
E46F4A0B155E002100CCF8B8 /* KRDataBlock.h in Headers */,
E42CB1EC158446940066E0D8 /* KRQuaternion.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -573,6 +582,7 @@
E46C214815364BC8009CABF3 /* tinyxml2.h in Headers */,
E46A6B701559EF0A000DBD37 /* KRResource+blend.h in Headers */,
E46F4A0C155E002100CCF8B8 /* KRDataBlock.h in Headers */,
E42CB1ED158446940066E0D8 /* KRQuaternion.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -691,6 +701,7 @@
E4A9DEC1154120E8009DF363 /* light_point.vsh in Sources */,
E46F4A04155DF47C00CCF8B8 /* KRWorld.cpp in Sources */,
E46F4A0E155E003000CCF8B8 /* KRDataBlock.cpp in Sources */,
E42CB1F0158446AB0066E0D8 /* KRQuaternion.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -728,6 +739,7 @@
E46A6B6D1559E97D000DBD37 /* KRResource+blend.cpp in Sources */,
E46F4A05155DF47C00CCF8B8 /* KRWorld.cpp in Sources */,
E46F4A0F155E003000CCF8B8 /* KRDataBlock.cpp in Sources */,
E42CB1F1158446AB0066E0D8 /* KRQuaternion.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@@ -30,6 +30,7 @@
//
#include <iostream>
#include <math.h>
#import "KRBoundingVolume.h"

View File

@@ -12,9 +12,11 @@
#import "KRResource.h"
#import "KRNode.h"
static const float KRLIGHT_MIN_INFLUENCE = 0.05f;
class KRLight : public KRNode {
public:
static const float KRLIGHT_MIN_INFLUENCE = 0.05f;
virtual ~KRLight();
virtual std::string getElementName() = 0;

View File

@@ -378,7 +378,7 @@ void KRMesh::LoadData(std::vector<KRVector3> vertices, std::vector<KRVector2> uv
// -- Calculate normal --
if(pVertex->normal.x == 0 && pVertex->normal.y == 0 && pVertex->normal.z == 0) {
KRVector3 normal = v1.cross( v2 );
KRVector3 normal = KRVector3::Cross(v1, v2);
normal.normalize();

View File

@@ -7,6 +7,7 @@
//
#include <iostream>
#include <math.h>
#import "KRPointLight.h"
#import "KRMat4.h"

View File

@@ -0,0 +1,235 @@
//
// KRQuaternion.cpp
// KREngine
//
// Copyright 2012 Kearwood Gilbert. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
//
#include "KRQuaternion.h"
#include "KRVector3.h"
KRQuaternion::KRQuaternion() {
m_val[0] = 1.0;
m_val[1] = 0.0;
m_val[2] = 0.0;
m_val[3] = 0.0;
}
KRQuaternion::KRQuaternion(float w, float x, float y, float z) {
m_val[0] = w;
m_val[1] = x;
m_val[2] = y;
m_val[3] = z;
}
KRQuaternion::KRQuaternion(const KRQuaternion& p) {
m_val[0] = p[0];
m_val[1] = p[1];
m_val[2] = p[2];
m_val[3] = p[3];
}
KRQuaternion& KRQuaternion::operator =( const KRQuaternion& p ) {
m_val[0] = p[0];
m_val[1] = p[1];
m_val[2] = p[2];
m_val[3] = p[3];
return *this;
}
KRQuaternion::KRQuaternion(const KRVector3 &euler) {
setEuler(euler);
}
KRQuaternion::~KRQuaternion() {
}
void KRQuaternion::setEuler(const KRVector3 &euler) {
m_val[0] = cos(euler[0] / 2.0) * cos(euler[1] / 2.0) * cos(euler[2] / 2.0) + sin(euler[0] / 2.0) * sin(euler[1] / 2.0) * sin(euler[2] / 2.0);
m_val[1] = sin(euler[0] / 2.0) * cos(euler[1] / 2.0) * cos(euler[2] / 2.0) - cos(euler[0] / 2.0) * sin(euler[1] / 2.0) * sin(euler[2] / 2.0);
m_val[2] = cos(euler[0] / 2.0) * sin(euler[1] / 2.0) * cos(euler[2] / 2.0) + sin(euler[0] / 2.0) * cos(euler[1] / 2.0) * sin(euler[2] / 2.0);
m_val[3] = cos(euler[0] / 2.0) * cos(euler[1] / 2.0) * sin(euler[2] / 2.0) - sin(euler[0] / 2.0) * sin(euler[1] / 2.0) * cos(euler[2] / 2.0);
}
float KRQuaternion::operator [](unsigned i) const {
return m_val[i];
}
float &KRQuaternion::operator [](unsigned i) {
return m_val[i];
}
KRVector3 KRQuaternion::euler() const {
KRVector3 euler;
euler[0] = atan2(2.0 * (m_val[0] * m_val[1] + m_val[2] * m_val[3]), 1.0 - 2.0 * (m_val[1] * m_val[1] + m_val[2] * m_val[2]));
euler[1] = asin(2.0 * (m_val[0] * m_val[2] - m_val[3] * m_val[1]));
euler[2] = atan2(2.0 * (m_val[0] * m_val[3] + m_val[1] * m_val[2]), 1.0 - 2.0 * (m_val[2] * m_val[2] + m_val[3] * m_val[3]));
return euler;
}
bool operator ==(KRQuaternion &v1, KRQuaternion &v2) {
return
v1[0] == v2[0]
&& v1[1] == v2[1]
&& v1[2] == v2[2]
&& v1[3] == v2[3];
}
bool operator !=(KRQuaternion &v1, KRQuaternion &v2) {
return
v1[0] != v2[0]
|| v1[1] != v2[1]
|| v1[2] != v2[2]
|| v1[3] != v2[3];
}
KRQuaternion KRQuaternion::operator *(const KRQuaternion &v) {
float t0 = (m_val[3]-m_val[2])*(v[2]-v[3]);
float t1 = (m_val[0]+m_val[1])*(v[0]+v[1]);
float t2 = (m_val[0]-m_val[1])*(v[2]+v[3]);
float t3 = (m_val[3]+m_val[2])*(v[0]-v[1]);
float t4 = (m_val[3]-m_val[1])*(v[1]-v[2]);
float t5 = (m_val[3]+m_val[1])*(v[1]+v[2]);
float t6 = (m_val[0]+m_val[2])*(v[0]-v[3]);
float t7 = (m_val[0]-m_val[2])*(v[0]+v[3]);
float t8 = t5+t6+t7;
float t9 = (t4+t8)/2;
return KRQuaternion(
t0+t9-t5,
t1+t9-t8,
t2+t9-t7,
t3+t9-t6
);
}
KRQuaternion KRQuaternion::operator *(float v) const {
return KRQuaternion(m_val[0] * v, m_val[1] * v, m_val[2] * v, m_val[3] * v);
}
KRQuaternion KRQuaternion::operator /(float num) const {
return KRQuaternion(m_val[0] / num, m_val[1] / num, m_val[2] / num, m_val[3] / num);
}
KRQuaternion KRQuaternion::operator +(const KRQuaternion &v) const {
return KRQuaternion(m_val[0] + v[0], m_val[1] + v[1], m_val[2] + v[2], m_val[3] + v[3]);
}
KRQuaternion KRQuaternion::operator -(const KRQuaternion &v) const {
return KRQuaternion(m_val[0] - v[0], m_val[1] - v[1], m_val[2] - v[2], m_val[3] - v[3]);
}
KRQuaternion& KRQuaternion::operator +=(const KRQuaternion& v) {
m_val[0] += v[0];
m_val[1] += v[1];
m_val[2] += v[2];
m_val[3] += v[3];
return *this;
}
KRQuaternion& KRQuaternion::operator -=(const KRQuaternion& v) {
m_val[0] += v[0];
m_val[1] += v[1];
m_val[2] += v[2];
m_val[3] += v[3];
return *this;
}
KRQuaternion& KRQuaternion::operator *=(const KRQuaternion& v) {
float t0 = (m_val[3]-m_val[2])*(v[2]-v[3]);
float t1 = (m_val[0]+m_val[1])*(v[0]+v[1]);
float t2 = (m_val[0]-m_val[1])*(v[2]+v[3]);
float t3 = (m_val[3]+m_val[2])*(v[0]-v[1]);
float t4 = (m_val[3]-m_val[1])*(v[1]-v[2]);
float t5 = (m_val[3]+m_val[1])*(v[1]+v[2]);
float t6 = (m_val[0]+m_val[2])*(v[0]-v[3]);
float t7 = (m_val[0]-m_val[2])*(v[0]+v[3]);
float t8 = t5+t6+t7;
float t9 = (t4+t8)/2;
m_val[0] = t0+t9-t5;
m_val[1] = t1+t9-t8;
m_val[2] = t2+t9-t7;
m_val[3] = t3+t9-t6;
return *this;
}
KRQuaternion& KRQuaternion::operator *=(const float& v) {
m_val[0] *= v;
m_val[1] *= v;
m_val[2] *= v;
m_val[3] *= v;
return *this;
}
KRQuaternion& KRQuaternion::operator /=(const float& v) {
m_val[0] /= v;
m_val[1] /= v;
m_val[2] /= v;
m_val[3] /= v;
return *this;
}
KRQuaternion KRQuaternion::operator +() const {
return *this;
}
KRQuaternion KRQuaternion::operator -() const {
return KRQuaternion(-m_val[0], -m_val[1], -m_val[2], -m_val[3]);
}
KRQuaternion Normalize(const KRQuaternion &v1) {
float magnitude = sqrtf(v1[0] * v1[0] + v1[1] * v1[1] + v1[2] * v1[2] + v1[3] * v1[3]);
return KRQuaternion(
v1[0] / magnitude,
v1[1] / magnitude,
v1[2] / magnitude,
v1[3] / magnitude
);
}
void KRQuaternion::normalize() {
float magnitude = sqrtf(m_val[0] * m_val[0] + m_val[1] * m_val[1] + m_val[2] * m_val[2] + m_val[3] * m_val[3]);
m_val[0] /= magnitude;
m_val[1] /= magnitude;
m_val[2] /= magnitude;
m_val[3] /= magnitude;
}
KRQuaternion Conjugate(const KRQuaternion &v1) {
return KRQuaternion(v1[0], -v1[1], -v1[2], -v1[3]);
}
void KRQuaternion::conjugate() {
m_val[1] = -m_val[1];
m_val[2] = -m_val[2];
m_val[3] = -m_val[3];
}

View File

@@ -0,0 +1,82 @@
//
// KRQuaternion.h
// KREngine
//
// Copyright 2012 Kearwood Gilbert. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
//
#ifndef KRQUATERNION
#define KRQUATERNION
#include <math.h>
#import "KREngine-common.h"
class KRVector3;
class KRQuaternion {
public:
KRQuaternion();
KRQuaternion(float w, float x, float y, float z);
KRQuaternion(const KRQuaternion& p);
KRQuaternion(const KRVector3 &euler);
~KRQuaternion();
KRQuaternion& operator =( const KRQuaternion& p );
KRQuaternion operator +(const KRQuaternion &v) const;
KRQuaternion operator -(const KRQuaternion &v) const;
KRQuaternion operator +() const;
KRQuaternion operator -() const;
KRQuaternion operator *(const KRQuaternion &v);
KRQuaternion operator *(float num) const;
KRQuaternion operator /(float num) const;
KRQuaternion& operator +=(const KRQuaternion& v);
KRQuaternion& operator -=(const KRQuaternion& v);
KRQuaternion& operator *=(const KRQuaternion& v);
KRQuaternion& operator *=(const float& v);
KRQuaternion& operator /=(const float& v);
friend bool operator ==(KRVector3 &v1, KRVector3 &v2);
friend bool operator !=(KRVector3 &v1, KRVector3 &v2);
float& operator [](unsigned i);
float operator [](unsigned i) const;
void setEuler(const KRVector3 &euler);
KRVector3 euler() const;
void normalize();
static KRQuaternion Normalize(const KRQuaternion &v1);
void conjugate();
static KRQuaternion Conjugate(const KRQuaternion &v1);
private:
float m_val[4];
};
#endif

View File

@@ -7,6 +7,7 @@
//
#include <iostream>
#include <math.h>
#include "KRVector2.h"
@@ -20,14 +21,138 @@ KRVector2::KRVector2(float X, float Y) {
y = Y;
}
KRVector2::KRVector2(float v) {
x = v;
y = v;
}
KRVector2::KRVector2(const KRVector2 &v) {
x = v.x;
y = v.y;
}
KRVector2 KRVector2::ZeroVector() {
return KRVector2(0.0f);
}
KRVector2 KRVector2::OneVector() {
return KRVector2(1.0f);
}
KRVector2::~KRVector2() {
}
bool operator== (KRVector2 &v1, KRVector2 &v2) {
return v1.x == v2.x && v1.y == v2.y;
KRVector2& KRVector2::operator =(const KRVector2& b) {
x = b.x;
y = b.y;
return *this;
}
bool operator!= (KRVector2 &v1, KRVector2 &v2) {
return !(v1 == v2);
}
KRVector2 KRVector2::operator +(const KRVector2& b) const {
return KRVector2(x + b.x, y + b.y);
}
KRVector2 KRVector2::operator -(const KRVector2& b) const {
return KRVector2(x - b.x, y - b.y);
}
KRVector2 KRVector2::operator +() const {
return *this;
}
KRVector2 KRVector2::operator -() const {
return KRVector2(-x, -y);
}
KRVector2 KRVector2::operator *(const float v) const {
return KRVector2(x * v, y * v);
}
KRVector2 KRVector2::operator /(const float v) const {
return KRVector2(x / v, y / v);
}
KRVector2& KRVector2::operator +=(const KRVector2& b) {
x += b.x;
y += b.y;
return *this;
}
KRVector2& KRVector2::operator -=(const KRVector2& b) {
x -= b.x;
y -= b.y;
return *this;
}
KRVector2& KRVector2::operator *=(const float v) {
x *= v;
y *= v;
return *this;
}
KRVector2& KRVector2::operator /=(const float v) {
x /= v;
y /= v;
return *this;
}
bool KRVector2::operator ==(const KRVector2& b) const {
return x == b.x && y == b.y;
}
bool KRVector2::operator !=(const KRVector2& b) const {
return x != b.x || y != b.y;
}
float& KRVector2::operator[] (unsigned i) {
switch(i) {
case 0:
return x;
case 1:
default:
return y;
}
}
float KRVector2::operator[](unsigned i) const {
switch(i) {
case 0:
return x;
case 1:
default:
return y;
}
}
void KRVector2::normalize() {
float magnitude = sqrtf(x * x + y * y);
x /= magnitude;
y /= magnitude;
}
float KRVector2::sqrMagnitude() const {
return x * x + y * y;
}
float KRVector2::magnitude() const {
return sqrtf(x * x + y * y);
}
KRVector2 KRVector2::Normalize(const KRVector2 &v) {
float magnitude = sqrtf(v.x * v.x + v.y * v.y);
return KRVector2(v.x / magnitude, v.y / magnitude);
}
float KRVector2::Cross(const KRVector2 &v1, const KRVector2 &v2) {
return v1.x * v2.y - v1.y * v2.x;
}
float KRVector2::Dot(const KRVector2 &v1, const KRVector2 &v2) {
return v1.x * v2.x + v1.y * v2.y;
}

View File

@@ -2,22 +2,80 @@
// KRVector2.h
// KREngine
//
// Created by Kearwood Gilbert on 12-03-22.
// Copyright (c) 2012 Kearwood Software. All rights reserved.
// Copyright 2012 Kearwood Gilbert. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
//
#ifndef KREngine_KRVector2_h
#define KREngine_KRVector2_h
#ifndef KRVECTOR2
#define KRVECTOR2
#import "KREngine-common.h"
class KRVector2 {
public:
float x, y;
KRVector2();
KRVector2(float X, float Y);
KRVector2(float v);
KRVector2(const KRVector2 &v);
~KRVector2();
friend bool operator== (KRVector2 &v1, KRVector2 &v2);
friend bool operator!= (KRVector2 &v1, KRVector2 &v2);
KRVector2& operator =(const KRVector2& b);
KRVector2 operator +(const KRVector2& b) const;
KRVector2 operator -(const KRVector2& b) const;
KRVector2 operator +() const;
KRVector2 operator -() const;
KRVector2 operator *(const float v) const;
KRVector2 operator /(const float v) const;
KRVector2& operator +=(const KRVector2& b);
KRVector2& operator -=(const KRVector2& b);
KRVector2& operator *=(const float v);
KRVector2& operator /=(const float v);
bool operator ==(const KRVector2& b) const;
bool operator !=(const KRVector2& b) const;
float& operator[](unsigned i);
float operator[](unsigned i) const;
float sqrMagnitude() const;
float magnitude() const;
void normalize();
static KRVector2 Normalize(const KRVector2 &v);
static float Cross(const KRVector2 &v1, const KRVector2 &v2);
static float Dot(const KRVector2 &v1, const KRVector2 &v2);
static KRVector2 ZeroVector();
static KRVector2 OneVector();
private:

View File

@@ -31,6 +31,8 @@
#include "KRVector3.h"
#include <math.h>
//default constructor
KRVector3::KRVector3()
{
@@ -39,6 +41,12 @@ KRVector3::KRVector3()
z = 0.0f;
}
KRVector3::KRVector3(const KRVector3 &v) {
x = v.x;
y = v.y;
z = v.z;
}
KRVector3 KRVector3::ZeroVector() {
return KRVector3(0.0f, 0.0f, 0.0f);
}
@@ -113,92 +121,128 @@ KRVector3::KRVector3(float X, float Y, float Z)
z = Z;
}
KRVector3::KRVector3(const KRVector3& p) {
x = p.x;
y = p.y;
z = p.z;
}
KRVector3& KRVector3::operator = ( const KRVector3& p ) {
x = p.x;
y = p.y;
z = p.z;
return *this;
}
KRVector3::~KRVector3()
{
}
//calculate and return the magnitude of this vector
float KRVector3::GetMagnitude()
{
return sqrtf(GetSqrMagnitude());
KRVector3& KRVector3::operator =(const KRVector3& b) {
x = b.x;
y = b.y;
z = b.z;
return *this;
}
KRVector3 KRVector3::operator +(const KRVector3& b) const {
return KRVector3(x + b.x, y + b.y, z + b.z);
}
KRVector3 KRVector3::operator -(const KRVector3& b) const {
return KRVector3(x - b.x, y - b.y, z - b.z);
}
KRVector3 KRVector3::operator +() const {
return *this;
}
KRVector3 KRVector3::operator -() const {
return KRVector3(-x, -y, -z);
}
float KRVector3::GetSqrMagnitude()
{
KRVector3 KRVector3::operator *(const float v) const {
return KRVector3(x * v, y * v, z * v);
}
KRVector3 KRVector3::operator /(const float v) const {
return KRVector3(x / v, y / v, z / v);
}
KRVector3& KRVector3::operator +=(const KRVector3& b) {
x += b.x;
y += b.y;
z += b.z;
return *this;
}
KRVector3& KRVector3::operator -=(const KRVector3& b) {
x -= b.x;
y -= b.y;
z -= b.z;
return *this;
}
KRVector3& KRVector3::operator *=(const float v) {
x *= v;
y *= v;
z *= v;
return *this;
}
KRVector3& KRVector3::operator /=(const float v) {
x /= v;
y /= v;
z /= v;
return *this;
}
bool KRVector3::operator ==(const KRVector3& b) const {
return x == b.x && y == b.y && z == b.z;
}
bool KRVector3::operator !=(const KRVector3& b) const {
return x != b.x || y != b.y || z != b.z;
}
float& KRVector3::operator[](unsigned i) {
switch(i) {
case 0:
return x;
case 1:
return y;
default:
case 2:
return z;
}
}
float KRVector3::operator[](unsigned i) const {
switch(i) {
case 0:
return x;
case 1:
return y;
case 2:
default:
return z;
}
}
float KRVector3::sqrMagnitude() const {
// calculate the square of the magnitude (useful for comparison of magnitudes without the cost of a sqrt() function)
return x * x + y * y + z * z;
}
//multiply this vector by a scalar
KRVector3 KRVector3::operator*(float num) const
{
return KRVector3(x * num, y * num, z * num);
float KRVector3::magnitude() const {
return sqrtf(x * x + y * y + z * z);
}
//pass in a vector, pass in a scalar, return the product
/*
KRVector3 KRVector3::operator*(float num, KRVector3 const &vec)
{
return KRVector3(vec.x * num, vec.y * num, vec.z * num);
}
*/
//add two vectors
KRVector3 KRVector3::operator+(const KRVector3 &vec) const
{
return KRVector3(x + vec.x, y + vec.y, z + vec.z);
}
//subtract two vectors
KRVector3 KRVector3::operator-(const KRVector3 &vec) const
{
return KRVector3(x - vec.x, y - vec.y, z - vec.z);
}
//normalize this vector
void KRVector3::normalize()
{
void KRVector3::normalize() {
float magnitude = sqrtf(x * x + y * y + z * z);
x /= magnitude;
y /= magnitude;
z /= magnitude;
}
KRVector3 KRVector3::Normalize(const KRVector3 &v) {
float magnitude = sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
return KRVector3(v.x / magnitude, v.y / magnitude, v.z / magnitude);
}
KRVector3 KRVector3::Cross(const KRVector3 &v1, const KRVector3 &v2) {
return KRVector3(v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x);
}
float KRVector3::Dot(const KRVector3 &v1, const KRVector3 &v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
//calculate and return dot product
float KRVector3::dot(const KRVector3 &vec) const
{
return x * vec.x + y * vec.y + z * vec.z;
}
//calculate and return cross product
KRVector3 KRVector3::cross(const KRVector3 &vec) const
{
return KRVector3(y * vec.z - z * vec.y,
z * vec.x - x * vec.z,
x * vec.y - y * vec.x);
}
bool operator== (KRVector3 &v1, KRVector3 &v2) {
return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;
}
bool operator!= (KRVector3 &v1, KRVector3 &v2) {
return !(v1 == v2);
}

View File

@@ -31,23 +31,50 @@
#ifndef KRVECTOR3
#define KRVECTOR3
#include <math.h>
#import "KREngine-common.h"
class KRVector3
{
class KRVector3 {
public:
float x, y, z;
//default constructor
KRVector3();
KRVector3(float X, float Y, float Z);
KRVector3(float v);
KRVector3();
KRVector3(const KRVector3 &v);
~KRVector3();
KRVector3& operator =(const KRVector3& b);
KRVector3 operator +(const KRVector3& b) const;
KRVector3 operator -(const KRVector3& b) const;
KRVector3 operator +() const;
KRVector3 operator -() const;
KRVector3 operator *(const float v) const;
KRVector3 operator /(const float v) const;
KRVector3& operator +=(const KRVector3& b);
KRVector3& operator -=(const KRVector3& b);
KRVector3& operator *=(const float v);
KRVector3& operator /=(const float v);
bool operator ==(const KRVector3& b) const;
bool operator !=(const KRVector3& b) const;
float& operator[](unsigned i);
float operator[](unsigned i) const;
float sqrMagnitude() const; // calculate the square of the magnitude (useful for comparison of magnitudes without the cost of a sqrt() function)
float magnitude() const;
void normalize();
static KRVector3 Normalize(const KRVector3 &v);
static KRVector3 Cross(const KRVector3 &v1, const KRVector3 &v2);
static float Dot(const KRVector3 &v1, const KRVector3 &v2);
static KRVector3 ZeroVector();
static KRVector3 OneVector();
static KRVector3 ForwardVector();
@@ -58,42 +85,7 @@ public:
static KRVector3 RightVector();
static KRVector3 Lerp(const KRVector3 &v1, const KRVector3 &v2, float d);
static KRVector3 Slerp(const KRVector3 &v1, const KRVector3 &v2, float d);
static float Dot(const KRVector3 &v1, const KRVector3 &v2);
static void OrthoNormalize(KRVector3 &normal, KRVector3 &tangent); // Gram-Schmidt Orthonormalization
KRVector3(const KRVector3& p);
KRVector3& operator = ( const KRVector3& p );
friend bool operator== (KRVector3 &v1, KRVector3 &v2);
friend bool operator!= (KRVector3 &v1, KRVector3 &v2);
// calculate and return the magnitude of this vector
float GetMagnitude();
// calculate the square of the magnitude (useful for comparison of magnitudes without the cost of a sqrt() function used in GetMagnitude
float GetSqrMagnitude();
//multiply this vector by a scalar
KRVector3 operator*(float num) const;
//pass in a vector, pass in a scalar, return the product
//friend KRVector3 operator*(float num, KRVector3 const &vec);
//add two vectors
KRVector3 operator+(const KRVector3 &vec) const;
//subtract two vectors
KRVector3 operator-(const KRVector3 &vec) const;
//normalize this vector
void normalize();
//calculate and return dot product
float dot(const KRVector3 &vec) const;
//calculate and return cross product
KRVector3 cross(const KRVector3 &vec) const;
};
#endif