transformations.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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) Translate3D(x, y, z float32) * Transform {
  72. C.al_translate_transform_3d(trans.toC(), C.float(x), C.float(y), C.float(z))
  73. return trans
  74. }
  75. func (trans * Transform) Rotate(theta float32) * Transform {
  76. C.al_rotate_transform(trans.toC(), C.float(theta))
  77. return trans
  78. }
  79. func (trans * Transform) Rotate3D(x, y, z, angle float32) * Transform {
  80. C.al_rotate_transform_3d(trans.toC(), C.float(x), C.float(y), C.float(z), C.float(angle))
  81. return trans
  82. }
  83. func (trans * Transform) Scale(x, y float32) * Transform {
  84. C.al_scale_transform(trans.toC(), C.float(x), C.float(y))
  85. return trans
  86. }
  87. func (trans * Transform) Scale3D(x, y, z float32) * Transform {
  88. C.al_scale_transform_3d(trans.toC(), C.float(x), C.float(y), C.float(z))
  89. return trans
  90. }
  91. func (trans * Transform) Coordinates(x, y float32) (float32, float32) {
  92. cx, cy := cf2(x, y)
  93. C.al_transform_coordinates(trans.toC(), &cx, &cy)
  94. return float32(cx), float32(cy)
  95. }
  96. func (trans * Transform) Coordinates3D(x, y, z float32) (float32, float32, float32) {
  97. cx, cy, cz := cf3(x, y, z)
  98. C.al_transform_coordinates_3d(trans.toC(), &cx, &cy, &cz)
  99. return float32(cx), float32(cy), float32(cz)
  100. }
  101. func (trans * Transform) Compose(other * Transform) * Transform {
  102. C.al_compose_transform(trans.toC(), other.toC())
  103. return trans
  104. }
  105. func (trans * Transform) CheckInverse(tolerance float32) bool {
  106. return 1 == (C.al_check_inverse(trans.toC(), C.float(tolerance)))
  107. }
  108. func (trans * Transform) Invert() * Transform {
  109. if trans.CheckInverse(1.0e-7) {
  110. C.al_invert_transform(trans.toC())
  111. return trans
  112. } else {
  113. return nil
  114. }
  115. }
  116. func CurrentTransform() * Transform {
  117. return wrapTransformRaw(C.al_get_current_transform())
  118. }
  119. func CurrentProjectionTransform() * Transform {
  120. return wrapTransformRaw(C.al_get_current_projection_transform())
  121. }
  122. func (trans * Transform) Orthographic(l, t, n, r, b, f float32) * Transform {
  123. cl, ct, cn := cf3(l,t,n)
  124. cr, cb, cf := cf3(r,b,f)
  125. C.al_orthographic_transform(trans.toC(), cl, ct, cn, cr, cb, cf)
  126. return trans
  127. }
  128. func (trans * Transform) Perspective(l, t, n, r, b, f float32) * Transform {
  129. cl, ct, cn := cf3(l,t,n)
  130. cr, cb, cf := cf3(r,b,f)
  131. C.al_perspective_transform(trans.toC(), cl, ct, cn, cr, cb, cf)
  132. return trans
  133. }
  134. func (trans * Transform) HorizontalShear(theta float32) * Transform {
  135. C.al_horizontal_shear_transform(trans.toC(), C.float(theta))
  136. return trans
  137. }
  138. func (trans * Transform) VerticalShear(theta float32) * Transform {
  139. C.al_horizontal_shear_transform(trans.toC(), C.float(theta))
  140. return trans
  141. }