ModelDirectionUtil.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package com.zjinja.mcmod.decor.utils.model_dir_util;
  2. import com.zjinja.mcmod.decor.ZJinJaDecoration;
  3. import net.minecraft.entity.EntityLivingBase;
  4. import net.minecraft.item.ItemStack;
  5. import net.minecraft.util.MathHelper;
  6. import net.minecraft.util.Vec3;
  7. import net.minecraft.world.World;
  8. import org.lwjgl.opengl.GL11;
  9. public class ModelDirectionUtil {
  10. public RotationVector RotationMetaTable[];
  11. public BoundingBoxVector RelBoundingBox;
  12. public BoundingBoxVector AbsBoundingBoxMetaTable[];
  13. public ModelDirectionUtil(){
  14. RotationMetaTable = new RotationVector[16];
  15. for(int i = 0; i < 16; i++){
  16. RotationMetaTable[i] = new RotationVector();
  17. }
  18. RelBoundingBox = new BoundingBoxVector();
  19. AbsBoundingBoxMetaTable = new BoundingBoxVector[16];
  20. for(int i = 0; i < 16; i++){
  21. AbsBoundingBoxMetaTable[i] = new BoundingBoxVector();
  22. }
  23. }
  24. public void DefineMetaRotation(int Meta, RotationVector rv){
  25. int index = Meta % 16;
  26. RotationMetaTable[index] = rv;
  27. }
  28. public void DefineRelBoundingBox(BoundingBoxVector rbbv){
  29. RelBoundingBox = rbbv;
  30. for(int i = 0; i < 16; i++){
  31. AbsBoundingBoxMetaTable[i] = CalcMetaBoundingBoxTableItem(i);
  32. }
  33. }
  34. public RotationVector GetRotationByMeta(int meta){
  35. int rmeta = meta % 16;
  36. RotationVector rrv = RotationMetaTable[rmeta];
  37. return rrv;
  38. }
  39. private BoundingBoxVector CalcMetaBoundingBoxTableItem(int meta){
  40. RotationVector rrv = GetRotationByMeta(meta);
  41. int rx,ry,rz;
  42. float tx1,tx2,ty1,ty2,tz1,tz2;
  43. float ux1,ux2,uy1,uy2,uz1,uz2;
  44. rx = rrv.RotateX % 4;
  45. ry = rrv.RotateY % 4;
  46. rz = rrv.RotateZ % 4;
  47. if(ZJinJaDecoration.DebugMode){
  48. ZJinJaDecoration.logger.warn(
  49. String.format("Rotation @ Meta=%d: (%d,%d,%d)",
  50. meta, rx, ry, rz
  51. )
  52. );
  53. }
  54. // Coordinate Transformation Before Rotation
  55. tx1 = RelBoundingBox.x1 - 0.5F;
  56. tx2 = RelBoundingBox.x2 - 0.5F;
  57. ty1 = RelBoundingBox.y1 - 0.5F;
  58. ty2 = RelBoundingBox.y2 - 0.5F;
  59. tz1 = RelBoundingBox.z1 - 0.5F;
  60. tz2 = RelBoundingBox.z2 - 0.5F;
  61. if(ZJinJaDecoration.DebugMode){
  62. ZJinJaDecoration.logger.warn(
  63. String.format("Coordinate Transformation: Meta=%d: Pos1=(%f,%f,%f), Pos2=(%f,%f,%f)",
  64. meta, tx1, ty1, tz1, tx2, ty2, tz2
  65. )
  66. );
  67. }
  68. //Rotating X Axis
  69. switch (rx){
  70. default:
  71. ux1 = tx1;
  72. ux2 = tx2;
  73. uy1 = ty1;
  74. uy2 = ty2;
  75. uz1 = tz1;
  76. uz2 = tz2;
  77. break;
  78. case 1:
  79. ux1 = tx1;
  80. ux2 = tx2;
  81. uy1 = -tz1;
  82. uy2 = -tz2;
  83. uz1 = ty1;
  84. uz2 = ty2;
  85. break;
  86. case 2:
  87. ux1 = tx1;
  88. ux2 = tx2;
  89. uy1 = -ty1;
  90. uy2 = -ty2;
  91. uz1 = -tz1;
  92. uz2 = -tz2;
  93. break;
  94. case 3:
  95. ux1 = tx1;
  96. ux2 = tx2;
  97. uy1 = tz1;
  98. uy2 = tz2;
  99. uz1 = -ty1;
  100. uz2 = -ty2;
  101. break;
  102. }
  103. if(ZJinJaDecoration.DebugMode){
  104. ZJinJaDecoration.logger.warn(
  105. String.format("After X Axis Trans: Meta=%d: Pos1=(%f,%f,%f), Pos2=(%f,%f,%f)",
  106. meta, ux1, uy1, uz1, ux2, uy2, uz2
  107. )
  108. );
  109. }
  110. //Rotating Y Axis
  111. switch (ry){
  112. default:
  113. tx1 = ux1;
  114. tx2 = ux2;
  115. ty1 = uy1;
  116. ty2 = uy2;
  117. tz1 = uz1;
  118. tz2 = uz2;
  119. break;
  120. case 1:
  121. tx1 = -uz1;
  122. tx2 = -uz2;
  123. ty1 = uy1;
  124. ty2 = uy2;
  125. tz1 = ux1;
  126. tz2 = ux2;
  127. break;
  128. case 2:
  129. tx1 = -ux1;
  130. tx2 = -ux2;
  131. ty1 = uy1;
  132. ty2 = uy2;
  133. tz1 = -uz1;
  134. tz2 = -uz2;
  135. break;
  136. case 3:
  137. tx1 = uz1;
  138. tx2 = uz2;
  139. ty1 = uy1;
  140. ty2 = uy2;
  141. tz1 = -ux1;
  142. tz2 = -ux2;
  143. break;
  144. }
  145. if(ZJinJaDecoration.DebugMode){
  146. ZJinJaDecoration.logger.warn(
  147. String.format("After Y Axis Trans: Meta=%d: Pos1=(%f,%f,%f), Pos2=(%f,%f,%f)",
  148. meta, tx1, ty1, tz1, tx2, ty2, tz2
  149. )
  150. );
  151. }
  152. //Rotating Z Axis
  153. switch (rz){
  154. default:
  155. ux1 = tx1;
  156. ux2 = tx2;
  157. uy1 = ty1;
  158. uy2 = ty2;
  159. uz1 = tz1;
  160. uz2 = tz2;
  161. break;
  162. case 1:
  163. ux1 = -ty1;
  164. ux2 = -ty2;
  165. uy1 = tx1;
  166. uy2 = tx2;
  167. uz1 = tz1;
  168. uz2 = tz2;
  169. break;
  170. case 2:
  171. ux1 = -tx1;
  172. ux2 = -tx2;
  173. uy1 = -ty1;
  174. uy2 = -ty2;
  175. uz1 = tz1;
  176. uz2 = tz2;
  177. break;
  178. case 3:
  179. ux1 = ty1;
  180. ux2 = ty2;
  181. uy1 = -tx1;
  182. uy2 = -tx2;
  183. uz1 = tz1;
  184. uz2 = tz2;
  185. break;
  186. }
  187. if(ZJinJaDecoration.DebugMode){
  188. ZJinJaDecoration.logger.warn(
  189. String.format("After Z Axis Trans: Meta=%d: Pos1=(%f,%f,%f), Pos2=(%f,%f,%f)",
  190. meta, ux1, uy1, uz1, ux2, uy2, uz2
  191. )
  192. );
  193. }
  194. float minx,miny,minz,maxx,maxy,maxz;
  195. minx = Math.min(ux1 + 0.5F, ux2 + 0.5F);
  196. maxx = Math.max(ux1 + 0.5F, ux2 + 0.5F);
  197. miny = Math.min(uy1 + 0.5F, uy2 + 0.5F);
  198. maxy = Math.max(uy1 + 0.5F, uy2 + 0.5F);
  199. minz = Math.min(uz1 + 0.5F, uz2 + 0.5F);
  200. maxz = Math.max(uz1 + 0.5F, uz2 + 0.5F);
  201. // Coordinate Transformation After Rotation
  202. BoundingBoxVector abbv = new BoundingBoxVector(minx, miny, minz, maxx, maxy, maxz);
  203. if(ZJinJaDecoration.DebugMode){
  204. ZJinJaDecoration.logger.warn(
  205. String.format("BoundingBox @ Meta=%d: Pos1=(%f,%f,%f), Pos2=(%f,%f,%f)",
  206. meta, minx, miny, minz, maxx, maxy, maxz)
  207. );
  208. }
  209. return abbv;
  210. }
  211. public BoundingBoxVector GetAbsBoundingBoxByMeta(int meta){
  212. int rmeta = meta % 16;
  213. return AbsBoundingBoxMetaTable[rmeta];
  214. }
  215. public void GLTransformByMeta(int meta){
  216. RotationVector rv = GetRotationByMeta(meta);
  217. float ax,ay,az;
  218. ax = rv.RotateX % 4 * 90.0F;
  219. ay = rv.RotateY % 4 * 90.0F;
  220. az = rv.RotateZ % 4 * 90.0F;
  221. GL11.glPushMatrix();
  222. GL11.glRotatef(ax, -1.0F, 0.0F, 0.0F);
  223. GL11.glPushMatrix();
  224. GL11.glRotatef(ay, 0.0F, -1.0F, 0.0F);
  225. GL11.glPushMatrix();
  226. GL11.glRotatef(az, 0.0F, 0.0F, -1.0F);
  227. }
  228. public OffsetVector CalcOffsetFromRelOffset(int meta, float OffsetX, float OffsetY, float OffsetZ){
  229. RotationVector rrv = GetRotationByMeta(meta);
  230. int rx,ry,rz;
  231. float tx,ty,tz, ux, uy, uz;
  232. rx = rrv.RotateX % 4;
  233. ry = rrv.RotateY % 4;
  234. rz = rrv.RotateZ % 4;
  235. //Rotating X Axis
  236. switch (rx){
  237. default:
  238. ux = OffsetX;
  239. uy = OffsetY;
  240. uz = OffsetZ;
  241. break;
  242. case 1:
  243. ux = OffsetX;
  244. uy = -OffsetZ;
  245. uz = OffsetY;
  246. break;
  247. case 2:
  248. ux = OffsetX;
  249. uy = -OffsetY;
  250. uz = -OffsetZ;
  251. break;
  252. case 3:
  253. ux = OffsetX;
  254. uy = OffsetZ;
  255. uz = -OffsetY;
  256. break;
  257. }
  258. //Rotating Y Axis
  259. switch (ry){
  260. default:
  261. tx = ux;
  262. ty = uy;
  263. tz = uz;
  264. break;
  265. case 1:
  266. tx = -uz;
  267. ty = uy;
  268. tz = ux;
  269. break;
  270. case 2:
  271. tx = -ux;
  272. ty = uy;
  273. tz = -uz;
  274. break;
  275. case 3:
  276. tx = uz;
  277. ty = uy;
  278. tz = -ux;
  279. break;
  280. }
  281. //Rotating Z Axis
  282. switch (rz){
  283. default:
  284. ux = tx;
  285. uy = ty;
  286. uz = tz;
  287. break;
  288. case 1:
  289. ux = -ty;
  290. uy = tx;
  291. uz = tz;
  292. break;
  293. case 2:
  294. ux = -tx;
  295. uy = -ty;
  296. uz = tz;
  297. break;
  298. case 3:
  299. ux = ty;
  300. uy = -tx;
  301. uz = tz;
  302. break;
  303. }
  304. return new OffsetVector(ux, uy, uz);
  305. }
  306. public void FastDefine_4Dir(){
  307. DefineMetaRotation(0, new RotationVector(0,0,0));
  308. DefineMetaRotation(1, new RotationVector(0,1,0));
  309. DefineMetaRotation(2, new RotationVector(0,2,0));
  310. DefineMetaRotation(3, new RotationVector(0,3,0));
  311. DefineMetaRotation(4, new RotationVector(0,0,0));
  312. DefineMetaRotation(5, new RotationVector(0,1,0));
  313. DefineMetaRotation(6, new RotationVector(0,2,0));
  314. DefineMetaRotation(7, new RotationVector(0,3,0));
  315. DefineMetaRotation(8, new RotationVector(0,0,0));
  316. DefineMetaRotation(9, new RotationVector(0,1,0));
  317. DefineMetaRotation(10, new RotationVector(0,2,0));
  318. DefineMetaRotation(11, new RotationVector(0,3,0));
  319. DefineMetaRotation(12, new RotationVector(0,0,0));
  320. DefineMetaRotation(13, new RotationVector(0,1,0));
  321. DefineMetaRotation(14, new RotationVector(0,2,0));
  322. DefineMetaRotation(15, new RotationVector(0,3,0));
  323. }
  324. public void FastPlaceConv_4Dir(World world, int x, int y, int z, EntityLivingBase player, ItemStack itemStack){
  325. int side = world.getBlockMetadata(x, y, z);
  326. int l = MathHelper.floor_double((double)(player.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
  327. int ro = PlaceSideEnum.TransToRotation(PlaceSideEnum.valueOf(side));
  328. if(ro == -1){
  329. ro = PlaceSideEnum.TransToRotation(PlaceSideEnum.fromYaw(l));
  330. }
  331. world.setBlockMetadataWithNotify(x, y, z, ro, 2);
  332. }
  333. }