2
0

GLTypes.pas 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. unit GLTypes;
  2. interface
  3. uses
  4. WebGL, JS, Math, SysUtils;
  5. type
  6. TScalar = single;
  7. type
  8. TVec2 = record
  9. x, y: TScalar;
  10. end;
  11. type
  12. TVec3 = record
  13. x, y, z: TScalar;
  14. end;
  15. type
  16. TRGBAb = array of GLubyte;
  17. TRGBAf = array of TScalar;
  18. { Vec3 }
  19. function V3(x, y, z: TScalar): TVec3;
  20. function ToFloats(v: TVec3): TJSFloat32List; overload;
  21. function VecStr(v: TVec3): string; overload;
  22. procedure Show (v: TVec3);
  23. // math functions since we don't have advanced records
  24. function Add (v: TVec3; amount: TScalar): TVec3; overload;
  25. function Add (v: TVec3; amount: TVec3): TVec3; overload;
  26. function Subtract (v: TVec3; amount: TScalar): TVec3; overload;
  27. function Subtract (v: TVec3; amount: TVec3): TVec3; overload;
  28. function Multiply (v: TVec3; amount: TScalar): TVec3; overload;
  29. function Multiply (v: TVec3; amount: TVec3): TVec3; overload;
  30. function Divide (v: TVec3; amount: TScalar): TVec3; overload;
  31. function Divide (v: TVec3; amount: TVec3): TVec3; overload;
  32. function Sum (v: TVec3): TScalar; overload;
  33. function Magnitude(v: TVec3): TScalar; overload;
  34. function SquaredLength (v: TVec3): TScalar; overload;
  35. function Normalize (v: TVec3): TVec3; overload;
  36. function Dot (v: TVec3; point: TVec3): TScalar; overload;
  37. function Cross (v: TVec3; point: TVec3): TVec3; overload;
  38. { Vec2 }
  39. function V2(x, y: TScalar): TVec2;
  40. function ToFloats(v: TVec2): TJSFloat32List; overload;
  41. function RGBAb(r, g, b, a: GLubyte): TRGBAb;
  42. function RGBAf(r, g, b, a: TScalar): TRGBAf;
  43. implementation
  44. {=============================================}
  45. {@! ___Vec2___ }
  46. {=============================================}
  47. function V2(x, y: TScalar): TVec2;
  48. begin
  49. result.x := x;
  50. result.y := y;
  51. end;
  52. function ToFloats(v: TVec2): TJSFloat32List;
  53. begin
  54. SetLength(result, 2);
  55. result[0] := v.x;
  56. result[1] := v.y;
  57. end;
  58. {=============================================}
  59. {@! ___Vec3___ }
  60. {=============================================}
  61. function V3(x, y, z: TScalar): TVec3;
  62. begin
  63. result.x := x;
  64. result.y := y;
  65. result.z := z;
  66. end;
  67. function Add (v: TVec3; amount: TScalar): TVec3;
  68. begin
  69. result := V3(v.x + amount, v.y + amount, v.z + amount);
  70. end;
  71. function Add (v: TVec3; amount: TVec3): TVec3;
  72. begin
  73. result := V3(v.x + amount.x, v.y + amount.y, v.z + amount.z);
  74. end;
  75. function Subtract (v: TVec3; amount: TScalar): TVec3;
  76. begin
  77. result := V3(v.x - amount, v.y - amount, v.z - amount);
  78. end;
  79. function Subtract (v: TVec3; amount: TVec3): TVec3;
  80. begin
  81. result := V3(v.x - amount.x, v.y - amount.y, v.z - amount.z);
  82. end;
  83. function Multiply (v: TVec3; amount: TScalar): TVec3;
  84. begin
  85. result := V3(v.x * amount, v.y * amount, v.z * amount);
  86. end;
  87. function Multiply (v: TVec3; amount: TVec3): TVec3;
  88. begin
  89. result := V3(v.x * amount.x, v.y * amount.y, v.z * amount.z);
  90. end;
  91. function Divide (v: TVec3; amount: TScalar): TVec3;
  92. begin
  93. result := V3(v.x / amount, v.y / amount, v.z / amount);
  94. end;
  95. function Divide (v: TVec3; amount: TVec3): TVec3;
  96. begin
  97. result := V3(v.x / amount.x, v.y / amount.y, v.z / amount.z);
  98. end;
  99. function Sum (v: TVec3): TScalar;
  100. begin
  101. result := v.x + v.y + v.z;
  102. end;
  103. function Magnitude(v: TVec3): TScalar;
  104. begin
  105. result := Sqrt(Power(v.x, 2) + Power(v.y, 2) + Power(v.z, 2));
  106. end;
  107. function SquaredLength (v: TVec3): TScalar;
  108. begin
  109. result := Sqr(v.x) + Sqr(v.y) + Sqr(v.z);
  110. end;
  111. function Normalize (v: TVec3): TVec3;
  112. begin
  113. result := Divide(v, Magnitude(v));
  114. end;
  115. function Dot (v: TVec3; point: TVec3): TScalar;
  116. begin
  117. result := (v.x * point.x) + (v.y * point.y) + (v.z * point.z);
  118. end;
  119. function Cross (v: TVec3; point: TVec3): TVec3;
  120. begin
  121. result.x := (v.y * point.z) - (v.z * point.y);
  122. result.y := (v.z * point.x) - (v.x * point.z);
  123. result.z := (v.x * point.y) - (v.y * point.x);
  124. end;
  125. function ToFloats(v: TVec3): TJSFloat32List;
  126. begin
  127. SetLength(result, 3);
  128. result[0] := v.x;
  129. result[1] := v.y;
  130. result[2] := v.z;
  131. end;
  132. function VecStr(v: TVec3): string;
  133. begin
  134. result := '{'+FloatToStr(v.x)+','+FloatToStr(v.y)+','+FloatToStr(v.z)+'}';
  135. end;
  136. procedure Show (v: TVec3);
  137. begin
  138. writeln('{',v.x,',',v.y,',',v.z,'}');
  139. end;
  140. {=============================================}
  141. {@! ___Colors___ }
  142. {=============================================}
  143. function RGBAb(r, g, b, a: GLubyte): TRGBAb;
  144. begin
  145. result[0] := r;
  146. result[1] := g;
  147. result[2] := b;
  148. result[3] := a;
  149. end;
  150. function RGBAf(r, g, b, a: GLfloat): TRGBAf;
  151. begin
  152. result[0] := r;
  153. result[1] := g;
  154. result[2] := b;
  155. result[3] := a;
  156. end;
  157. end.