transformations.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package al
  2. /*
  3. #include <stdlib.h>
  4. #include <allegro5/allegro.h>
  5. #include "helpers.h"
  6. #include "callbacks.h"
  7. */
  8. import "C"
  9. type Transform C.ALLEGRO_TRANSFORM
  10. func wrapTransformRaw(ctrans * C.ALLEGRO_TRANSFORM) * Transform {
  11. return (*Transform)(ctrans)
  12. }
  13. func (trans * Transform) toC() * C.ALLEGRO_TRANSFORM {
  14. return (* C.ALLEGRO_TRANSFORM)(trans)
  15. }
  16. func (trans * Transform) Init(matrix [4][4]float32) {
  17. for i:=0; i <4; i ++ {
  18. for j:=0; j < 4; j ++ {
  19. trans.Put(i, j, matrix[i][j])
  20. }
  21. }
  22. }
  23. func (trans * Transform) Matrix() (matrix [4][4]float32) {
  24. for i:=0; i <4; i ++ {
  25. for j:=0; j < 4; j ++ {
  26. matrix[i][j] = trans.Get(i,j)
  27. }
  28. }
  29. return matrix
  30. }
  31. func (trans * Transform) Get(i, j int) float32 {
  32. return float32(trans.m[i][j])
  33. }
  34. func (trans * Transform) Put(i, j int, v float32) {
  35. trans.m[i][j] = C.float(v)
  36. }
  37. func CreateIdentityTransform() * Transform {
  38. trans := &Transform{}
  39. return trans.Identity()
  40. }
  41. func CreateTransform(x, y, sx, sy, theta float32) * Transform {
  42. trans := &Transform{}
  43. return trans.Build(x, y, sx, sy, theta)
  44. }
  45. func (trans * Transform) Identity() * Transform {
  46. C.al_identity_transform(trans.toC())
  47. return trans
  48. }
  49. func (trans * Transform) Build(x, y, sx, sy, theta float32) * Transform {
  50. C.al_build_transform(trans.toC(), C.float(x), C.float(y), C.float(sx), C.float(sy), C.float(theta))
  51. return trans
  52. }
  53. func (trans * Transform) Use() {
  54. C.al_use_transform(trans.toC())
  55. }
  56. func (trans * Transform) UseProjection() {
  57. C.al_use_projection_transform(trans.toC())
  58. }
  59. func (trans * Transform) BuildCamera(px, py, pz,
  60. lx, ly, lz, ux, uy, uz float32) * Transform {
  61. cpx, cpy, cpz := cf3(px, py, pz)
  62. clx, cly, clz := cf3(lx, ly, lz)
  63. cux, cuy, cuz := cf3(ux, uy, uz)
  64. C.al_build_camera_transform(trans.toC(), cpx, cpy, cpz, clx, cly, clz, cux, cuy, cuz)
  65. return trans
  66. }
  67. func (trans * Transform) Translate(x, y float32) * Transform {
  68. C.al_translate_transform(trans.toC(), C.float(x), C.float(y))
  69. return trans
  70. }
  71. func (trans * Transform) TranslateInt(x, y int) * Transform {
  72. C.al_translate_transform(trans.toC(), C.float(x), C.float(y))
  73. return trans
  74. }
  75. func (trans * Transform) Translate3D(x, y, z float32) * Transform {
  76. C.al_translate_transform_3d(trans.toC(), C.float(x), C.float(y), C.float(z))
  77. return trans
  78. }
  79. func (trans * Transform) Rotate(theta float32) * Transform {
  80. C.al_rotate_transform(trans.toC(), C.float(theta))
  81. return trans
  82. }
  83. func (trans * Transform) Rotate3D(x, y, z, angle float32) * Transform {
  84. C.al_rotate_transform_3d(trans.toC(), C.float(x), C.float(y), C.float(z), C.float(angle))
  85. return trans
  86. }
  87. func (trans * Transform) Scale(x, y float32) * Transform {
  88. C.al_scale_transform(trans.toC(), C.float(x), C.float(y))
  89. return trans
  90. }
  91. func (trans * Transform) ScaleInt(x, y int) * Transform {
  92. C.al_scale_transform(trans.toC(), C.float(x), C.float(y))
  93. return trans
  94. }
  95. func (trans * Transform) Scale3D(x, y, z float32) * Transform {
  96. C.al_scale_transform_3d(trans.toC(), C.float(x), C.float(y), C.float(z))
  97. return trans
  98. }
  99. func (trans * Transform) Coordinates(x, y float32) (float32, float32) {
  100. cx, cy := cf2(x, y)
  101. C.al_transform_coordinates(trans.toC(), &cx, &cy)
  102. return float32(cx), float32(cy)
  103. }
  104. func (trans * Transform) Coordinates3D(x, y, z float32) (float32, float32, float32) {
  105. cx, cy, cz := cf3(x, y, z)
  106. C.al_transform_coordinates_3d(trans.toC(), &cx, &cy, &cz)
  107. return float32(cx), float32(cy), float32(cz)
  108. }
  109. func (trans * Transform) Compose(other * Transform) * Transform {
  110. C.al_compose_transform(trans.toC(), other.toC())
  111. return trans
  112. }
  113. func (trans * Transform) CheckInverse(tolerance float32) bool {
  114. return 1 == (C.al_check_inverse(trans.toC(), C.float(tolerance)))
  115. }
  116. func (trans * Transform) Invert() * Transform {
  117. if trans.CheckInverse(1.0e-7) {
  118. C.al_invert_transform(trans.toC())
  119. return trans
  120. } else {
  121. return nil
  122. }
  123. }
  124. func CurrentTransform() * Transform {
  125. return wrapTransformRaw(C.al_get_current_transform())
  126. }
  127. func CurrentProjectionTransform() * Transform {
  128. return wrapTransformRaw(C.al_get_current_projection_transform())
  129. }
  130. func (trans * Transform) Orthographic(l, t, n, r, b, f float32) * Transform {
  131. cl, ct, cn := cf3(l,t,n)
  132. cr, cb, cf := cf3(r,b,f)
  133. C.al_orthographic_transform(trans.toC(), cl, ct, cn, cr, cb, cf)
  134. return trans
  135. }
  136. func (trans * Transform) Perspective(l, t, n, r, b, f float32) * Transform {
  137. cl, ct, cn := cf3(l,t,n)
  138. cr, cb, cf := cf3(r,b,f)
  139. C.al_perspective_transform(trans.toC(), cl, ct, cn, cr, cb, cf)
  140. return trans
  141. }
  142. func (trans * Transform) HorizontalShear(theta float32) * Transform {
  143. C.al_horizontal_shear_transform(trans.toC(), C.float(theta))
  144. return trans
  145. }
  146. func (trans * Transform) VerticalShear(theta float32) * Transform {
  147. C.al_horizontal_shear_transform(trans.toC(), C.float(theta))
  148. return trans
  149. }