Port Math to C, remove stuff from OpenTK we don't use.

This commit is contained in:
UnknownShadow200 2017-04-29 15:15:32 +10:00
parent 3e8de6a371
commit 02513b971e
9 changed files with 271 additions and 136 deletions

View File

@ -60,128 +60,6 @@ namespace OpenTK {
W = w;
}
public Vector4(Vector2 v) {
X = v.X;
Y = v.Y;
Z = 0.0f;
W = 0.0f;
}
public Vector4(Vector3 v) {
X = v.X;
Y = v.Y;
Z = v.Z;
W = 0.0f;
}
public Vector4(Vector3 v, float w) {
X = v.X;
Y = v.Y;
Z = v.Z;
W = w;
}
public Vector4(Vector4 v) {
X = v.X;
Y = v.Y;
Z = v.Z;
W = v.W;
}
public float Length {
get { return (float)Math.Sqrt(X * X + Y * Y + Z * Z + W * W); }
}
public float LengthSquared {
get { return X * X + Y * Y + Z * Z + W * W; }
}
public static void Add(ref Vector4 a, ref Vector4 b, out Vector4 result) {
result = new Vector4(a.X + b.X, a.Y + b.Y, a.Z + b.Z, a.W + b.W);
}
public static void Subtract(ref Vector4 a, ref Vector4 b, out Vector4 result) {
result = new Vector4(a.X - b.X, a.Y - b.Y, a.Z - b.Z, a.W - b.W);
}
public static void Multiply(ref Vector4 vector, float scale, out Vector4 result) {
result = new Vector4(vector.X * scale, vector.Y * scale, vector.Z * scale, vector.W * scale);
}
public static void Multiply(ref Vector4 vector, ref Vector4 scale, out Vector4 result) {
result = new Vector4(vector.X * scale.X, vector.Y * scale.Y, vector.Z * scale.Z, vector.W * scale.W);
}
public static void Divide(ref Vector4 vector, float scale, out Vector4 result) {
Multiply(ref vector, 1 / scale, out result);
}
public static void Divide(ref Vector4 vector, ref Vector4 scale, out Vector4 result) {
result = new Vector4(vector.X / scale.X, vector.Y / scale.Y, vector.Z / scale.Z, vector.W / scale.W);
}
public static Vector4 Transform(Vector4 vec, Matrix4 mat) {
Vector4 result;
Transform(ref vec, ref mat, out result);
return result;
}
public static void Transform(ref Vector4 vec, ref Matrix4 mat, out Vector4 result) {
result = new Vector4(
vec.X * mat.Row0.X + vec.Y * mat.Row1.X + vec.Z * mat.Row2.X + vec.W * mat.Row3.X,
vec.X * mat.Row0.Y + vec.Y * mat.Row1.Y + vec.Z * mat.Row2.Y + vec.W * mat.Row3.Y,
vec.X * mat.Row0.Z + vec.Y * mat.Row1.Z + vec.Z * mat.Row2.Z + vec.W * mat.Row3.Z,
vec.X * mat.Row0.W + vec.Y * mat.Row1.W + vec.Z * mat.Row2.W + vec.W * mat.Row3.W);
}
public static Vector4 operator + (Vector4 left, Vector4 right) {
left.X += right.X;
left.Y += right.Y;
left.Z += right.Z;
left.W += right.W;
return left;
}
public static Vector4 operator - (Vector4 left, Vector4 right) {
left.X -= right.X;
left.Y -= right.Y;
left.Z -= right.Z;
left.W -= right.W;
return left;
}
public static Vector4 operator - (Vector4 vec) {
vec.X = -vec.X;
vec.Y = -vec.Y;
vec.Z = -vec.Z;
vec.W = -vec.W;
return vec;
}
public static Vector4 operator * (Vector4 vec, float scale) {
vec.X *= scale;
vec.Y *= scale;
vec.Z *= scale;
vec.W *= scale;
return vec;
}
public static Vector4 operator * (Vector4 vec, Vector4 scale) {
vec.X *= scale.X;
vec.Y *= scale.Y;
vec.Z *= scale.Z;
vec.W *= scale.W;
return vec;
}
public static Vector4 operator / (Vector4 vec, float scale) {
float mult = 1.0f / scale;
vec.X *= mult;
vec.Y *= mult;
vec.Z *= mult;
vec.W *= mult;
return vec;
}
public static bool operator == (Vector4 left, Vector4 right) {
return left.Equals(right);

View File

@ -127,21 +127,19 @@ String Block_DefaultName(BlockID block) {
return buffer.ToString();
}
static void SplitUppercase(StringBuffer buffer, int start, int end) {
int index = 0;
for (int i = start; i < end; i++) {
char c = Block.Names[i];
bool upper = Char.IsUpper(c) && i > start;
bool nextLower = i < end - 1 && !Char.IsUpper(Block.Names[i + 1]);
static void SplitUppercase(StringBuffer buffer, int start, int end) {
int index = 0;
for (int i = start; i < end; i++) {
char c = Block.Names[i];
bool upper = Char.IsUpper(c) && i > start;
bool nextLower = i < end - 1 && !Char.IsUpper(Block.Names[i + 1]);
if (upper && nextLower) {
buffer.Append(ref index, ' ');
buffer.Append(ref index, Char.ToLower(c));
}
else {
buffer.Append(ref index, c);
}
}
if (upper && nextLower) {
buffer.Append(ref index, ' ');
buffer.Append(ref index, Char.ToLower(c));
}
else {
buffer.Append(ref index, c);
}
}
}

View File

@ -175,21 +175,26 @@
<ClInclude Include="DefaultSet.h" />
<ClInclude Include="FastColour.h" />
<ClInclude Include="Funcs.h" />
<ClInclude Include="Matrix.h" />
<ClInclude Include="Noise.h" />
<ClInclude Include="Platform.h" />
<ClInclude Include="Random.h" />
<ClInclude Include="NotchyGenerator.h" />
<ClInclude Include="String.h" />
<ClInclude Include="Typedefs.h" />
<ClInclude Include="Vector3.h" />
<ClInclude Include="Vector4.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Block.c" />
<ClCompile Include="Compiler.c" />
<ClCompile Include="DefaultSet.c" />
<ClCompile Include="Matrix.c" />
<ClCompile Include="Noise.c" />
<ClCompile Include="NotchyGenerator.c" />
<ClCompile Include="Random.c" />
<ClCompile Include="String.c" />
<ClCompile Include="Vector3.c" />
<ClCompile Include="WinPlatform.c" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -46,6 +46,12 @@
<Filter Include="Header Files\2D\Utils">
<UniqueIdentifier>{7d28399b-8919-4015-b261-cfd8d18a7367}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\Math">
<UniqueIdentifier>{7a7adf1b-3c7f-4c3b-833b-656e2e61a2a2}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Math">
<UniqueIdentifier>{eb2aab2d-07f1-4d52-b13f-7ef7d40dcc70}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="NotchyGenerator.h">
@ -84,6 +90,15 @@
<ClInclude Include="Block.h">
<Filter>Header Files\Blocks</Filter>
</ClInclude>
<ClInclude Include="Vector3.h">
<Filter>Header Files\Math</Filter>
</ClInclude>
<ClInclude Include="Vector4.h">
<Filter>Header Files\Math</Filter>
</ClInclude>
<ClInclude Include="Matrix.h">
<Filter>Header Files\Math</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="NotchyGenerator.c">
@ -110,5 +125,11 @@
<ClCompile Include="Block.c">
<Filter>Source Files\Blocks</Filter>
</ClCompile>
<ClCompile Include="Vector3.c">
<Filter>Source Files\Math</Filter>
</ClCompile>
<ClCompile Include="Matrix.c">
<Filter>Source Files\Math</Filter>
</ClCompile>
</ItemGroup>
</Project>

0
src/Client/Matrix.c Normal file
View File

20
src/Client/Matrix.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef CS_MATRIX_H
#define CS_MATRIX_H
#include "Typedefs.h"
#include "Vector4.h"
/* Represents a 4 by 4 matrix.
Copyright 2017 ClassicalSharp | Licensed under BSD-3
*/
typedef struct Matrix {
/* Top row of the matrix */
Vector4 Row0;
/* 2nd row of the matrix */
Vector4 Row1;
/* 3rd row of the matrix */
Vector4 Row2;
/* Bottom row of the matrix */
Vector4 Row3;
} Matrix;
#endif

100
src/Client/Vector3.c Normal file
View File

@ -0,0 +1,100 @@
#include "Vector3.h"
Vector3 Vector3_Create3(Real32 x, Real32 y, Real32 z) {
Vector3 v; v.X = x; v.Y = y; v.Z = z; return v;
}
Vector3 Vector3_Create1(Real32 value) {
Vector3 v; v.X = value; v.Y = value; v.Z = value; return v;
}
/* Returns the length of the given vector. */
Real32 Vector3_Length(Vector3* v) {
Real32 lenSquared = v->X * v->X + v->Y * v->Y + v->Z * v->Z;
return Math_Sqrt(lenSquared);
}
/* Returns the squared length of the given vector. */
Real32 Vector3_LengthSquared(Vector3* v) {
return v->X * v->X + v->Y * v->Y + v->Z * v->Z;
}
void Vector3_Add(Vector3* a, Vector3* b, Vector3* result) {
result->X = a->X + b->X;
result->Y = a->Y + b->Y;
result->Z = a->Z + b->Z;
}
void Vector3_Subtract(Vector3* a, Vector3* b, Vector3* result) {
result->X = a->X - b->X;
result->Y = a->Y - b->Y;
result->Z = a->Z - b->Z;
}
void Vector3_Multiply(Vector3* a, Real32 scale, Vector3* result) {
result->X = a->X * scale;
result->Y = a->Y * scale;
result->Z = a->Z * scale;
}
void Vector3_Multiply(Vector3* a, Vector3* scale, Vector3* result) {
result->X = a->X * scale->X;
result->Y = a->Y * scale->Y;
result->Z = a->Z * scale->Z;
}
void Vector3_Divide(Vector3* a, Real32 scale, Vector3* result) {
Vector3_Multiply(a, 1.0f / scale, result);
}
void Vector3_Divide(Vector3* a, Vector3* scale, Vector3* result) {
result->X = a->X / scale->X;
result->Y = a->Y / scale->Y;
result->Z = a->Z / scale->Z;
}
void Vector3_Lerp(Vector3* a, Vector3* b, float blend, Vector3* result) {
result->X = blend * (b->X - a->X) + a->X;
result->Y = blend * (b->Y - a->Y) + a->Y;
result->Z = blend * (b->Z - a->Z) + a->Z;
}
Real32 Vector3_Dot(Vector3* a, Vector3* b) {
return a->X * b->X + a->Y * b->Y + a->Z * b->Z;
}
void Vector3_Cross(Vector3* a, Vector3* b, Vector3* result) {
result->X = a->Y * b->Z - a->Z * b->Y;
result->Y = a->Z * b->X - a->X * b->Z;
result->Z = a->X * b->Y - a->Y * b->X;
}
void Vector3_Normalize(Vector3* a, Vector3* result) {
float scale = 1.0f / Vector3_Length(a);
result->X = a->X * scale;
result->Y = a->Y * scale;
result->Z = a->Z * scale;
}
void Transform(Vector3* a, Matrix* mat, Vector3* result) {
result->X = a->X * mat->Row0.X + a->Y * mat->Row1.X + a->Z * mat->Row2.X + mat->Row3.X;
result->Y = a->X * mat->Row0.Y + a->Y * mat->Row1.Y + a->Z * mat->Row2.Y + mat->Row3.Y;
result->Z = a->X * mat->Row0.Z + a->Y * mat->Row1.Z + a->Z * mat->Row2.Z + mat->Row3.Z;
}
void TransformY(Real32 y, Matrix* mat, Vector3* result) {
result->X = y * mat->Row1.X + mat->Row3.X;
result->Y = y * mat->Row1.Y + mat->Row3.Y;
result->Z = y * mat->Row1.Z + mat->Row3.Z;;
}
bool Vector3_Equals(Vector3* a, Vector3* b) {
return a->X == b->X && a->Y == b->Y && a->Z == b->Z;
}
bool Vector3_NotEquals(Vector3* a, Vector3* b) {
return a->X != b->X || a->Y != b->Y || a->Z != b->Z;
}

82
src/Client/Vector3.h Normal file
View File

@ -0,0 +1,82 @@
#ifndef CS_VECTOR3_H
#define CS_VECTOR3_H
#include "Typedefs.h"
#include "Matrix.h"
/* Represents a 3 dimensional vector.
Copyright 2017 ClassicalSharp | Licensed under BSD-3
*/
typedef struct Vector3 {
Real32 X, Y, Z;
} Vector3;
/* Constructs a vector from the given coordinates. */
Vector3 Vector3_Create3(Real32 x, Real32 y, Real32 z);
/* Constructs a vector with X, Y and Z set to given value. */
Vector3 Vector3_Create1(Real32 value);
/* Returns the length of the given vector. */
Real32 Vector3_Length(Vector3* v);
/* Returns the squared length of the given vector. */
Real32 Vector3_LengthSquared(Vector3* v);
#define Vector3_UnitX Vector3_Create3(1.0f, 0.0f, 0.0f)
#define Vector3_UnitY Vector3_Create3(0.0f, 1.0f, 0.0f)
#define Vector3_UnitZ Vector3_Create3(0.0f, 0.0f, 1.0f)
#define Vector3_Zero Vector3_Create3(0.0f, 0.0f, 0.0f)
#define Vector3_One Vector3_Create3(1.0f, 1.0f, 1.0f)
/* Adds a and b. */
void Vector3_Add(Vector3* a, Vector3* b, Vector3* result);
/* Subtracts b from a. */
void Vector3_Subtract(Vector3* a, Vector3* b, Vector3* result);
/* Multiplies all components of a by scale. */
void Vector3_Multiply(Vector3* a, Real32 scale, Vector3* result);
/* Multiplies components of a by scale. */
void Vector3_Multiply(Vector3* a, Vector3* scale, Vector3* result);
/* Divides all components of a by scale. */
void Vector3_Divide(Vector3* a, Real32 scale, Vector3* result);
/* Divides components of a by scale. */
void Vector3_Divide(Vector3* a, Vector3* scale, Vector3* result);
/* Linearly interpolates between two vectors. */
void Vector3_Lerp(Vector3* a, Vector3* b, float blend, Vector3* result);
/* Calculates the dot product of two vectors. */
Real32 Vector3_Dot(Vector3* left, Vector3* right);
/* Calculates the cross product of two vectors. */
void Vector3_Cross(Vector3* a, Vector3* b, Vector3* result);
/* Calculates the normal of a vector. */
void Vector3_Normalize(Vector3* a, Vector3* result);
/* Transforms a vector by the given matrix. */
void Transform(Vector3* a, Matrix* mat, Vector3* result);
/* Transforms the Y component of a vector by the given matrix. */
void TransformY(Real32 y, Matrix* mat, Vector3* result);
/* Returns whether the two vectors are exact same on all axes. */
bool Vector3_Equals(Vector3* a, Vector3* b);
/* Returns whether the two vectors are different on any axis. */
bool Vector3_NotEquals(Vector3* a, Vector3* b);
#endif

31
src/Client/Vector4.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef CS_VECTOR4_H
#define CS_VECTOR4_H
#include "Typedefs.h"
/* Represents a 4 dimensional vector.
Copyright 2017 ClassicalSharp | Licensed under BSD-3
*/
typedef struct Vector4 {
Real32 X, Y, Z, W;
} Vector4;
/* Constructs a vector from the given coordinates. */
Vector4 Vector4_Create4(Real32 x, Real32 y, Real32 z, Real32 w) {
Vector4 v; v.X = x; v.Y = y; v.Z = z; v.W = w; return v;
}
#define Vector4_UnitX Vector4_Create4(1.0f, 0.0f, 0.0f, 0.0f)
#define Vector4_UnitY Vector4_Create4(0.0f, 1.0f, 0.0f, 0.0f)
#define Vector4_UnitZ Vector4_Create4(0.0f, 0.0f, 1.0f, 0.0f)
#define Vector4_UnitW Vector4_Create4(0.0f, 0.0f, 1.0f, 0.0f)
#define Vector4_Zero Vector4_Create4(0.0f, 0.0f, 0.0f, 0.0f)
#define Vector4_One Vector4_Create4(1.0f, 1.0f, 1.0f, 1.0f)
#endif