Structs continued
parent
d57258ae1c
commit
1c275dbd78
|
@ -1,3 +1,19 @@
|
|||
// * This file is part of the COLOBOT source code
|
||||
// * Copyright (C) 2012, Polish Portal of Colobot (PPC)
|
||||
// *
|
||||
// * This program is free software: you can redistribute it and/or modify
|
||||
// * it under the terms of the GNU General Public License as published by
|
||||
// * the Free Software Foundation, either version 3 of the License, or
|
||||
// * (at your option) any later version.
|
||||
// *
|
||||
// * This program is distributed in the hope that it will be useful,
|
||||
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// * GNU General Public License for more details.
|
||||
// *
|
||||
// * You should have received a copy of the GNU General Public License
|
||||
// * along with this program. If not, see http://www.gnu.org/licenses/.
|
||||
|
||||
// math/const.h
|
||||
|
||||
/* Math constants */
|
||||
|
|
|
@ -1,3 +1,19 @@
|
|||
// * This file is part of the COLOBOT source code
|
||||
// * Copyright (C) 2012, Polish Portal of Colobot (PPC)
|
||||
// *
|
||||
// * This program is free software: you can redistribute it and/or modify
|
||||
// * it under the terms of the GNU General Public License as published by
|
||||
// * the Free Software Foundation, either version 3 of the License, or
|
||||
// * (at your option) any later version.
|
||||
// *
|
||||
// * This program is distributed in the hope that it will be useful,
|
||||
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// * GNU General Public License for more details.
|
||||
// *
|
||||
// * You should have received a copy of the GNU General Public License
|
||||
// * along with this program. If not, see http://www.gnu.org/licenses/.
|
||||
|
||||
// math/func.h
|
||||
|
||||
/* Common math functions */
|
||||
|
|
|
@ -1,3 +1,19 @@
|
|||
// * This file is part of the COLOBOT source code
|
||||
// * Copyright (C) 2012, Polish Portal of Colobot (PPC)
|
||||
// *
|
||||
// * This program is free software: you can redistribute it and/or modify
|
||||
// * it under the terms of the GNU General Public License as published by
|
||||
// * the Free Software Foundation, either version 3 of the License, or
|
||||
// * (at your option) any later version.
|
||||
// *
|
||||
// * This program is distributed in the hope that it will be useful,
|
||||
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// * GNU General Public License for more details.
|
||||
// *
|
||||
// * You should have received a copy of the GNU General Public License
|
||||
// * along with this program. If not, see http://www.gnu.org/licenses/.
|
||||
|
||||
// math/matrix.h
|
||||
|
||||
/* Matrix struct and functions */
|
||||
|
@ -8,13 +24,6 @@
|
|||
|
||||
#include <cmath>
|
||||
|
||||
/* TODO
|
||||
|
||||
void MatRotateXZY(D3DMATRIX &mat, D3DVECTOR angle);
|
||||
void MatRotateZXY(D3DMATRIX &mat, D3DVECTOR angle);
|
||||
|
||||
*/
|
||||
|
||||
// Math module namespace
|
||||
namespace Math
|
||||
{
|
||||
|
@ -56,7 +65,7 @@ struct Matrix
|
|||
inline void LoadIdentity()
|
||||
{
|
||||
LoadZero();
|
||||
m[0] = m[1][1] = m[2][2] = m[3][3] = 1.0f;
|
||||
m[0] = m[5] = m[10] = m[15] = 1.0f;
|
||||
}
|
||||
|
||||
//! Calculates the determinant of the matrix
|
||||
|
@ -79,7 +88,7 @@ struct Matrix
|
|||
{
|
||||
for (int c = 0; c < 4; ++c)
|
||||
{
|
||||
m[r][c] = temp.m[c][r];
|
||||
m[4*r+c] = temp.m[4*c+r];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -102,7 +111,7 @@ struct Matrix
|
|||
for (int c = 0; c < 4; ++c)
|
||||
{
|
||||
if (c == j) continue;
|
||||
tab[tabR][tabC] = m[r][c];
|
||||
tab[tabR][tabC] = m[4*r + c];
|
||||
++tabC;
|
||||
}
|
||||
++tabR;
|
||||
|
@ -149,11 +158,74 @@ struct Matrix
|
|||
m[r][c] = 0.0;
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
m[r][c] += left.m[r][i] * right.m[i][c];
|
||||
m[4*r+c] += left.m[4*r+i] * right.m[4*i+c];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void LoadViewMatrix(const Vector &from, const Vector &at, const Vector &up)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
inline void LoadProjectionMatrix(float fov = 1.570795f, float aspect = 1.0f,
|
||||
float nearPlane = 1.0f, float farPlane = 1000.0f)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
inline void LoadTranslateMatrix(const Vector &trans)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
inline void LoadScaleMatrix(const Vector &scale)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
inline void LoadRotateXMatrix(float angle)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
inline void LoadRotateYMatrix(float angle)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
inline void LoadRotateZMatrix(float angle)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
inline void LoadRotationMatrix(const Vector &dir, float angle)
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
//! Calculates the matrix to make three rotations in the order X, Z and Y
|
||||
inline void RotateXZY(const Vector &angle)
|
||||
{
|
||||
Matrix temp;
|
||||
temp.SetRotateXMatrix(angle.x);
|
||||
this->SetRotateZMatrix(angle.z);
|
||||
this->Multiply(temp);
|
||||
temp.SetRotateYMatrix(angle.y);
|
||||
this->Multiply(temp);
|
||||
}
|
||||
|
||||
//! Calculates the matrix to make three rotations in the order Z, X and Y
|
||||
inline void MatRotateZXY(const Vector &angle)
|
||||
{
|
||||
Matrix temp;
|
||||
temp.SetRotateZMatrix(angle.z);
|
||||
this->SetRotateXMatrix(angle.x);
|
||||
this->Multiply(temp);
|
||||
temp.SetRotateYMatrix(angle.y);
|
||||
this->Multiply(temp);
|
||||
}
|
||||
};
|
||||
|
||||
//! Convenience function for inverting a matrix
|
||||
|
|
|
@ -1,3 +1,23 @@
|
|||
// * This file is part of the COLOBOT source code
|
||||
// * Copyright (C) 2012, Polish Portal of Colobot (PPC)
|
||||
// *
|
||||
// * This program is free software: you can redistribute it and/or modify
|
||||
// * it under the terms of the GNU General Public License as published by
|
||||
// * the Free Software Foundation, either version 3 of the License, or
|
||||
// * (at your option) any later version.
|
||||
// *
|
||||
// * This program is distributed in the hope that it will be useful,
|
||||
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// * GNU General Public License for more details.
|
||||
// *
|
||||
// * You should have received a copy of the GNU General Public License
|
||||
// * along with this program. If not, see http://www.gnu.org/licenses/.
|
||||
|
||||
// math/point.h
|
||||
|
||||
/* Point struct and functions */
|
||||
|
||||
#pragma once
|
||||
|
||||
/* TODO
|
||||
|
|
|
@ -1,3 +1,19 @@
|
|||
// * This file is part of the COLOBOT source code
|
||||
// * Copyright (C) 2012, Polish Portal of Colobot (PPC)
|
||||
// *
|
||||
// * This program is free software: you can redistribute it and/or modify
|
||||
// * it under the terms of the GNU General Public License as published by
|
||||
// * the Free Software Foundation, either version 3 of the License, or
|
||||
// * (at your option) any later version.
|
||||
// *
|
||||
// * This program is distributed in the hope that it will be useful,
|
||||
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// * GNU General Public License for more details.
|
||||
// *
|
||||
// * You should have received a copy of the GNU General Public License
|
||||
// * along with this program. If not, see http://www.gnu.org/licenses/.
|
||||
|
||||
// math/vector.h
|
||||
|
||||
/* Vector struct and functions */
|
||||
|
@ -11,13 +27,8 @@
|
|||
/*
|
||||
TODO
|
||||
|
||||
HRESULT D3DMath_VectorMatrixMultiply( D3DVECTOR& vDest, D3DVECTOR& vSrc, D3DMATRIX& mat)
|
||||
|
||||
float Length(const D3DVECTOR &a, const D3DVECTOR &b);
|
||||
float Length2d(const D3DVECTOR &a, const D3DVECTOR &b);
|
||||
float Angle( D3DVECTOR u, D3DVECTOR v );
|
||||
D3DVECTOR Cross( D3DVECTOR u, D3DVECTOR v );
|
||||
D3DVECTOR ComputeNormal( D3DVECTOR p1, D3DVECTOR p2, D3DVECTOR p3 );
|
||||
D3DVECTOR Transform(const D3DMATRIX &m, D3DVECTOR p);
|
||||
D3DVECTOR Projection(const D3DVECTOR &a, const D3DVECTOR &b, const D3DVECTOR &p);
|
||||
|
||||
|
@ -41,9 +52,9 @@ BOOL IsSamePlane(D3DVECTOR *plan1, D3DVECTOR *plan2);
|
|||
namespace Math
|
||||
{
|
||||
|
||||
/** 4x1 Vector
|
||||
/** 3x1 Vector
|
||||
|
||||
Represents an universal 4x1 vector that can be used in OpenGL and DirectX engines.
|
||||
Represents an universal 3x1 vector that can be used in OpenGL and DirectX engines.
|
||||
Contains the required methods for operating on vectors.
|
||||
|
||||
All methods are made inline to maximize optimization.
|
||||
|
@ -57,63 +68,86 @@ struct Vector
|
|||
float y;
|
||||
//! Z - 3rd coord
|
||||
float z;
|
||||
//! W - 4th coord
|
||||
float w;
|
||||
|
||||
//! Creates an identity vector (0, 0, 0, 1)
|
||||
Vector()
|
||||
//! Creates a zero vector (0, 0, 0)
|
||||
inline Vector()
|
||||
{
|
||||
LoadIdentity();
|
||||
LoadZero();
|
||||
}
|
||||
|
||||
//! Creates a vector from given values
|
||||
Vector(float x, float y, float z, float w = 1.0f)
|
||||
inline Vector(float x, float y, float z)
|
||||
{
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
this->w = w;
|
||||
}
|
||||
|
||||
//! Loads the identity vector (0, 0, 0, 1)
|
||||
void LoadIdentity()
|
||||
{
|
||||
x = y = z = 0.0f;
|
||||
w = 1.0f;
|
||||
}
|
||||
|
||||
//! Loads the zero vector (0, 0, 0, 0)
|
||||
void LoadZero()
|
||||
inline void LoadZero()
|
||||
{
|
||||
x = y = z = w = 0.0f;
|
||||
}
|
||||
|
||||
//! Returns the vector length
|
||||
float Length() const
|
||||
inline float Length() const
|
||||
{
|
||||
return sqrt(x*x + y*y + z*z + w*w);
|
||||
}
|
||||
|
||||
//! Normalizes the vector
|
||||
void Normalize()
|
||||
inline void Normalize()
|
||||
{
|
||||
|
||||
float l = Length();
|
||||
x /= l;
|
||||
y /= l;
|
||||
z /= l;
|
||||
w /= l;
|
||||
}
|
||||
|
||||
const Vector3D& operator-();
|
||||
const Vector3D& operator+=(const Vector3D &vector);
|
||||
const Vector3D& operator-=(const Vector3D &vector);
|
||||
const Vector3D& operator*=(double value);
|
||||
const Vector3D& operator/=(double value);
|
||||
|
||||
friend Vector3D operator+(const Vector3D &left, const Vector3D &right);
|
||||
friend Vector3D operator-(const Vector3D &left, const Vector3D &right);
|
||||
friend Vector3D operator*(double left, const Vector3D &right);
|
||||
friend Vector3D operator*(const Vector3D &left, double right);
|
||||
friend Vector3D operator/(const Vector3D &left, double right);
|
||||
|
||||
friend Vector3D crossProduct(const Vector3D &left, const Vector3D &right);
|
||||
friend double dotProduct(const Vector3D &left, const Vector3D &right);
|
||||
friend double cosAngle(const Vector3D &vector1, const Vector3D &vector2);
|
||||
friend double angle(const Vector3D &vector1, const Vector3D &vector2);
|
||||
};
|
||||
//! Calculates the cross product with another vector
|
||||
/** \a right right-hand side vector */
|
||||
inline void Cross(const Vector &right)
|
||||
{
|
||||
Vector left = *this;
|
||||
x = left.y * right.z - left.z * right.y;
|
||||
y = left.z * right.x - left.x * right.z;
|
||||
z = left.x * right.y - left.y * right.x;
|
||||
}
|
||||
|
||||
//! Calculates the dot product with another vector
|
||||
inline float Dot(const Vector &right) const
|
||||
{
|
||||
return x * right.x + y * right.y + z * right.z;
|
||||
}
|
||||
|
||||
//! Returns the cosine of angle between this and another vector
|
||||
inline float CosAngle(const Vector &right) const
|
||||
{
|
||||
return Dot(right) / (Length() * right.Length());
|
||||
}
|
||||
|
||||
//! Returns angle (in radians) between this and another vector
|
||||
inline float Angle(const Vector &right) const
|
||||
{
|
||||
return acos(CosAngle(right));
|
||||
}
|
||||
|
||||
//! Calculates the result of multiplying m * this vector
|
||||
inline MatrixMultiply(const Matrix &m)
|
||||
{
|
||||
float tx = x * m.m[0 ] + y * m.m[4 ] + z * m.m[8 ] + m.m[12];
|
||||
float ty = x * m.m[1 ] + y * m.m[5 ] + z * m.m[9 ] + m.m[13];
|
||||
float tz = x * m.m[2 ] + y * m.m[6 ] + z * m.m[10] + m.m[14];
|
||||
float tw = x * m.m[4 ] + y * m.m[7 ] + z * m.m[11] + m.m[15];
|
||||
|
||||
if (IsZero(tw))
|
||||
return;
|
||||
|
||||
x = tx / tw;
|
||||
y = ty / tw;
|
||||
z = tz / tw;
|
||||
}
|
||||
};
|
||||
|
||||
}; // namespace Math
|
||||
|
|
Loading…
Reference in New Issue