123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- unit GLTypes;
- interface
- uses
- WebGL, JS, Math, SysUtils;
- type
- TScalar = single;
- type
- TVec2 = record
- x, y: TScalar;
- end;
- type
- TVec3 = record
- x, y, z: TScalar;
- end;
- type
- TRGBAb = array of GLubyte;
- TRGBAf = array of TScalar;
- { Vec3 }
- function V3(x, y, z: TScalar): TVec3;
- function ToFloats(v: TVec3): TJSFloat32List; overload;
- function VecStr(v: TVec3): string; overload;
- procedure Show (v: TVec3);
- // math functions since we don't have advanced records
- function Add (v: TVec3; amount: TScalar): TVec3; overload;
- function Add (v: TVec3; amount: TVec3): TVec3; overload;
- function Subtract (v: TVec3; amount: TScalar): TVec3; overload;
- function Subtract (v: TVec3; amount: TVec3): TVec3; overload;
- function Multiply (v: TVec3; amount: TScalar): TVec3; overload;
- function Multiply (v: TVec3; amount: TVec3): TVec3; overload;
- function Divide (v: TVec3; amount: TScalar): TVec3; overload;
- function Divide (v: TVec3; amount: TVec3): TVec3; overload;
- function Sum (v: TVec3): TScalar; overload;
- function Magnitude(v: TVec3): TScalar; overload;
- function SquaredLength (v: TVec3): TScalar; overload;
- function Normalize (v: TVec3): TVec3; overload;
- function Dot (v: TVec3; point: TVec3): TScalar; overload;
- function Cross (v: TVec3; point: TVec3): TVec3; overload;
- { Vec2 }
- function V2(x, y: TScalar): TVec2;
- function ToFloats(v: TVec2): TJSFloat32List; overload;
- function RGBAb(r, g, b, a: GLubyte): TRGBAb;
- function RGBAf(r, g, b, a: TScalar): TRGBAf;
- implementation
- {=============================================}
- {@! ___Vec2___ }
- {=============================================}
- function V2(x, y: TScalar): TVec2;
- begin
- result.x := x;
- result.y := y;
- end;
- function ToFloats(v: TVec2): TJSFloat32List;
- begin
- SetLength(result, 2);
- result[0] := v.x;
- result[1] := v.y;
- end;
- {=============================================}
- {@! ___Vec3___ }
- {=============================================}
- function V3(x, y, z: TScalar): TVec3;
- begin
- result.x := x;
- result.y := y;
- result.z := z;
- end;
- function Add (v: TVec3; amount: TScalar): TVec3;
- begin
- result := V3(v.x + amount, v.y + amount, v.z + amount);
- end;
- function Add (v: TVec3; amount: TVec3): TVec3;
- begin
- result := V3(v.x + amount.x, v.y + amount.y, v.z + amount.z);
- end;
- function Subtract (v: TVec3; amount: TScalar): TVec3;
- begin
- result := V3(v.x - amount, v.y - amount, v.z - amount);
- end;
- function Subtract (v: TVec3; amount: TVec3): TVec3;
- begin
- result := V3(v.x - amount.x, v.y - amount.y, v.z - amount.z);
- end;
- function Multiply (v: TVec3; amount: TScalar): TVec3;
- begin
- result := V3(v.x * amount, v.y * amount, v.z * amount);
- end;
- function Multiply (v: TVec3; amount: TVec3): TVec3;
- begin
- result := V3(v.x * amount.x, v.y * amount.y, v.z * amount.z);
- end;
- function Divide (v: TVec3; amount: TScalar): TVec3;
- begin
- result := V3(v.x / amount, v.y / amount, v.z / amount);
- end;
- function Divide (v: TVec3; amount: TVec3): TVec3;
- begin
- result := V3(v.x / amount.x, v.y / amount.y, v.z / amount.z);
- end;
- function Sum (v: TVec3): TScalar;
- begin
- result := v.x + v.y + v.z;
- end;
- function Magnitude(v: TVec3): TScalar;
- begin
- result := Sqrt(Power(v.x, 2) + Power(v.y, 2) + Power(v.z, 2));
- end;
- function SquaredLength (v: TVec3): TScalar;
- begin
- result := Sqr(v.x) + Sqr(v.y) + Sqr(v.z);
- end;
- function Normalize (v: TVec3): TVec3;
- begin
- result := Divide(v, Magnitude(v));
- end;
- function Dot (v: TVec3; point: TVec3): TScalar;
- begin
- result := (v.x * point.x) + (v.y * point.y) + (v.z * point.z);
- end;
- function Cross (v: TVec3; point: TVec3): TVec3;
- begin
- result.x := (v.y * point.z) - (v.z * point.y);
- result.y := (v.z * point.x) - (v.x * point.z);
- result.z := (v.x * point.y) - (v.y * point.x);
- end;
- function ToFloats(v: TVec3): TJSFloat32List;
- begin
- SetLength(result, 3);
- result[0] := v.x;
- result[1] := v.y;
- result[2] := v.z;
- end;
- function VecStr(v: TVec3): string;
- begin
- result := '{'+FloatToStr(v.x)+','+FloatToStr(v.y)+','+FloatToStr(v.z)+'}';
- end;
- procedure Show (v: TVec3);
- begin
- writeln('{',v.x,',',v.y,',',v.z,'}');
- end;
- {=============================================}
- {@! ___Colors___ }
- {=============================================}
- function RGBAb(r, g, b, a: GLubyte): TRGBAb;
- begin
- result[0] := r;
- result[1] := g;
- result[2] := b;
- result[3] := a;
- end;
- function RGBAf(r, g, b, a: GLfloat): TRGBAf;
- begin
- result[0] := r;
- result[1] := g;
- result[2] := b;
- result[3] := a;
- end;
- end.
|