changed text and math classes

This commit is contained in:
devZoGok
2021-10-19 19:06:41 +03:00
parent 8f49bd1125
commit 72a9a33e7d
8 changed files with 233 additions and 211 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ using namespace Assimp;
namespace vb01{
AssimpModelReader::AssimpModelReader(Model *model, string path) : ModelReader(model, path){
Importer importer;
const aiScene *scene = importer.ReadFile(path,aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenNormals | aiProcess_CalcTangentSpace);
const aiScene *scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenNormals | aiProcess_CalcTangentSpace);
if(!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode){
cout<<"Failed to load model:"<<importer.GetErrorString()<<endl;
exit(-1);
+4 -4
View File
@@ -1,8 +1,8 @@
#include"quaternion.h"
namespace vb01{
const Quaternion Quaternion::QUAT_W(1,0,0,0);
const Quaternion Quaternion::QUAT_I(0,1,0,0);
const Quaternion Quaternion::QUAT_J(0,0,1,0);
const Quaternion Quaternion::QUAT_K(0,0,0,1);
const Quaternion Quaternion::QUAT_W(1, 0, 0, 0);
const Quaternion Quaternion::QUAT_I(0, 1, 0, 0);
const Quaternion Quaternion::QUAT_J(0, 0, 1, 0);
const Quaternion Quaternion::QUAT_K(0, 0, 0, 1);
}
+35 -35
View File
@@ -7,55 +7,55 @@
namespace vb01{
class Quaternion{
public:
Quaternion(float w,float x,float y,float z){
this->w=w;
this->x=x;
this->y=y;
this->z=z;
Quaternion(float w, float x, float y, float z){
this->w = w;
this->x = x;
this->y = y;
this->z = z;
}
Quaternion(float angle, Vector3 axis){
*this=this->fromAngle(angle,axis);
*this = this->fromAngle(angle, axis);
}
Quaternion(){}
inline Quaternion operator- (){return Quaternion(w,-x,-y,-z);}
inline Quaternion operator- (){return Quaternion(w, -x, -y, -z);}
inline Quaternion 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;
float w=a1*a2-b1*b2-c1*c2-d1*d2;
float x=a1*b2+b1*a2+c1*d2-d1*c2;
float y=a1*c2-b1*d2+c1*a2+d1*b2;
float z=a1*d2+b1*c2-c1*b2+d1*a2;
return Quaternion(w,x,y,z);
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;
float w = a1 * a2 - b1 * b2 - c1 * c2 - d1 * d2;
float x = a1 * b2 + b1 * a2 + c1 * d2 - d1 * c2;
float y = a1 * c2 - b1 * d2 + c1 * a2 + d1 * b2;
float z = a1 * d2 + b1 * c2 - c1 * b2 + d1 * a2;
return Quaternion(w, x, y, z);
}
inline Vector3 operator* (Vector3 v){
Quaternion vQuat=(*this)*Quaternion(0,v.x,v.y,v.z)*recip();
return Vector3(vQuat.x,vQuat.y,vQuat.z);
Quaternion vQuat = (*this) * Quaternion(0, v.x, v.y, v.z) * recip();
return Vector3(vQuat.x, vQuat.y, vQuat.z);
}
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));
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<typename T> inline Quaternion operator*(T s){return Quaternion(w*s,x*s,y*s,z*s);}
template<typename T> inline Quaternion operator/(T s){return Quaternion(w/s,x/s,y/s,z/s);}
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<typename T> inline Quaternion operator*(T s){return Quaternion(w * s, x * s, y * s, z * s);}
template<typename T> 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)
*(*this)*Quaternion(0,0,1,0)+Quaternion(0,0,0,1)
*(*this)*Quaternion(0,0,0,1))*-.5;
return (*this + Quaternion(0, 1, 0, 0)
*(*this) * Quaternion(0, 1, 0, 0) + Quaternion(0, 0, 1, 0)
*(*this) * Quaternion(0, 0, 1, 0) + Quaternion(0, 0, 0, 1)
*(*this) * Quaternion(0, 0, 0, 1)) * -.5;
}
inline Quaternion norm(){return *this/getLength();}
inline Quaternion recip(){return conj()/getLengthSq();}
inline Quaternion invert(){return conj()/(getLengthSq());}
inline float getLengthSq(){return w*w+x*x+y*y+z*z;}
inline Quaternion norm(){return *this / getLength();}
inline Quaternion recip(){return conj() / getLengthSq();}
inline Quaternion invert(){return conj() / (getLengthSq());}
inline float getLengthSq(){return w * w + x * x + y * y + z * z;}
inline float getLength(){return std::sqrt(getLengthSq());}
float w,x,y,z;
const static Quaternion QUAT_I,QUAT_J,QUAT_K,QUAT_W;
float w, x, y, z;
const static Quaternion QUAT_I, QUAT_J, QUAT_K, QUAT_W;
private:
};
+45 -41
View File
@@ -6,59 +6,63 @@
using namespace std;
namespace vb01{
void retrieveCollisions(Vector3 rayPos, Vector3 rayDir,Node *node,std::vector<CollisionResult> &results, const float rayLength){
Vector3 pos=node->localToGlobalPosition(Vector3::VEC_ZERO);
Quaternion rot=node->localToGlobalOrientation(Quaternion::QUAT_W);
for(Mesh *m : node->getMeshes()){
const int numVerts=m->getNumVerts();
Mesh::Vertex *vertices=m->getVerts();
u32 *indices=m->getIndices();
void Ray::retrieveCollisions(Vector3 rayPos, Vector3 rayDir, Node *node, std::vector<CollisionResult> &results, const float rayLength){
castRay(rayPos, rayDir, node, results, rayLength);
for(Node *c : node->getChildren())
retrieveCollisions(rayPos, rayDir, c, results, rayLength);
}
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());
if(a1>PI/2){
a1=PI-a1;
perpVec=-perpVec;
void Ray::castRay(Vector3 rayPos, Vector3 rayDir, Node *node, vector<CollisionResult> &results, float rayLength){
Vector3 pos = node->localToGlobalPosition(Vector3::VEC_ZERO);
Quaternion rot = node->localToGlobalOrientation(Quaternion::QUAT_W);
for(Mesh *m : node->getMeshes()){
const int numVerts = m->getNumVerts();
Mesh::Vertex *vertices = m->getVerts();
u32 *indices = m->getIndices();
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());
if(a1 > PI / 2){
a1 = PI - a1;
perpVec = -perpVec;
}
float perpLine=hypVec.getLength()*cos(a1);
float a2=perpVec.norm().getAngleBetween(rayDir.norm());
if(a2<=PI/2){
float distance=perpLine/cos(a2);
if((distance<=rayLength&&rayLength!=.0)||rayLength==.0){
Vector3 contactPoint=rayPos+rayDir.norm()*distance;
float angleA=(pointB-pointA).norm().getAngleBetween((pointC-pointA).norm());
float angleB=(pointA-pointB).norm().getAngleBetween((pointC-pointB).norm());
float angleC=(pointB-pointC).norm().getAngleBetween((pointA-pointC).norm());
Vector3 bisecAVec=((pointB-pointA)+(pointC-pointA));
Vector3 bisecBVec=((pointA-pointB)+(pointC-pointB));
Vector3 bisecCVec=((pointB-pointC)+(pointA-pointC));
bool withinBisecA=(contactPoint-pointA).norm().getAngleBetween(bisecAVec.norm())<=angleA/2;
bool withinBisecB=(contactPoint-pointB).norm().getAngleBetween(bisecBVec.norm())<=angleB/2;
bool withinBisecC=(contactPoint-pointC).norm().getAngleBetween(bisecCVec.norm())<=angleC/2;
if(withinBisecA&&withinBisecB&&withinBisecC){
float perpLine = hypVec.getLength() * cos(a1);
float a2 = perpVec.norm().getAngleBetween(rayDir.norm());
if(a2 <= PI / 2){
float distance = perpLine / cos(a2);
if((distance <= rayLength && rayLength != .0) || rayLength == .0){
Vector3 contactPoint = rayPos + rayDir.norm() * distance;
float angleA = (pointB - pointA).norm().getAngleBetween((pointC - pointA).norm());
float angleB = (pointA - pointB).norm().getAngleBetween((pointC - pointB).norm());
float angleC = (pointB - pointC).norm().getAngleBetween((pointA - pointC).norm());
Vector3 bisecAVec = ((pointB - pointA) + (pointC - pointA));
Vector3 bisecBVec = ((pointA - pointB) + (pointC - pointB));
Vector3 bisecCVec = ((pointB - pointC) + (pointA - pointC));
bool withinBisecA = (contactPoint - pointA).norm().getAngleBetween(bisecAVec.norm()) <= angleA / 2;
bool withinBisecB = (contactPoint - pointB).norm().getAngleBetween(bisecBVec.norm()) <= angleB / 2;
bool withinBisecC = (contactPoint - pointC).norm().getAngleBetween(bisecCVec.norm()) <= angleC / 2;
if(withinBisecA && withinBisecB && withinBisecC){
CollisionResult result;
result.pos=contactPoint;
result.distance=distance;
result.mesh=m;
result.pos = contactPoint;
result.distance = distance;
result.mesh = m;
results.push_back(result);
}
}
}
}
}
for(Node *c : node->getChildren())
retrieveCollisions(rayPos,rayDir,c,results,rayLength);
}
void sortResults(std::vector<CollisionResult> &results){
void Ray::sortResults(std::vector<CollisionResult> &results){
if(!results.empty())
for(int i=0;i<results.size();i++){
for(int i2=i;i2<results.size();i2++)
if(results[i].distance>results[i2].distance)
swap(results[i],results[i2]);
for(int i = 0; i < results.size(); i++){
for(int i2 = i; i2 < results.size(); i2++)
if(results[i].distance > results[i2].distance)
swap(results[i], results[i2]);
}
}
}
+11 -6
View File
@@ -6,12 +6,17 @@ namespace vb01{
class Node;
class Mesh;
struct CollisionResult{
Vector3 pos;
float distance;
Mesh *mesh=nullptr;
class Ray{
public:
struct CollisionResult{
Vector3 pos;
float distance;
Mesh *mesh = nullptr;
};
static void retrieveCollisions(Vector3, Vector3, Node*, std::vector<CollisionResult>&, const float = .0);
private:
static void castRay(Vector3, Vector3, Node*, std::vector<CollisionResult>&, const float);
static void sortResults(std::vector<CollisionResult>&);
};
void retrieveCollisions(Vector3,Vector3,Node*,std::vector<CollisionResult>&,const float=.0);
void sortResults(std::vector<CollisionResult>&);
}
+77 -67
View File
@@ -12,103 +12,113 @@
using namespace std;
namespace vb01{
Text::Text(string fontPath,string entry){
string basePath="../../vb01/text.";
shader=new Shader(basePath+"vert",basePath+"frag");
FT_Library ft;
FT_Face face;
if(FT_Init_FreeType(&ft))
cout<<"Couldn't init Freetype\n";
if(FT_New_Face(ft,fontPath.c_str(),0,&face))
cout<<"Could not load font\n";
FT_Set_Pixel_Sizes(face,0,100);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
for(u32 i=0;i<256;i++){
if(FT_Load_Char(face,i,FT_LOAD_RENDER)){
cout<<"Could not load glyph\n";
continue;
}
Glyph c;
c.ch=i;
c.texture=new Texture(face,i);
c.size=Vector2(face->glyph->bitmap.width,face->glyph->bitmap.rows);
c.bearing=Vector2(face->glyph->bitmap_left,face->glyph->bitmap_top);
c.advance=face->glyph->advance.x;
characters.push_back(c);
}
FT_Done_Face(face);
FT_Done_FreeType(ft);
Text::Text(string fontPath, string entry){
initFont(fontPath);
setText(entry);
}
Text::~Text(){
delete shader;
glDeleteVertexArrays(1,&VAO);
glDeleteBuffers(1,&VBO);
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
}
void Text::initFont(string fontPath){
string basePath = "../../vb01/text.";
shader = new Shader(basePath + "vert", basePath + "frag");
FT_Library ft;
FT_Face face;
if(FT_Init_FreeType(&ft))
cout << "Couldn't init Freetype\n";
if(FT_New_Face(ft, fontPath.c_str(), 0, &face))
cout << "Could not load font\n";
FT_Set_Pixel_Sizes(face, 0, 100);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for(u32 i = 0; i < 256; i++){
if(FT_Load_Char(face, i, FT_LOAD_RENDER)){
cout << "Could not load glyph\n";
continue;
}
Glyph c;
c.ch = i;
c.texture = new Texture(face, i);
c.size = Vector2(face->glyph->bitmap.width, face->glyph->bitmap.rows);
c.bearing = Vector2(face->glyph->bitmap_left, face->glyph->bitmap_top);
c.advance = face->glyph->advance.x;
characters.push_back(c);
}
FT_Done_Face(face);
FT_Done_FreeType(ft);
}
void Text::update(){
Root *root=Root::getSingleton();
int width=root->getWidth(),height=root->getHeight();
Root *root = Root::getSingleton();
int width = root->getWidth(), height=root->getHeight();
shader->use();
shader->setVec4(color,"color");
shader->setVec2(Vector2(width,height),"screen");
shader->setVec3(node->getPosition(),"pos");
shader->setVec4(color, "color");
shader->setVec2(Vector2(width, height), "screen");
shader->setVec3(node->getPosition(), "pos");
Vector3 origin=node->getPosition();
for(int i=0;i<entry.length();i++){
renderGlyphs();
}
void Text::renderGlyphs(){
Vector3 origin = node->getPosition();
for(int i = 0; i < entry.length(); i++){
Glyph ch;
bool foundChar=false;
bool foundChar = false;
for(Glyph c : characters)
if(entry.c_str()[i]==c.ch){
ch=c;
foundChar=true;
if(entry.c_str()[i] == c.ch){
ch = c;
foundChar = true;
}
if(!foundChar)continue;
Vector2 size=ch.size*scale,bearing=ch.bearing*scale;
float data[]={
origin.x+bearing.x,origin.y-bearing.y,0,0,
origin.x+bearing.x+size.x,origin.y-bearing.y,1,0,
origin.x+bearing.x+size.x,origin.y-bearing.y+size.y,1,1,
if(!foundChar)
continue;
origin.x+bearing.x+size.x,origin.y-bearing.y+size.y,1,1,
origin.x+bearing.x,origin.y-bearing.y+size.y,0,1,
origin.x+bearing.x,origin.y-bearing.y,0,0
Vector2 size = ch.size * scale, bearing = ch.bearing * scale;
float data[] = {
origin.x + bearing.x, origin.y - bearing.y, 0, 0,
origin.x + bearing.x + size.x, origin.y - bearing.y, 1, 0,
origin.x + bearing.x + size.x, origin.y - bearing.y + size.y, 1, 1,
origin.x + bearing.x + size.x, origin.y - bearing.y + size.y, 1, 1,
origin.x + bearing.x, origin.y - bearing.y + size.y, 0, 1,
origin.x + bearing.x, origin.y - bearing.y, 0, 0
};
origin.x+=(ch.advance>>6)*scale;
origin.x += (ch.advance >> 6) * scale;
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferSubData(GL_ARRAY_BUFFER,0,sizeof(data),data);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(data), data);
ch.texture->select();
glDrawArrays(GL_TRIANGLES,0,6);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
}
void Text::setText(string text){
this->entry=text;
this->entry = text;
glGenVertexArrays(1,&VAO);
glGenBuffers(1,&VBO);
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(float)*6*4,NULL,GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,4,GL_FLOAT,GL_FALSE,4*sizeof(float),(void*)0);
glBindBuffer(GL_ARRAY_BUFFER,0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
float Text::getCharWidth(char c){
for(Glyph g : characters)
if(g.ch==c)
return g.size.x*scale;
if(g.ch == c)
return g.size.x * scale;
}
}
+14 -11
View File
@@ -15,30 +15,33 @@ namespace vb01{
public:
struct Glyph{
u32 ch;
Texture *texture=nullptr;
unsigned int advance;
Vector2 size,bearing;
Texture *texture = nullptr;
u32 advance;
Vector2 size, bearing;
};
Text(std::string,std::string);
Text(std::string, std::string);
~Text();
void update();
void setText(std::string);
float getCharWidth(char);
inline Node* getNode(){return node;}
inline void setNode(Node *node){this->node=node;}
inline void setScale(float s){this->scale=s;}
inline void setColor(Vector4 c){this->color=c;}
inline void setNode(Node *node){this->node = node;}
inline void setScale(float s){this->scale = s;}
inline void setColor(Vector4 c){this->color = c;}
inline int getLength(){return entry.length();}
inline std::string getText(){return entry;}
private:
Node *node=nullptr;
void initFont(std::string);
void renderGlyphs();
Node *node = nullptr;
std::string entry;
std::vector<Glyph> characters;
Shader *shader=nullptr;
Shader *shader = nullptr;
unsigned int VAO, VBO;
float scale=1;
Vector4 color=Vector4::VEC_IJKL;
float scale = 1;
Vector4 color = Vector4::VEC_IJKL;
};
}
+46 -46
View File
@@ -7,42 +7,42 @@
namespace vb01{
struct Vector4{
Vector4(){};
Vector4(float x,float y,float z,float w){
this->x=x;
this->y=y;
this->z=z;
this->w=w;
Vector4(float x, float y, float z, float w){
this->x = x;
this->y = y;
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);}
template<typename T>Vector4 operator+(T s){return Vector4(x+s,y+s,z+s,w+s);}
template<typename T>Vector4 operator-(T s){return Vector4(x-s,y-s,z-s,w-s);}
template<typename T>Vector4 operator*(T s){return Vector4(x*s,y*s,z*s,w*s);}
template<typename T>Vector4 operator/(T s){return Vector4(x/s,y/s,z/s,w/s);}
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);}
template<typename T>Vector4 operator+(T s){return Vector4(x + s, y + s, z + s, w + s);}
template<typename T>Vector4 operator-(T s){return Vector4(x - s, y - s, z - s, w - s);}
template<typename T>Vector4 operator*(T s){return Vector4(x * s, y * s, z * s, w * s);}
template<typename T>Vector4 operator/(T s){return Vector4(x / s, y / s, z / s, w / s);}
float x,y,z,w;
float x, y, z, w;
static const Vector4 VEC_IJKL, VEC_I, VEC_J, VEC_K, VEC_L, VEC_ZERO;
};
struct Vector3{
Vector3(){};
Vector3(float x,float y,float z){
this->x=x;
this->y=y;
this->z=z;
Vector3(float x, float y, float z){
this->x = x;
this->y = y;
this->z = z;
}
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);}
template<typename T>Vector3 operator+(T s){return Vector3(x+s,y+s,z+s);}
template<typename T>Vector3 operator-(T s){return Vector3(x-s,y-s,z-s);}
template<typename T>Vector3 operator*(T s){return Vector3(x*s,y*s,z*s);}
template<typename T>Vector3 operator/(T s){return Vector3(x/s,y/s,z/s);}
float getLengthSq(){return x*x+y*y+z*z;}
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);}
template<typename T>Vector3 operator+(T s){return Vector3(x + s, y + s, z + s);}
template<typename T>Vector3 operator-(T s){return Vector3(x - s, y - s, z - s);}
template<typename T>Vector3 operator*(T s){return Vector3(x * s, y * s, z * s);}
template<typename T>Vector3 operator/(T s){return Vector3(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;}
float dot(Vector3 v){return x * v.x + y * v.y + z * v.z;}
float getAngleBetween(Vector3 v){
float dotProd = dot(v);
if(dotProd > 1.0)
@@ -51,35 +51,35 @@ namespace vb01{
dotProd = -1;
return std::acos(dotProd);
}
float getDistanceFrom(Vector3 v){return (v-*this).getLength();}
Vector3 norm(){return (*this!=Vector3::VEC_ZERO?*this/getLength():Vector3::VEC_ZERO);}
float getDistanceFrom(Vector3 v){return (v - *this).getLength();}
Vector3 norm(){return (*this != Vector3::VEC_ZERO ? *this / getLength() : Vector3::VEC_ZERO);}
Vector3 cross(Vector3 v){return Vector3(y * v.z - v.y * z, z * v.x - x * v.z, x * v.y - v.x * y);}
float x,y,z;
static const Vector3 VEC_IJK, VEC_I,VEC_J,VEC_K,VEC_ZERO;
float x, y, z;
static const Vector3 VEC_IJK, VEC_I, VEC_J, VEC_K, VEC_ZERO;
};
struct Vector2{
Vector2(){};
Vector2(float x,float y){
this->x=x;
this->y=y;
Vector2(float x, float y){
this->x = x;
this->y = 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);}
template<typename T>Vector2 operator+(T s){return Vector2(x+s,y+s);}
template<typename T>Vector2 operator-(T s){return Vector2(x-s,y-s);}
template<typename T>Vector2 operator*(T s){return Vector2(x*s,y*s);}
template<typename T>Vector2 operator/(T s){return Vector2(x/s,y/s);}
float getLengthSq(){return x*x+y*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);}
template<typename T>Vector2 operator+(T s){return Vector2(x + s, y + s);}
template<typename T>Vector2 operator-(T s){return Vector2(x - s, y - s);}
template<typename T>Vector2 operator*(T s){return Vector2(x * s, y * s);}
template<typename T>Vector2 operator/(T s){return Vector2(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;}
float dot(Vector2 v){return x * v.x + y * v.y;}
float getAngleBetween(Vector2 v){return std::acos(dot(v));}
float getDistanceFrom(Vector2 v){return (v-*this).getLengthSq();}
Vector2 norm(){return *this/getLength();}
float getDistanceFrom(Vector2 v){return (v - *this).getLength();}
Vector2 norm(){return *this / getLength();}
float x,y;
static const Vector2 VEC_IJ, VEC_I,VEC_J,VEC_ZERO;
float x, y;
static const Vector2 VEC_IJ, VEC_I, VEC_J, VEC_ZERO;
};
}