building_recast.adoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. = How to Build the jNavigation Recast Bindings
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :relfileprefix: ../../
  6. :imagesdir: ../..
  7. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  8. jNavigation is Java jME port for Recast Navigation written in C++. The project has two parts:
  9. . link:https://github.com/QuietOne/jNavigation-native[jNavigationNative] contains Recast Navigation library and C++ wrapper for java
  10. . link:https://github.com/QuietOne/jNavigation[jNavigation] is Java project that uses jNavigationNative and is the project that the end user will use
  11. If there is need for updating Recast Navigation from native side, there are two kinds of updating bindings:
  12. . only updating methods as the Recast made more efficient or more precise
  13. . adding new methods for Recast use
  14. == Updating methods
  15. Only updating methods are easy. The requirements needed:
  16. * C++ compiler
  17. The jNavigationNative that has following folders and files (it has more, but these are the most important for now):
  18. * Recast
  19. ** Include
  20. ** Source
  21. * README.md
  22. * Recast_wrap.cxx - Java - C++ wrapper
  23. Updating the methods is only the matter of putting all headers from Recast Navigation to Include folder, source to Source folders, and then building the project.
  24. As author of this project used the NetBeans 7.4 for building the project, the following instruction is included, if the building from terminal doesn't work.
  25. . Setting link:https://netbeans.org/kb/docs/cnd/beginning-jni-linux.html[ parameters for NetBeans compiler]
  26. . Remove all headers from Header Files
  27. . Remove all source files *EXCEPT Recast_wrap.cxx* from Source Files
  28. . Right click on Header files, select `Add Existing Item…` or `Add Existing Items from Folders…` and select needed headers
  29. . Right click on Source files, select `Add Existing Item…` or `Add Existing Items from Folders…` and select needed source files
  30. . Build
  31. . Add built project to jNavigation project
  32. . Build jNavigation project
  33. === Adding new methods from native side
  34. This is more complicated process and it includes the all work done in NetBeans mentioned in previous section. After that, there are two ways to add new function:
  35. * manually adding code to wrapper
  36. * creating new wrapper with link:http://swig.org/[SWIG]
  37. ==== Manually adding code to wrapper
  38. Example of method in wrapper:
  39. [source,java]
  40. ----
  41. SWIGEXPORT jint JNICALL Java_com_jme3_ai_navigation_utils_RecastJNI_rcIntArray_1size(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
  42. ...
  43. }
  44. ----
  45. The Recast_wrap.cxx uses SWIG wrapper so for declaring method in wrapper you must first use the keyword `SWIGEXPORT` then returning type (for more information on returning types see link:http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/types.html[link]), then again keyword `JNICALL` and then as the name of method `Java_com_jme3_ai_navigation_utils_RecastJNI_` + `name of class` + `name of method`, after that, there goes list of parameters needed for the function (for more information see link:http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html[link]). In body of method write how the function should be used.
  46. After adding method to wrapper, compile project and add it to jNavigation.
  47. In jNavigation project in class `com.jme3.ai.navigation.utils.RecastJNI.java` add native method, and after that add in class from which you would like to use this method to call this native method. It seems a little bit more complicated than it should be, but this also for support for updating native side with SWIG.
  48. ==== Creating new wrapper with SWIG
  49. In some temporary folder add all headers. It shouldn't contain any subfolders.
  50. The following script was used for generating wrapper:
  51. [source]
  52. ----
  53. %module Recast
  54. %include "carrays.i"
  55. %array_class(double, DoubleArray);
  56. %array_class(float, FloatArray);
  57. %array_class(int, IntArray);
  58. %array_class(unsigned char, UCharArray);
  59. %array_class(unsigned short, UShortArray);
  60. %array_class(unsigned int, UIntArray);
  61. %array_class(long, LongArray);
  62. %array_class(bool, BooleanArray)
  63. %{
  64. #include "DetourAlloc.h"
  65. #include "DetourAssert.h"
  66. #include "DetourCommon.h"
  67. #include "DetourCrowd.h"
  68. #include "DetourLocalBoundary.h"
  69. #include "DetourMath.h"
  70. #include "DetourNavMesh.h"
  71. #include "DetourNavMeshBuilder.h"
  72. #include "DetourNavMeshQuery.h"
  73. #include "DetourNode.h"
  74. #include "DetourObstacleAvoidance.h"
  75. #include "DetourPathCorridor.h"
  76. #include "DetourPathQueue.h"
  77. #include "DetourProximityGrid.h"
  78. #include "DetourStatus.h"
  79. #include "DetourTileCache.h"
  80. #include "DetourTileCacheBuilder.h"
  81. #include "Recast.h"
  82. #include "RecastAlloc.h"
  83. #include "RecastAssert.h"
  84. %}
  85. /* Let's just grab the original header file here */
  86. %include "DetourAlloc.h"
  87. %include "DetourAssert.h"
  88. %include "DetourCommon.h"
  89. %include "DetourCrowd.h"
  90. %include "DetourLocalBoundary.h"
  91. %include "DetourMath.h"
  92. %include "DetourNavMesh.h"
  93. %include "DetourNavMeshBuilder.h"
  94. %include "DetourNavMeshQuery.h"
  95. %include "DetourNode.h"
  96. %include "DetourObstacleAvoidance.h"
  97. %include "DetourPathCorridor.h"
  98. %include "DetourPathQueue.h"
  99. %include "DetourProximityGrid.h"
  100. %include "DetourStatus.h"
  101. %include "DetourTileCache.h"
  102. %include "DetourTileCacheBuilder.h"
  103. %include "Recast.h"
  104. %include "RecastAlloc.h"
  105. %include "RecastAssert.h"
  106. %pragma(java) jniclasscode=%{
  107. static {
  108. System.load("Recast");
  109. }
  110. %}
  111. ----
  112. If there are more headers at some moment, include them in both places.
  113. . Save script as Recast.i into temp folder with rest of the headers
  114. . Install SWIG if not already
  115. . Open terminal and go to folder where the script is
  116. . Execute command `swig -c++ -java Recast.i`
  117. . Now SWIG will generate Java classes and new Recast_wrap.cxx
  118. . Recast_wrap.cxx put in jNavigationNative with new headers and source files, as previously mentioned
  119. . Build that project
  120. . For jNavigation side, put only new methods in RecastJNI, and use where they are being used. For that you can see in Java class that are build with SWIG.
  121. . If method uses some explicit SWIG type, try to use some method for converting it into jME type, or similar. You can probably find something in package `com.jme3.ai.navigation.utils`