Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--[-rwxr-xr-x]build_files/cmake/example_scripts/make_quiet.sh0
-rw-r--r--[-rwxr-xr-x]build_files/package_spec/build_debian.sh0
-rw-r--r--build_files/package_spec/debian/changelog5
-rw-r--r--build_files/package_spec/debian/compat1
-rw-r--r--build_files/package_spec/debian/control24
-rw-r--r--build_files/package_spec/debian/copyright41
-rw-r--r--build_files/package_spec/debian/docs2
-rw-r--r--build_files/package_spec/debian/menu4
-rwxr-xr-xbuild_files/package_spec/debian/rules44
-rw-r--r--build_files/package_spec/debian/source/format1
-rw-r--r--build_files/package_spec/debian/watch3
-rw-r--r--build_files/package_spec/pacman/blender.install58
-rw-r--r--doc/guides/blender-guardedalloc.txt57
-rw-r--r--doc/guides/interface_API.txt515
-rw-r--r--doc/guides/python-dev-guide.txt170
-rw-r--r--doc/license/BL-license.txt35
-rw-r--r--doc/license/GPL-license.txt340
-rw-r--r--doc/license/bf-members.txt1393
-rw-r--r--doc/manpage/blender.1397
-rw-r--r--doc/manpage/blender.1.py136
-rw-r--r--extern/CMakeLists.txt1
-rw-r--r--extern/SConscript1
-rw-r--r--extern/recastnavigation/CMakeLists.txt66
-rw-r--r--extern/recastnavigation/Detour/Include/DetourCommon.h167
-rw-r--r--extern/recastnavigation/Detour/Include/DetourNode.h149
-rw-r--r--extern/recastnavigation/Detour/Include/DetourStatNavMesh.h234
-rw-r--r--extern/recastnavigation/Detour/Include/DetourStatNavMeshBuilder.h33
-rw-r--r--extern/recastnavigation/Detour/Include/DetourTileNavMesh.h315
-rw-r--r--extern/recastnavigation/Detour/Include/DetourTileNavMeshBuilder.h29
-rw-r--r--extern/recastnavigation/Detour/Source/DetourCommon.cpp244
-rw-r--r--extern/recastnavigation/Detour/Source/DetourNode.cpp140
-rw-r--r--extern/recastnavigation/Detour/Source/DetourStatNavMesh.cpp876
-rw-r--r--extern/recastnavigation/Detour/Source/DetourStatNavMeshBuilder.cpp346
-rw-r--r--extern/recastnavigation/Detour/Source/DetourTileNavMesh.cpp1428
-rw-r--r--extern/recastnavigation/Detour/Source/DetourTileNavMeshBuilder.cpp213
-rw-r--r--extern/recastnavigation/License.txt18
-rw-r--r--extern/recastnavigation/Readme.txt120
-rw-r--r--extern/recastnavigation/Recast/Include/Recast.h501
-rw-r--r--extern/recastnavigation/Recast/Include/RecastLog.h80
-rw-r--r--extern/recastnavigation/Recast/Include/RecastTimer.h31
-rw-r--r--extern/recastnavigation/Recast/Source/Recast.cpp272
-rw-r--r--extern/recastnavigation/Recast/Source/RecastContour.cpp732
-rw-r--r--extern/recastnavigation/Recast/Source/RecastFilter.cpp249
-rw-r--r--extern/recastnavigation/Recast/Source/RecastLog.cpp77
-rw-r--r--extern/recastnavigation/Recast/Source/RecastMesh.cpp1212
-rw-r--r--extern/recastnavigation/Recast/Source/RecastMeshDetail.cpp981
-rw-r--r--extern/recastnavigation/Recast/Source/RecastRasterization.cpp308
-rw-r--r--extern/recastnavigation/Recast/Source/RecastRegion.cpp1081
-rw-r--r--extern/recastnavigation/Recast/Source/RecastTimer.cpp58
-rw-r--r--extern/recastnavigation/SConscript11
-rw-r--r--intern/ghost/intern/GHOST_SystemPathsCocoa.mm242
-rw-r--r--release/freedesktop/icons/48x48/apps/blender.pngbin3289 -> 0 bytes
-rw-r--r--release/scripts/presets/framerate/23.98.py3
-rw-r--r--release/scripts/presets/framerate/24.py3
-rw-r--r--release/scripts/presets/framerate/25.py3
-rw-r--r--release/scripts/presets/framerate/29.97.py3
-rw-r--r--release/scripts/presets/framerate/30.py3
-rw-r--r--release/scripts/presets/framerate/50.py3
-rw-r--r--release/scripts/presets/framerate/59.94.py3
-rw-r--r--release/scripts/presets/framerate/60.py3
-rw-r--r--release/scripts/presets/framerate/Custom.py1
-rw-r--r--release/scripts/startup/bl_ui/properties_data_modifier.py8
-rw-r--r--release/scripts/startup/bl_ui/properties_game.py39
-rw-r--r--release/scripts/startup/bl_ui/properties_scene.py71
-rw-r--r--source/blender/blenkernel/intern/customdata.c12
-rw-r--r--source/blender/blenkernel/intern/object.c1
-rw-r--r--source/blender/blenkernel/intern/sca.c16
-rw-r--r--source/blender/blenkernel/intern/scene.c17
-rw-r--r--source/blender/blenloader/intern/readfile.c64
-rw-r--r--source/blender/blenloader/intern/writefile.c3
-rw-r--r--source/blender/editors/include/ED_navmesh_conversion.h96
-rw-r--r--source/blender/editors/object/CMakeLists.txt2
-rw-r--r--source/blender/editors/object/SConscript3
-rw-r--r--source/blender/editors/object/object_intern.h5
-rw-r--r--source/blender/editors/object/object_navmesh.cpp629
-rw-r--r--source/blender/editors/object/object_ops.c4
-rw-r--r--source/blender/editors/space_logic/logic_window.c46
-rw-r--r--source/blender/editors/util/CMakeLists.txt3
-rw-r--r--source/blender/editors/util/SConscript5
-rw-r--r--source/blender/editors/util/navmesh_conversion.cpp444
-rw-r--r--source/blender/gpu/intern/gpu_shader_material.glsl1777
-rw-r--r--source/blender/gpu/intern/gpu_shader_vertex.glsl15
-rw-r--r--source/blender/makesdna/DNA_actuator_types.h25
-rw-r--r--source/blender/makesdna/DNA_customdata_types.h4
-rw-r--r--source/blender/makesdna/DNA_meshdata_types.h4
-rw-r--r--source/blender/makesdna/DNA_modifier_types.h5
-rw-r--r--source/blender/makesdna/DNA_object_types.h5
-rw-r--r--source/blender/makesdna/DNA_scene_types.h29
-rw-r--r--source/blender/makesrna/intern/rna_actuator.c119
-rw-r--r--source/blender/makesrna/intern/rna_modifier.c15
-rw-r--r--source/blender/makesrna/intern/rna_object.c30
-rw-r--r--source/blender/makesrna/intern/rna_scene.c124
-rw-r--r--source/blender/modifiers/CMakeLists.txt5
-rw-r--r--source/blender/modifiers/MOD_modifiertypes.h1
-rw-r--r--source/blender/modifiers/SConscript6
-rw-r--r--source/blender/modifiers/intern/MOD_navmesh.cpp269
-rw-r--r--source/blender/modifiers/intern/MOD_util.c1
-rw-r--r--source/creator/CMakeLists.txt1
-rw-r--r--source/gameengine/Converter/BL_BlenderDataConversion.cpp52
-rw-r--r--source/gameengine/Converter/CMakeLists.txt1
-rw-r--r--source/gameengine/Converter/KX_ConvertActuators.cpp41
-rw-r--r--source/gameengine/Converter/SConscript1
-rw-r--r--source/gameengine/GameLogic/SCA_IActuator.h1
-rw-r--r--source/gameengine/Ketsji/CMakeLists.txt9
-rw-r--r--source/gameengine/Ketsji/KX_GameObject.cpp18
-rw-r--r--source/gameengine/Ketsji/KX_GameObject.h14
-rw-r--r--source/gameengine/Ketsji/KX_KetsjiEngine.cpp4
-rw-r--r--source/gameengine/Ketsji/KX_NavMeshObject.cpp708
-rw-r--r--source/gameengine/Ketsji/KX_NavMeshObject.h83
-rw-r--r--source/gameengine/Ketsji/KX_ObstacleSimulation.cpp869
-rw-r--r--source/gameengine/Ketsji/KX_ObstacleSimulation.h145
-rw-r--r--source/gameengine/Ketsji/KX_PythonInit.cpp19
-rw-r--r--source/gameengine/Ketsji/KX_PythonInit.h3
-rw-r--r--source/gameengine/Ketsji/KX_PythonInitTypes.cpp4
-rw-r--r--source/gameengine/Ketsji/KX_Scene.cpp33
-rw-r--r--source/gameengine/Ketsji/KX_Scene.h8
-rw-r--r--source/gameengine/Ketsji/KX_SteeringActuator.cpp630
-rw-r--r--source/gameengine/Ketsji/KX_SteeringActuator.h130
-rw-r--r--source/gameengine/Ketsji/SConscript2
-rw-r--r--source/gameengine/Rasterizer/RAS_IRasterizer.h4
-rw-r--r--source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp64
-rw-r--r--source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.h39
122 files changed, 16864 insertions, 3387 deletions
diff --git a/build_files/cmake/example_scripts/make_quiet.sh b/build_files/cmake/example_scripts/make_quiet.sh
index 9e179201541..9e179201541 100755..100644
--- a/build_files/cmake/example_scripts/make_quiet.sh
+++ b/build_files/cmake/example_scripts/make_quiet.sh
diff --git a/build_files/package_spec/build_debian.sh b/build_files/package_spec/build_debian.sh
index 4594095451f..4594095451f 100755..100644
--- a/build_files/package_spec/build_debian.sh
+++ b/build_files/package_spec/build_debian.sh
diff --git a/build_files/package_spec/debian/changelog b/build_files/package_spec/debian/changelog
deleted file mode 100644
index 0559bb0c4d8..00000000000
--- a/build_files/package_spec/debian/changelog
+++ /dev/null
@@ -1,5 +0,0 @@
-blender (2.56+svn34749-bf) unstable; urgency=low
-
- * New upstream SVN snapshot.
-
- -- Dan Eicher <dan@trollwerks.org> Wed, 09 Feb 2011 18:55:24 -0700
diff --git a/build_files/package_spec/debian/compat b/build_files/package_spec/debian/compat
deleted file mode 100644
index 7f8f011eb73..00000000000
--- a/build_files/package_spec/debian/compat
+++ /dev/null
@@ -1 +0,0 @@
-7
diff --git a/build_files/package_spec/debian/control b/build_files/package_spec/debian/control
deleted file mode 100644
index be703e05071..00000000000
--- a/build_files/package_spec/debian/control
+++ /dev/null
@@ -1,24 +0,0 @@
-Source: blender
-Section: graphics
-Priority: extra
-Maintainer: Dan Eicher <dan@trollwerks.org>
-Build-Depends: debhelper (>= 7.0.50~), cmake, python3, python, libfreetype6-dev, libglu1-mesa-dev, libilmbase-dev, libopenexr-dev, libjpeg62-dev, libopenal-dev, libpng12-dev, libsamplerate0-dev, libsdl-dev, libtiff4-dev, libx11-dev, libxi-dev, zlib1g-dev, python3.2-dev, libopenjpeg-dev
-Standards-Version: 3.9.1
-Homepage: http://blender.org/
-X-Python3-Version: >= 3.2, << 3.3
-
-Package: blender-snapshot
-Architecture: any
-Depends: ${shlibs:Depends}, ${python3:Depends}, ${misc:Depends}
-Provides: blender
-Conflicts: blender
-Replaces: blender
-Description: Very fast and versatile 3D modeller/renderer
- Blender is an integrated 3d suite for modelling, animation, rendering,
- post-production, interactive creation and playback (games). Blender has its
- own particular user interface, which is implemented entirely in OpenGL and
- designed with speed in mind. Python bindings are available for scripting;
- import/export features for popular file formats like 3D Studio and Wavefront
- Obj are implemented as scripts by the community. Stills, animations, models
- for games or other third party engines and interactive content in the form of
- a standalone binary and/or a web plug-in are common products of Blender use.
diff --git a/build_files/package_spec/debian/copyright b/build_files/package_spec/debian/copyright
deleted file mode 100644
index 0f7287208af..00000000000
--- a/build_files/package_spec/debian/copyright
+++ /dev/null
@@ -1,41 +0,0 @@
-This work was packaged for Debian by:
-
- Dan Eicher <dan@trollwerks.org> on Tue, 08 Feb 2011 21:59:32 -0700
-
-It was downloaded from:
-
- http://blender.org
-
-Copyright:
-
- Copyright (C) 2002-2011 Blender Foundation
-
-License:
-
- This package is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This package is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>
-
-On Debian systems, the complete text of the GNU General
-Public License version 2 can be found in "/usr/share/common-licenses/GPL-2".
-
-
-The Debian packaging is:
-
- Copyright (C) 2011 Dan Eicher <dan@trollwerks.org>
-
-you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
-
-
diff --git a/build_files/package_spec/debian/docs b/build_files/package_spec/debian/docs
deleted file mode 100644
index 886845257bc..00000000000
--- a/build_files/package_spec/debian/docs
+++ /dev/null
@@ -1,2 +0,0 @@
-release/text/copyright.txt
-release/text/readme.html
diff --git a/build_files/package_spec/debian/menu b/build_files/package_spec/debian/menu
deleted file mode 100644
index d69c7354f3c..00000000000
--- a/build_files/package_spec/debian/menu
+++ /dev/null
@@ -1,4 +0,0 @@
-?package(blender-snapshot):needs="X11" section="Applications/Graphics"\
- longtitle="Blender 3D modeler / renderer"\
- icon="/usr/share/icons/hicolor/scalable/apps/blender.svg"\
- title="blender" command="/usr/bin/blender"
diff --git a/build_files/package_spec/debian/rules b/build_files/package_spec/debian/rules
deleted file mode 100755
index 7a3d2d52adc..00000000000
--- a/build_files/package_spec/debian/rules
+++ /dev/null
@@ -1,44 +0,0 @@
-#!/usr/bin/make -f
-# -*- makefile -*-
-
-SVN_URL := https://svn.blender.org/svnroot/bf-blender/trunk/blender
-REV := $(shell dpkg-parsechangelog | sed -rne 's,^Version: .*[+~]svn([0-9]+).*,\1,p')
-VER := $(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^-]+).*,\1,p')
-REL := $(shell dpkg-parsechangelog | sed -rne 's,^Version: ([0-9]+\.[0-9]+).*,\1,p')
-TARBALL = blender_$(VER).orig.tar.gz
-BLDDIR = debian/cmake
-
-%:
- dh $@ -Scmake -B$(BLDDIR) --parallel --with python3 --without python-support
-
-override_dh_auto_configure:
- # blender spesific CMake options
- dh_auto_configure -- \
- -DCMAKE_BUILD_TYPE:STRING=Release \
- -DWITH_INSTALL_PORTABLE:BOOL=OFF \
- -DWITH_PYTHON_INSTALL:BOOL=OFF \
- -DWITH_OPENCOLLADA:BOOL=OFF
-
-override_dh_auto_test:
- # don't run CTest
-
-override_dh_install:
- dh_install
-
- # remove duplicated docs
- rm -rf debian/blender-snapshot/usr/share/doc/blender
-
-override_dh_python3:
- dh_python3 -V 3.2-3.3 /usr/share/blender/$(REL)/scripts
-
-get-orig-source:
- rm -rf get-orig-source $(TARBALL)
- mkdir get-orig-source
- if [ "$(SVN_URL)" = . ] && [ `svnversion` = "$(REV)" ]; then \
- svn -q export . get-orig-source/blender-$(VER); \
- else \
- svn -q export -r $(REV) $(SVN_URL) get-orig-source/blender-$(VER); \
- fi
- GZIP='--best --no-name' tar czf $(TARBALL) -C get-orig-source blender-$(VER)
- rm -rf get-orig-source
- @echo "$(TARBALL) created; move it to the right destination to build the package"
diff --git a/build_files/package_spec/debian/source/format b/build_files/package_spec/debian/source/format
deleted file mode 100644
index 163aaf8d82b..00000000000
--- a/build_files/package_spec/debian/source/format
+++ /dev/null
@@ -1 +0,0 @@
-3.0 (quilt)
diff --git a/build_files/package_spec/debian/watch b/build_files/package_spec/debian/watch
deleted file mode 100644
index 0f8473b25ef..00000000000
--- a/build_files/package_spec/debian/watch
+++ /dev/null
@@ -1,3 +0,0 @@
-version=3
-opts=uversionmangle=s/[a-z]$/.$&/;s/[j-s]$/1$&/;s/[t-z]$/2$&/;tr/a-z/1-90-90-6/ \
-http://download.blender.org/source/blender-([0-9.]+[a-z]?)\.tar\.gz
diff --git a/build_files/package_spec/pacman/blender.install b/build_files/package_spec/pacman/blender.install
index f2d37ec7a2b..fc339139957 100644
--- a/build_files/package_spec/pacman/blender.install
+++ b/build_files/package_spec/pacman/blender.install
@@ -1,29 +1,29 @@
-post_install() {
- cat << EOF
-
-NOTE
-----
-Happy blending!
-
-EOF
- echo "update desktop mime database..."
- update-desktop-database
-}
-
-post_upgrade() {
- post_install $1
-}
-
-pre_remove() {
- /bin/true
-}
-
-post_remove() {
- echo "update desktop mime database..."
- update-desktop-database
-}
-
-op=$1
-shift
-
-$op $*
+post_install() {
+ cat << EOF
+
+NOTE
+----
+Happy blending!
+
+EOF
+ echo "update desktop mime database..."
+ update-desktop-database
+}
+
+post_upgrade() {
+ post_install $1
+}
+
+pre_remove() {
+ /bin/true
+}
+
+post_remove() {
+ echo "update desktop mime database..."
+ update-desktop-database
+}
+
+op=$1
+shift
+
+$op $*
diff --git a/doc/guides/blender-guardedalloc.txt b/doc/guides/blender-guardedalloc.txt
deleted file mode 100644
index 44a9722a4e4..00000000000
--- a/doc/guides/blender-guardedalloc.txt
+++ /dev/null
@@ -1,57 +0,0 @@
-MEMORY MANAGEMENT IN BLENDER (guardedalloc)
--------------------------------------------
-
-NOTE: This file does not cover memutil and smart pointers and rerefence counted
- garbage collection, which are contained in the memutil module.
-
-Blender takes care of dynamic memory allocation using a set of own functions
-which are recognizeable through their MEM_ prefix. All memory allocation and
-deallocation in blender is done through these functions.
-
-The following functions are available through MEM_guardedalloc.h:
-
-For normal operation:
----------------------
-
-void *MEM_[mc]allocN(unsigned int len, char * str);
-
-- nearest ANSI counterpart: malloc()
-- str must be a static string describing the memory block (used for debugging
-memory management problems)
-- returns a memory block of length len
-- MEM_callocN clears the memory block to 0
-
-void *MEM_dupallocN(void *vmemh);
-
-- nearest ANSI counterpart: combination malloc() and memcpy()
-- returns a pointer to a copy of the given memory area
-
-short MEM_freeN(void *vmemh);
-
-- nearest ANSI counterpart: free()
-- frees the memory area given by the pointer
-- returns 0 on success and !=0 on error
-
-int MEM_allocN_len(void *vmemh);
-
-- nearest ANSI counterpart: none known
-- returns the length of the given memory area
-
-For debugging:
---------------
-
-void MEM_set_error_stream(FILE*);
-
-- this sets the file the memory manager should use to output debugging messages
-- if the parameter is NULL the messages are suppressed
-- default is that messages are suppressed
-
-void MEM_printmemlist(void);
-
-- if err_stream is set by MEM_set_error_stream() this function dumps a list of all
-currently allocated memory blocks with length and name to the stream
-
-int MEM_check_memory_integrity(void);
-
-- this function tests if the internal structures of the memory manager are intact
-- returns 0 on success and !=0 on error
diff --git a/doc/guides/interface_API.txt b/doc/guides/interface_API.txt
deleted file mode 100644
index c98794444e6..00000000000
--- a/doc/guides/interface_API.txt
+++ /dev/null
@@ -1,515 +0,0 @@
----------------------------------------------------
-Blender interface.c API toolkit notes
-(july 2003, Ton Roosendaal)
----------------------------------------------------
-
-Contents
-
-1 General notes
-1.1 C and H files
-
-2. Windows & Blocks
-2.1 Memory allocation
-2.2 And how it works internally
-
-3. API for uiBlock
-3.1 uiBlock Controlling functions
-3.2 Internal function to know
-
-4. API for uiButton
-4.1 UiDefBut
- 1. BUT
- 2. TOG or TOGN or TOGR
- TOG|BIT|<nr>
- 3. TOG3|BIT|<nr>
- 4. ROW
- 5. SLI or NUMSLI or HSVSLI
- 6. NUM
- 7. TEX
- 8. LABEL
- 9 SEPR
- 10. MENU
- 11. COL
-4.2 Icon buttons
- 12. ICONROW
- 13. ICONTEXTROW
-4.3 pulldown menus / block buttons
- 14. BLOCK
-4.4 specials
- 15. KEYEVT
- 16. LINK and INLINK
- 17. IDPOIN
-4.5 uiButton control fuctions
-
-
-----------------1. General notes
-
-- The API is built with Blender in mind, with some buttons acting on lists of Blender data.
- It was not meant to be available as a separate SDK, nor to be used for other applications.
-
-- It works with only OpenGL calls, for the full 100%. This means that it has some quirks
- built-in to work with all OS's and OpenGL versions. Especially frontbuffer drawing is
- a continuous point of attention. Buttons can be drawn with any window matrix. However,
- errors can still occor when buttons are created in windows with non-standard glViewports.
-
-- The code was written to replace the old 1.8 button system, but under high pressure. Quite
- some button methods from the old system were copied for that reason.
-
-- I tried to design a unified GUI system, which equally works for pulldown menus, pop up menus,
- and normal button layouts. Although it gives nice features and freedom in design, the code
- looks quite hard to understand for that reason. Not all 'normal' pulldown menu features
- could be hacked in easily, they just differ too much from other UI elements. Could be
- looked at once...
-
-- During the past period of NaN (beginning of 2002) someone tried to make a more 'high' level
- API for it, with less low level defines and structure info needed in calling code. I am not
- really sure if this was done well... or even finished. In the bottom of interface.c you can
- see the 'new' API which is now used in Blender code. It used to be so much more simple!
- Nevertheless, I will use that convention in this doc.
-
-- Unfinished stuff: the code was scheduled to be expanded with 'floating blocks' which can
- serve as permanent little button-fields in Blender windows. Think for example of having
- an (optional) extra field in the 3d window displaying object loc/rot/size.
- After that, the existing button windows can be reorganized in such blocks as well, allowing
- a user to configure the genereal buttons layout (make vertical for example).
-
-
---------------1.1 C and H files
-
-blender/source/blender/src/interface.c /* almost all code */
-blender/source/blender/include/interface.h /* internals for previous code */
-blender/source/blender/include/BIF_interface.h /* externals for previous code */
-
-(the previous 2 include files have not been separated fully yet)
-
-Color and icons stuff has been put in: (unfinished code, under development)
-blender/source/blender/src/resources.c
-blender/source/blender/include/BIF_resources.h
-
-Related code:
-blender/source/blender/src/toolbox.c (extra GUI elements built on top of this API)
-
-
---------------2. Windows & Blocks
-
-All GUI elements are collected in uiBlocks, which in turn are linked together in a list that's
-part of a Blender Area-window.
-
- uiBlock *block= uiNewBlock(&curarea->uiblocks, "stuff", UI_EMBOSSX, UI_HELV, curarea->win);
-
-The next code example makes a new block, and puts it in the list of blocks of the current active
-Area:
-
- uiDoBlocks(&curarea->uiblocks, event);
-
-This code is usually available in each area-window event queue handler. You give uiDoBlocks
-an event code, and the uiDoBlocks handles whatever is to be handled. Blocks can be
-standard buttons or pull down menus. Can return immediately, or jump to an internal handling
-loop.
-
-2.1 Memory allocation
-
-Important to know is that for this toolkit there's no difference in "creating blocks" or
-"drawing blocks". In fact, for each window redraw all blocks are created again. Constructing
-button interfaces in Blender always happens in the main drawing function itself.
-
-Memory allocation is handled as follows:
-- if in this window a uiBlock with the same name existed, it is freed
-- when you close a window (or blender) the uiBlocks get freed.
-- when you duplicate (split) a window, the uiBlocks get copied
-
-2.2 And how it works internally
-
-With a call to uiDoblocks, all blocks in the current active window are evaluated.
-It walks through the lists in a rather complex manner:
-
-- while(looping)
-
- /* the normal buttons handling */
- - for each block
- - call uiDoBlock (handles buttons for single block)
- - (end for)
-
- /* at this moment, a new block can be created, for a menu */
- /* so we create a 2nd loop for it */
- - while first block is a menu
- - if block is a menu and not initialized:
- - initalize 'saveunder'
- - draw it
- - get event from queue
- - call uiDoBlock (handles buttons for single block)
- /* here, a new block again can be created, for a sub menu */
- - if return "end" from uiDoBlock
- restore 'saveunder's
- free all menu blocks
- exit from loop
- - do tooltip if nothing has happened
- - (end while)
-
- - if there was menu, it does this loop once more
- (when you click outside a menu, at another button)
-
-- (end while)
-
-- do tooltip if nothing has happened
-
-
-
--------------3. API for uiBlock
-
-Create a new buttons block, and link it to the window:
-
-uiBlock *uiNewBlock(ListBase *lb, char *name, short dt, short font, short win)
- ListBase *lb pointer to list basis, where the block will be appended to (blenlib)
- char *name unique name to identify the block. When the name exists in the list,
- the old uiBlock gets freed.
- short dt drawtype. See below
- short font font id number
- short win blender area-window id
-
-drawtype:
- UI_EMBOSSX 0 /* Rounded embossed button (standard in Blender) */
- UI_EMBOSSW 1 /* Simpler embossed button */
- UI_EMBOSSN 2 /* Button with no border */
- UI_EMBOSSF 3 /* Square embossed button (file select) */
- UI_EMBOSSM 4 /* Colored, for pulldown menus */
- UI_EMBOSSP 5 /* Simple borderless coloured button (like blender sensors) */
-
-font:
- UI_HELV 0 /* normal font */
- UI_HELVB 1 /* bold font */
-With the new truetype option in Blender, this is used for all font families
-
-When a uiBlock is created, each uiButton that is defined gets the uiBlock properties.
-Changing Block properties inbetween will affact uiButtons defined thereafter.
-
-
-
-----------3.1 uiBlock Controlling functions:
-
-void uiDrawBlock(block)
- draws the block
-
-void uiBlockSetCol(uiBlock *block, int col)
-
-col:
- BUTGREY,
- BUTGREEN,
- BUTBLUE,
- BUTSALMON,
- MIDGREY,
- BUTPURPLE,
-
-void uiBlockSetEmboss(uiBlock *block, int emboss)
- changes drawtype
-
-void uiBlockSetDirection(uiBlock *block, int direction)
- for pop-up and pulldown menus:
-
-direction:
- UI_TOP
- UI_DOWN
- UI_LEFT
- UI_RIGHT
-
-void uiBlockSetXOfs(uiBlock *block, int xofs)
- for menus, offset from parent
-
-void uiBlockSetButmFunc(uiBlock *block, void (*menufunc)(void *arg, int event), void *arg)
- sets function to be handled when a menu-block is marked "OK"
-
-void uiAutoBlock(uiBlock *block, float minx, float miny, float sizex, float sizey, UI_BLOCK_ROWS)
-
- Sets the buttons in this block to automatically align, and fit within boundaries.
- Internally it allows multiple collums or rows as well. Only 'row order' has been implemented.
- The uiDefBut definitions don't need coordinates as input here, but instead:
- - first value (x1) to indicate row number
- - width and height values (if filled in) will be used to define a relative width/height.
- A call to uiDrawBlock will invoke the calculus to fit in all buttons.
-
-
-
----------- 3.2 Internal function to know:
-
-These flags used to be accessible from outside of interface.c. Currently it is only
-used elsewhere by toolbox.c, so it can be considered 'internal' somewhat.
-
-void uiBlockSetFlag(uiBlock *block, int flag) /* block types, can be 'OR'ed */
- UI_BLOCK_LOOP 1 a sublooping block, drawn in frontbuffer, i.e. menus
- UI_BLOCK_REDRAW 2 block needs a redraw
- UI_BLOCK_RET_1 4 block is closed when an event happens with value '1' (press key, not for mouse)
- UI_BLOCK_BUSY 8 internal
- UI_BLOCK_NUMSELECT 16 keys 1-2-...-9-0 can be used to select items
- UI_BLOCK_ENTER_OK 32 enter key closes block with "OK"
-
-(these values are being set within the interface.c and toolbox.c code.)
-
-
--------------4. API for uiButton
-
-In Blender a button can do four things:
-
-- directly visualize data, and write to it.
-- put event codes (shorts) back in the queue, to be handled
-- call a user-defined function pointer (while being pressed, etc)
-- create and call another block (i.e. menu)
-
-Internally, each button or menu item is a 'uiButton', with a generic API and handling:
-ui_def_but(block, type, retval, str, x1, y1, x2, y2, poin, min, max, a1, a2, tip);
-
-Beacause a lot of obscure generic (re-use) happens here, translation calls have been made
-for each most button types individually.
-
-
------------4.1 UiDefBut
-
-uiBut *UiDefBut[CSIF]( uiBlock *block, int type, int retval, char *str,
- short x1, short y1, short x2, short y2, xxxx *poin,
- float min, float max, float a1, float a2, char *tip)
-
-UiDefButC operatates on char
-UiDefButS operatates on short
-UiDefButI operatates on int
-UiDefButF operatates on float
-
-*block: current uiBlock pointer
-type: see below
-retval: return value, which is put back in queue
-*str: button name
-x1, y1: coordinates of left-lower corner
-x2, y2: width, height
-*poin: pointer to char, short, int, float
-min, max used for slider buttons
-a1, a2 extra info for some buttons
-*tip: tooltip string
-
-type:
-
-1. BUT
- Activation button. (like "Render")
- Passing on a pointer is not needed
-
-2. TOG or TOGN or TOGR
- Toggle button (like "Lock")
- The pointer value is set either at 0 or 1
- If pressed, it calls the optional function with arguments provided.
- Type TOGN: works negative, when pressed it sets at 0
- Type TOGR: is part of a row, redraws automatically all buttons with same *poin
-
- "|BIT|<nr>"
- When added to type, it works on a single bit <nr> (lowest order bit: nr = '0')
-
-3. TOG3|BIT|<nr>
- A toggle with 3 values!
- Can be only used for short *poin.
- In the third toggle setting, the bit <nr> of *( poin+1) is set.
-
-4. ROW
- Button that's part of a row.
- in "min" you set a row-id number, in "max" the value you want *poin to be
- assigned when you press the button. Always pass on these values as floats.
- When this button is pressed, it sets the "max" value to *poin, and redraws
- all buttons with the same row-id number.
-
-5. SLI or NUMSLI or HSVSLI
- Slider, number-slider or hsv-slider button.
- "min" and "max" are to clamp the value to.
- If you want a button type "Col" to be updated, make 'a1' equal to 'retval'
- from the COL button.
-
-6. NUM
- Number button
- Set the clamping values 'min' and 'max' always as float.
- For UiDefButF, set a 'step' in 'a1', in 1/100's. The step value is the increment or
- decrement when you click once on the right or left side of a button.
- The optional button function is additionally called for each change of the *poin value.
-
-7. TEX
- Text string button.
- Pointertype is standard a char. Value 'max' is length of string (pass as float).
- When button is left with ESC, it doesn't put the 'retval' at the queue.
-
-8. LABEL
- Label button.
- Only displays text.
- If 'min' is set at 1.0, the text is printed in white.
-
-9 SEPR
- A separator line, typically used within pulldown menus.
-
-10. MENU
- Menu button.
- The syntax of the string in *name defines the menu items:
- - %t means the previous text becomes the title
- - item separator is '|'
- - return values are indicated with %x[nr] (i.e: %x12).
- without returnvalues, the first item gets value 0 (incl. title!)
- Example: "Do something %t| turn left %2| turn right %1| nothing %0"
-
-11. COL
- A special button that only visualizes a RGB value
- In 'retval' you can put a code, which is used to identify for sliders if it needs
- redraws while using the sliders. Check button '5'.
- As *poin you input the pointer to the 'r' value, 'g' and 'b' are supposed to be
- next to that.
-
-
-------------4.2 Icon buttons
-
-Instead of a 'name', all buttons as described for uiDefBut also can have an icon:
-
-uiBut *uiDefIconBut(uiBlock *block, int type, int retval, int icon,
- short x1, short y1, short x2, short y2, void *poin,
- float min, float max, float a1, float a2, char *tip)
-
- Same syntax and types available as previous uiDefBut, but now with an icon code
- instead of a name. THe icons are numbered in resources.c
-
-uiBut *uiDefIconTextButF(uiBlock *block, int type, int retval, int icon, char *str,
- short x1, short y1, short x2, short y2, float *poin,
- float min, float max, float a1, float a2, char *tip)
-
- Same again, but now with an icon and string as button name.
-
-Two special icon buttons are available in Blender:
-
-12. ICONROW
- (uiDefIconBut)
- This button pops up a vertical menu with a row of icons to choose from.
- 'max' = amount of icons. The icons are supposed to be ordered in a sequence
- It writes in *poin which item in the menu was choosen (starting with 0).
-
-13. ICONTEXTROW
- (uiDefIconTextBut)
- Same as previous, but with the texts next to it.
-
-
-
------------4.3 pulldown menus / block buttons
-
-14. BLOCK
-void uiDefBlockBut(uiBlock *block, uiBlockFuncFP func, void *arg, char *str,
- short x1, short y1, short x2, short y2, char *tip)
-
- This button creates a new block when pressed. The function argument 'func' is called
- to take care of this. An example func:
-
- static uiBlock *info_file_importmenu(void *arg_unused)
- {
- uiBlock *block;
- short yco= 0, xco = 20;
-
- block= uiNewBlock(&curarea->uiblocks, "importmenu", UI_EMBOSSW, UI_HELV, G.curscreen->mainwin);
- uiBlockSetXOfs(block, -40); // offset to parent button
-
- /* flags are defines */
- uiDefBut(block, LABEL, 0, "VRML 2.0 options", xco, yco, 125, 19, NULL, 0.0, 0.0, 0, 0, "");
- uiDefButS(block, TOG|BIT|0, 0, "SepLayers", xco, yco-=20, 75, 19, &U.vrmlflag, 0.0, 0.0, 0, 0, "");
- uiDefButS(block, TOG|BIT|1, 0, "Scale 1/100", xco, yco-=20, 75, 19, &U.vrmlflag, 0.0, 0.0, 0, 0, "");
- uiDefButS(block, TOG|BIT|2, 0, "Two Sided", xco, yco-=20, 75, 19, &U.vrmlflag, 0.0, 0.0, 0, 0, "");
-
- uiBlockSetDirection(block, UI_RIGHT);
- uiTextBoundsBlock(block, 50); // checks for fontsize
-
- return block;
- }
-
- The uiDef coordinates here are only relative. When this function is called, the interface
- code automatically makes sure the buttons fit in the menu nicely.
-
- Inside a menu uiBlock, other uiBlocks can be invoked to make a hierarchical menu.
-
-
-
------------4.4 specials
-
-15. KEYEVT
-
-void uiDefKeyevtButS(uiBlock *block, int retval, char *str,
- short x1, short y1, short x2, short y2, short *spoin, char *tip)
-
- A special button, which stores a keyvalue in *spoin. When the button is pressed,
- it displays the text 'Press any Key'. A keypress then stores the value.
-
-16. LINK and INLINK
-
- These button present a method of linking data in Blender, by drawing a line from one
- icon to another. It consists of two button types:
-
- LINK, the 'linking from' part, can be:
- - a single pointer to data (only one line allowed)
- - an array of pointers to data. The LINK buttons system keeps track of allocating
- space for the array, and set the correct pointers in it.
-
- INLINK, the 'linking to' part activates creating a link, when a user releases the mouse
- cursor over it, while dragging a line from the LINK button.
-
- These buttons are defined as follows:
-
-
-uiBut but= uiDefIconBut(block, LINK, 0, ICON_LINK, x1, y1, w, h, NULL, 0, 0, 0, 0, "");
- /* create the LINK icon */
-
-uiSetButLink(but, void **pt, void ***ppt, short *totlink, short fromcode, short tocode);
- **pt: pointer to pointer (only one link allowed)
- ***ppt: pointer to pointerpointer (an array of pointers)
- (Either one of these values should be NULL)
-
- fromcode: (currently unused)
- tocode: a short indicating which blocks it can link to.
-
-
-uiDefIconBut(block, INLINK, 0, ICON_INLINK, x1, y1, w, h, void *poin, short fromcode, 0, 0, 0, "");
- poin: the pointer of the datablock you want to create links to
- fromcode: a short identifying which LINK buttons can connect to it
-
-
-
-17. IDPOIN
-void uiDefIDPoinBut(uiBlock *block, uiIDPoinFuncFP func, int retval, char *str,
- short x1, short y1, short x2, short y2, void *idpp, char *tip)
-
- The ID struct is a generic part in structs like Object, Material, Mesh, etc.
- Most linking options in Blender happens using ID's. (Mesh -> Material).
-
- This special button in Blender visualizes an ID pointer with its name. Typing in
- a new name, changes the pointer. For most ID types in Blender functions have been
- written already, needed by this button, to check validity of names, and assign the pointer.
-
- (BTW: the 'void *idpp' has to be a pointer to the ID pointer!)
-
- Example function that can be put in 'func':
-
- void test_scriptpoin_but(char *name, ID **idpp)
- {
- ID *id;
-
- id= G.main->text.first;
- while(id) {
- if( strcmp(name, id->name+2)==0 ) {
- *idpp= id;
- return;
- }
- id= id->next;
- }
- *idpp= 0;
- }
-
-
-------------- 4.5 uiButton control fuctions
-
-
-void uiButSetFunc(uiBut *but, void (*func)(void *arg1, void *arg2), void *arg1, void *arg2)
- When the button is pressed and released, it calls this function, with the 2 arguments.
-
-void uiButSetFlag(uiBut *but, int flag)
- set a flag for further control of button behaviour:
- flag:
- UI_TEXT_LEFT
-
-int uiButGetRetVal(uiBut *but)
- gives return value
-
-
-</body>
-<br><br><br>
diff --git a/doc/guides/python-dev-guide.txt b/doc/guides/python-dev-guide.txt
deleted file mode 100644
index 75c9ccb57e5..00000000000
--- a/doc/guides/python-dev-guide.txt
+++ /dev/null
@@ -1,170 +0,0 @@
-Simple Blender Python Developer's Guide
----------------------------------------
-
-This is an outline for a future guide yet to be written. It is meant for
-programmers wanting to understand and maybe help with the embedding of Python
-inside Blender.
-
-I - Introduction
-
-We could praise Python here for its many qualities, but it's probably better
-to just give some links:
-
-The main site is at www.python.org , with documentation at www.python.org/doc/
-
-Also worth of mention: it's an interpreted language and is available for
-many different systems. The download includes the interpreter, many modules
-(think libs), good documentation and some programs / examples. If you use
-linux, there's a high chance you already have Python installed, just try
-"man python".
-
-The reason for embedding a language environment inside Blender is to give
-users the ability to access the program's internal data and functionality.
-This can be used to import / export (from / to other 2d / 3d formats) or
-change the data (to create new objects procedurally, among many other
-interesting possibilities). Script writers (Blender Python programmers) can
-also expand Blender in new ways, adding new features on-the-fly, without having
-to recompile it. It is usually much easier and faster to write scripts in
-Python than to code the equivalent in C.
-
-II - Reference material:
-
-There are two important texts for us in the documentation that comes
-with Python ( docs also available online at www.python.org ):
-
-- Extending and Embedding (tutorial for C/C++ programmers)
-
-and specially
-
-- Python/C API.
-
-You can read the first one to get a feel for how things are done
-(reference counting is probably the most important part), but the second
-doc is a must. Specially useful as a fast reference is its Index, at letter
-P, where all commands are.
-
-Specially useful commands are Py_BuildValue and the family of parsing
-functions, PyArg_Parse* (PyArg_Parse(), PyArg_ParseTuple(),
-PyArg_ParseTupleAndKeywords()). Py_BuildValue is usually the best way to make
-Python Objects (the 'variables' that the Python Interpreter understands)
-out of C ones. The PyArg_Parse* functions do the opposite, they parse
-Python Objects to C variables.
-
-So, understand PyArg_Parse* functions, Py_BuildValue and reference
-counting. The first doc has a good discussion about them.
-
-- C knowledge is also necessary, of course, use your favorite resource.
-
-- The Blender 2.25 API documentation ( www.blender.org ) is, along with
-the source, our basic API ref.
-
-III - Directories
-
-The previous Blender Python API's are spread in blender/intern/python
-and the C part of the current one, bpython, is at
-blender/source/blender/bpython/, specially in intern/. The current
-solution is a Python wrapper on top of this bpython one, at
-blender/intern/python/modules/Blender/
-
-Note: since it's in Python, they needed the freeze Python utility, a
-process/program that creates stand-alone executables out of Python
-source files -- that is, it packs together an interpreter, the needed
-modules and the source of a Python program so that users of this program
-don't need to have the Python interpreter already installed in their
-machines to run the program -- Blender, in this case.
-
-The new implementation is pure C, so we won't need to "freeze" it.
-
-Another important dir for starters is blender/source/blender/makesdna,
-where the headers with Blender structs lie.
-
-IV - Experimental Python
-
-The new implementation, currently referred to as experimental python -
-exppython - was started by Michel Selten. He chose to solve the mess in
-Blender Python by starting over from scratch, in C, but keeping API
-compatibility with the current 2.25 API used by Blender.
-
-It is in blender/source/blender/python , more specifically inside
-api2_2x/
-
-To make it clear, exppython is the new implementation being worked on. It
-will possibly become the de-facto implementation in Blender 2.28, the next
-Blender version. Currently, Blender still comes with the same implementation
-found in the 2.25 version of the program. So we call that the 2.25
-implementation, or bpython.
-
-BPython had plenty of "macro magic", lot's of complicate #define's, etc.,
-since a lot of the embedding work is quite repetitive. But that makes it
-much harder for newbies to jump in and learn, so the new files in exppython
-avoid that.
-
-This means: Blender, Object, Camera, Lamp, Image, Text, Window modules
-(the files have the same names, ending obviously with .c and .h)
-
-To speed things up, some independent parts of bpython are being
-integrated directly into exppython. That already happened with Draw and
-BGL, both taken from opy_draw.c in the bpython/intern dir. The same is
-happening with NMesh (Mesh is written in Python and imports NMesh to
-extend / change its functionality).
-
-For a good example of dexterity with macros (cheers to the NaN
-programmer(s)!), look at BGL.[ch], the OpenGL API wrapper. The defines
-are in the header.
-
-Besides keeping compatibility with the 2.25 API, there are already some
-additions to exppython:
-
-- some modules have access to more variables than 2.25 had;
-- there are more method functions and the access is safer;
-- the file selector (or file browser, if you prefer) is back:
- It's now in the Window module, along with an image selector, too.
-- there are totally new modules, unavailable in 2.25:
- Fellow new developers joining our team are contributing new modules
- that have been requested by the community for a long time.
-
-
-V - Coding
-
-The Camera module is a good reference, since it is like most others, in
-terms of programming, but is smaller and simple. It's in Camera.c and
-Camera.h . To have it working, it was also necessary to include a line to
-the end of Blender.c (registering it as a Blender submodule) and another to
-modules.h (declaring its init and CreateObject method)
-
-Currently, one of our conventions is to prepend M_ to module functions,
-doc strings, etc. and C_ to the new types we had to create for Python,
-like C_Camera, C_Lamp, etc.
-
-If you look at Camera.[ch], you'll find code for creating the Camera
-module and the Camera "type", with all its methods and access policies.
-It's really a new type defined in Python, like PyInt or PyFloat,
-PyString, etc. In practice, it's a "thin" (because it doesn't make
-copies of the variables) wrapper for the Blender Camera Data Object.
-
-A note about Blender: objects in Blender share a common base, the
-Object, whose attributes are things like the matrix, the location, the
-rotation, the size, etc. A Camera is actually an Object of type Camera
-(which means that its "data" field points to a Camera Data obj) and a
-Camera Data object, which is the specific camera part of the object
-(attributes like lens, clip start, etc.). Same for other objects, like
-Lamp, Mesh, etc.
-
-That's why C_Camera is a wrapper for the Blender Camera **Data**
-object. The full wrapper is Object("Camera") linked with
-Camera("camera_name").
-
-How to write a new module for a simple object? Use Camera.[ch] as
-templates, check the specifics of your object in the makesdna dir
-(for example, the camera one is DNA_camera_types.h) and make the
-necessary changes.
-
-If you want to help exppython and in the process possibly learn more about
-embedding, the Python/C API and Blender internals, there's this mailing list:
-
-Bf-python mailing list
-Bf-python@blender.org
-http://www.blender.org/mailman/listinfo/bf-python
-
-There you can ask what hasn't been done yet, get help, make suggestions for
-new features we should consider, send bug reports, etc.
diff --git a/doc/license/BL-license.txt b/doc/license/BL-license.txt
deleted file mode 100644
index 3f079767198..00000000000
--- a/doc/license/BL-license.txt
+++ /dev/null
@@ -1,35 +0,0 @@
-Blender License (the "BL", see http://www.blender.org/BL/ ).
-
-Copyright (C) 2002-2005 Blender Foundation. All Rights Reserved.
-
-This text supersedes the previous BL description, called Blender License 1.0.
-
-When the Blender source code was released in 2002, the Blender Foundation reserved
-the right to offer licenses outside of the GNU GPL. This so-called "dual license"
-model was chosen to provide potential revenues for the Blender Foundation.
-
-The BL has not been activated yet. Partially because;
-
-- there has to be a clear benefit for Blender itself and its community of
- developers and users.
-- the developers who have copyrighted additions to the source code need to approve
- the decision.
-- the (c) holder NaN Holding has to approve on a standard License Contract
-
-But most important;
-
-- the Blender Foundation is financially healthy, based on community support
- (e-shop sales), sponsoring and subsidy grants
-- current focus for the Blender Foundation is to not set up any commercial
- activity related to Blender development.
-- the GNU GPL provides sufficient freedom for third parties to conduct business
- with Blender
-
-For these reasons we've decided to cancel the BL offering for an indefinite period.
-
-Third parties interested to discuss usage or exploitation of Blender can email
-license@blender.org for further information.
-
-Ton Roosendaal
-Chairman Blender Foundation.
-June 2005
diff --git a/doc/license/GPL-license.txt b/doc/license/GPL-license.txt
deleted file mode 100644
index 8860b2a8afa..00000000000
--- a/doc/license/GPL-license.txt
+++ /dev/null
@@ -1,340 +0,0 @@
- GNU GENERAL PUBLIC LICENSE
- Version 2, June 1991
-
- Copyright (C) 1989, 1991 Free Software Foundation, Inc.
- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- Everyone is permitted to copy and distribute verbatim copies
- of this license document, but changing it is not allowed.
-
- Preamble
-
- The licenses for most software are designed to take away your
-freedom to share and change it. By contrast, the GNU General Public
-License is intended to guarantee your freedom to share and change free
-software--to make sure the software is free for all its users. This
-General Public License applies to most of the Free Software
-Foundation's software and to any other program whose authors commit to
-using it. (Some other Free Software Foundation software is covered by
-the GNU Library General Public License instead.) You can apply it to
-your programs, too.
-
- When we speak of free software, we are referring to freedom, not
-price. Our General Public Licenses are designed to make sure that you
-have the freedom to distribute copies of free software (and charge for
-this service if you wish), that you receive source code or can get it
-if you want it, that you can change the software or use pieces of it
-in new free programs; and that you know you can do these things.
-
- To protect your rights, we need to make restrictions that forbid
-anyone to deny you these rights or to ask you to surrender the rights.
-These restrictions translate to certain responsibilities for you if you
-distribute copies of the software, or if you modify it.
-
- For example, if you distribute copies of such a program, whether
-gratis or for a fee, you must give the recipients all the rights that
-you have. You must make sure that they, too, receive or can get the
-source code. And you must show them these terms so they know their
-rights.
-
- We protect your rights with two steps: (1) copyright the software, and
-(2) offer you this license which gives you legal permission to copy,
-distribute and/or modify the software.
-
- Also, for each author's protection and ours, we want to make certain
-that everyone understands that there is no warranty for this free
-software. If the software is modified by someone else and passed on, we
-want its recipients to know that what they have is not the original, so
-that any problems introduced by others will not reflect on the original
-authors' reputations.
-
- Finally, any free program is threatened constantly by software
-patents. We wish to avoid the danger that redistributors of a free
-program will individually obtain patent licenses, in effect making the
-program proprietary. To prevent this, we have made it clear that any
-patent must be licensed for everyone's free use or not licensed at all.
-
- The precise terms and conditions for copying, distribution and
-modification follow.
-
- GNU GENERAL PUBLIC LICENSE
- TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
-
- 0. This License applies to any program or other work which contains
-a notice placed by the copyright holder saying it may be distributed
-under the terms of this General Public License. The "Program", below,
-refers to any such program or work, and a "work based on the Program"
-means either the Program or any derivative work under copyright law:
-that is to say, a work containing the Program or a portion of it,
-either verbatim or with modifications and/or translated into another
-language. (Hereinafter, translation is included without limitation in
-the term "modification".) Each licensee is addressed as "you".
-
-Activities other than copying, distribution and modification are not
-covered by this License; they are outside its scope. The act of
-running the Program is not restricted, and the output from the Program
-is covered only if its contents constitute a work based on the
-Program (independent of having been made by running the Program).
-Whether that is true depends on what the Program does.
-
- 1. You may copy and distribute verbatim copies of the Program's
-source code as you receive it, in any medium, provided that you
-conspicuously and appropriately publish on each copy an appropriate
-copyright notice and disclaimer of warranty; keep intact all the
-notices that refer to this License and to the absence of any warranty;
-and give any other recipients of the Program a copy of this License
-along with the Program.
-
-You may charge a fee for the physical act of transferring a copy, and
-you may at your option offer warranty protection in exchange for a fee.
-
- 2. You may modify your copy or copies of the Program or any portion
-of it, thus forming a work based on the Program, and copy and
-distribute such modifications or work under the terms of Section 1
-above, provided that you also meet all of these conditions:
-
- a) You must cause the modified files to carry prominent notices
- stating that you changed the files and the date of any change.
-
- b) You must cause any work that you distribute or publish, that in
- whole or in part contains or is derived from the Program or any
- part thereof, to be licensed as a whole at no charge to all third
- parties under the terms of this License.
-
- c) If the modified program normally reads commands interactively
- when run, you must cause it, when started running for such
- interactive use in the most ordinary way, to print or display an
- announcement including an appropriate copyright notice and a
- notice that there is no warranty (or else, saying that you provide
- a warranty) and that users may redistribute the program under
- these conditions, and telling the user how to view a copy of this
- License. (Exception: if the Program itself is interactive but
- does not normally print such an announcement, your work based on
- the Program is not required to print an announcement.)
-
-These requirements apply to the modified work as a whole. If
-identifiable sections of that work are not derived from the Program,
-and can be reasonably considered independent and separate works in
-themselves, then this License, and its terms, do not apply to those
-sections when you distribute them as separate works. But when you
-distribute the same sections as part of a whole which is a work based
-on the Program, the distribution of the whole must be on the terms of
-this License, whose permissions for other licensees extend to the
-entire whole, and thus to each and every part regardless of who wrote it.
-
-Thus, it is not the intent of this section to claim rights or contest
-your rights to work written entirely by you; rather, the intent is to
-exercise the right to control the distribution of derivative or
-collective works based on the Program.
-
-In addition, mere aggregation of another work not based on the Program
-with the Program (or with a work based on the Program) on a volume of
-a storage or distribution medium does not bring the other work under
-the scope of this License.
-
- 3. You may copy and distribute the Program (or a work based on it,
-under Section 2) in object code or executable form under the terms of
-Sections 1 and 2 above provided that you also do one of the following:
-
- a) Accompany it with the complete corresponding machine-readable
- source code, which must be distributed under the terms of Sections
- 1 and 2 above on a medium customarily used for software interchange; or,
-
- b) Accompany it with a written offer, valid for at least three
- years, to give any third party, for a charge no more than your
- cost of physically performing source distribution, a complete
- machine-readable copy of the corresponding source code, to be
- distributed under the terms of Sections 1 and 2 above on a medium
- customarily used for software interchange; or,
-
- c) Accompany it with the information you received as to the offer
- to distribute corresponding source code. (This alternative is
- allowed only for noncommercial distribution and only if you
- received the program in object code or executable form with such
- an offer, in accord with Subsection b above.)
-
-The source code for a work means the preferred form of the work for
-making modifications to it. For an executable work, complete source
-code means all the source code for all modules it contains, plus any
-associated interface definition files, plus the scripts used to
-control compilation and installation of the executable. However, as a
-special exception, the source code distributed need not include
-anything that is normally distributed (in either source or binary
-form) with the major components (compiler, kernel, and so on) of the
-operating system on which the executable runs, unless that component
-itself accompanies the executable.
-
-If distribution of executable or object code is made by offering
-access to copy from a designated place, then offering equivalent
-access to copy the source code from the same place counts as
-distribution of the source code, even though third parties are not
-compelled to copy the source along with the object code.
-
- 4. You may not copy, modify, sublicense, or distribute the Program
-except as expressly provided under this License. Any attempt
-otherwise to copy, modify, sublicense or distribute the Program is
-void, and will automatically terminate your rights under this License.
-However, parties who have received copies, or rights, from you under
-this License will not have their licenses terminated so long as such
-parties remain in full compliance.
-
- 5. You are not required to accept this License, since you have not
-signed it. However, nothing else grants you permission to modify or
-distribute the Program or its derivative works. These actions are
-prohibited by law if you do not accept this License. Therefore, by
-modifying or distributing the Program (or any work based on the
-Program), you indicate your acceptance of this License to do so, and
-all its terms and conditions for copying, distributing or modifying
-the Program or works based on it.
-
- 6. Each time you redistribute the Program (or any work based on the
-Program), the recipient automatically receives a license from the
-original licensor to copy, distribute or modify the Program subject to
-these terms and conditions. You may not impose any further
-restrictions on the recipients' exercise of the rights granted herein.
-You are not responsible for enforcing compliance by third parties to
-this License.
-
- 7. If, as a consequence of a court judgment or allegation of patent
-infringement or for any other reason (not limited to patent issues),
-conditions are imposed on you (whether by court order, agreement or
-otherwise) that contradict the conditions of this License, they do not
-excuse you from the conditions of this License. If you cannot
-distribute so as to satisfy simultaneously your obligations under this
-License and any other pertinent obligations, then as a consequence you
-may not distribute the Program at all. For example, if a patent
-license would not permit royalty-free redistribution of the Program by
-all those who receive copies directly or indirectly through you, then
-the only way you could satisfy both it and this License would be to
-refrain entirely from distribution of the Program.
-
-If any portion of this section is held invalid or unenforceable under
-any particular circumstance, the balance of the section is intended to
-apply and the section as a whole is intended to apply in other
-circumstances.
-
-It is not the purpose of this section to induce you to infringe any
-patents or other property right claims or to contest validity of any
-such claims; this section has the sole purpose of protecting the
-integrity of the free software distribution system, which is
-implemented by public license practices. Many people have made
-generous contributions to the wide range of software distributed
-through that system in reliance on consistent application of that
-system; it is up to the author/donor to decide if he or she is willing
-to distribute software through any other system and a licensee cannot
-impose that choice.
-
-This section is intended to make thoroughly clear what is believed to
-be a consequence of the rest of this License.
-
- 8. If the distribution and/or use of the Program is restricted in
-certain countries either by patents or by copyrighted interfaces, the
-original copyright holder who places the Program under this License
-may add an explicit geographical distribution limitation excluding
-those countries, so that distribution is permitted only in or among
-countries not thus excluded. In such case, this License incorporates
-the limitation as if written in the body of this License.
-
- 9. The Free Software Foundation may publish revised and/or new versions
-of the General Public License from time to time. Such new versions will
-be similar in spirit to the present version, but may differ in detail to
-address new problems or concerns.
-
-Each version is given a distinguishing version number. If the Program
-specifies a version number of this License which applies to it and "any
-later version", you have the option of following the terms and conditions
-either of that version or of any later version published by the Free
-Software Foundation. If the Program does not specify a version number of
-this License, you may choose any version ever published by the Free Software
-Foundation.
-
- 10. If you wish to incorporate parts of the Program into other free
-programs whose distribution conditions are different, write to the author
-to ask for permission. For software which is copyrighted by the Free
-Software Foundation, write to the Free Software Foundation; we sometimes
-make exceptions for this. Our decision will be guided by the two goals
-of preserving the free status of all derivatives of our free software and
-of promoting the sharing and reuse of software generally.
-
- NO WARRANTY
-
- 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
-FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
-OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
-PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
-OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
-TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
-PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
-REPAIR OR CORRECTION.
-
- 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
-WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
-REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
-INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
-OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
-TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
-YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
-PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
-POSSIBILITY OF SUCH DAMAGES.
-
- END OF TERMS AND CONDITIONS
-
- How to Apply These Terms to Your New Programs
-
- If you develop a new program, and you want it to be of the greatest
-possible use to the public, the best way to achieve this is to make it
-free software which everyone can redistribute and change under these terms.
-
- To do so, attach the following notices to the program. It is safest
-to attach them to the start of each source file to most effectively
-convey the exclusion of warranty; and each file should have at least
-the "copyright" line and a pointer to where the full notice is found.
-
- <one line to give the program's name and a brief idea of what it does.>
- Copyright (C) <year> <name of author>
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-
-
-Also add information on how to contact you by electronic and paper mail.
-
-If the program is interactive, make it output a short notice like this
-when it starts in an interactive mode:
-
- Gnomovision version 69, Copyright (C) year name of author
- Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
- This is free software, and you are welcome to redistribute it
- under certain conditions; type `show c' for details.
-
-The hypothetical commands `show w' and `show c' should show the appropriate
-parts of the General Public License. Of course, the commands you use may
-be called something other than `show w' and `show c'; they could even be
-mouse-clicks or menu items--whatever suits your program.
-
-You should also get your employer (if you work as a programmer) or your
-school, if any, to sign a "copyright disclaimer" for the program, if
-necessary. Here is a sample; alter the names:
-
- Yoyodyne, Inc., hereby disclaims all copyright interest in the program
- `Gnomovision' (which makes passes at compilers) written by James Hacker.
-
- <signature of Ty Coon>, 1 April 1989
- Ty Coon, President of Vice
-
-This General Public License does not permit incorporating your program into
-proprietary programs. If your program is a subroutine library, you may
-consider it more useful to permit linking proprietary applications with the
-library. If this is what you want to do, use the GNU Library General
-Public License instead of this License.
diff --git a/doc/license/bf-members.txt b/doc/license/bf-members.txt
deleted file mode 100644
index 41aad8b7264..00000000000
--- a/doc/license/bf-members.txt
+++ /dev/null
@@ -1,1393 +0,0 @@
-Blender Foundation Members
-
-Special thanks to the following people, who have supported the Blender Foundation
-by donating at least USD 50 to pay for opening the Blender sources:
-(in chronological order):
-
-Bradley Keyes
-Robert Knop
-Thomas Brown
-Adam Goodenough
-Steven Tally
-Jason Tally
-Alan Taylor
-Pascal COMPAGNON
-Christopher Koppler
-Matthew P. Carter
-Gideon Vaala
-F.O. Bossert
-Frank Mayhar
-Peter Volgyesi
-Mark E. Nelmes
-Peter Lammers
-Craig Maloney
-daniel e vertseegh
-Freek Zijlmans
-Anthony Ogden
-Anders Dys
-Gerald Saunders
-Fernando Cordas
-Joshua Smith
-Max R. Huskins
-imarend
-Olaf Arnold
-Eric Van Hensbergen
-Christian Reichlin
-brian moore
-Anthony Walker
-Carsten Hšfer
-Raymond Fordham
-David Megginson
-Jason Schmidt
-Christopher Walden
-Robert van Straten
-Daniel Schwartz
-ekzakt
-Jellybean
-Streit Eric
-Bob Ippolito
-Keith Frederick
-Ryan Heimbuch
-Martin Hess
-Shigeo Yoshida
-Rens Houben
-Jun IIO
-Derek Gladding
-Adam Wendt
-Habib Loew
-Jani Turkia
-Warren Landon
-Chip Lynch
-Hans Ruedisueli
-Keith Jones
-Eugenio Mario Longo
-Philippe Tanguay
-nekurai
-Ricardo Kustner
-Peter Van Loock
-Jun-ichi OKAMURA
-alain dejoux
-dario trombino
-Kenneth Johansson
-Felix Schwarz
-Eugenio Diaz
-Aoki Katsutoshi
-Norman J. Harman Jr.
-Ralf Habel
-Ken Payne
-DEBEUX Sylvain
-Douglas Philips
-Lai Che Hung
-Johan Bolmsjš
-Aaron Mitchell
-Teinye Horsfall
-Martin Marbach
-Jason Poole
-Cesar Delgado
-Gareth Clay
-Paul Wasserman
-Joeri Sebrechts
-Johannes Lehtinen
-Marcio L. Teixeira
-James A. Peltier
-George E. Nimmer III
-Matthew Fite
-Ken Sedgwick
-Gary Baltzell
-lukas schroeder
-Dan Lyke
-Mark Thorpe
-A.D. Arul Nirai Selvan
-Herbert Pštzl
-Andy Payne
-LAFFITTE Benoit (KenjForce)
-James Ertle
-Tom Turelinckx
-Rigo Ketelings
-Bryan Green
-Suzuki Akihiro
-Mason Staugler
-Guillaume Randon
-francois Gombault
-Harald Thelen
-Graziano Roccon
-Jonathan DuBois
-Jason Luros
-Drew M. Snyder
-Jesse DeFer
-Michael Keith
-Gaetano Tinat
-Ferris McCormick
-Sherman Boyd
-Thomas C Anth Jr
-Joseph Catto
-Damian Soto
-John Walsh
-Stephen Rosman
-Ross Hopkins
-Robert Wenzlaff
-Joe Galat
-LinuxFrench.NET
-Thomas Erichsen
-Gary E. Deschaines
-Denis McCarthy
-Michael Dowman
-John (J5) Palmieri
-alphawolf
-Peter Paul Fink
-Charles F. McKnight
-Avi Schwartz
-Jim Witte
-Jens Ansorg
-William Bate
-Ron Romano
-Marc Schraffenberger
-Steve Thompson
-Martin Whitaker
-Kendall Dugger
-Brice Due
-Simon Vogl
-Bernd Koestler
-clŽment CORDIER
-CreationAnimation.com
-Pete Carss
-HERSEN Antoine
-Charles R. Tersteeg
-James T Burnett
-Shawn Legrand
-David Choice
-patrick amaru
-David Eck
-Gabriel Welsche
-Henning von Zitzewitz
-Chris Impink
-Den Giles
-Jon Cisewski
-Ricki Myers
-Luis Collado
-Robert G. Watkins
-Rainer Perl
-YAGI, Toshiki
-Bruno Nitrosso
-Athalyba / Blender Brasil
-Dave Whittington
-Darryl Luscombe
-Benjamin A Moore
-Andreas Kollegger
-Stamatis Logos
-Ray Kelm
-Albrecht Jacobs
-Mirko Horstmann
-Ranjit Mathew
-Jaron Blackburn
-Antonio Hui
-Scott Moore
-CSN media
-Scott Carle
-Ted Milker
-Lars Brinkhoff
-Louise Edwards
-Alex Ruano
-Art Rozenbaum
-Manuel Hossfeld
-Brandon Ehle
-Lou Bruce
-ROMASZEWSKI
-cadic jean-yves
-Ralf Pietersz
-KAZY
-serge Jadot
-HervŽ LOUINET
-Tom Houtman
-Magnus Kššhler
-Martin Sinnaeve
-Kevin Yank
-Tomoichi Iriyama
-THIBAULT
-Dr Steven J. Baines
-Hans-Peter Berger
-Jeffrey D. Holland
-Philip XP Talbot
-Peter Haight
-Krister Bruhwel
-the Smoos
-Hohenegger Michael
-Alejandro Gabriel y Galan
-Boyer
-Alwin Eilers
-Tyberghein Jorrit
-Jaroslav Benkovsky
-Gianluigi Belli
-Naoki Abe
-NOTTE Jean-Pierre
-Jack Way
-Bjšrn Westlin
-Mitch Magee
-wizoptic
-Jake Edge
-David Hoover
-Xabier Go–i
-Daniel Fort
-Erik Noteboom
-Pavel Vostatek
-Javier Belanche Alonso
-Jeffrey Blank
-Nathan Ryan
-Peter Wrangell
-Josef Sie§
-Timm Krumnack
-Steve Martin
-Shigeru Matsumoto
-Marko Kohtala
-Aleix Conchillo
-Curtis Smith
-Tatjana Svizensky
-Matt Graham
-Karo Launonen
-Nikolaj Brandt Jensen
-Jean-Michel Smith
-Nathan Clegg
-Joaquim Carvalho
-Bomholt
-Martin Imobersteg
-Jordan
-Justin Warwick
-barussaud
-Eric Molbert
-Ben Berck
-Jacco van Beek
-Dominic Agoro-Ombaka
-Colin Foster
-Sascha Adler
-Stuart Duncan
-Brendon Smith
-SŽbastien COLLETTE
-Clemens Auer
-Kay Fricke
-Fabian Fagerholm
-Norman Lin
-Aaron Digulla
-Camilo Moreno
-Detlev Reppin
-Denis NICOLAS
-Armin Antons
-Darren Heighington
-Mario Latronico
-Frank Meier
-Joerg Frochte
-Matthew Ingle
-Anters
-Bjoern C. Schaefer
-STEMax
-Jeff Cox
-Trevor Ratliff
-Matt Henley
-Franois VALETTE
-Rob Saunders
-Mike Luethi
-Rami Aubourg-Kaires
-Matthew Thomas
-Allan Jacobsen
-Adam Lowe
-David Hostetler
-Lo•c Vigneras
-Dan Reiland
-Pedro D’az del Arco
-Pierre Beyssac
-Chris Davey
-YOSHIAKI Nishino
-Cyber Perk
-Fabrizio Cali
-Harunobu Yoda
-Gerrit Jan Baarda
-LarstiQ
-Ico Doornekamp
-Emiel den Biesen
-Willem Zwarthoed
-Carl Giesberts
-Buzz
-lieve vanderschaeve
-Margreet de Brie
-hans verwaal
-Saskia van Dijk
-Martin Gosch
-Alexander Konopka
-Thomas Gebert
-Gerard de Vries
-Hans Lambermont
-Kim Beumer
-Kay Thust
-Alexander Jung
-Tony Corlett
-Olivier Thouverey
-Hideo Nomoto
-Giel Peters
-Ray Poynter
-Edoardo Galvagno
-Pim Feijen
-Hua-lun Li
-William Reynish
-Bryan Carpenter
-Jim Scott
-NGUYEN Fabien
-Dave Stone
-Andy Kugyelka
-Andrew Ross
-MerieJean-Christophe
-Marko Mestrovic
-Jack
-Lars Gunnar West
-Wile E. Coyote
-Rick Clark
-Carlos A. Duque
-Franck Barnier
-Matt Shirley
-Viktor Zacek
-Peter Williamson
-Chema Celorio
-Paul Cleveland
-Guo-Rong Koh
-Andrew Potter
-Daniel Smeltzer
-Cativo
-Dmitri Bichko
-Jeremy Brinkley
-Stephen Saville
-Michael Proto
-Kurt Korbatits
-Ruan Muller
-Mike Beggs
-Beat Schiess
-Sven Marcel Buchholz
-Enrique Monfort
-Simon Posnjak
-Steven Ahkim
-theGyre
-William Marble
-Allen Smith
-Joshua SS Miller
-Mike Edwards (PinkFreud)
-Mark D. Butala
-Auriea Harvey&Michael Samyn
-Randy Newman
-Leslie Spring
-Marc Schefer
-Dean Edmonds
-David Whitehouse
-Tim Humphrey
-Phillip Richdale
-Jason McHugh
-Claudio de Sario
-Artur Kedzierski
-Richard Garcia
-Isaac Rivas Alvarez
-Kiernan Holland
-Holger Malessa
-Masanori Okuda
-Andrea Maracci
-Kai-Peter BŠckman
-Gregg Patton
-Luis M Ibarra
-Julian Green
-Christian Bender
-Mark Winkelman
-Ebbe L. Nielsen
-Carlos Orozco
-magnetHead
-KrŸckel Oliver
-Thomas Ingham
-Wes Devauld
-Uwe Steinmann
-Jesus Gonzalez
-DenMeridiS
-Marc Hofer
-Marko Mestrovic
-Leslie Owen Everett
-Knut Bueltemann
-Eric Garaventa
-Wolfram Schwenzer
-Edwin Brouwer
-mauro ujetto
-Franck Vibert
-Daniel Pacheco
-Justin Shumaker
-Steve Wallace
-Forensic Engineering Inc.
-Steve Mackay
-NDNWarrior
-Christopher Gray
-Darius Clarke (Socinian)
-Jean-SŽbastien SEVESTRE
-Douglas Fellows
-Craig Symons
-Quincin Gonjon
-Brian B. Kelley
-Kie Brooks
-Scott Whitney
-kwbrads
-Keijiro Takahashi
-Geno Ruffalo
-Zzeb
-Peter Dohnt
-Jeff E Schram
-ernst bunders
-Nobutoshi Shigemori
-Mariano Aured
-Rab Gordon
-Kurt Maurer
-Ron Cavagnaro (doogs)
-Johan Bjorklund
-Cyril M. Hansen
-JOSEPH GRACI
-Rafael Rubio
-fabrice meynard
-Emilio Aguirre
-Mark Lewis
-Markus Q. Roberts
-Christoher Bartak
-Peter Truog
-Eric Dynowski
-Philipp GŸhring
-Pierre-Yves RANNO
-Jason Nollan (Cluh)
-Launay Jean-Claude
-Wayne Buckhanan
-Jan Magne Landsvik
-Douglas Dragan Njegovan
-Brian David Hale
-Randal Walser
-Matthew A. Nicholson
-Thomas
-Phillip Kinsley
-Jerome Zago
-David Rosky
-TROJANI Cedric
-David Polston
-Patrick Mullen
-Tetsuya Okuno
-Bodo JŠger
-Leon Brooks
-Cedric Adjih
-Michael L. Fisher
-Dan Tomalesky
-Sky Schulz
-Scott Brickert
-James Purdon
-Miles Langham
-N Yiannakoulias
-Stephen Swaney
-www.artwave.nl
-Take Vos
-sspickle
-Denis Oliver Kropp
-Gustavo
-rodgers whittington jr
-George Dvorak
-Norman J Davis, Jr.
-Javier Velazquez
-John Tougas
-John Howland
-Steve Organek
-Jano Lukac
-David Cardon
-Mick Balaban
-Michael Kaufman
-Viper Expo
-Chassoulier
-Sjoerd Zaalberg van Zelst
-B. Douglas Hilton
-Benoit CANTAGREL
-Kai Fischer
-Mark Osborne
-Michael Hollenbeck
-iconjunky
-luca de giorgi
-Jonathan Mullen
-Javier Fernandez-Ivern
-Reuben Burgess
-Olivier Scalbert
-claudio luis Rogers
-Jeremy Peterson
-Mark Streutker
-Lourens Veen
-Neil Davis
-John McGurk
-Stephen Teegarden
-Dave LeCompte
-Michael Lang
-Mortal_Enemy
-Arigo Stanescu
-Vincent Stoessel
-Adrian Virnig
-Chris Dixon
-Travis Cole
-MŒrten MŒrtensson
-Evan Scott
-Mark Coletti
-Ken Burke
-Karl Holland
-ScRaze
-Wilco Wilbrink
-chris montijn
-Michael Wagenmann
-Viktor Pracht
-henrik wilming
-Jim Evins
-Christophe Massaloux
-Marc Sommer
-Luc Stepniewski
-Eric M. Clark
-Iain Russell
-Thomas Bleicher
-Anthony Zishka
-Jefferson Dubrule
-Esa PiirilŠ
-Bill Thiede
-William Anderson
-Alexander Kittel
-Joe Tennies
-Sam Cable
-Christian Peper
-Joshua Frederick
-Steve Sutton
-Pete Toscano
-Michael Zucchi
-Peter Degenhardt
-Horizont Entertainment
-Olivier Bornet
-Richard D. Laudenslager
-sha
-John DiLauro
-John Miller
-Frederic Crozat
-Matt Welland
-Paul CalcŽ
-Joe Prochilo
-Justin Shafer
-Joe Croft
-Detlef Mueller
-Raschbacher Thomas
-Alain Gallo
-Phuoc Ngo
-Krabat
-Derek Harmison
-SŽbastien Devine
-Kenneth P. Stox
-Wade Felde
-Kai Groshert
-Michael Soibelman
-janine dalisda-davis
-Philippe Raxhon
-Daniel Sheridan
-Michael Pressler
-Daniel Lundqvist
-Tim Oertel
-James Ahlschwede
-William Henstrom
-PETIT-PHAR Patrice
-Marc-Aurele DARCHE
-Wei-ju Wu
-Robert Wilson
-Gerald Severs
-Alejandro Conty Estevez
-Tetsuya Yatagai
-David Dolder
-Luigi Cristiano
-Posse Press / Romain Canonge
-Vania Smrkovski
-Bartosch Pixa
-Dr. Jens Rosenboom
-Gunnar Stahl
-Robert Kroeger
-Martin Forisch
-Guillermina Manon
-Randal D. Adams
-Kevin Reagh (kevin3D)
-Wolfgang KŸhn
-BEAUSOLEIL Arnaud
-Stan Jakubek
-Klaus Brand
-Holger Mueller
-Fankhauser
-Jon McCallum
-Rob Olsen
-Joel Davis
-Kenneth Bledsoe
-James F. Campanella
-Jens Kilian
-wim hendrix
-Karri Kaksonen
-Luis Roldan
-Jason L. Shiffer
-Jacco Marks
-Dale Schoeck
-Agustin Catalan
-A. T. Meinen
-Kamiel Wanrooij
-Samuel Seay
-Mike Schaudies
-Robert Sherwood
-Fernando Villalon Panzano
-Jšrg Roschke
-Carl Symons
-Peter Pichler
-Alan Heckart
-seth nagy
-pelletier
-Jeff Kowalczyk
-LIONEL ALLORGE
-Bhishma Patel
-Richard Rowell
-Barbara Seaton
-Aschwin van der Woude
-William J Black
-Kars Meyboom
-Glen Nakamura
-Jim Belcher
-Tim Scott
-mea van amstel
-Robert-Reinder Nederhoed
-Neil Hunt
-WizardNx
-Brad Choate
-scott schoen
-Dan Merillat
-Martin Krueger
-Samuel Greenfeld
-Damon Tkoch
-Colin Millar
-Mike Roseman
-Patrick Trussell
-Hans-Peter Bock
-Daniel Haertle
-Wiremu Te Kani
-Joep Mathijssen
-LATRY
-Witold Hazuka
-geoff marshall
-dave howard
-Jan Zielinski
-Fernando Buelna Sanchez
-Timo Mihaljov
-Allard Willem van den Brul
-T.J.T van Kooten
-Thijs van der Vossen
-Antonio de Santis
-Jama Poulsen
-Robert Emanuele
-Timothy Lord
-Tom Parker
-Jarrod Willard
-Leon Merts
-Tom Dow
-Eric Anholt
-mercier
-Marc van Kempen
-wim van hoydonck
-Joe Hacobian
-Jose Luis Fresquet Febrer
-toni calmardo
-Sam Littlewood
-Charles Wardlaw
-Javier Rodriguez
-Michael C. Schwab
-Corey Bell
-Emmanuel Preveraud de La Boutresse
-Davide Desogus
-Larry Phillips
-Marko Teiste
-John Roesink
-Legrand Johan
-Jeff Boody
-James Turner
-Peter Petrakis
-Ravi Ravindran
-dickie
-Rene Geerlings
-Richard Braakman
-Arthur Frenslich
-Carsten Schmidt
-Ronny Martin
-Erwin Kuiper
-Lopez-Garcia, Juan-Carlos
-Rau, Bernhard
-Stephen Uithoven
-Ken Beyer
-Matjaz Jakopec
-Eckhard M. JŠger
-Mike Siebach
-John Harger
-Justin W. Carper
-Markus Weber
-Elena Grandi
-Arndt Heuvel
-Miguel Cancela Da Fonseca
-Kirk Lancaster
-Phillip Elliott
-Chris Halsall
-Andre Voitel
-benjamin scheers
-Robinder S. Bains
-David Brunton
-David Brown
-Craig Duncan
-du Song
-Ben Wart
-Eryk Kedzierski
-Ronald Hayden
-Raymond de Vries
-David Johnson
-Kenichi Fujihiro
-WANG
-Chris Weber
-Christian Lehmann
-DENDIEVEL Stephane
-Bryan Jaycox
-Karl Bartel
-Ralph Howes
-Matthew J. Stott
-Omar Priego
-ke Westerstršm
-Imago Viva
-James E Hill
-Rune Myrland
-James Hill
-Nick
-Ramiro Santos
-Giuseppe Chiesa
-Philippe Ney
-Tom Woodyard
-Jim Gray
-Jim Newman
-Robert I Black III
-Peter Marouelli
-Ian Roberts
-Guy Van Rentergem
-Jose Luis Perez Rubio
-Gabriel Mainberger
-Klein Poelhuis
-Luke Sargeant
-Edomaur
-robert stok
-Karl J. Smith
-Harry Splinter
-Paul Boese
-Dave Mata
-piet visser
-Nicolas Christin
-Erik Zidowecki
-Markus Michael Egger
-Edward R. Helwig
-Alyx Flannery
-raphael betemps
-Masahiro Takahata
-Claude Bilat
-Mario Palomo
-Neipi
-Grethus Bode
-Jan MŸller
-Mark Pearson
-Stanislav Osicka
-DataCare Solutions AG
-Russell Elliott
-Antonio Campos
-bordeaux samuel
-Herman Vereycken
-Sergio Avila
-George Dobrozemsky
-Ocie Mitchell
-Ryuuji Kusajima
-Nick Austin
-Edward Wei
-Gregg Tavares
-Aaron Bredon
-Hideki Suzuki
-josef radinger
-Robert Friemer
-Jšrg Zastrau
-Burton Bicksler
-Kimmo Mšsš
-Robert F Johnson
-Mark Johnson
-Avi Bercovich
-Mike Armitage
-Roy Jones
-Kathleen Sinnott
-Per-Erik Westerberg
-Reid Ellis
-Phillip J. Birmingham
-Ken Jordan
-Duffaud
-Marco Ardito
-Simon Suter
-Tobias Huelsdau
-Winfried EnglŠnder
-Stephen Groundwater
-Joel Ray Holveck
-Mag. Tibor Rudas
-Hartmut Wolf
-Douglas Jones
-brett hartshorn
-Beat MŠgert
-Javon Prince
-bri
-James Klicman
-Harvey Fong
-jeroen v.d. Meulen
-Wim Vandersmissen
-Carlos Moreno Rodr’guez
-Trausti Kristjansson
-Larry Snyder
-olivier
-jip
-Thomas Hintz
-Daan van der Munnik
-gideon
-jared
-Erick I. Jimenez Alvarado
-John Martinez
-Daniel Stephens
-Alaric Haag
-Michael Edwards
-Sam Tregillus
-Joe Pribele
-Jeffry Archambeault
-robert schiffers
-shane smith
-elout de kok
-Felix Esser
-Dietrich Lange
-Richard Clark
-errol garner
-Jose M Cerqueira Esteves
-Jeroen Janssen
-Sven Meyer
-Mike Bayer
-Arash Vahdat
-Bernd Hoppe
-Tobias Geiger
-Ronnie Stevens
-W. van Ophoven
-Artem Baguinski
-Peter Morse
-Nicholas Phillips
-Leo Mannhart
-philippe eygonnet
-Hans Klerk
-wes uchida
-Guido Winkelmann
-Doug Ferguson
-Robert Lindsay
-John Lovgren
-Rogers Cadenhead
-Philippe Crassous
-Dirk Krause
-lexpattison
-Fred Roberts
-Njin-Zu Chen
-GUILLON Marc
-Felix Klock
-Ernesto Salas Rodr’guez
-Pavel Roskin
-Jaap
-Stefan Maass
-Adam Bower
-Gary Thomas
-Chris Gottbrath
-seyssel claude
-Chris Winberry
-Chip Collier
-Balint Reczey
-Nuno Sucena Almeida
-Landon Bradshaw
-Chua Mei Ling
-JC Hythloday
-Dustin Knie
-artshow
-Ralf Gauglitz
-Ted Schundler
-Bernard Hurley
-delerue
-Dirk Behrens
-Doc Holiday
-Wouter Kerkhoven
-Andreas BŸttner
-James Black
-Nicholas Ward
-David Oberbeck
-The Shizit
-Erich Moog
-James Amendolagine
-Alex Penner
-Dieter Finkenzeller
-giol franco
-Tobias Regenbrecht
-Ryan Dewalt
-Spencer Cathey
-Benoit SERRA
-System Impuls
-Jason Tranter
-macherb
-LCC Consulting AG
-Ivan So
-Cori Verschoor
-Henauer Pascal
-Chris Bilderback
-Eddie Sheffield
-DEHEN Pierre
-henk van den toorn
-Ian Whitworth
-Ruud H.G. van Tol
-Pierre Lo Cicero
-Srinivas Digumarty
-digitalvelocity¨
-Alan J Booth
-Tony OBrien
-Douglas Toltzman
-Christophe BOUCHET
-Paul Robertson
-SCHNEIDER Philippe
-Jason Cunliffe
-Dick Damian
-Charles Hakari
-jeff knowlton
-Kevyn Shortell
-robin
-Craig Spitzer
-Jeffrey Van Ness
-Lucas Vergnettes
-Wolfgang Michaelis
-Luis JimŽnez Linares
-Julian Eden
-Ms Lilo von Hanffstengel
-Kurt B. Kaiser
-Mark Ping
-CombŽe
-Diego Matho
-MELIN Philippe
-Damian Sobieralski
-Luigino Masarati
-Leonard Wikberg III
-Tom Howsman
-Christopher R Marquard
-Claus Munk
-Chris D'Iorio
-Graphics Solutions Pty Ltd
-Bernd Stein
-Jose Angel Vallejo Pinto
-Matthew William Turner
-Kelly A. Flinn
-Hai T. Nguyen
-Tim Formica
-Kevin Purrington
-Andreas Lindenblatt
-Brian Musker
-Diego Silvio Novo
-Close Pierre-Yves
-Wayne Pierce
-Hattan Lee
-Didier PEYRET
-Jacquelin GAZEAU
-Kenneth Balmer
-Robert Hwang
-Gaertner, Klaus
-Peter Gumieniak
-Simon Pigot
-Ian Thompson
-Jason Southwell
-Stephan
-Robert Kuehn
-IUT Mesures Physiques:gilbert Garnier CAEN France
-Nigel I Phillip
-powermacg4
-Joan Touzet
-Mike Mormando
-katsuhiko ebisawa
-Baheux Tony
-Randi Joseph
-Rick Comeau
-michiel van der kley
-Thijs Seelen
-Robert Roos
-ton
-david seccombe
-Joshua Eckert
-Joshua Newman
-Paolo Sammicheli
-Gentilini Bruno
-Martin Herren
-STRAPPAZON Mathias
-Steven Gibbs
-Florian Kruesch
-Wesley Blijlevens
-Fabianne Balvedi
-Colin Henry
-Mark Deepak Puttnam
-LE GOULVEN
-evolutie
-Stephane Portha
-Robert Gentner
-David B. Camhy
-RenŽ Foucart
-Coyok Drio
-Mark Ng
-klein michael
-Antonin Kral
-Edward Jomisko
-Wouter de Mik
-Matteo De Simone
-Gal Barak
-Thomas Dyar
-Nathan Allworth
-Dean Giberson
-Larry Cummings
-Eric Augustine
-IngieBee
-Michael Velikanje
-Andre M. Czausov
-H@dj
-Thorsten Burghaus
-Renzo Lauper
-Martin White
-Ferdinand Strixner
-Neil Mannall
-Maxime Wacker
-Gal Buki
-Rene Christen
-Andreas Neukoetter
-DAVID JOHNSON
-John Walton
-Volker Mische
-Michel Vilain
-Luigi/Blender Brasil
-Clifton A. Cross
-Kent B. Mein (SirDude)
-Anne Postma
-Matt Runion
-Bob Tackett
-stanislas nanchen
-Jorge Monteiro
-S68
-Ville Kaajakari
-Adrien Rebollo
-Al Curatolo
-Frank Rutten
-Dierk Dohmann
-Roel Spruit
-Rob Coldwell
-Yann Vernier
-Haunt_House (PH)
-Ravinder Singh Bhara
-Erik Augustijns
-Ryan Earle Freckleton
-Andreas Sturm
-matt mullin
-MOREAUX Denis
-Brendan Stromberger
-Hideki Saito
-Christian Ullrich
-Courty Didier
-Steve Newton
-Brecht Van Lommel
-Jasper Op de Coul
-Stuart MacKinnon
-Dietrich Dietz - the IMaGiNation project (IMGN)
-Tina Hirsch
-John R Thorp
-FrŽdŽric Bouvier
-LINETTE
-Felix Rabe
-Chay Adamou
-nick harron
-stephen john ford
-Kino
-Daniel Sjšlie
-Matthias Derer
-Alain VALLETON
-Kervin Pierre
-Mike Laughton
-Frank van Puffelen
-Andreas Dluzewski
-Christofer Edvardsen
-Maciek Szczyglowski
-Rakesh Ravuri
-Robert Schouwenburg
-Bob Bayens
-Devas73
-Mika Saari
-Robert Ives
-Adam McBride
-IAN ROSS
-Uriah Liggett
-stefan fischer
-Didac Miro
-Tim Barlow
-G. T. Blackwell
-Tim Weaver
-Dalet
-Erwin Vervacke
-Benjamin Eduardo Rodriguez Berumen
-Ozono Multimedia s.l.l.
-James Kersey
-Michael DeLuca
-Mark Swann
-Andreas Altenburger
-John Smith
-Simon de Haan
-David Gregory
-Harald Krummeck
-E Warren McNee
-Marc-Andre Levesque
-Satoshi Yamasaki
-Rolf-Dieter Klein
-J. Deetman
-Helge Walter
-Roland StrŒlberg
-Nicolas Morenas (Caronte)
-Simon Clarke
-Maigrot Michel
-Rod Robinson
-Kevin Cozens
-Germ‡n Alonso (taz)
-Martin Stegeman
-Henrik Jordfald Olsen
-Mitchell Skinner
-Michael Lei
-Spiridon G. Kontarakis
-Bas Denissen
-Loic Dachary
-Michael Rutter
-Thorsten SchlŸter
-hijothema
-Andreas Hauser
-Holger Haase
-Danilo Duina
-Matthias Thurau
-Flavio Curti
-Ian Beardslee
-Cristina Lozano Ballester
-Jason Roberson
-Victor Lim
-andreas palsson
-Richard Charvat
-James Ezell
-Florian Eggenberger
-John J. Marshall
-Paul Lunneberg
-Peter Wolkerstorfer
-Gerhard Siegesmund
-Nat Budin
-NDNChief
-Dave Leeak
-Michael Siebecker
-Jan Walter
-John Aughey
-Corey Rice
-Darren F. Coolen
-tony principali
-Joe Kimmes
-Pekka Karvonen
-Rick Kimball
-Christopher Capoccia
-Bertram Zeller
-Vanpoucke Alexandre
-Bassam Kurdali
-Michael Baker
-Michael Schwemmle
-Ford "fullback" Roberts
-Lama Angelo
-Kevin Roeder
-oliv
-pinhead_66
-Zach Millis
-Ryan C. Stallings
-Adam LaJeunesse
-Satish Goda
-Erik Haagsman
-Lorenzo Moro
-Nathan Letwory
-stephan f. haupt
-Alex Papadopoulos
-Gianluigi Berrone
-andrea casentini
-David Randall Simpson
-Jason Oppel
-Mark Bloomfield
-Alexander Ehrath
-Owen Swerkstrom
-Aurelio Caliaro
-Karsten Dambekalns
-Eddy MOUSSA
-Bernd Roetter
-Roland Hess
-Luis Eduardo P. Gervasio
-Pierre-Luc Robert (bedsitter)
-Andreu Cuartiella
-Nathan Vegdahl
-Scott Ross
-Jacob Kafka
-Mario Bolte
-Wolfgang Draxinger
-Starr Kline
-John Lullie
-Chiffi Cosimo
-Morgan McMillian
-Stefan HŸbner
-Loic de MONTAIGNAC
-AndrŽs Castillo
-Francesco Anselmo
-Ingo Guenther
-James C. Davis, Jr.
-Claudio Andaur
-Caleb Williams
-Orest Dubay
-Adam Casto
-David
-Joost Burger
-Ken Hahn
-Mark Herring
-Stig Oeveraas
-Robert Rainthorpe
-Martin Middleton
-Simon Sedgwick
-Joseph Firestine
-Miguel Melo
-Wooster Dennis
-Gary Lucas
-Johannes Schwarz
-Mark Dalton
-Mike Norton
-Michael Schardt
-jean-michel soler
-Chris Bracewell
-Ross Dawson
-Tim De Graeve
-Adam Wiseman
-Luiz Olsson Barbosa
-Donna Carey
-Auke van Balen
-Mike Birdsong
-Martin Curran
-Shawn M. Murray
-Roy Simmons
-Malik
-Teguh Pangestu
-Mats Holmberg
-Julien Jehannet
-Michael A. Nelson
-Axel Cushing
-erik vogan
-Datenflug GmbH
-KC Roeyer
-Ryan J. Parker
-Robert E. Meuse
-Alex Heizer
-Netsail Ltd / Mats Holmberg
-Jonathan Lawman
-Seikoh Hokama
-Perry Faulkner
-Will Hanson
-Lyubomir Pavlov Kovachev
-Ennio Quattrini
-Marcelo "xfer" Alves
-Joseph Winston IV
-Jorge Otero Rueda
-Timothy Murakami
-Matthew Johnson
-Erik Rombouts
-Petr Vlk
-Thomas Dammann
-Henrik Turkerud
-Erlend Hamberg
-Arnaud Lebegue
-Kevin O'Brien
-Michael Buettner
-Engel A. Sanchez
-Alexander Hilgert
-FAUGE Thierry
-Wulf Huesken
-John M. Adams
-Dell'Aiera Pol
-Waylena McCully
-Russell Smith
-Luke Titley
-marinus meijers
-Henry Kaminski
-Alistair Riddoch
-Daniel NŸmm
-Matthew Meadows
-Bjoern Paschen
-Paul Fredrickson
-Gavin Baker
-Jan Illetschko
-Aaron C. McLeod
-Thomas Muldowney
-Cheyenne Cloud, LLC
-Helmut A. Goettl
-Martin A. Boegelund
-Beno”t Cousson
-Scott Brooks
-Ferlet Patrice
-Aaron Porterfield
-Ivan Florentin
-Scott Isaacson
-Rui Paulo Sanguinheira Diogo
-Jason Saville
-Active-Websight GbR
-Bryon Roche
-Gustavo Mu–oz
-Christopher Gillanders
-Phil Frost Tate
-Gilles Gonon
-Kay
-James C. Franklin
-Luis Enrique Caama–o Navas
-Alexander "estartu" Felder
-Marc Ledermann
-vrijschrift.org
-Holger Weihe
-Peter Cammaert
-Andres Meyer
-Tony Arnett
-Adam Hughes
-Tony Farley
-Dmitriy Teresh
-Maickel Swart
-Peter den Bak
-Steve Bennett
-Romain Rubi
-John Bolt
-Michael Gaston
-Philip Brown
-Wasi
-Mike Hirst
-Lloyd J Robinson, Jr
-Charles Rinker
-Nick Vogtmann
-Frank van Beek
-Bruce Mitchener
-ROBERTO A. RUIZ VIAL
-Maurizio Sibaud
-Ron Bolger
-Nathan Parton
-Andrew Fry
-VINCENT StŽphane
-Yan Yan
-Justin L Graham
-Wade Beasley
-Salvador Mata Rodriguez
-Jan Tiemersma
-Luis A. R. Fernandez
-Jacob Moy
-Stefano Francesi
-Jochen Hanne
-David Stephenson
-Brent O'Dell
-Mike Kasprzak
-Tom Thompson
-David Fung
-Radoslav Dejanovic
-James H. Cloos, Jr.
-Karl Erlandsen (LethalSideParting)
-Kari Pulli
-Dave Shemano
diff --git a/doc/manpage/blender.1 b/doc/manpage/blender.1
deleted file mode 100644
index ddf3a79b104..00000000000
--- a/doc/manpage/blender.1
+++ /dev/null
@@ -1,397 +0,0 @@
-.TH "BLENDER" "1" "June 03, 2011" "Blender Blender 2\&.57 (sub 1)"
-
-.SH NAME
-blender \- a 3D modelling and rendering package
-.SH SYNOPSIS
-.B blender [args ...] [file] [args ...]
-.br
-.SH DESCRIPTION
-.PP
-.B blender
-is a 3D modelling and rendering package. It is the in-house software of a high quality animation studio, Blender has proven to be an extremely fast and versatile design instrument. The software has a personal touch, offering a unique approach to the world of Three Dimensions.
-
-Use Blender to create TV commercials, to make technical visualizations, business graphics, to do some morphing, or design user interfaces. You can easy build and manage complex environments. The renderer is versatile and extremely fast. All basic animation principles (curves & keys) are well implemented.
-
-http://www.blender.org
-.SH OPTIONS
-
-Blender 2.57 (sub 1)
-Usage: blender [args ...] [file] [args ...]
-.br
-.SS "Render Options:"
-
-.TP
-.B \-b or \-\-background <file>
-.br
-Load <file> in background (often used for UI\-less rendering)
-.br
-
-.TP
-.B \-a or \-\-render\-anim
-.br
-Render frames from start to end (inclusive)
-.br
-
-.TP
-.B \-S or \-\-scene <name>
-.br
-Set the active scene <name> for rendering
-.br
-
-.TP
-.B \-f or \-\-render\-frame <frame>
-.br
-Render frame <frame> and save it.
-.br
-+<frame> start frame relative, \-<frame> end frame relative.
-.br
-
-.TP
-.B \-s or \-\-frame\-start <frame>
-.br
-Set start to frame <frame> (use before the \-a argument)
-.br
-
-.TP
-.B \-e or \-\-frame\-end <frame>
-.br
-Set end to frame <frame> (use before the \-a argument)
-.br
-
-.TP
-.B \-j or \-\-frame\-jump <frames>
-.br
-Set number of frames to step forward after each rendered frame
-.br
-
-.TP
-.B \-o or \-\-render\-output <path>
-.br
-Set the render path and file name.
-.br
-Use // at the start of the path to
-.br
- render relative to the blend file.
-.br
-The # characters are replaced by the frame number, and used to define zero padding.
-.br
- ani_##_test.png becomes ani_01_test.png
-.br
- test\-######.png becomes test\-000001.png
-.br
- When the filename does not contain #, The suffix #### is added to the filename
-.br
-The frame number will be added at the end of the filename.
-.br
- eg: blender \-b foobar.blend \-o //render_ \-F PNG \-x 1 \-a
-.br
- //render_ becomes //render_####, writing frames as //render_0001.png//
-.br
-
-.TP
-.B \-E or \-\-engine <engine>
-.br
-Specify the render engine
-.br
-use \-E help to list available engines
-.br
-
-.IP
-
-.SS "Format Options:"
-
-.TP
-.B \-F or \-\-render\-format <format>
-.br
-Set the render format, Valid options are...
-.br
- TGA IRIS JPEG MOVIE IRIZ RAWTGA
-.br
- AVIRAW AVIJPEG PNG BMP FRAMESERVER
-.br
-(formats that can be compiled into blender, not available on all systems)
-.br
- HDR TIFF EXR MULTILAYER MPEG AVICODEC QUICKTIME CINEON DPX DDS
-.br
-
-.TP
-.B \-x or \-\-use\-extension <bool>
-.br
-Set option to add the file extension to the end of the file
-.br
-
-.TP
-.B \-t or \-\-threads <threads>
-.br
-Use amount of <threads> for rendering in background
-.br
-[1\-64], 0 for systems processor count.
-.br
-
-.IP
-
-.SS "Animation Playback Options:"
-
-.TP
-.B \-a or \-\-render\-anim
-.br
-Render frames from start to end (inclusive)
-.br
-
-.IP
-
-.SS "Window Options:"
-
-.TP
-.B \-w or \-\-window\-border
-.br
-Force opening with borders (default)
-.br
-
-.TP
-.B \-W or \-\-window\-borderless
-.br
-Force opening without borders
-.br
-
-.TP
-.B \-p or \-\-window\-geometry <sx> <sy> <w> <h>
-.br
-Open with lower left corner at <sx>, <sy> and width and height as <w>, <h>
-.br
-
-.TP
-.B \-con or \-\-start\-console
-.br
-Start with the console window open (ignored if \-b is set)
-.br
-
-.IP
-
-.SS "Game Engine Specific Options:"
-
-.TP
-.B \-g Game Engine specific options
-.br
-\-g fixedtime Run on 50 hertz without dropping frames
-.br
-\-g vertexarrays Use Vertex Arrays for rendering (usually faster)
-.br
-\-g nomipmap No Texture Mipmapping
-.br
-\-g linearmipmap Linear Texture Mipmapping instead of Nearest (default)
-.br
-
-.IP
-
-.SS "Misc Options:"
-
-.TP
-.B \-d or \-\-debug
-.br
-Turn debugging on
-.br
-
-.IP
-* Prints every operator call and their arguments
-.br
-* Disables mouse grab (to interact with a debugger in some cases)
-.br
-* Keeps python sys.stdin rather than setting it to None
-.br
-
-.TP
-.B \-\-debug\-fpe
-.br
-Enable floating point exceptions
-.br
-
-.IP
-
-.TP
-.B \-\-factory\-startup
-.br
-Skip reading the "startup.blend" in the users home directory
-.br
-
-.IP
-
-.TP
-.B \-\-env\-system\-datafiles
-.br
-Set the BLENDER_SYSTEM_DATAFILES environment variable
-.br
-
-.TP
-.B \-\-env\-system\-scripts
-.br
-Set the BLENDER_SYSTEM_SCRIPTS environment variable
-.br
-
-.TP
-.B \-\-env\-system\-plugins
-.br
-Set the BLENDER_SYSTEM_PLUGINS environment variable
-.br
-
-.TP
-.B \-\-env\-system\-python
-.br
-Set the BLENDER_SYSTEM_PYTHON environment variable
-.br
-
-.IP
-
-.TP
-.B \-nojoystick
-.br
-Disable joystick support
-.br
-
-.TP
-.B \-noglsl
-.br
-Disable GLSL shading
-.br
-
-.TP
-.B \-noaudio
-.br
-Force sound system to None
-.br
-
-.TP
-.B \-setaudio
-.br
-Force sound system to a specific device
-.br
-NULL SDL OPENAL JACK
-.br
-
-.IP
-
-.TP
-.B \-h or \-\-help
-.br
-Print this help text and exit
-.br
-
-.IP
-
-.TP
-.B \-y or \-\-enable\-autoexec
-.br
-Enable automatic python script execution, (default)
-.br
-
-.TP
-.B \-Y or \-\-disable\-autoexec
-.br
-Disable automatic python script execution (pydrivers, pyconstraints, pynodes)
-.br
-
-.IP
-
-.TP
-.B \-P or \-\-python <filename>
-.br
-Run the given Python script (filename or Blender Text)
-.br
-
-.TP
-.B \-\-python\-console
-.br
-Run blender with an interactive console
-.br
-
-.TP
-.B \-\-addons
-.br
-Comma separated list of addons (no spaces)
-.br
-
-.TP
-.B \-v or \-\-version
-.br
-Print Blender version and exit
-.br
-
-.TP
-.B \-\-
-.br
-Ends option processing, following arguments passed unchanged. Access via python's sys.argv
-.br
-
-.SS "Other Options:"
-
-.TP
-.B /?
-.br
-Print this help text and exit (windows only)
-.br
-
-.TP
-.B \-a <options> <file(s)>
-.br
-Playback <file(s)>, only operates this way when not running in background.
-.br
- \-p <sx> <sy> Open with lower left corner at <sx>, <sy>
-.br
- \-m Read from disk (Don't buffer)
-.br
- \-f <fps> <fps\-base> Specify FPS to start with
-.br
- \-j <frame> Set frame step to <frame>
-.br
-
-.TP
-.B \-R
-.br
-Register .blend extension, then exit (Windows only)
-.br
-
-.TP
-.B \-r
-.br
-Silently register .blend extension, then exit (Windows only)
-.br
-
-.SS "Argument Parsing:"
-
- arguments must be separated by white space. eg
- "blender \-ba test.blend"
- ...will ignore the 'a'
- "blender \-b test.blend \-f8"
- ...will ignore 8 because there is no space between the \-f and the frame value
-.br
-.SS "Argument Order:"
-
-Arguments are executed in the order they are given. eg
- "blender \-\-background test.blend \-\-render\-frame 1 \-\-render\-output /tmp"
- ...will not render to /tmp because '\-\-render\-frame 1' renders before the output path is set
- "blender \-\-background \-\-render\-output /tmp test.blend \-\-render\-frame 1"
- ...will not render to /tmp because loading the blend file overwrites the render output that was set
- "blender \-\-background test.blend \-\-render\-output /tmp \-\-render\-frame 1" works as expected.
-.br
-.br
-.SH "ENVIRONMENT VARIABLES"
- \fIBLENDER_USER_CONFIG\fR Directory for user configuration files.
- \fIBLENDER_USER_SCRIPTS\fR Directory for user scripts.
- \fIBLENDER_SYSTEM_SCRIPTS\fR Directory for system wide scripts.
- \fIBLENDER_USER_DATAFILES\fR Directory for user data files (icons, translations, ..).
- \fIBLENDER_SYSTEM_DATAFILES\fR Directory for system wide data files.
- \fIBLENDER_SYSTEM_PYTHON\fR Directory for system python libraries.
- \fITMP\fR or \fITMPDIR\fR Store temporary files here.
- \fIPYTHONHOME\fR Path to the python directory, eg. /usr/lib/python.
-.br
-.br
-
-.br
-.SH SEE ALSO
-.B yafaray(1)
-
-.br
-.SH AUTHORS
-This manpage was written for a Debian GNU/Linux system by Daniel Mester
-<mester@uni-bremen.de> and updated by Cyril Brulebois
-<cyril.brulebois@enst-bretagne.fr> and Dan Eicher <dan@trollwerks.org>.
diff --git a/doc/manpage/blender.1.py b/doc/manpage/blender.1.py
deleted file mode 100644
index 40234300994..00000000000
--- a/doc/manpage/blender.1.py
+++ /dev/null
@@ -1,136 +0,0 @@
-#!/usr/bin/python
-
-# ##### BEGIN GPL LICENSE BLOCK #####
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software Foundation,
-# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# ##### END GPL LICENSE BLOCK #####
-
-# <pep8 compliant>
-
-import subprocess
-import os
-
-import time
-import datetime
-
-
-def man_format(data):
- data = data.replace("-", "\\-")
- data = data.replace("\t", " ")
- # data = data.replace("$", "\\fI")
-
- data_ls = []
- for w in data.split():
- if w.startswith("$"):
- w = "\\fI" + w[1:] + "\\fR"
-
- data_ls.append(w)
-
- data = data[:len(data) - len(data.lstrip())] + " ".join(data_ls)
-
- return data
-
-
-blender_bin = os.path.join(os.path.dirname(__file__), "../../blender.bin")
-
-blender_help = subprocess.Popen([blender_bin, "--help"], stdout=subprocess.PIPE).communicate()[0].decode()
-
-blender_version = subprocess.Popen([blender_bin, "--version"], stdout=subprocess.PIPE).communicate()[0].decode().strip()
-blender_version = blender_version.split("Build")[0]
-
-date_string = datetime.date.fromtimestamp(time.time()).strftime("%B %d, %Y")
-
-filepath = os.path.splitext(__file__)[0]
-
-file = open(filepath, "w")
-
-fw = file.write
-
-fw('.TH "BLENDER" "1" "%s" "Blender %s"\n' % (date_string, blender_version.replace(".", "\\&.")))
-
-fw('''
-.SH NAME
-blender \- a 3D modelling and rendering package''')
-
-fw('''
-.SH SYNOPSIS
-.B blender [args ...] [file] [args ...]''')
-
-fw('''
-.br
-.SH DESCRIPTION
-.PP
-.B blender
-is a 3D modelling and rendering package. It is the in-house software of a high quality animation studio, Blender has proven to be an extremely fast and versatile design instrument. The software has a personal touch, offering a unique approach to the world of Three Dimensions.
-
-Use Blender to create TV commercials, to make technical visualizations, business graphics, to do some morphing, or design user interfaces. You can easy build and manage complex environments. The renderer is versatile and extremely fast. All basic animation principles (curves & keys) are well implemented.
-
-http://www.blender.org''')
-
-fw('''
-.SH OPTIONS''')
-
-fw("\n\n")
-
-lines = [line.rstrip() for line in blender_help.split("\n")]
-
-while lines:
- l = lines.pop(0)
- if l.startswith("Environment Variables:"):
- fw('.SH "ENVIRONMENT VARIABLES"\n')
- elif l.endswith(":"): # one line
- fw('.SS "%s"\n\n' % l)
- elif l.startswith("-") or l.startswith("/"): # can be multi line
-
- fw('.TP\n')
- fw('.B %s\n' % man_format(l))
-
- while lines:
- # line with no
- if lines[0].strip() and len(lines[0].lstrip()) == len(lines[0]): # no white space
- break
-
- if not l: # second blank line
- fw('.IP\n')
- else:
- fw('.br\n')
-
- l = lines.pop(0)
- l = l[1:] # remove first whitespace (tab)
-
- fw('%s\n' % man_format(l))
-
- else:
- if not l.strip():
- fw('.br\n')
- else:
- fw('%s\n' % man_format(l))
-
-# footer
-
-fw('''
-.br
-.SH SEE ALSO
-.B yafaray(1)
-
-.br
-.SH AUTHORS
-This manpage was written for a Debian GNU/Linux system by Daniel Mester
-<mester@uni-bremen.de> and updated by Cyril Brulebois
-<cyril.brulebois@enst-bretagne.fr> and Dan Eicher <dan@trollwerks.org>.
-''')
-
-print("written:", filepath)
diff --git a/extern/CMakeLists.txt b/extern/CMakeLists.txt
index 3990d132757..7a59e41c89b 100644
--- a/extern/CMakeLists.txt
+++ b/extern/CMakeLists.txt
@@ -42,6 +42,7 @@ endif()
if(WITH_BUILTIN_GLEW)
add_subdirectory(glew)
endif()
+add_subdirectory(recastnavigation)
if(WITH_IMAGE_OPENJPEG AND (NOT UNIX OR APPLE))
add_subdirectory(libopenjpeg)
diff --git a/extern/SConscript b/extern/SConscript
index 061bd1ba6a3..88aeb400d13 100644
--- a/extern/SConscript
+++ b/extern/SConscript
@@ -3,6 +3,7 @@
Import('env')
SConscript(['glew/SConscript'])
+SConscript(['recastnavigation/SConscript'])
if env['WITH_BF_ELTOPO']:
SConscript(['eltopo/SConscript'])
diff --git a/extern/recastnavigation/CMakeLists.txt b/extern/recastnavigation/CMakeLists.txt
new file mode 100644
index 00000000000..694f14293c2
--- /dev/null
+++ b/extern/recastnavigation/CMakeLists.txt
@@ -0,0 +1,66 @@
+# $Id$
+# ***** BEGIN GPL LICENSE BLOCK *****
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# The Original Code is Copyright (C) 2006, Blender Foundation
+# All rights reserved.
+#
+# The Original Code is: all of this file.
+#
+# Contributor(s): Daniel Genrich
+#
+# ***** END GPL LICENSE BLOCK *****
+
+set(INC
+ Recast/Include
+ Detour/Include
+)
+
+set(INC_SYS
+
+)
+
+set(SRC
+ Detour/Source/DetourCommon.cpp
+ Detour/Source/DetourNode.cpp
+ Detour/Source/DetourStatNavMesh.cpp
+ Detour/Source/DetourStatNavMeshBuilder.cpp
+ Detour/Source/DetourTileNavMesh.cpp
+ Detour/Source/DetourTileNavMeshBuilder.cpp
+
+ Detour/Include/DetourCommon.h
+ Detour/Include/DetourNode.h
+ Detour/Include/DetourStatNavMesh.h
+ Detour/Include/DetourStatNavMeshBuilder.h
+ Detour/Include/DetourTileNavMesh.h
+ Detour/Include/DetourTileNavMeshBuilder.h
+
+ Recast/Source/Recast.cpp
+ Recast/Source/RecastContour.cpp
+ Recast/Source/RecastFilter.cpp
+ Recast/Source/RecastLog.cpp
+ Recast/Source/RecastMesh.cpp
+ Recast/Source/RecastMeshDetail.cpp
+ Recast/Source/RecastRasterization.cpp
+ Recast/Source/RecastRegion.cpp
+ Recast/Source/RecastTimer.cpp
+
+ Recast/Include/Recast.h
+ Recast/Include/RecastLog.h
+ Recast/Include/RecastTimer.h
+)
+
+blender_add_lib(extern_recastnavigation "${SRC}" "${INC}" "${INC_SYS}") \ No newline at end of file
diff --git a/extern/recastnavigation/Detour/Include/DetourCommon.h b/extern/recastnavigation/Detour/Include/DetourCommon.h
new file mode 100644
index 00000000000..d824efc06e4
--- /dev/null
+++ b/extern/recastnavigation/Detour/Include/DetourCommon.h
@@ -0,0 +1,167 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#ifndef DETOURCOMMON_H
+#define DETOURCOMMON_H
+
+//////////////////////////////////////////////////////////////////////////////////////////
+
+template<class T> inline void swap(T& a, T& b) { T t = a; a = b; b = t; }
+template<class T> inline T min(T a, T b) { return a < b ? a : b; }
+template<class T> inline T max(T a, T b) { return a > b ? a : b; }
+template<class T> inline T abs(T a) { return a < 0 ? -a : a; }
+template<class T> inline T sqr(T a) { return a*a; }
+template<class T> inline T clamp(T v, T mn, T mx) { return v < mn ? mn : (v > mx ? mx : v); }
+
+inline void vcross(float* dest, const float* v1, const float* v2)
+{
+ dest[0] = v1[1]*v2[2] - v1[2]*v2[1];
+ dest[1] = v1[2]*v2[0] - v1[0]*v2[2];
+ dest[2] = v1[0]*v2[1] - v1[1]*v2[0];
+}
+
+inline float vdot(const float* v1, const float* v2)
+{
+ return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
+}
+
+inline void vmad(float* dest, const float* v1, const float* v2, const float s)
+{
+ dest[0] = v1[0]+v2[0]*s;
+ dest[1] = v1[1]+v2[1]*s;
+ dest[2] = v1[2]+v2[2]*s;
+}
+
+inline void vadd(float* dest, const float* v1, const float* v2)
+{
+ dest[0] = v1[0]+v2[0];
+ dest[1] = v1[1]+v2[1];
+ dest[2] = v1[2]+v2[2];
+}
+
+inline void vsub(float* dest, const float* v1, const float* v2)
+{
+ dest[0] = v1[0]-v2[0];
+ dest[1] = v1[1]-v2[1];
+ dest[2] = v1[2]-v2[2];
+}
+
+inline void vmin(float* mn, const float* v)
+{
+ mn[0] = min(mn[0], v[0]);
+ mn[1] = min(mn[1], v[1]);
+ mn[2] = min(mn[2], v[2]);
+}
+
+inline void vmax(float* mx, const float* v)
+{
+ mx[0] = max(mx[0], v[0]);
+ mx[1] = max(mx[1], v[1]);
+ mx[2] = max(mx[2], v[2]);
+}
+
+inline void vcopy(float* dest, const float* a)
+{
+ dest[0] = a[0];
+ dest[1] = a[1];
+ dest[2] = a[2];
+}
+
+inline float vdist(const float* v1, const float* v2)
+{
+ float dx = v2[0] - v1[0];
+ float dy = v2[1] - v1[1];
+ float dz = v2[2] - v1[2];
+ return sqrtf(dx*dx + dy*dy + dz*dz);
+}
+
+inline float vdistSqr(const float* v1, const float* v2)
+{
+ float dx = v2[0] - v1[0];
+ float dy = v2[1] - v1[1];
+ float dz = v2[2] - v1[2];
+ return dx*dx + dy*dy + dz*dz;
+}
+
+inline void vnormalize(float* v)
+{
+ float d = 1.0f / sqrtf(sqr(v[0]) + sqr(v[1]) + sqr(v[2]));
+ v[0] *= d;
+ v[1] *= d;
+ v[2] *= d;
+}
+
+inline bool vequal(const float* p0, const float* p1)
+{
+ static const float thr = sqr(1.0f/16384.0f);
+ const float d = vdistSqr(p0, p1);
+ return d < thr;
+}
+
+inline int nextPow2(int v)
+{
+ v--;
+ v |= v >> 1;
+ v |= v >> 2;
+ v |= v >> 4;
+ v |= v >> 8;
+ v |= v >> 16;
+ v++;
+ return v;
+}
+
+inline float vdot2D(const float* u, const float* v)
+{
+ return u[0]*v[0] + u[2]*v[2];
+}
+
+inline float vperp2D(const float* u, const float* v)
+{
+ return u[2]*v[0] - u[0]*v[2];
+}
+
+inline float triArea2D(const float* a, const float* b, const float* c)
+{
+ return ((b[0]*a[2] - a[0]*b[2]) + (c[0]*b[2] - b[0]*c[2]) + (a[0]*c[2] - c[0]*a[2])) * 0.5f;
+}
+
+inline bool checkOverlapBox(const unsigned short amin[3], const unsigned short amax[3],
+ const unsigned short bmin[3], const unsigned short bmax[3])
+{
+ bool overlap = true;
+ overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
+ overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
+ overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap;
+ return overlap;
+}
+
+void closestPtPointTriangle(float* closest, const float* p,
+ const float* a, const float* b, const float* c);
+
+bool closestHeightPointTriangle(const float* p, const float* a, const float* b, const float* c, float& h);
+
+bool intersectSegmentPoly2D(const float* p0, const float* p1,
+ const float* verts, int nverts,
+ float& tmin, float& tmax,
+ int& segMin, int& segMax);
+
+float distancePtSegSqr2D(const float* pt, const float* p, const float* q, float& t);
+
+void calcPolyCenter(float* tc, const unsigned short* idx, int nidx, const float* verts);
+
+#endif // DETOURCOMMON_H \ No newline at end of file
diff --git a/extern/recastnavigation/Detour/Include/DetourNode.h b/extern/recastnavigation/Detour/Include/DetourNode.h
new file mode 100644
index 00000000000..316d5bf4cf6
--- /dev/null
+++ b/extern/recastnavigation/Detour/Include/DetourNode.h
@@ -0,0 +1,149 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#ifndef DETOURNODE_H
+#define DETOURNODE_H
+
+enum dtNodeFlags
+{
+ DT_NODE_OPEN = 0x01,
+ DT_NODE_CLOSED = 0x02,
+};
+
+struct dtNode
+{
+ float cost;
+ float total;
+ unsigned int id;
+ unsigned int pidx : 30;
+ unsigned int flags : 2;
+};
+
+class dtNodePool
+{
+public:
+ dtNodePool(int maxNodes, int hashSize);
+ ~dtNodePool();
+ inline void operator=(const dtNodePool&) {}
+ void clear();
+ dtNode* getNode(unsigned int id);
+ const dtNode* findNode(unsigned int id) const;
+
+ inline unsigned int getNodeIdx(const dtNode* node) const
+ {
+ if (!node) return 0;
+ return (unsigned int)(node - m_nodes)+1;
+ }
+
+ inline dtNode* getNodeAtIdx(unsigned int idx)
+ {
+ if (!idx) return 0;
+ return &m_nodes[idx-1];
+ }
+
+ inline int getMemUsed() const
+ {
+ return sizeof(*this) +
+ sizeof(dtNode)*m_maxNodes +
+ sizeof(unsigned short)*m_maxNodes +
+ sizeof(unsigned short)*m_hashSize;
+ }
+
+private:
+ inline unsigned int hashint(unsigned int a) const
+ {
+ a += ~(a<<15);
+ a ^= (a>>10);
+ a += (a<<3);
+ a ^= (a>>6);
+ a += ~(a<<11);
+ a ^= (a>>16);
+ return a;
+ }
+
+ dtNode* m_nodes;
+ unsigned short* m_first;
+ unsigned short* m_next;
+ const int m_maxNodes;
+ const int m_hashSize;
+ int m_nodeCount;
+};
+
+class dtNodeQueue
+{
+public:
+ dtNodeQueue(int n);
+ ~dtNodeQueue();
+ inline void operator=(dtNodeQueue&) {}
+
+ inline void clear()
+ {
+ m_size = 0;
+ }
+
+ inline dtNode* top()
+ {
+ return m_heap[0];
+ }
+
+ inline dtNode* pop()
+ {
+ dtNode* result = m_heap[0];
+ m_size--;
+ trickleDown(0, m_heap[m_size]);
+ return result;
+ }
+
+ inline void push(dtNode* node)
+ {
+ m_size++;
+ bubbleUp(m_size-1, node);
+ }
+
+ inline void modify(dtNode* node)
+ {
+ for (int i = 0; i < m_size; ++i)
+ {
+ if (m_heap[i] == node)
+ {
+ bubbleUp(i, node);
+ return;
+ }
+ }
+ }
+
+ inline bool empty() const { return m_size == 0; }
+
+ inline int getMemUsed() const
+ {
+ return sizeof(*this) +
+ sizeof(dtNode*)*(m_capacity+1);
+ }
+
+
+private:
+ void bubbleUp(int i, dtNode* node);
+ void trickleDown(int i, dtNode* node);
+
+ dtNode** m_heap;
+ const int m_capacity;
+ int m_size;
+};
+
+
+#endif // DETOURNODE_H \ No newline at end of file
diff --git a/extern/recastnavigation/Detour/Include/DetourStatNavMesh.h b/extern/recastnavigation/Detour/Include/DetourStatNavMesh.h
new file mode 100644
index 00000000000..71ee4cb0b51
--- /dev/null
+++ b/extern/recastnavigation/Detour/Include/DetourStatNavMesh.h
@@ -0,0 +1,234 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#ifndef DETOURSTATNAVMESH_H
+#define DETOURSTATNAVMESH_H
+
+// Reference to navigation polygon.
+typedef unsigned short dtStatPolyRef;
+
+// Maximum number of vertices per navigation polygon.
+static const int DT_STAT_VERTS_PER_POLYGON = 6;
+
+// Structure holding the navigation polygon data.
+struct dtStatPoly
+{
+ unsigned short v[DT_STAT_VERTS_PER_POLYGON]; // Indices to vertices of the poly.
+ dtStatPolyRef n[DT_STAT_VERTS_PER_POLYGON]; // Refs to neighbours of the poly.
+ unsigned char nv; // Number of vertices.
+ unsigned char flags; // Flags (not used).
+};
+
+struct dtStatPolyDetail
+{
+ unsigned short vbase; // Offset to detail vertex array.
+ unsigned short nverts; // Number of vertices in the detail mesh.
+ unsigned short tbase; // Offset to detail triangle array.
+ unsigned short ntris; // Number of triangles.
+};
+
+const int DT_STAT_NAVMESH_MAGIC = 'NAVM';
+const int DT_STAT_NAVMESH_VERSION = 3;
+
+struct dtStatBVNode
+{
+ unsigned short bmin[3], bmax[3];
+ int i;
+};
+
+struct dtStatNavMeshHeader
+{
+ int magic;
+ int version;
+ int npolys;
+ int nverts;
+ int nnodes;
+ int ndmeshes;
+ int ndverts;
+ int ndtris;
+ float cs;
+ float bmin[3], bmax[3];
+ dtStatPoly* polys;
+ float* verts;
+ dtStatBVNode* bvtree;
+ dtStatPolyDetail* dmeshes;
+ float* dverts;
+ unsigned char* dtris;
+};
+
+class dtStatNavMesh
+{
+public:
+
+ dtStatNavMesh();
+ ~dtStatNavMesh();
+
+ // Initializes the navmesh with data.
+ // Params:
+ // data - (in) Pointer to navmesh data.
+ // dataSize - (in) size of the navmesh data.
+ // ownsData - (in) Flag indicating if the navmesh should own and delete the data.
+ bool init(unsigned char* data, int dataSize, bool ownsData);
+
+ // Finds the nearest navigation polygon around the center location.
+ // Params:
+ // center - (in) The center of the search box.
+ // extents - (in) The extents of the search box.
+ // Returns: Reference identifier for the polygon, or 0 if no polygons found.
+ dtStatPolyRef findNearestPoly(const float* center, const float* extents);
+
+ // Returns polygons which touch the query box.
+ // Params:
+ // center - (in) the center of the search box.
+ // extents - (in) the extents of the search box.
+ // polys - (out) array holding the search result.
+ // maxPolys - (in) The max number of polygons the polys array can hold.
+ // Returns: Number of polygons in search result array.
+ int queryPolygons(const float* center, const float* extents,
+ dtStatPolyRef* polys, const int maxPolys);
+
+ // Finds path from start polygon to end polygon.
+ // If target polygon canno be reached through the navigation graph,
+ // the last node on the array is nearest node to the end polygon.
+ // Params:
+ // startRef - (in) ref to path start polygon.
+ // endRef - (in) ref to path end polygon.
+ // path - (out) array holding the search result.
+ // maxPathSize - (in) The max number of polygons the path array can hold.
+ // Returns: Number of polygons in search result array.
+ int findPath(dtStatPolyRef startRef, dtStatPolyRef endRef,
+ const float* startPos, const float* endPos,
+ dtStatPolyRef* path, const int maxPathSize);
+
+ // Finds a straight path from start to end locations within the corridor
+ // described by the path polygons.
+ // Start and end locations will be clamped on the corridor.
+ // Params:
+ // startPos - (in) Path start location.
+ // endPos - (in) Path end location.
+ // path - (in) Array of connected polygons describing the corridor.
+ // pathSize - (in) Number of polygons in path array.
+ // straightPath - (out) Points describing the straight path.
+ // maxStraightPathSize - (in) The max number of points the straight path array can hold.
+ // Returns: Number of points in the path.
+ int findStraightPath(const float* startPos, const float* endPos,
+ const dtStatPolyRef* path, const int pathSize,
+ float* straightPath, const int maxStraightPathSize);
+
+ // Finds intersection againts walls starting from start pos.
+ // Params:
+ // startRef - (in) ref to the polygon where the start lies.
+ // startPos - (in) start position of the query.
+ // endPos - (in) end position of the query.
+ // t - (out) hit parameter along the segment, 0 if no hit.
+ // endRef - (out) ref to the last polygon which was processed.
+ // Returns: Number of polygons in path or 0 if failed.
+ int raycast(dtStatPolyRef startRef, const float* startPos, const float* endPos,
+ float& t, dtStatPolyRef* path, const int pathSize);
+
+ // Returns distance to nearest wall from the specified location.
+ // Params:
+ // centerRef - (in) ref to the polygon where the center lies.
+ // centerPos - (in) center if the query circle.
+ // maxRadius - (in) max search radius.
+ // hitPos - (out) location of the nearest hit.
+ // hitNormal - (out) normal of the nearest hit.
+ // Returns: Distance to nearest wall from the test location.
+ float findDistanceToWall(dtStatPolyRef centerRef, const float* centerPos, float maxRadius,
+ float* hitPos, float* hitNormal);
+
+ // Finds polygons found along the navigation graph which touch the specified circle.
+ // Params:
+ // centerRef - (in) ref to the polygon where the center lies.
+ // centerPos - (in) center if the query circle
+ // radius - (in) radius of the query circle
+ // resultRef - (out, opt) refs to the polygons touched by the circle.
+ // resultParent - (out, opt) parent of each result polygon.
+ // resultCost - (out, opt) search cost at each result polygon.
+ // maxResult - (int) maximum capacity of search results.
+ // Returns: Number of results.
+ int findPolysAround(dtStatPolyRef centerRef, const float* centerPos, float radius,
+ dtStatPolyRef* resultRef, dtStatPolyRef* resultParent, float* resultCost,
+ const int maxResult);
+
+ // Returns closest point on navigation polygon.
+ // Params:
+ // ref - (in) ref to the polygon.
+ // pos - (in) the point to check.
+ // closest - (out) closest point.
+ // Returns: true if closest point found.
+ bool closestPointToPoly(dtStatPolyRef ref, const float* pos, float* closest) const;
+
+ // Returns height of the polygon at specified location.
+ // Params:
+ // ref - (in) ref to the polygon.
+ // pos - (in) the point where to locate the height.
+ // height - (out) height at the location.
+ // Returns: true if oer polygon.
+ bool getPolyHeight(dtStatPolyRef ref, const float* pos, float* height) const;
+
+ // Returns pointer to a polygon based on ref.
+ const dtStatPoly* getPolyByRef(dtStatPolyRef ref) const;
+ // Returns polygon index based on ref, or -1 if failed.
+ int getPolyIndexByRef(dtStatPolyRef ref) const;
+ // Returns number of navigation polygons.
+ inline int getPolyCount() const { return m_header ? m_header->npolys : 0; }
+ // Rerturns pointer to specified navigation polygon.
+ inline const dtStatPoly* getPoly(int i) const { return &m_header->polys[i]; }
+ // Returns number of vertices.
+ inline int getVertexCount() const { return m_header ? m_header->nverts : 0; }
+ // Returns pointer to specified vertex.
+ inline const float* getVertex(int i) const { return &m_header->verts[i*3]; }
+ // Returns number of navigation polygons details.
+ inline int getPolyDetailCount() const { return m_header ? m_header->ndmeshes : 0; }
+ // Rerturns pointer to specified navigation polygon detail.
+ const dtStatPolyDetail* getPolyDetail(int i) const { return &m_header->dmeshes[i]; }
+ // Returns pointer to specified vertex.
+ inline const float* getDetailVertex(int i) const { return &m_header->dverts[i*3]; }
+ // Returns pointer to specified vertex.
+ inline const unsigned char* getDetailTri(int i) const { return &m_header->dtris[i*4]; }
+
+ bool isInClosedList(dtStatPolyRef ref) const;
+
+ int getMemUsed() const;
+
+ inline unsigned char* getData() const { return m_data; }
+ inline int getDataSize() const { return m_dataSize; }
+ inline const dtStatNavMeshHeader* getHeader() const { return m_header; }
+ inline const dtStatBVNode* getBvTreeNodes() const { return m_header ? m_header->bvtree : 0; }
+ inline int getBvTreeNodeCount() const { return m_header ? m_header->nnodes : 0; }
+
+private:
+
+ // Copies the locations of vertices of a polygon to an array.
+ int getPolyVerts(dtStatPolyRef ref, float* verts) const;
+ // Returns portal points between two polygons.
+ bool getPortalPoints(dtStatPolyRef from, dtStatPolyRef to, float* left, float* right) const;
+ // Returns edge mid point between two polygons.
+ bool getEdgeMidPoint(dtStatPolyRef from, dtStatPolyRef to, float* mid) const;
+
+ unsigned char* m_data;
+ int m_dataSize;
+
+ dtStatNavMeshHeader* m_header;
+
+ class dtNodePool* m_nodePool;
+ class dtNodeQueue* m_openList;
+};
+
+#endif // DETOURSTATNAVMESH_H
diff --git a/extern/recastnavigation/Detour/Include/DetourStatNavMeshBuilder.h b/extern/recastnavigation/Detour/Include/DetourStatNavMeshBuilder.h
new file mode 100644
index 00000000000..03c79c429e7
--- /dev/null
+++ b/extern/recastnavigation/Detour/Include/DetourStatNavMeshBuilder.h
@@ -0,0 +1,33 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#ifndef DETOURSTATNAVMESHBUILDER_H
+#define DETOURSTATNAVMESHBUILDER_H
+
+bool dtCreateNavMeshData(const unsigned short* verts, const int nverts,
+ const unsigned short* polys, const int npolys, const int nvp,
+ const float* bmin, const float* bmax, float cs, float ch,
+ const unsigned short* dmeshes, const float* dverts, const int ndverts,
+ const unsigned char* dtris, const int ndtris,
+ unsigned char** outData, int* outDataSize);
+
+int createBVTree(const unsigned short* verts, const int nverts,
+ const unsigned short* polys, const int npolys, const int nvp,
+ float cs, float ch, int nnodes, dtStatBVNode* nodes);
+
+#endif // DETOURSTATNAVMESHBUILDER_H \ No newline at end of file
diff --git a/extern/recastnavigation/Detour/Include/DetourTileNavMesh.h b/extern/recastnavigation/Detour/Include/DetourTileNavMesh.h
new file mode 100644
index 00000000000..d615b1748f0
--- /dev/null
+++ b/extern/recastnavigation/Detour/Include/DetourTileNavMesh.h
@@ -0,0 +1,315 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#ifndef DETOURTILENAVMESH_H
+#define DETOURTILENAVMESH_H
+
+// Reference to navigation polygon.
+typedef unsigned int dtTilePolyRef;
+
+// The bits used in the poly ref.
+static const int DT_TILE_REF_SALT_BITS = 12;
+static const int DT_TILE_REF_TILE_BITS = 12;
+static const int DT_TILE_REF_POLY_BITS = 8;
+static const int DT_TILE_REF_SALT_MASK = (1<<DT_TILE_REF_SALT_BITS)-1;
+static const int DT_TILE_REF_TILE_MASK = (1<<DT_TILE_REF_TILE_BITS)-1;
+static const int DT_TILE_REF_POLY_MASK = (1<<DT_TILE_REF_POLY_BITS)-1;
+
+// Maximum number of vertices per navigation polygon.
+static const int DT_TILE_VERTS_PER_POLYGON = 6;
+
+static const int DT_MAX_TILES = 1 << DT_TILE_REF_TILE_BITS;
+static const int DT_MAX_POLYGONS = 1 << DT_TILE_REF_POLY_BITS;
+
+static const int DT_TILE_NAVMESH_MAGIC = 'NAVT';
+static const int DT_TILE_NAVMESH_VERSION = 2;
+
+// Structure holding the navigation polygon data.
+struct dtTilePoly
+{
+ unsigned short v[DT_TILE_VERTS_PER_POLYGON]; // Indices to vertices of the poly.
+ unsigned short n[DT_TILE_VERTS_PER_POLYGON]; // Refs to neighbours of the poly.
+ unsigned short links; // Base index to header 'links' array.
+ unsigned char nlinks; // Number of links for
+ unsigned char nv; // Number of vertices.
+ unsigned char flags; // Flags (not used).
+};
+
+struct dtTilePolyDetail
+{
+ unsigned short vbase; // Offset to detail vertex array.
+ unsigned short nverts; // Number of vertices in the detail mesh.
+ unsigned short tbase; // Offset to detail triangle array.
+ unsigned short ntris; // Number of triangles.
+};
+
+// Stucture holding a link to another polygon.
+struct dtTileLink
+{
+ dtTilePolyRef ref; // Neighbour reference.
+ unsigned short p; // Index to polygon which owns this link.
+ unsigned char e; // Index to polygon edge which owns this link.
+ unsigned char side; // If boundary link, defines on which side the link is.
+ unsigned char bmin, bmax; // If boundary link, defines the sub edge area.
+};
+
+struct dtTileHeader
+{
+ int magic; // Magic number, used to identify the data.
+ int version; // Data version number.
+ int npolys; // Number of polygons in the tile.
+ int nverts; // Number of vertices in the tile.
+ int nlinks; // Number of links in the tile (will be updated when tile is added).
+ int maxlinks; // Number of allocated links.
+ int ndmeshes;
+ int ndverts;
+ int ndtris;
+ float bmin[3], bmax[3]; // Bounding box of the tile.
+ dtTilePoly* polys; // Pointer to the polygons (will be updated when tile is added).
+ float* verts; // Pointer to the vertices (will be updated when tile added).
+ dtTileLink* links; // Pointer to the links (will be updated when tile added).
+ dtTilePolyDetail* dmeshes;
+ float* dverts;
+ unsigned char* dtris;
+};
+
+struct dtTile
+{
+ int salt; // Counter describing modifications to the tile.
+ int x,y; // Grid location of the tile.
+ dtTileHeader* header; // Pointer to tile header.
+ unsigned char* data; // Pointer to tile data.
+ int dataSize; // Size of the tile data.
+ bool ownsData; // Flag indicating of the navmesh should release the data.
+ dtTile* next; // Next free tile or, next tile in spatial grid.
+};
+
+// Encodes a tile id.
+inline dtTilePolyRef dtEncodeTileId(unsigned int salt, unsigned int it, unsigned int ip)
+{
+ return (salt << (DT_TILE_REF_POLY_BITS+DT_TILE_REF_TILE_BITS)) | ((it+1) << DT_TILE_REF_POLY_BITS) | ip;
+}
+
+// Decodes a tile id.
+inline void dtDecodeTileId(dtTilePolyRef ref, unsigned int& salt, unsigned int& it, unsigned int& ip)
+{
+ salt = (ref >> (DT_TILE_REF_POLY_BITS+DT_TILE_REF_TILE_BITS)) & DT_TILE_REF_SALT_MASK;
+ it = ((ref >> DT_TILE_REF_POLY_BITS) & DT_TILE_REF_TILE_MASK) - 1;
+ ip = ref & DT_TILE_REF_POLY_MASK;
+}
+
+static const int DT_TILE_LOOKUP_SIZE = DT_MAX_TILES/4;
+
+class dtTiledNavMesh
+{
+public:
+ dtTiledNavMesh();
+ ~dtTiledNavMesh();
+
+ // Initializes the nav mesh.
+ // Params:
+ // orig - (in) origin of the nav mesh tile space.
+ // tileSiz - (in) size of a tile.
+ // portalheight - (in) height of the portal region between tiles.
+ // Returns: True if succeed, else false.
+ bool init(const float* orig, float tileSize, float portalHeight);
+
+ // Adds new tile into the navmesh.
+ // The add will fail if the data is in wrong format,
+ // there is not enough tiles left, or if there is a tile already at the location.
+ // Params:
+ // x,y - (in) Location of the new tile.
+ // data - (in) Data of the new tile mesh.
+ // dataSize - (in) Data size of the new tile mesh.
+ // ownsData - (in) Flag indicating if the navmesh should own and delete the data.
+ // Returns: True if tile was added, else false.
+ bool addTileAt(int x, int y, unsigned char* data, int dataSize, bool ownsData);
+
+ // Removes tile at specified location.
+ // Params:
+ // x,y - (in) Location of the tile to remove.
+ // data - (out) Data associated with deleted tile.
+ // dataSize - (out) Size of the data associated with deleted tile.
+ // Returns: True if remove suceed, else false.
+ bool removeTileAt(int x, int y, unsigned char** data, int* dataSize);
+
+ // Returns pointer to tile at specified location.
+ // Params:
+ // x,y - (in) Location of the tile to get.
+ // Returns: pointer to tile if tile exists or 0 tile does not exists.
+ dtTile* getTileAt(int x, int y);
+
+ // Returns pointer to tile in the tile array.
+ // Params:
+ // i - (in) Index to the tile to retrieve, must be in range [0,DT_MAX_TILES[
+ // Returns: Pointer to specified tile.
+ dtTile* getTile(int i);
+ const dtTile* getTile(int i) const;
+
+ // Finds the nearest navigation polygon around the center location.
+ // Params:
+ // center - (in) The center of the search box.
+ // extents - (in) The extents of the search box.
+ // Returns: Reference identifier for the polygon, or 0 if no polygons found.
+ dtTilePolyRef findNearestPoly(const float* center, const float* extents);
+
+ // Returns polygons which touch the query box.
+ // Params:
+ // center - (in) the center of the search box.
+ // extents - (in) the extents of the search box.
+ // polys - (out) array holding the search result.
+ // maxPolys - (in) The max number of polygons the polys array can hold.
+ // Returns: Number of polygons in search result array.
+ int queryPolygons(const float* center, const float* extents,
+ dtTilePolyRef* polys, const int maxPolys);
+
+ // Finds path from start polygon to end polygon.
+ // If target polygon canno be reached through the navigation graph,
+ // the last node on the array is nearest node to the end polygon.
+ // Params:
+ // startRef - (in) ref to path start polygon.
+ // endRef - (in) ref to path end polygon.
+ // path - (out) array holding the search result.
+ // maxPathSize - (in) The max number of polygons the path array can hold.
+ // Returns: Number of polygons in search result array.
+ int findPath(dtTilePolyRef startRef, dtTilePolyRef endRef,
+ const float* startPos, const float* endPos,
+ dtTilePolyRef* path, const int maxPathSize);
+
+ // Finds a straight path from start to end locations within the corridor
+ // described by the path polygons.
+ // Start and end locations will be clamped on the corridor.
+ // Params:
+ // startPos - (in) Path start location.
+ // endPos - (in) Path end location.
+ // path - (in) Array of connected polygons describing the corridor.
+ // pathSize - (in) Number of polygons in path array.
+ // straightPath - (out) Points describing the straight path.
+ // maxStraightPathSize - (in) The max number of points the straight path array can hold.
+ // Returns: Number of points in the path.
+ int findStraightPath(const float* startPos, const float* endPos,
+ const dtTilePolyRef* path, const int pathSize,
+ float* straightPath, const int maxStraightPathSize);
+
+ // Finds intersection againts walls starting from start pos.
+ // Params:
+ // startRef - (in) ref to the polygon where the start lies.
+ // startPos - (in) start position of the query.
+ // endPos - (in) end position of the query.
+ // t - (out) hit parameter along the segment, 0 if no hit.
+ // endRef - (out) ref to the last polygon which was processed.
+ // Returns: Number of polygons in path or 0 if failed.
+ int raycast(dtTilePolyRef startRef, const float* startPos, const float* endPos,
+ float& t, dtTilePolyRef* path, const int pathSize);
+
+ // Returns distance to nearest wall from the specified location.
+ // Params:
+ // centerRef - (in) ref to the polygon where the center lies.
+ // centerPos - (in) center if the query circle.
+ // maxRadius - (in) max search radius.
+ // hitPos - (out) location of the nearest hit.
+ // hitNormal - (out) normal of the nearest hit.
+ // Returns: Distance to nearest wall from the test location.
+ float findDistanceToWall(dtTilePolyRef centerRef, const float* centerPos, float maxRadius,
+ float* hitPos, float* hitNormal);
+
+ // Finds polygons found along the navigation graph which touch the specified circle.
+ // Params:
+ // centerRef - (in) ref to the polygon where the center lies.
+ // centerPos - (in) center if the query circle
+ // radius - (in) radius of the query circle
+ // resultRef - (out, opt) refs to the polygons touched by the circle.
+ // resultParent - (out, opt) parent of each result polygon.
+ // resultCost - (out, opt) search cost at each result polygon.
+ // maxResult - (int) maximum capacity of search results.
+ // Returns: Number of results.
+ int findPolysAround(dtTilePolyRef centerRef, const float* centerPos, float radius,
+ dtTilePolyRef* resultRef, dtTilePolyRef* resultParent, float* resultCost,
+ const int maxResult);
+
+ // Returns closest point on navigation polygon.
+ // Params:
+ // ref - (in) ref to the polygon.
+ // pos - (in) the point to check.
+ // closest - (out) closest point.
+ // Returns: true if closest point found.
+ bool closestPointToPoly(dtTilePolyRef ref, const float* pos, float* closest) const;
+
+ // Returns height of the polygon at specified location.
+ // Params:
+ // ref - (in) ref to the polygon.
+ // pos - (in) the point where to locate the height.
+ // height - (out) height at the location.
+ // Returns: true if over polygon.
+ bool getPolyHeight(dtTilePolyRef ref, const float* pos, float* height) const;
+
+ // Returns pointer to a polygon based on ref.
+ const dtTilePoly* getPolyByRef(dtTilePolyRef ref) const;
+
+ // Returns pointer to a polygon vertices based on ref.
+ const float* getPolyVertsByRef(dtTilePolyRef ref) const;
+
+ // Returns pointer to a polygon link based on ref.
+ const dtTileLink* getPolyLinksByRef(dtTilePolyRef ref) const;
+
+private:
+
+ // Returns base id for the tile.
+ dtTilePolyRef getTileId(dtTile* tile);
+ // Returns neighbour tile based on side.
+ dtTile* getNeighbourTileAt(int x, int y, int side);
+ // Returns all polygons in neighbour tile based on portal defined by the segment.
+ int findConnectingPolys(const float* va, const float* vb,
+ dtTile* tile, int side,
+ dtTilePolyRef* con, float* conarea, int maxcon);
+ // Builds internal polygons links for a tile.
+ void buildIntLinks(dtTile* tile);
+ // Builds external polygon links for a tile.
+ void buildExtLinks(dtTile* tile, dtTile* target, int side);
+ // Removes external links at specified side.
+ void removeExtLinks(dtTile* tile, int side);
+ // Queries polygons within a tile.
+ int queryTilePolygons(dtTile* tile, const float* qmin, const float* qmax,
+ dtTilePolyRef* polys, const int maxPolys);
+
+ float getCost(dtTilePolyRef prev, dtTilePolyRef from, dtTilePolyRef to) const;
+ float getFirstCost(const float* pos, dtTilePolyRef from, dtTilePolyRef to) const;
+ float getLastCost(dtTilePolyRef from, dtTilePolyRef to, const float* pos) const;
+ float getHeuristic(const float* from, const float* to) const;
+
+ // Returns portal points between two polygons.
+ bool getPortalPoints(dtTilePolyRef from, dtTilePolyRef to, float* left, float* right) const;
+ // Returns edge mid point between two polygons.
+ bool getEdgeMidPoint(dtTilePolyRef from, dtTilePolyRef to, float* mid) const;
+
+ float m_orig[3];
+ float m_tileSize;
+ float m_portalHeight;
+
+ dtTile* m_posLookup[DT_TILE_LOOKUP_SIZE];
+ dtTile* m_nextFree;
+ dtTile m_tiles[DT_MAX_TILES];
+
+ dtTileLink* m_tmpLinks;
+ int m_ntmpLinks;
+
+ class dtNodePool* m_nodePool;
+ class dtNodeQueue* m_openList;
+};
+
+#endif // DETOURTILENAVMESH_H
diff --git a/extern/recastnavigation/Detour/Include/DetourTileNavMeshBuilder.h b/extern/recastnavigation/Detour/Include/DetourTileNavMeshBuilder.h
new file mode 100644
index 00000000000..643dec1edb1
--- /dev/null
+++ b/extern/recastnavigation/Detour/Include/DetourTileNavMeshBuilder.h
@@ -0,0 +1,29 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#ifndef DETOURTILEDNAVMESHBUILDER_H
+#define DETOURTILEDNAVMESHBUILDER_H
+
+bool dtCreateNavMeshTileData(const unsigned short* verts, const int nverts,
+ const unsigned short* polys, const int npolys, const int nvp,
+ const unsigned short* dmeshes, const float* dverts, const int ndverts,
+ const unsigned char* dtris, const int ndtris,
+ const float* bmin, const float* bmax, float cs, float ch, int tileSize, int walkableClimb,
+ unsigned char** outData, int* outDataSize);
+
+#endif // DETOURTILEDNAVMESHBUILDER_H \ No newline at end of file
diff --git a/extern/recastnavigation/Detour/Source/DetourCommon.cpp b/extern/recastnavigation/Detour/Source/DetourCommon.cpp
new file mode 100644
index 00000000000..e55ce5e6351
--- /dev/null
+++ b/extern/recastnavigation/Detour/Source/DetourCommon.cpp
@@ -0,0 +1,244 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#include <math.h>
+#include "DetourCommon.h"
+
+void closestPtPointTriangle(float* closest, const float* p,
+ const float* a, const float* b, const float* c)
+{
+ // Check if P in vertex region outside A
+ float ab[3], ac[3], ap[3];
+ vsub(ab, b, a);
+ vsub(ac, c, a);
+ vsub(ap, p, a);
+ float d1 = vdot(ab, ap);
+ float d2 = vdot(ac, ap);
+ if (d1 <= 0.0f && d2 <= 0.0f)
+ {
+ // barycentric coordinates (1,0,0)
+ vcopy(closest, a);
+ return;
+ }
+
+ // Check if P in vertex region outside B
+ float bp[3];
+ vsub(bp, p, b);
+ float d3 = vdot(ab, bp);
+ float d4 = vdot(ac, bp);
+ if (d3 >= 0.0f && d4 <= d3)
+ {
+ // barycentric coordinates (0,1,0)
+ vcopy(closest, b);
+ return;
+ }
+
+ // Check if P in edge region of AB, if so return projection of P onto AB
+ float vc = d1*d4 - d3*d2;
+ if (vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f)
+ {
+ // barycentric coordinates (1-v,v,0)
+ float v = d1 / (d1 - d3);
+ closest[0] = a[0] + v * ab[0];
+ closest[1] = a[1] + v * ab[1];
+ closest[2] = a[2] + v * ab[2];
+ return;
+ }
+
+ // Check if P in vertex region outside C
+ float cp[3];
+ vsub(cp, p, c);
+ float d5 = vdot(ab, cp);
+ float d6 = vdot(ac, cp);
+ if (d6 >= 0.0f && d5 <= d6)
+ {
+ // barycentric coordinates (0,0,1)
+ vcopy(closest, c);
+ return;
+ }
+
+ // Check if P in edge region of AC, if so return projection of P onto AC
+ float vb = d5*d2 - d1*d6;
+ if (vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f)
+ {
+ // barycentric coordinates (1-w,0,w)
+ float w = d2 / (d2 - d6);
+ closest[0] = a[0] + w * ac[0];
+ closest[1] = a[1] + w * ac[1];
+ closest[2] = a[2] + w * ac[2];
+ return;
+ }
+
+ // Check if P in edge region of BC, if so return projection of P onto BC
+ float va = d3*d6 - d5*d4;
+ if (va <= 0.0f && (d4 - d3) >= 0.0f && (d5 - d6) >= 0.0f)
+ {
+ // barycentric coordinates (0,1-w,w)
+ float w = (d4 - d3) / ((d4 - d3) + (d5 - d6));
+ closest[0] = b[0] + w * (c[0] - b[0]);
+ closest[1] = b[1] + w * (c[1] - b[1]);
+ closest[2] = b[2] + w * (c[2] - b[2]);
+ return;
+ }
+
+ // P inside face region. Compute Q through its barycentric coordinates (u,v,w)
+ float denom = 1.0f / (va + vb + vc);
+ float v = vb * denom;
+ float w = vc * denom;
+ closest[0] = a[0] + ab[0] * v + ac[0] * w;
+ closest[1] = a[1] + ab[1] * v + ac[1] * w;
+ closest[2] = a[2] + ab[2] * v + ac[2] * w;
+}
+
+bool intersectSegmentPoly2D(const float* p0, const float* p1,
+ const float* verts, int nverts,
+ float& tmin, float& tmax,
+ int& segMin, int& segMax)
+{
+ static const float EPS = 0.00000001f;
+
+ tmin = 0;
+ tmax = 1;
+ segMin = -1;
+ segMax = -1;
+
+ float dir[3];
+ vsub(dir, p1, p0);
+
+ for (int i = 0, j = nverts-1; i < nverts; j=i++)
+ {
+ float edge[3], diff[3];
+ vsub(edge, &verts[i*3], &verts[j*3]);
+ vsub(diff, p0, &verts[j*3]);
+ float n = vperp2D(edge, diff);
+ float d = -vperp2D(edge, dir);
+ if (fabs(d) < EPS)
+ {
+ // S is nearly parallel to this edge
+ if (n < 0)
+ return false;
+ else
+ continue;
+ }
+ float t = n / d;
+ if (d < 0)
+ {
+ // segment S is entering across this edge
+ if (t > tmin)
+ {
+ tmin = t;
+ segMin = j;
+ // S enters after leaving polygon
+ if (tmin > tmax)
+ return false;
+ }
+ }
+ else
+ {
+ // segment S is leaving across this edge
+ if (t < tmax)
+ {
+ tmax = t;
+ segMax = j;
+ // S leaves before entering polygon
+ if (tmax < tmin)
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+float distancePtSegSqr2D(const float* pt, const float* p, const float* q, float& t)
+{
+ float pqx = q[0] - p[0];
+ float pqz = q[2] - p[2];
+ float dx = pt[0] - p[0];
+ float dz = pt[2] - p[2];
+ float d = pqx*pqx + pqz*pqz;
+ t = pqx*dx + pqz*dz;
+ if (d > 0)
+ t /= d;
+ if (t < 0)
+ t = 0;
+ else if (t > 1)
+ t = 1;
+
+ dx = p[0] + t*pqx - pt[0];
+ dz = p[2] + t*pqz - pt[2];
+
+ return dx*dx + dz*dz;
+}
+
+void calcPolyCenter(float* tc, const unsigned short* idx, int nidx, const float* verts)
+{
+ tc[0] = 0.0f;
+ tc[1] = 0.0f;
+ tc[2] = 0.0f;
+ for (int j = 0; j < nidx; ++j)
+ {
+ const float* v = &verts[idx[j]*3];
+ tc[0] += v[0];
+ tc[1] += v[1];
+ tc[2] += v[2];
+ }
+ const float s = 1.0f / nidx;
+ tc[0] *= s;
+ tc[1] *= s;
+ tc[2] *= s;
+}
+
+inline float vdot2(const float* a, const float* b)
+{
+ return a[0]*b[0] + a[2]*b[2];
+}
+
+#include <stdio.h>
+
+bool closestHeightPointTriangle(const float* p, const float* a, const float* b, const float* c, float& h)
+{
+ float v0[3], v1[3], v2[3];
+ vsub(v0, c,a);
+ vsub(v1, b,a);
+ vsub(v2, p,a);
+
+ const float dot00 = vdot2(v0, v0);
+ const float dot01 = vdot2(v0, v1);
+ const float dot02 = vdot2(v0, v2);
+ const float dot11 = vdot2(v1, v1);
+ const float dot12 = vdot2(v1, v2);
+
+ // Compute barycentric coordinates
+ float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01);
+ float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
+ float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
+
+ // The (sloppy) epsilon is needed to allow to get height of points which
+ // are interpolated along the edges of the triangles.
+ static const float EPS = 1e-4f;
+
+ // If point lies inside the triangle, return interpolated ycoord.
+ if (u >= -EPS && v >= -EPS && (u+v) <= 1+EPS)
+ {
+ h = a[1] + v0[1]*u + v1[1]*v;
+ return true;
+ }
+
+ return false;
+}
diff --git a/extern/recastnavigation/Detour/Source/DetourNode.cpp b/extern/recastnavigation/Detour/Source/DetourNode.cpp
new file mode 100644
index 00000000000..1a2305fcede
--- /dev/null
+++ b/extern/recastnavigation/Detour/Source/DetourNode.cpp
@@ -0,0 +1,140 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#include "DetourNode.h"
+#include <string.h>
+
+//////////////////////////////////////////////////////////////////////////////////////////
+dtNodePool::dtNodePool(int maxNodes, int hashSize) :
+
+ m_nodes(0),
+ m_first(0),
+ m_next(0),
+ m_maxNodes(maxNodes),
+ m_hashSize(hashSize),
+ m_nodeCount(0)
+{
+ m_nodes = new dtNode[m_maxNodes];
+ m_next = new unsigned short[m_maxNodes];
+ m_first = new unsigned short[hashSize];
+ memset(m_first, 0xff, sizeof(unsigned short)*m_hashSize);
+ memset(m_next, 0xff, sizeof(unsigned short)*m_maxNodes);
+}
+
+dtNodePool::~dtNodePool()
+{
+ delete [] m_nodes;
+ delete [] m_next;
+ delete [] m_first;
+}
+
+void dtNodePool::clear()
+{
+ memset(m_first, 0xff, sizeof(unsigned short)*m_hashSize);
+ m_nodeCount = 0;
+}
+
+const dtNode* dtNodePool::findNode(unsigned int id) const
+{
+ unsigned int bucket = hashint(id) & (m_hashSize-1);
+ unsigned short i = m_first[bucket];
+ while (i != 0xffff)
+ {
+ if (m_nodes[i].id == id)
+ return &m_nodes[i];
+ i = m_next[i];
+ }
+ return 0;
+}
+
+dtNode* dtNodePool::getNode(unsigned int id)
+{
+ unsigned int bucket = hashint(id) & (m_hashSize-1);
+ unsigned short i = m_first[bucket];
+ dtNode* node = 0;
+ while (i != 0xffff)
+ {
+ if (m_nodes[i].id == id)
+ return &m_nodes[i];
+ i = m_next[i];
+ }
+
+ if (m_nodeCount >= m_maxNodes)
+ return 0;
+
+ i = (unsigned short)m_nodeCount;
+ m_nodeCount++;
+
+ // Init node
+ node = &m_nodes[i];
+ node->pidx = 0;
+ node->cost = 0;
+ node->total = 0;
+ node->id = id;
+ node->flags = 0;
+
+ m_next[i] = m_first[bucket];
+ m_first[bucket] = i;
+
+ return node;
+}
+
+
+//////////////////////////////////////////////////////////////////////////////////////////
+dtNodeQueue::dtNodeQueue(int n) :
+ m_heap(0),
+ m_capacity(n),
+ m_size(0)
+{
+ m_heap = new dtNode*[m_capacity+1];
+}
+
+dtNodeQueue::~dtNodeQueue()
+{
+ delete [] m_heap;
+}
+
+void dtNodeQueue::bubbleUp(int i, dtNode* node)
+{
+ int parent = (i-1)/2;
+ // note: (index > 0) means there is a parent
+ while ((i > 0) && (m_heap[parent]->total > node->total))
+ {
+ m_heap[i] = m_heap[parent];
+ i = parent;
+ parent = (i-1)/2;
+ }
+ m_heap[i] = node;
+}
+
+void dtNodeQueue::trickleDown(int i, dtNode* node)
+{
+ int child = (i*2)+1;
+ while (child < m_size)
+ {
+ if (((child+1) < m_size) &&
+ (m_heap[child]->total > m_heap[child+1]->total))
+ {
+ child++;
+ }
+ m_heap[i] = m_heap[child];
+ i = child;
+ child = (i*2)+1;
+ }
+ bubbleUp(i, node);
+}
diff --git a/extern/recastnavigation/Detour/Source/DetourStatNavMesh.cpp b/extern/recastnavigation/Detour/Source/DetourStatNavMesh.cpp
new file mode 100644
index 00000000000..bf59cd89dda
--- /dev/null
+++ b/extern/recastnavigation/Detour/Source/DetourStatNavMesh.cpp
@@ -0,0 +1,876 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#include <math.h>
+#include <float.h>
+#include <string.h>
+#include <stdio.h>
+#include "DetourStatNavMesh.h"
+#include "DetourNode.h"
+#include "DetourCommon.h"
+
+
+//////////////////////////////////////////////////////////////////////////////////////////
+dtStatNavMesh::dtStatNavMesh() :
+ m_data(0),
+ m_dataSize(0),
+ m_header(0),
+ m_nodePool(0),
+ m_openList(0)
+{
+}
+
+dtStatNavMesh::~dtStatNavMesh()
+{
+ delete m_nodePool;
+ delete m_openList;
+ if (m_data)
+ delete [] m_data;
+}
+
+bool dtStatNavMesh::init(unsigned char* data, int dataSize, bool ownsData)
+{
+ dtStatNavMeshHeader* header = (dtStatNavMeshHeader*)data;
+
+ if (header->magic != DT_STAT_NAVMESH_MAGIC)
+ return false;
+ if (header->version != DT_STAT_NAVMESH_VERSION)
+ return false;
+
+ const int headerSize = sizeof(dtStatNavMeshHeader);
+ const int vertsSize = sizeof(float)*3*header->nverts;
+ const int polysSize = sizeof(dtStatPoly)*header->npolys;
+ const int nodesSize = sizeof(dtStatBVNode)*header->npolys*2;
+ const int detailMeshesSize = sizeof(dtStatPolyDetail)*header->ndmeshes;
+ const int detailVertsSize = sizeof(float)*3*header->ndverts;
+ const int detailTrisSize = sizeof(unsigned char)*4*header->ndtris;
+
+
+ unsigned char* d = data + headerSize;
+ header->verts = (float*)d; d += vertsSize;
+ header->polys = (dtStatPoly*)d; d += polysSize;
+ header->bvtree = (dtStatBVNode*)d; d += nodesSize;
+ header->dmeshes = (dtStatPolyDetail*)d; d += detailMeshesSize;
+ header->dverts = (float*)d; d += detailVertsSize;
+ header->dtris = (unsigned char*)d; d += detailTrisSize;
+
+ m_nodePool = new dtNodePool(2048, 256);
+ if (!m_nodePool)
+ return false;
+
+ m_openList = new dtNodeQueue(2048);
+ if (!m_openList)
+ return false;
+
+ if (ownsData)
+ {
+ m_data = data;
+ m_dataSize = dataSize;
+ }
+
+ m_header = header;
+
+ return true;
+}
+
+const dtStatPoly* dtStatNavMesh::getPolyByRef(dtStatPolyRef ref) const
+{
+ if (!m_header || ref == 0 || (int)ref > m_header->npolys) return 0;
+ return &m_header->polys[ref-1];
+}
+
+int dtStatNavMesh::getPolyIndexByRef(dtStatPolyRef ref) const
+{
+ if (!m_header || ref == 0 || (int)ref > m_header->npolys) return -1;
+ return (int)ref-1;
+}
+
+int dtStatNavMesh::findPath(dtStatPolyRef startRef, dtStatPolyRef endRef,
+ const float* startPos, const float* endPos,
+ dtStatPolyRef* path, const int maxPathSize)
+{
+ if (!m_header) return 0;
+
+ if (!startRef || !endRef)
+ return 0;
+
+ if (!maxPathSize)
+ return 0;
+
+ if (startRef == endRef)
+ {
+ path[0] = startRef;
+ return 1;
+ }
+
+ m_nodePool->clear();
+ m_openList->clear();
+
+ static const float H_SCALE = 1.1f; // Heuristic scale.
+
+ dtNode* startNode = m_nodePool->getNode(startRef);
+ startNode->pidx = 0;
+ startNode->cost = 0;
+ startNode->total = vdist(startPos, endPos) * H_SCALE;
+ startNode->id = startRef;
+ startNode->flags = DT_NODE_OPEN;
+ m_openList->push(startNode);
+
+ dtNode* lastBestNode = startNode;
+ float lastBestNodeCost = startNode->total;
+ while (!m_openList->empty())
+ {
+ dtNode* bestNode = m_openList->pop();
+
+ if (bestNode->id == endRef)
+ {
+ lastBestNode = bestNode;
+ break;
+ }
+
+ const dtStatPoly* poly = getPoly(bestNode->id-1);
+ for (int i = 0; i < (int)poly->nv; ++i)
+ {
+ dtStatPolyRef neighbour = poly->n[i];
+ if (neighbour)
+ {
+ // Skip parent node.
+ if (bestNode->pidx && m_nodePool->getNodeAtIdx(bestNode->pidx)->id == neighbour)
+ continue;
+
+ dtNode* parent = bestNode;
+ dtNode newNode;
+ newNode.pidx = m_nodePool->getNodeIdx(parent);
+ newNode.id = neighbour;
+
+ // Calculate cost.
+ float p0[3], p1[3];
+ if (!parent->pidx)
+ vcopy(p0, startPos);
+ else
+ getEdgeMidPoint(m_nodePool->getNodeAtIdx(parent->pidx)->id, parent->id, p0);
+ getEdgeMidPoint(parent->id, newNode.id, p1);
+ newNode.cost = parent->cost + vdist(p0,p1);
+ // Special case for last node.
+ if (newNode.id == endRef)
+ newNode.cost += vdist(p1, endPos);
+
+ // Heuristic
+ const float h = vdist(p1,endPos)*H_SCALE;
+ newNode.total = newNode.cost + h;
+
+ dtNode* actualNode = m_nodePool->getNode(newNode.id);
+ if (!actualNode)
+ continue;
+
+ if (!((actualNode->flags & DT_NODE_OPEN) && newNode.total > actualNode->total) &&
+ !((actualNode->flags & DT_NODE_CLOSED) && newNode.total > actualNode->total))
+ {
+ actualNode->flags &= ~DT_NODE_CLOSED;
+ actualNode->pidx = newNode.pidx;
+ actualNode->cost = newNode.cost;
+ actualNode->total = newNode.total;
+
+ if (h < lastBestNodeCost)
+ {
+ lastBestNodeCost = h;
+ lastBestNode = actualNode;
+ }
+
+ if (actualNode->flags & DT_NODE_OPEN)
+ {
+ m_openList->modify(actualNode);
+ }
+ else
+ {
+ actualNode->flags |= DT_NODE_OPEN;
+ m_openList->push(actualNode);
+ }
+ }
+ }
+ }
+ bestNode->flags |= DT_NODE_CLOSED;
+ }
+
+ // Reverse the path.
+ dtNode* prev = 0;
+ dtNode* node = lastBestNode;
+ do
+ {
+ dtNode* next = m_nodePool->getNodeAtIdx(node->pidx);
+ node->pidx = m_nodePool->getNodeIdx(prev);
+ prev = node;
+ node = next;
+ }
+ while (node);
+
+ // Store path
+ node = prev;
+ int n = 0;
+ do
+ {
+ path[n++] = node->id;
+ node = m_nodePool->getNodeAtIdx(node->pidx);
+ }
+ while (node && n < maxPathSize);
+
+ return n;
+}
+
+bool dtStatNavMesh::closestPointToPoly(dtStatPolyRef ref, const float* pos, float* closest) const
+{
+ int idx = getPolyIndexByRef(ref);
+ if (idx == -1)
+ return false;
+
+ float closestDistSqr = FLT_MAX;
+ const dtStatPoly* p = getPoly(idx);
+ const dtStatPolyDetail* pd = getPolyDetail(idx);
+
+ for (int j = 0; j < pd->ntris; ++j)
+ {
+ const unsigned char* t = getDetailTri(pd->tbase+j);
+ const float* v[3];
+ for (int k = 0; k < 3; ++k)
+ {
+ if (t[k] < p->nv)
+ v[k] = getVertex(p->v[t[k]]);
+ else
+ v[k] = getDetailVertex(pd->vbase+(t[k]-p->nv));
+ }
+ float pt[3];
+ closestPtPointTriangle(pt, pos, v[0], v[1], v[2]);
+ float d = vdistSqr(pos, pt);
+ if (d < closestDistSqr)
+ {
+ vcopy(closest, pt);
+ closestDistSqr = d;
+ }
+ }
+
+ return true;
+}
+
+bool dtStatNavMesh::getPolyHeight(dtStatPolyRef ref, const float* pos, float* height) const
+{
+ int idx = getPolyIndexByRef(ref);
+ if (idx == -1)
+ return false;
+
+ const dtStatPoly* p = getPoly(idx);
+ const dtStatPolyDetail* pd = getPolyDetail(idx);
+
+ for (int i = 0; i < pd->ntris; ++i)
+ {
+ const unsigned char* t = getDetailTri(pd->tbase+i);
+ const float* v[3];
+ for (int j = 0; j < 3; ++j)
+ {
+ if (t[j] < p->nv)
+ v[j] = getVertex(p->v[t[j]]);
+ else
+ v[j] = getDetailVertex(pd->vbase+(t[j]-p->nv));
+ }
+ float h;
+ if (closestHeightPointTriangle(pos, v[0], v[1], v[2], h))
+ {
+ if (height)
+ *height = h;
+ return true;
+ }
+ }
+
+ return false;
+}
+
+int dtStatNavMesh::findStraightPath(const float* startPos, const float* endPos,
+ const dtStatPolyRef* path, const int pathSize,
+ float* straightPath, const int maxStraightPathSize)
+{
+ if (!m_header) return 0;
+
+ if (!maxStraightPathSize)
+ return 0;
+
+ if (!path[0])
+ return 0;
+
+ int straightPathSize = 0;
+
+ float closestStartPos[3];
+ if (!closestPointToPoly(path[0], startPos, closestStartPos))
+ return 0;
+
+ // Add start point.
+ vcopy(&straightPath[straightPathSize*3], closestStartPos);
+ straightPathSize++;
+ if (straightPathSize >= maxStraightPathSize)
+ return straightPathSize;
+
+ float closestEndPos[3];
+ if (!closestPointToPoly(path[pathSize-1], endPos, closestEndPos))
+ return 0;
+
+ float portalApex[3], portalLeft[3], portalRight[3];
+
+ if (pathSize > 1)
+ {
+ vcopy(portalApex, closestStartPos);
+ vcopy(portalLeft, portalApex);
+ vcopy(portalRight, portalApex);
+ int apexIndex = 0;
+ int leftIndex = 0;
+ int rightIndex = 0;
+
+ for (int i = 0; i < pathSize; ++i)
+ {
+ float left[3], right[3];
+ if (i < pathSize-1)
+ {
+ // Next portal.
+ getPortalPoints(path[i], path[i+1], left, right);
+ }
+ else
+ {
+ // End of the path.
+ vcopy(left, closestEndPos);
+ vcopy(right, closestEndPos);
+ }
+
+ // Right vertex.
+ if (vequal(portalApex, portalRight))
+ {
+ vcopy(portalRight, right);
+ rightIndex = i;
+ }
+ else
+ {
+ if (triArea2D(portalApex, portalRight, right) <= 0.0f)
+ {
+ if (triArea2D(portalApex, portalLeft, right) > 0.0f)
+ {
+ vcopy(portalRight, right);
+ rightIndex = i;
+ }
+ else
+ {
+ vcopy(portalApex, portalLeft);
+ apexIndex = leftIndex;
+
+ if (!vequal(&straightPath[(straightPathSize-1)*3], portalApex))
+ {
+ vcopy(&straightPath[straightPathSize*3], portalApex);
+ straightPathSize++;
+ if (straightPathSize >= maxStraightPathSize)
+ return straightPathSize;
+ }
+
+ vcopy(portalLeft, portalApex);
+ vcopy(portalRight, portalApex);
+ leftIndex = apexIndex;
+ rightIndex = apexIndex;
+
+ // Restart
+ i = apexIndex;
+
+ continue;
+ }
+ }
+ }
+
+ // Left vertex.
+ if (vequal(portalApex, portalLeft))
+ {
+ vcopy(portalLeft, left);
+ leftIndex = i;
+ }
+ else
+ {
+ if (triArea2D(portalApex, portalLeft, left) >= 0.0f)
+ {
+ if (triArea2D(portalApex, portalRight, left) < 0.0f)
+ {
+ vcopy(portalLeft, left);
+ leftIndex = i;
+ }
+ else
+ {
+ vcopy(portalApex, portalRight);
+ apexIndex = rightIndex;
+
+ if (!vequal(&straightPath[(straightPathSize-1)*3], portalApex))
+ {
+ vcopy(&straightPath[straightPathSize*3], portalApex);
+ straightPathSize++;
+ if (straightPathSize >= maxStraightPathSize)
+ return straightPathSize;
+ }
+
+ vcopy(portalLeft, portalApex);
+ vcopy(portalRight, portalApex);
+ leftIndex = apexIndex;
+ rightIndex = apexIndex;
+
+ // Restart
+ i = apexIndex;
+
+ continue;
+ }
+ }
+ }
+ }
+ }
+
+ // Add end point.
+ vcopy(&straightPath[straightPathSize*3], closestEndPos);
+ straightPathSize++;
+
+ return straightPathSize;
+}
+
+int dtStatNavMesh::getPolyVerts(dtStatPolyRef ref, float* verts) const
+{
+ if (!m_header) return 0;
+ const dtStatPoly* poly = getPolyByRef(ref);
+ if (!poly) return 0;
+ float* v = verts;
+ for (int i = 0; i < (int)poly->nv; ++i)
+ {
+ const float* cv = &m_header->verts[poly->v[i]*3];
+ *v++ = cv[0];
+ *v++ = cv[1];
+ *v++ = cv[2];
+ }
+ return (int)poly->nv;
+}
+
+int dtStatNavMesh::raycast(dtStatPolyRef centerRef, const float* startPos, const float* endPos,
+ float& t, dtStatPolyRef* path, const int pathSize)
+{
+ if (!m_header) return 0;
+ if (!centerRef) return 0;
+
+ dtStatPolyRef prevRef = centerRef;
+ dtStatPolyRef curRef = centerRef;
+ t = 0;
+
+ float verts[DT_STAT_VERTS_PER_POLYGON*3];
+ int n = 0;
+
+ while (curRef)
+ {
+ // Cast ray against current polygon.
+ int nv = getPolyVerts(curRef, verts);
+ if (nv < 3)
+ {
+ // Hit bad polygon, report hit.
+ return n;
+ }
+
+ float tmin, tmax;
+ int segMin, segMax;
+ if (!intersectSegmentPoly2D(startPos, endPos, verts, nv, tmin, tmax, segMin, segMax))
+ {
+ // Could not a polygon, keep the old t and report hit.
+ return n;
+ }
+ // Keep track of furthest t so far.
+ if (tmax > t)
+ t = tmax;
+
+ if (n < pathSize)
+ path[n++] = curRef;
+
+ // Check the neighbour of this polygon.
+ const dtStatPoly* poly = getPolyByRef(curRef);
+ dtStatPolyRef nextRef = poly->n[segMax];
+ if (!nextRef)
+ {
+ // No neighbour, we hit a wall.
+ return n;
+ }
+
+ // No hit, advance to neighbour polygon.
+ prevRef = curRef;
+ curRef = nextRef;
+ }
+
+ return n;
+}
+
+
+float dtStatNavMesh::findDistanceToWall(dtStatPolyRef centerRef, const float* centerPos, float maxRadius,
+ float* hitPos, float* hitNormal)
+{
+ if (!m_header) return 0;
+ if (!centerRef) return 0;
+
+ m_nodePool->clear();
+ m_openList->clear();
+
+ dtNode* startNode = m_nodePool->getNode(centerRef);
+ startNode->pidx = 0;
+ startNode->cost = 0;
+ startNode->total = 0;
+ startNode->id = centerRef;
+ startNode->flags = DT_NODE_OPEN;
+ m_openList->push(startNode);
+
+ float radiusSqr = sqr(maxRadius);
+
+ hitNormal[0] = 1;
+ hitNormal[1] = 0;
+ hitNormal[2] = 0;
+
+ while (!m_openList->empty())
+ {
+ dtNode* bestNode = m_openList->pop();
+ const dtStatPoly* poly = getPoly(bestNode->id-1);
+
+ // Hit test walls.
+ for (int i = 0, j = (int)poly->nv-1; i < (int)poly->nv; j = i++)
+ {
+ // Skip non-solid edges.
+ if (poly->n[j]) continue;
+
+ // Calc distance to the edge.
+ const float* vj = getVertex(poly->v[j]);
+ const float* vi = getVertex(poly->v[i]);
+ float tseg;
+ float distSqr = distancePtSegSqr2D(centerPos, vj, vi, tseg);
+
+ // Edge is too far, skip.
+ if (distSqr > radiusSqr)
+ continue;
+
+ // Hit wall, update radius.
+ radiusSqr = distSqr;
+ // Calculate hit pos.
+ hitPos[0] = vj[0] + (vi[0] - vj[0])*tseg;
+ hitPos[1] = vj[1] + (vi[1] - vj[1])*tseg;
+ hitPos[2] = vj[2] + (vi[2] - vj[2])*tseg;
+ }
+
+ // Check to see if teh circle expands to one of the neighbours and expand.
+ for (int i = 0, j = (int)poly->nv-1; i < (int)poly->nv; j = i++)
+ {
+ // Skip solid edges.
+ if (!poly->n[j]) continue;
+
+ // Expand to neighbour if not visited yet.
+ dtStatPolyRef neighbour = poly->n[j];
+
+ // Skip parent node.
+ if (bestNode->pidx && m_nodePool->getNodeAtIdx(bestNode->pidx)->id == neighbour)
+ continue;
+
+ // Calc distance to the edge.
+ const float* vj = getVertex(poly->v[j]);
+ const float* vi = getVertex(poly->v[i]);
+ float tseg;
+ float distSqr = distancePtSegSqr2D(centerPos, vj, vi, tseg);
+
+ // Edge is too far, skip.
+ if (distSqr > radiusSqr)
+ continue;
+
+ dtNode* parent = bestNode;
+ dtNode newNode;
+ newNode.pidx = m_nodePool->getNodeIdx(parent);
+ newNode.id = neighbour;
+
+ // Cost
+ float p0[3], p1[3];
+ if (!parent->pidx)
+ vcopy(p0, centerPos);
+ else
+ getEdgeMidPoint(m_nodePool->getNodeAtIdx(parent->pidx)->id, parent->id, p0);
+ getEdgeMidPoint(parent->id, newNode.id, p1);
+ newNode.total = parent->total + vdist(p0,p1);
+
+ dtNode* actualNode = m_nodePool->getNode(newNode.id);
+ if (!actualNode)
+ continue;
+
+ if (!((actualNode->flags & DT_NODE_OPEN) && newNode.total > actualNode->total) &&
+ !((actualNode->flags & DT_NODE_CLOSED) && newNode.total > actualNode->total))
+ {
+ actualNode->flags &= ~DT_NODE_CLOSED;
+ actualNode->pidx = newNode.pidx;
+ actualNode->total = newNode.total;
+
+ if (actualNode->flags & DT_NODE_OPEN)
+ {
+ m_openList->modify(actualNode);
+ }
+ else
+ {
+ actualNode->flags |= DT_NODE_OPEN;
+ m_openList->push(actualNode);
+ }
+ }
+ }
+ bestNode->flags |= DT_NODE_CLOSED;
+ }
+
+ // Calc hit normal.
+ vsub(hitNormal, centerPos, hitPos);
+ vnormalize(hitNormal);
+
+ return sqrtf(radiusSqr);
+}
+
+int dtStatNavMesh::findPolysAround(dtStatPolyRef centerRef, const float* centerPos, float radius,
+ dtStatPolyRef* resultRef, dtStatPolyRef* resultParent, float* resultCost,
+ const int maxResult)
+{
+ if (!m_header) return 0;
+ if (!centerRef) return 0;
+
+ m_nodePool->clear();
+ m_openList->clear();
+
+ dtNode* startNode = m_nodePool->getNode(centerRef);
+ startNode->pidx = 0;
+ startNode->cost = 0;
+ startNode->total = 0;
+ startNode->id = centerRef;
+ startNode->flags = DT_NODE_OPEN;
+ m_openList->push(startNode);
+
+ int n = 0;
+ if (n < maxResult)
+ {
+ if (resultRef)
+ resultRef[n] = startNode->id;
+ if (resultParent)
+ resultParent[n] = 0;
+ if (resultCost)
+ resultCost[n] = 0;
+ ++n;
+ }
+
+ const float radiusSqr = sqr(radius);
+
+ while (!m_openList->empty())
+ {
+ dtNode* bestNode = m_openList->pop();
+ const dtStatPoly* poly = getPoly(bestNode->id-1);
+ for (unsigned i = 0, j = (int)poly->nv-1; i < (int)poly->nv; j=i++)
+ {
+ dtStatPolyRef neighbour = poly->n[j];
+
+ if (neighbour)
+ {
+ // Skip parent node.
+ if (bestNode->pidx && m_nodePool->getNodeAtIdx(bestNode->pidx)->id == neighbour)
+ continue;
+
+ // Calc distance to the edge.
+ const float* vj = getVertex(poly->v[j]);
+ const float* vi = getVertex(poly->v[i]);
+ float tseg;
+ float distSqr = distancePtSegSqr2D(centerPos, vj, vi, tseg);
+
+ // If the circle is not touching the next polygon, skip it.
+ if (distSqr > radiusSqr)
+ continue;
+
+ dtNode* parent = bestNode;
+ dtNode newNode;
+ newNode.pidx = m_nodePool->getNodeIdx(parent);
+ newNode.id = neighbour;
+
+ // Cost
+ float p0[3], p1[3];
+ if (!parent->pidx)
+ vcopy(p0, centerPos);
+ else
+ getEdgeMidPoint(m_nodePool->getNodeAtIdx(parent->pidx)->id, parent->id, p0);
+ getEdgeMidPoint(parent->id, newNode.id, p1);
+ newNode.total = parent->total + vdist(p0,p1);
+
+ dtNode* actualNode = m_nodePool->getNode(newNode.id);
+ if (!actualNode)
+ continue;
+
+ if (!((actualNode->flags & DT_NODE_OPEN) && newNode.total > actualNode->total) &&
+ !((actualNode->flags & DT_NODE_CLOSED) && newNode.total > actualNode->total))
+ {
+ actualNode->flags &= ~DT_NODE_CLOSED;
+ actualNode->pidx = newNode.pidx;
+ actualNode->total = newNode.total;
+
+ if (actualNode->flags & DT_NODE_OPEN)
+ {
+ m_openList->modify(actualNode);
+ }
+ else
+ {
+ if (n < maxResult)
+ {
+ if (resultRef)
+ resultRef[n] = actualNode->id;
+ if (resultParent)
+ resultParent[n] = m_nodePool->getNodeAtIdx(actualNode->pidx)->id;
+ if (resultCost)
+ resultCost[n] = actualNode->total;
+ ++n;
+ }
+ actualNode->flags |= DT_NODE_OPEN;
+ m_openList->push(actualNode);
+ }
+ }
+ }
+ }
+ bestNode->flags |= DT_NODE_CLOSED;
+
+ }
+
+ return n;
+}
+
+// Returns polygons which are withing certain radius from the query location.
+int dtStatNavMesh::queryPolygons(const float* center, const float* extents,
+ dtStatPolyRef* polys, const int maxIds)
+{
+ if (!m_header) return 0;
+
+ const dtStatBVNode* node = &m_header->bvtree[0];
+ const dtStatBVNode* end = &m_header->bvtree[m_header->nnodes];
+
+ // Calculate quantized box
+ const float ics = 1.0f / m_header->cs;
+ unsigned short bmin[3], bmax[3];
+ // Clamp query box to world box.
+ float minx = clamp(center[0] - extents[0], m_header->bmin[0], m_header->bmax[0]) - m_header->bmin[0];
+ float miny = clamp(center[1] - extents[1], m_header->bmin[1], m_header->bmax[1]) - m_header->bmin[1];
+ float minz = clamp(center[2] - extents[2], m_header->bmin[2], m_header->bmax[2]) - m_header->bmin[2];
+ float maxx = clamp(center[0] + extents[0], m_header->bmin[0], m_header->bmax[0]) - m_header->bmin[0];
+ float maxy = clamp(center[1] + extents[1], m_header->bmin[1], m_header->bmax[1]) - m_header->bmin[1];
+ float maxz = clamp(center[2] + extents[2], m_header->bmin[2], m_header->bmax[2]) - m_header->bmin[2];
+ // Quantize
+ bmin[0] = (unsigned short)(ics * minx) & 0xfffe;
+ bmin[1] = (unsigned short)(ics * miny) & 0xfffe;
+ bmin[2] = (unsigned short)(ics * minz) & 0xfffe;
+ bmax[0] = (unsigned short)(ics * maxx + 1) | 1;
+ bmax[1] = (unsigned short)(ics * maxy + 1) | 1;
+ bmax[2] = (unsigned short)(ics * maxz + 1) | 1;
+
+ // Traverse tree
+ int n = 0;
+ while (node < end)
+ {
+ bool overlap = checkOverlapBox(bmin, bmax, node->bmin, node->bmax);
+ bool isLeafNode = node->i >= 0;
+
+ if (isLeafNode && overlap)
+ {
+ if (n < maxIds)
+ {
+ polys[n] = (dtStatPolyRef)node->i;
+ n++;
+ }
+ }
+
+ if (overlap || isLeafNode)
+ node++;
+ else
+ {
+ const int escapeIndex = -node->i;
+ node += escapeIndex;
+ }
+ }
+
+ return n;
+}
+
+dtStatPolyRef dtStatNavMesh::findNearestPoly(const float* center, const float* extents)
+{
+ if (!m_header) return 0;
+
+ // Get nearby polygons from proximity grid.
+ dtStatPolyRef polys[128];
+ int npolys = queryPolygons(center, extents, polys, 128);
+
+ // Find nearest polygon amongst the nearby polygons.
+ dtStatPolyRef nearest = 0;
+ float nearestDistanceSqr = FLT_MAX;
+ for (int i = 0; i < npolys; ++i)
+ {
+ dtStatPolyRef ref = polys[i];
+ float closest[3];
+ if (!closestPointToPoly(ref, center, closest))
+ continue;
+ float d = vdistSqr(center, closest);
+ if (d < nearestDistanceSqr)
+ {
+ nearestDistanceSqr = d;
+ nearest = ref;
+ }
+ }
+
+ return nearest;
+}
+
+bool dtStatNavMesh::getPortalPoints(dtStatPolyRef from, dtStatPolyRef to, float* left, float* right) const
+{
+ const dtStatPoly* fromPoly = getPolyByRef(from);
+ if (!fromPoly)
+ return false;
+
+ // Find common edge between the polygons and returns the segment end points.
+ for (unsigned i = 0, j = (int)fromPoly->nv - 1; i < (int)fromPoly->nv; j = i++)
+ {
+ unsigned short neighbour = fromPoly->n[j];
+ if (neighbour == to)
+ {
+ vcopy(left, getVertex(fromPoly->v[j]));
+ vcopy(right, getVertex(fromPoly->v[i]));
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool dtStatNavMesh::getEdgeMidPoint(dtStatPolyRef from, dtStatPolyRef to, float* mid) const
+{
+ float left[3], right[3];
+ if (!getPortalPoints(from, to, left,right)) return false;
+ mid[0] = (left[0]+right[0])*0.5f;
+ mid[1] = (left[1]+right[1])*0.5f;
+ mid[2] = (left[2]+right[2])*0.5f;
+ return true;
+}
+
+bool dtStatNavMesh::isInClosedList(dtStatPolyRef ref) const
+{
+ if (!m_nodePool) return false;
+ const dtNode* node = m_nodePool->findNode(ref);
+ return node && node->flags & DT_NODE_CLOSED;
+}
+
+int dtStatNavMesh::getMemUsed() const
+{
+ if (!m_nodePool || ! m_openList)
+ return 0;
+ return sizeof(*this) + m_dataSize +
+ m_nodePool->getMemUsed() +
+ m_openList->getMemUsed();
+}
diff --git a/extern/recastnavigation/Detour/Source/DetourStatNavMeshBuilder.cpp b/extern/recastnavigation/Detour/Source/DetourStatNavMeshBuilder.cpp
new file mode 100644
index 00000000000..2ca455fb53d
--- /dev/null
+++ b/extern/recastnavigation/Detour/Source/DetourStatNavMeshBuilder.cpp
@@ -0,0 +1,346 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+#include "DetourStatNavMesh.h"
+
+struct BVItem
+{
+ unsigned short bmin[3];
+ unsigned short bmax[3];
+ int i;
+};
+
+static int compareItemX(const void* va, const void* vb)
+{
+ const BVItem* a = (const BVItem*)va;
+ const BVItem* b = (const BVItem*)vb;
+ if (a->bmin[0] < b->bmin[0])
+ return -1;
+ if (a->bmin[0] > b->bmin[0])
+ return 1;
+ return 0;
+}
+
+static int compareItemY(const void* va, const void* vb)
+{
+ const BVItem* a = (const BVItem*)va;
+ const BVItem* b = (const BVItem*)vb;
+ if (a->bmin[1] < b->bmin[1])
+ return -1;
+ if (a->bmin[1] > b->bmin[1])
+ return 1;
+ return 0;
+}
+
+static int compareItemZ(const void* va, const void* vb)
+{
+ const BVItem* a = (const BVItem*)va;
+ const BVItem* b = (const BVItem*)vb;
+ if (a->bmin[2] < b->bmin[2])
+ return -1;
+ if (a->bmin[2] > b->bmin[2])
+ return 1;
+ return 0;
+}
+
+static void calcExtends(BVItem* items, int nitems, int imin, int imax,
+ unsigned short* bmin, unsigned short* bmax)
+{
+ bmin[0] = items[imin].bmin[0];
+ bmin[1] = items[imin].bmin[1];
+ bmin[2] = items[imin].bmin[2];
+
+ bmax[0] = items[imin].bmax[0];
+ bmax[1] = items[imin].bmax[1];
+ bmax[2] = items[imin].bmax[2];
+
+ for (int i = imin+1; i < imax; ++i)
+ {
+ const BVItem& it = items[i];
+ if (it.bmin[0] < bmin[0]) bmin[0] = it.bmin[0];
+ if (it.bmin[1] < bmin[1]) bmin[1] = it.bmin[1];
+ if (it.bmin[2] < bmin[2]) bmin[2] = it.bmin[2];
+
+ if (it.bmax[0] > bmax[0]) bmax[0] = it.bmax[0];
+ if (it.bmax[1] > bmax[1]) bmax[1] = it.bmax[1];
+ if (it.bmax[2] > bmax[2]) bmax[2] = it.bmax[2];
+ }
+}
+
+inline int longestAxis(unsigned short x, unsigned short y, unsigned short z)
+{
+ int axis = 0;
+ unsigned short maxVal = x;
+ if (y > maxVal)
+ {
+ axis = 1;
+ maxVal = y;
+ }
+ if (z > maxVal)
+ {
+ axis = 2;
+ maxVal = z;
+ }
+ return axis;
+}
+
+static void subdivide(BVItem* items, int nitems, int imin, int imax, int& curNode, dtStatBVNode* nodes)
+{
+ int inum = imax - imin;
+ int icur = curNode;
+
+ dtStatBVNode& node = nodes[curNode++];
+
+ if (inum == 1)
+ {
+ // Leaf
+ node.bmin[0] = items[imin].bmin[0];
+ node.bmin[1] = items[imin].bmin[1];
+ node.bmin[2] = items[imin].bmin[2];
+
+ node.bmax[0] = items[imin].bmax[0];
+ node.bmax[1] = items[imin].bmax[1];
+ node.bmax[2] = items[imin].bmax[2];
+
+ node.i = items[imin].i;
+ }
+ else
+ {
+ // Split
+ calcExtends(items, nitems, imin, imax, node.bmin, node.bmax);
+
+ int axis = longestAxis(node.bmax[0] - node.bmin[0],
+ node.bmax[1] - node.bmin[1],
+ node.bmax[2] - node.bmin[2]);
+
+ if (axis == 0)
+ {
+ // Sort along x-axis
+ qsort(items+imin, inum, sizeof(BVItem), compareItemX);
+ }
+ else if (axis == 1)
+ {
+ // Sort along y-axis
+ qsort(items+imin, inum, sizeof(BVItem), compareItemY);
+ }
+ else
+ {
+ // Sort along z-axis
+ qsort(items+imin, inum, sizeof(BVItem), compareItemZ);
+ }
+
+ int isplit = imin+inum/2;
+
+ // Left
+ subdivide(items, nitems, imin, isplit, curNode, nodes);
+ // Right
+ subdivide(items, nitems, isplit, imax, curNode, nodes);
+
+ int iescape = curNode - icur;
+ // Negative index means escape.
+ node.i = -iescape;
+ }
+}
+
+/*static*/ int createBVTree(const unsigned short* verts, const int nverts,
+ const unsigned short* polys, const int npolys, const int nvp,
+ float cs, float ch,
+ int nnodes, dtStatBVNode* nodes)
+{
+ // Build tree
+ BVItem* items = new BVItem[npolys];
+ for (int i = 0; i < npolys; i++)
+ {
+ BVItem& it = items[i];
+ it.i = i+1;
+ // Calc polygon bounds.
+ const unsigned short* p = &polys[i*nvp*2];
+ it.bmin[0] = it.bmax[0] = verts[p[0]*3+0];
+ it.bmin[1] = it.bmax[1] = verts[p[0]*3+1];
+ it.bmin[2] = it.bmax[2] = verts[p[0]*3+2];
+
+ for (int j = 1; j < nvp; ++j)
+ {
+ if (p[j] == 0xffff) break;
+ unsigned short x = verts[p[j]*3+0];
+ unsigned short y = verts[p[j]*3+1];
+ unsigned short z = verts[p[j]*3+2];
+
+ if (x < it.bmin[0]) it.bmin[0] = x;
+ if (y < it.bmin[1]) it.bmin[1] = y;
+ if (z < it.bmin[2]) it.bmin[2] = z;
+
+ if (x > it.bmax[0]) it.bmax[0] = x;
+ if (y > it.bmax[1]) it.bmax[1] = y;
+ if (z > it.bmax[2]) it.bmax[2] = z;
+ }
+ // Remap y
+ it.bmin[1] = (unsigned short)floorf((float)it.bmin[1]*ch/cs);
+ it.bmax[1] = (unsigned short)ceilf((float)it.bmax[1]*ch/cs);
+ }
+
+ int curNode = 0;
+ subdivide(items, npolys, 0, npolys, curNode, nodes);
+
+ delete [] items;
+
+ return curNode;
+}
+
+
+bool dtCreateNavMeshData(const unsigned short* verts, const int nverts,
+ const unsigned short* polys, const int npolys, const int nvp,
+ const float* bmin, const float* bmax, float cs, float ch,
+ const unsigned short* dmeshes, const float* dverts, const int ndverts,
+ const unsigned char* dtris, const int ndtris,
+ unsigned char** outData, int* outDataSize)
+{
+ if (nvp > DT_STAT_VERTS_PER_POLYGON)
+ return false;
+ if (nverts >= 0xffff)
+ return false;
+
+ if (!nverts)
+ return false;
+ if (!npolys)
+ return false;
+ if (!dmeshes || !dverts || ! dtris)
+ return false;
+
+ // Find unique detail vertices.
+ int uniqueDetailVerts = 0;
+ if (dmeshes)
+ {
+ for (int i = 0; i < npolys; ++i)
+ {
+ const unsigned short* p = &polys[i*nvp*2];
+ int ndv = dmeshes[i*4+1];
+ int nv = 0;
+ for (int j = 0; j < nvp; ++j)
+ {
+ if (p[j] == 0xffff) break;
+ nv++;
+ }
+ ndv -= nv;
+ uniqueDetailVerts += ndv;
+ }
+ }
+
+ // Calculate data size
+ const int headerSize = sizeof(dtStatNavMeshHeader);
+ const int vertsSize = sizeof(float)*3*nverts;
+ const int polysSize = sizeof(dtStatPoly)*npolys;
+ const int nodesSize = sizeof(dtStatBVNode)*npolys*2;
+ const int detailMeshesSize = sizeof(dtStatPolyDetail)*npolys;
+ const int detailVertsSize = sizeof(float)*3*uniqueDetailVerts;
+ const int detailTrisSize = sizeof(unsigned char)*4*ndtris;
+
+ const int dataSize = headerSize + vertsSize + polysSize + nodesSize +
+ detailMeshesSize + detailVertsSize + detailTrisSize;
+ unsigned char* data = new unsigned char[dataSize];
+ if (!data)
+ return false;
+ memset(data, 0, dataSize);
+
+ unsigned char* d = data;
+ dtStatNavMeshHeader* header = (dtStatNavMeshHeader*)d; d += headerSize;
+ float* navVerts = (float*)d; d += vertsSize;
+ dtStatPoly* navPolys = (dtStatPoly*)d; d += polysSize;
+ dtStatBVNode* navNodes = (dtStatBVNode*)d; d += nodesSize;
+ dtStatPolyDetail* navDMeshes = (dtStatPolyDetail*)d; d += detailMeshesSize;
+ float* navDVerts = (float*)d; d += detailVertsSize;
+ unsigned char* navDTris = (unsigned char*)d; d += detailTrisSize;
+
+ // Store header
+ header->magic = DT_STAT_NAVMESH_MAGIC;
+ header->version = DT_STAT_NAVMESH_VERSION;
+ header->npolys = npolys;
+ header->nverts = nverts;
+ header->cs = cs;
+ header->bmin[0] = bmin[0];
+ header->bmin[1] = bmin[1];
+ header->bmin[2] = bmin[2];
+ header->bmax[0] = bmax[0];
+ header->bmax[1] = bmax[1];
+ header->bmax[2] = bmax[2];
+ header->ndmeshes = dmeshes ? npolys : 0;
+ header->ndverts = dmeshes ? uniqueDetailVerts : 0;
+ header->ndtris = dmeshes ? ndtris : 0;
+
+ // Store vertices
+ for (int i = 0; i < nverts; ++i)
+ {
+ const unsigned short* iv = &verts[i*3];
+ float* v = &navVerts[i*3];
+ v[0] = bmin[0] + iv[0] * cs;
+ v[1] = bmin[1] + iv[1] * ch;
+ v[2] = bmin[2] + iv[2] * cs;
+ }
+
+ // Store polygons
+ const unsigned short* src = polys;
+ for (int i = 0; i < npolys; ++i)
+ {
+ dtStatPoly* p = &navPolys[i];
+ p->nv = 0;
+ for (int j = 0; j < nvp; ++j)
+ {
+ if (src[j] == 0xffff) break;
+ p->v[j] = src[j];
+ p->n[j] = src[nvp+j]+1;
+ p->nv++;
+ }
+ src += nvp*2;
+ }
+
+ header->nnodes = createBVTree(verts, nverts, polys, npolys, nvp,
+ cs, ch, npolys*2, navNodes);
+
+
+ // Store detail meshes and vertices.
+ // The nav polygon vertices are stored as the first vertices on each mesh.
+ // We compress the mesh data by skipping them and using the navmesh coordinates.
+ unsigned short vbase = 0;
+ for (int i = 0; i < npolys; ++i)
+ {
+ dtStatPolyDetail& dtl = navDMeshes[i];
+ const int vb = dmeshes[i*4+0];
+ const int ndv = dmeshes[i*4+1];
+ const int nv = navPolys[i].nv;
+ dtl.vbase = vbase;
+ dtl.nverts = ndv-nv;
+ dtl.tbase = dmeshes[i*4+2];
+ dtl.ntris = dmeshes[i*4+3];
+ // Copy vertices except the first 'nv' verts which are equal to nav poly verts.
+ if (ndv-nv)
+ {
+ memcpy(&navDVerts[vbase*3], &dverts[(vb+nv)*3], sizeof(float)*3*(ndv-nv));
+ vbase += ndv-nv;
+ }
+ }
+ // Store triangles.
+ memcpy(navDTris, dtris, sizeof(unsigned char)*4*ndtris);
+
+ *outData = data;
+ *outDataSize = dataSize;
+
+ return true;
+}
diff --git a/extern/recastnavigation/Detour/Source/DetourTileNavMesh.cpp b/extern/recastnavigation/Detour/Source/DetourTileNavMesh.cpp
new file mode 100644
index 00000000000..0813c7755cc
--- /dev/null
+++ b/extern/recastnavigation/Detour/Source/DetourTileNavMesh.cpp
@@ -0,0 +1,1428 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#include <math.h>
+#include <float.h>
+#include <string.h>
+#include <stdio.h>
+#include "DetourTileNavMesh.h"
+#include "DetourNode.h"
+#include "DetourCommon.h"
+
+
+inline int opposite(int side) { return (side+2) & 0x3; }
+
+inline bool overlapBoxes(const float* amin, const float* amax,
+ const float* bmin, const float* bmax)
+{
+ bool overlap = true;
+ overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
+ overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
+ overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap;
+ return overlap;
+}
+
+inline bool overlapRects(const float* amin, const float* amax,
+ const float* bmin, const float* bmax)
+{
+ bool overlap = true;
+ overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
+ overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
+ return overlap;
+}
+
+static void calcRect(const float* va, const float* vb,
+ float* bmin, float* bmax,
+ int side, float padx, float pady)
+{
+ if ((side&1) == 0)
+ {
+ bmin[0] = min(va[2],vb[2]) + padx;
+ bmin[1] = min(va[1],vb[1]) - pady;
+ bmax[0] = max(va[2],vb[2]) - padx;
+ bmax[1] = max(va[1],vb[1]) + pady;
+ }
+ else
+ {
+ bmin[0] = min(va[0],vb[0]) + padx;
+ bmin[1] = min(va[1],vb[1]) - pady;
+ bmax[0] = max(va[0],vb[0]) - padx;
+ bmax[1] = max(va[1],vb[1]) + pady;
+ }
+}
+
+inline int computeTileHash(int x, int y)
+{
+ const unsigned int h1 = 0x8da6b343; // Large multiplicative constants;
+ const unsigned int h2 = 0xd8163841; // here arbitrarily chosen primes
+ unsigned int n = h1 * x + h2 * y;
+ return (int)(n & (DT_TILE_LOOKUP_SIZE-1));
+}
+
+//////////////////////////////////////////////////////////////////////////////////////////
+dtTiledNavMesh::dtTiledNavMesh() :
+ m_tileSize(0),
+ m_portalHeight(0),
+ m_nextFree(0),
+ m_tmpLinks(0),
+ m_ntmpLinks(0),
+ m_nodePool(0),
+ m_openList(0)
+{
+}
+
+dtTiledNavMesh::~dtTiledNavMesh()
+{
+ for (int i = 0; i < DT_MAX_TILES; ++i)
+ {
+ if (m_tiles[i].data && m_tiles[i].dataSize < 0)
+ {
+ delete [] m_tiles[i].data;
+ m_tiles[i].data = 0;
+ m_tiles[i].dataSize = 0;
+ }
+ }
+ delete [] m_tmpLinks;
+ delete m_nodePool;
+ delete m_openList;
+}
+
+bool dtTiledNavMesh::init(const float* orig, float tileSize, float portalHeight)
+{
+ vcopy(m_orig, orig);
+ m_tileSize = tileSize;
+ m_portalHeight = portalHeight;
+
+ // Init tiles
+ memset(m_tiles, 0, sizeof(dtTile)*DT_MAX_TILES);
+ memset(m_posLookup, 0, sizeof(dtTile*)*DT_TILE_LOOKUP_SIZE);
+ m_nextFree = 0;
+ for (int i = DT_MAX_TILES-1; i >= 0; --i)
+ {
+ m_tiles[i].next = m_nextFree;
+ m_nextFree = &m_tiles[i];
+ }
+
+ if (!m_nodePool)
+ {
+ m_nodePool = new dtNodePool(2048, 256);
+ if (!m_nodePool)
+ return false;
+ }
+
+ if (!m_openList)
+ {
+ m_openList = new dtNodeQueue(2048);
+ if (!m_openList)
+ return false;
+ }
+
+ return true;
+}
+
+//////////////////////////////////////////////////////////////////////////////////////////
+int dtTiledNavMesh::findConnectingPolys(const float* va, const float* vb,
+ dtTile* tile, int side,
+ dtTilePolyRef* con, float* conarea, int maxcon)
+{
+ if (!tile) return 0;
+ dtTileHeader* h = tile->header;
+
+ float amin[2], amax[2];
+ calcRect(va,vb, amin,amax, side, 0.01f, m_portalHeight);
+
+ // Remove links pointing to 'side' and compact the links array.
+ float bmin[2], bmax[2];
+ unsigned short m = 0x8000 | (unsigned short)side;
+ int n = 0;
+
+ dtTilePolyRef base = getTileId(tile);
+
+ for (int i = 0; i < h->npolys; ++i)
+ {
+ dtTilePoly* poly = &h->polys[i];
+ for (int j = 0; j < poly->nv; ++j)
+ {
+ // Skip edges which do not point to the right side.
+ if (poly->n[j] != m) continue;
+ // Check if the segments touch.
+ const float* vc = &h->verts[poly->v[j]*3];
+ const float* vd = &h->verts[poly->v[(j+1) % (int)poly->nv]*3];
+ calcRect(vc,vd, bmin,bmax, side, 0.01f, m_portalHeight);
+ if (!overlapRects(amin,amax, bmin,bmax)) continue;
+ // Add return value.
+ if (n < maxcon)
+ {
+ conarea[n*2+0] = max(amin[0], bmin[0]);
+ conarea[n*2+1] = min(amax[0], bmax[0]);
+ con[n] = base | (unsigned int)i;
+ n++;
+ }
+ break;
+ }
+ }
+ return n;
+}
+
+void dtTiledNavMesh::removeExtLinks(dtTile* tile, int side)
+{
+ if (!tile) return;
+ dtTileHeader* h = tile->header;
+
+ // Remove links pointing to 'side' and compact the links array.
+ dtTileLink* pool = m_tmpLinks;
+ int nlinks = 0;
+ for (int i = 0; i < h->npolys; ++i)
+ {
+ dtTilePoly* poly = &h->polys[i];
+ int plinks = nlinks;
+ int nplinks = 0;
+ for (int j = 0; j < poly->nlinks; ++j)
+ {
+ dtTileLink* link = &h->links[poly->links+j];
+ if ((int)link->side != side)
+ {
+ if (nlinks < h->maxlinks)
+ {
+ dtTileLink* dst = &pool[nlinks++];
+ memcpy(dst, link, sizeof(dtTileLink));
+ nplinks++;
+ }
+ }
+ }
+ poly->links = plinks;
+ poly->nlinks = nplinks;
+ }
+ h->nlinks = nlinks;
+ if (h->nlinks)
+ memcpy(h->links, m_tmpLinks, sizeof(dtTileLink)*nlinks);
+}
+
+void dtTiledNavMesh::buildExtLinks(dtTile* tile, dtTile* target, int side)
+{
+ if (!tile) return;
+ dtTileHeader* h = tile->header;
+
+ // Remove links pointing to 'side' and compact the links array.
+ dtTileLink* pool = m_tmpLinks;
+ int nlinks = 0;
+ for (int i = 0; i < h->npolys; ++i)
+ {
+ dtTilePoly* poly = &h->polys[i];
+ int plinks = nlinks;
+ int nplinks = 0;
+ // Copy internal and other external links.
+ for (int j = 0; j < poly->nlinks; ++j)
+ {
+ dtTileLink* link = &h->links[poly->links+j];
+ if ((int)link->side != side)
+ {
+ if (nlinks < h->maxlinks)
+ {
+ dtTileLink* dst = &pool[nlinks++];
+ memcpy(dst, link, sizeof(dtTileLink));
+ nplinks++;
+ }
+ }
+ }
+ // Create new links.
+ unsigned short m = 0x8000 | (unsigned short)side;
+ for (int j = 0; j < poly->nv; ++j)
+ {
+ // Skip edges which do not point to the right side.
+ if (poly->n[j] != m) continue;
+
+ // Create new links
+ const float* va = &h->verts[poly->v[j]*3];
+ const float* vb = &h->verts[poly->v[(j+1)%(int)poly->nv]*3];
+ dtTilePolyRef nei[4];
+ float neia[4*2];
+ int nnei = findConnectingPolys(va,vb, target, opposite(side), nei,neia,4);
+ for (int k = 0; k < nnei; ++k)
+ {
+ if (nlinks < h->maxlinks)
+ {
+ dtTileLink* link = &pool[nlinks++];
+ link->ref = nei[k];
+ link->p = (unsigned short)i;
+ link->e = (unsigned char)j;
+ link->side = (unsigned char)side;
+
+ // Compress portal limits to a byte value.
+ if (side == 0 || side == 2)
+ {
+ const float lmin = min(va[2], vb[2]);
+ const float lmax = max(va[2], vb[2]);
+ link->bmin = (unsigned char)(clamp((neia[k*2+0]-lmin)/(lmax-lmin), 0.0f, 1.0f)*255.0f);
+ link->bmax = (unsigned char)(clamp((neia[k*2+1]-lmin)/(lmax-lmin), 0.0f, 1.0f)*255.0f);
+ }
+ else
+ {
+ const float lmin = min(va[0], vb[0]);
+ const float lmax = max(va[0], vb[0]);
+ link->bmin = (unsigned char)(clamp((neia[k*2+0]-lmin)/(lmax-lmin), 0.0f, 1.0f)*255.0f);
+ link->bmax = (unsigned char)(clamp((neia[k*2+1]-lmin)/(lmax-lmin), 0.0f, 1.0f)*255.0f);
+ }
+ nplinks++;
+ }
+ }
+ }
+
+ poly->links = plinks;
+ poly->nlinks = nplinks;
+ }
+ h->nlinks = nlinks;
+ if (h->nlinks)
+ memcpy(h->links, m_tmpLinks, sizeof(dtTileLink)*nlinks);
+}
+
+void dtTiledNavMesh::buildIntLinks(dtTile* tile)
+{
+ if (!tile) return;
+ dtTileHeader* h = tile->header;
+
+ dtTilePolyRef base = getTileId(tile);
+ dtTileLink* pool = h->links;
+ int nlinks = 0;
+ for (int i = 0; i < h->npolys; ++i)
+ {
+ dtTilePoly* poly = &h->polys[i];
+ poly->links = nlinks;
+ poly->nlinks = 0;
+ for (int j = 0; j < poly->nv; ++j)
+ {
+ // Skip hard and non-internal edges.
+ if (poly->n[j] == 0 || (poly->n[j] & 0x8000)) continue;
+
+ if (nlinks < h->maxlinks)
+ {
+ dtTileLink* link = &pool[nlinks++];
+ link->ref = base | (unsigned int)(poly->n[j]-1);
+ link->p = (unsigned short)i;
+ link->e = (unsigned char)j;
+ link->side = 0xff;
+ link->bmin = link->bmax = 0;
+ poly->nlinks++;
+ }
+ }
+ }
+ h->nlinks = nlinks;
+}
+
+bool dtTiledNavMesh::addTileAt(int x, int y, unsigned char* data, int dataSize, bool ownsData)
+{
+ if (getTileAt(x,y))
+ return false;
+ // Make sure there is enough space for new tile.
+ if (!m_nextFree)
+ return false;
+ // Make sure the data is in right format.
+ dtTileHeader* header = (dtTileHeader*)data;
+ if (header->magic != DT_TILE_NAVMESH_MAGIC)
+ return false;
+ if (header->version != DT_TILE_NAVMESH_VERSION)
+ return false;
+
+ // Make sure the tmp link array is large enough.
+ if (header->maxlinks > m_ntmpLinks)
+ {
+ m_ntmpLinks = header->maxlinks;
+ delete [] m_tmpLinks;
+ m_tmpLinks = 0;
+ m_tmpLinks = new dtTileLink[m_ntmpLinks];
+ }
+ if (!m_tmpLinks)
+ return false;
+
+ // Allocate a tile.
+ dtTile* tile = m_nextFree;
+ m_nextFree = tile->next;
+ tile->next = 0;
+
+ // Insert tile into the position lut.
+ int h = computeTileHash(x,y);
+ tile->next = m_posLookup[h];
+ m_posLookup[h] = tile;
+
+ // Patch header pointers.
+ const int headerSize = sizeof(dtTileHeader);
+ const int vertsSize = sizeof(float)*3*header->nverts;
+ const int polysSize = sizeof(dtTilePoly)*header->npolys;
+ const int linksSize = sizeof(dtTileLink)*(header->maxlinks);
+ const int detailMeshesSize = sizeof(dtTilePolyDetail)*header->ndmeshes;
+ const int detailVertsSize = sizeof(float)*3*header->ndverts;
+ const int detailTrisSize = sizeof(unsigned char)*4*header->ndtris;
+
+ unsigned char* d = data + headerSize;
+ header->verts = (float*)d; d += vertsSize;
+ header->polys = (dtTilePoly*)d; d += polysSize;
+ header->links = (dtTileLink*)d; d += linksSize;
+ header->dmeshes = (dtTilePolyDetail*)d; d += detailMeshesSize;
+ header->dverts = (float*)d; d += detailVertsSize;
+ header->dtris = (unsigned char*)d; d += detailTrisSize;
+
+ // Init tile.
+ tile->header = header;
+ tile->x = x;
+ tile->y = y;
+ tile->data = data;
+ tile->dataSize = dataSize;
+ tile->ownsData = ownsData;
+
+ buildIntLinks(tile);
+
+ // Create connections connections.
+ for (int i = 0; i < 4; ++i)
+ {
+ dtTile* nei = getNeighbourTileAt(x,y,i);
+ if (nei)
+ {
+ buildExtLinks(tile, nei, i);
+ buildExtLinks(nei, tile, opposite(i));
+ }
+ }
+
+ return true;
+}
+
+dtTile* dtTiledNavMesh::getTileAt(int x, int y)
+{
+ // Find tile based on hash.
+ int h = computeTileHash(x,y);
+ dtTile* tile = m_posLookup[h];
+ while (tile)
+ {
+ if (tile->x == x && tile->y == y)
+ return tile;
+ tile = tile->next;
+ }
+ return 0;
+}
+
+dtTile* dtTiledNavMesh::getTile(int i)
+{
+ return &m_tiles[i];
+}
+
+const dtTile* dtTiledNavMesh::getTile(int i) const
+{
+ return &m_tiles[i];
+}
+
+dtTile* dtTiledNavMesh::getNeighbourTileAt(int x, int y, int side)
+{
+ switch (side)
+ {
+ case 0: x++; break;
+ case 1: y++; break;
+ case 2: x--; break;
+ case 3: y--; break;
+ };
+ return getTileAt(x,y);
+}
+
+bool dtTiledNavMesh::removeTileAt(int x, int y, unsigned char** data, int* dataSize)
+{
+ // Remove tile from hash lookup.
+ int h = computeTileHash(x,y);
+ dtTile* prev = 0;
+ dtTile* tile = m_posLookup[h];
+ while (tile)
+ {
+ if (tile->x == x && tile->y == y)
+ {
+ if (prev)
+ prev->next = tile->next;
+ else
+ m_posLookup[h] = tile->next;
+ break;
+ }
+ prev = tile;
+ tile = tile->next;
+ }
+ if (!tile)
+ return false;
+
+ // Remove connections to neighbour tiles.
+ for (int i = 0; i < 4; ++i)
+ {
+ dtTile* nei = getNeighbourTileAt(x,y,i);
+ if (!nei) continue;
+ removeExtLinks(nei, opposite(i));
+ }
+
+
+ // Reset tile.
+ if (tile->ownsData)
+ {
+ // Owns data
+ delete [] tile->data;
+ tile->data = 0;
+ tile->dataSize = 0;
+ if (data) *data = 0;
+ if (dataSize) *dataSize = 0;
+ }
+ else
+ {
+ if (data) *data = tile->data;
+ if (dataSize) *dataSize = tile->dataSize;
+ }
+ tile->header = 0;
+ tile->x = tile->y = 0;
+ tile->salt++;
+
+ // Add to free list.
+ tile->next = m_nextFree;
+ m_nextFree = tile;
+
+ return true;
+}
+
+
+
+bool dtTiledNavMesh::closestPointToPoly(dtTilePolyRef ref, const float* pos, float* closest) const
+{
+ unsigned int salt, it, ip;
+ dtDecodeTileId(ref, salt, it, ip);
+ if (it >= DT_MAX_TILES) return false;
+ if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return false;
+ const dtTileHeader* header = m_tiles[it].header;
+
+ if (ip >= (unsigned int)header->npolys) return false;
+ const dtTilePoly* poly = &header->polys[ip];
+
+ float closestDistSqr = FLT_MAX;
+ const dtTilePolyDetail* pd = &header->dmeshes[ip];
+
+ for (int j = 0; j < pd->ntris; ++j)
+ {
+ const unsigned char* t = &header->dtris[(pd->tbase+j)*4];
+ const float* v[3];
+ for (int k = 0; k < 3; ++k)
+ {
+ if (t[k] < poly->nv)
+ v[k] = &header->verts[poly->v[t[k]]*3];
+ else
+ v[k] = &header->dverts[(pd->vbase+(t[k]-poly->nv))*3];
+ }
+ float pt[3];
+ closestPtPointTriangle(pt, pos, v[0], v[1], v[2]);
+ float d = vdistSqr(pos, pt);
+ if (d < closestDistSqr)
+ {
+ vcopy(closest, pt);
+ closestDistSqr = d;
+ }
+ }
+
+ return true;
+}
+
+bool dtTiledNavMesh::getPolyHeight(dtTilePolyRef ref, const float* pos, float* height) const
+{
+ unsigned int salt, it, ip;
+ dtDecodeTileId(ref, salt, it, ip);
+ if (it >= DT_MAX_TILES) return false;
+ if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return false;
+ const dtTileHeader* header = m_tiles[it].header;
+
+ if (ip >= (unsigned int)header->npolys) return false;
+ const dtTilePoly* poly = &header->polys[ip];
+
+ const dtTilePolyDetail* pd = &header->dmeshes[ip];
+ for (int j = 0; j < pd->ntris; ++j)
+ {
+ const unsigned char* t = &header->dtris[(pd->tbase+j)*4];
+ const float* v[3];
+ for (int k = 0; k < 3; ++k)
+ {
+ if (t[k] < poly->nv)
+ v[k] = &header->verts[poly->v[t[k]]*3];
+ else
+ v[k] = &header->dverts[(pd->vbase+(t[k]-poly->nv))*3];
+ }
+ float h;
+ if (closestHeightPointTriangle(pos, v[0], v[1], v[2], h))
+ {
+ if (height)
+ *height = h;
+ return true;
+ }
+ }
+
+ return false;
+}
+
+
+dtTilePolyRef dtTiledNavMesh::findNearestPoly(const float* center, const float* extents)
+{
+ // Get nearby polygons from proximity grid.
+ dtTilePolyRef polys[128];
+ int npolys = queryPolygons(center, extents, polys, 128);
+
+ // Find nearest polygon amongst the nearby polygons.
+ dtTilePolyRef nearest = 0;
+ float nearestDistanceSqr = FLT_MAX;
+ for (int i = 0; i < npolys; ++i)
+ {
+ dtTilePolyRef ref = polys[i];
+ float closest[3];
+ if (!closestPointToPoly(ref, center, closest))
+ continue;
+ float d = vdistSqr(center, closest);
+ if (d < nearestDistanceSqr)
+ {
+ nearestDistanceSqr = d;
+ nearest = ref;
+ }
+ }
+
+ return nearest;
+}
+
+dtTilePolyRef dtTiledNavMesh::getTileId(dtTile* tile)
+{
+ if (!tile) return 0;
+ const unsigned int it = tile - m_tiles;
+ return dtEncodeTileId(tile->salt, it, 0);
+}
+
+int dtTiledNavMesh::queryTilePolygons(dtTile* tile,
+ const float* qmin, const float* qmax,
+ dtTilePolyRef* polys, const int maxPolys)
+{
+ float bmin[3], bmax[3];
+ const dtTileHeader* header = tile->header;
+ int n = 0;
+ dtTilePolyRef base = getTileId(tile);
+ for (int i = 0; i < header->npolys; ++i)
+ {
+ // Calc polygon bounds.
+ dtTilePoly* p = &header->polys[i];
+ const float* v = &header->verts[p->v[0]*3];
+ vcopy(bmin, v);
+ vcopy(bmax, v);
+ for (int j = 1; j < p->nv; ++j)
+ {
+ v = &header->verts[p->v[j]*3];
+ vmin(bmin, v);
+ vmax(bmax, v);
+ }
+ if (overlapBoxes(qmin,qmax, bmin,bmax))
+ {
+ if (n < maxPolys)
+ polys[n++] = base | (dtTilePolyRef)i;
+ }
+ }
+ return n;
+}
+
+int dtTiledNavMesh::queryPolygons(const float* center, const float* extents,
+ dtTilePolyRef* polys, const int maxPolys)
+{
+ float bmin[3], bmax[3];
+ bmin[0] = center[0] - extents[0];
+ bmin[1] = center[1] - extents[1];
+ bmin[2] = center[2] - extents[2];
+
+ bmax[0] = center[0] + extents[0];
+ bmax[1] = center[1] + extents[1];
+ bmax[2] = center[2] + extents[2];
+
+ // Find tiles the query touches.
+ const int minx = (int)floorf((bmin[0]-m_orig[0]) / m_tileSize);
+ const int maxx = (int)ceilf((bmax[0]-m_orig[0]) / m_tileSize);
+
+ const int miny = (int)floorf((bmin[2]-m_orig[2]) / m_tileSize);
+ const int maxy = (int)ceilf((bmax[2]-m_orig[2]) / m_tileSize);
+
+ int n = 0;
+ for (int y = miny; y < maxy; ++y)
+ {
+ for (int x = minx; x < maxx; ++x)
+ {
+ dtTile* tile = getTileAt(x,y);
+ if (!tile) continue;
+ n += queryTilePolygons(tile, bmin, bmax, polys+n, maxPolys-n);
+ if (n >= maxPolys) return n;
+ }
+ }
+
+ return n;
+}
+
+int dtTiledNavMesh::findPath(dtTilePolyRef startRef, dtTilePolyRef endRef,
+ const float* startPos, const float* endPos,
+ dtTilePolyRef* path, const int maxPathSize)
+{
+ if (!startRef || !endRef)
+ return 0;
+
+ if (!maxPathSize)
+ return 0;
+
+ if (!getPolyByRef(startRef) || !getPolyByRef(endRef))
+ return 0;
+
+ if (startRef == endRef)
+ {
+ path[0] = startRef;
+ return 1;
+ }
+
+ if (!m_nodePool || !m_openList)
+ return 0;
+
+ m_nodePool->clear();
+ m_openList->clear();
+
+ static const float H_SCALE = 1.1f; // Heuristic scale.
+
+ dtNode* startNode = m_nodePool->getNode(startRef);
+ startNode->pidx = 0;
+ startNode->cost = 0;
+ startNode->total = vdist(startPos, endPos) * H_SCALE;
+ startNode->id = startRef;
+ startNode->flags = DT_NODE_OPEN;
+ m_openList->push(startNode);
+
+ dtNode* lastBestNode = startNode;
+ float lastBestNodeCost = startNode->total;
+ while (!m_openList->empty())
+ {
+ dtNode* bestNode = m_openList->pop();
+
+ if (bestNode->id == endRef)
+ {
+ lastBestNode = bestNode;
+ break;
+ }
+
+ // Get poly and tile.
+ unsigned int salt, it, ip;
+ dtDecodeTileId(bestNode->id, salt, it, ip);
+ // The API input has been cheked already, skip checking internal data.
+ const dtTileHeader* header = m_tiles[it].header;
+ const dtTilePoly* poly = &header->polys[ip];
+
+ for (int i = 0; i < poly->nlinks; ++i)
+ {
+ dtTilePolyRef neighbour = header->links[poly->links+i].ref;
+ if (neighbour)
+ {
+ // Skip parent node.
+ if (bestNode->pidx && m_nodePool->getNodeAtIdx(bestNode->pidx)->id == neighbour)
+ continue;
+
+ dtNode* parent = bestNode;
+ dtNode newNode;
+ newNode.pidx = m_nodePool->getNodeIdx(parent);
+ newNode.id = neighbour;
+
+ // Calculate cost.
+ float p0[3], p1[3];
+ if (!parent->pidx)
+ vcopy(p0, startPos);
+ else
+ getEdgeMidPoint(m_nodePool->getNodeAtIdx(parent->pidx)->id, parent->id, p0);
+ getEdgeMidPoint(parent->id, newNode.id, p1);
+ newNode.cost = parent->cost + vdist(p0,p1);
+ // Special case for last node.
+ if (newNode.id == endRef)
+ newNode.cost += vdist(p1, endPos);
+
+ // Heuristic
+ const float h = vdist(p1,endPos)*H_SCALE;
+ newNode.total = newNode.cost + h;
+
+ dtNode* actualNode = m_nodePool->getNode(newNode.id);
+ if (!actualNode)
+ continue;
+
+ if (!((actualNode->flags & DT_NODE_OPEN) && newNode.total > actualNode->total) &&
+ !((actualNode->flags & DT_NODE_CLOSED) && newNode.total > actualNode->total))
+ {
+ actualNode->flags &= DT_NODE_CLOSED;
+ actualNode->pidx = newNode.pidx;
+ actualNode->cost = newNode.cost;
+ actualNode->total = newNode.total;
+
+ if (h < lastBestNodeCost)
+ {
+ lastBestNodeCost = h;
+ lastBestNode = actualNode;
+ }
+
+ if (actualNode->flags & DT_NODE_OPEN)
+ {
+ m_openList->modify(actualNode);
+ }
+ else
+ {
+ actualNode->flags |= DT_NODE_OPEN;
+ m_openList->push(actualNode);
+ }
+ }
+ }
+ }
+ bestNode->flags |= DT_NODE_CLOSED;
+ }
+
+ // Reverse the path.
+ dtNode* prev = 0;
+ dtNode* node = lastBestNode;
+ do
+ {
+ dtNode* next = m_nodePool->getNodeAtIdx(node->pidx);
+ node->pidx = m_nodePool->getNodeIdx(prev);
+ prev = node;
+ node = next;
+ }
+ while (node);
+
+ // Store path
+ node = prev;
+ int n = 0;
+ do
+ {
+ path[n++] = node->id;
+ node = m_nodePool->getNodeAtIdx(node->pidx);
+ }
+ while (node && n < maxPathSize);
+
+ return n;
+}
+
+int dtTiledNavMesh::findStraightPath(const float* startPos, const float* endPos,
+ const dtTilePolyRef* path, const int pathSize,
+ float* straightPath, const int maxStraightPathSize)
+{
+ if (!maxStraightPathSize)
+ return 0;
+
+ if (!path[0])
+ return 0;
+
+ int straightPathSize = 0;
+
+ float closestStartPos[3];
+ if (!closestPointToPoly(path[0], startPos, closestStartPos))
+ return 0;
+
+ // Add start point.
+ vcopy(&straightPath[straightPathSize*3], closestStartPos);
+ straightPathSize++;
+ if (straightPathSize >= maxStraightPathSize)
+ return straightPathSize;
+
+ float closestEndPos[3];
+ if (!closestPointToPoly(path[pathSize-1], endPos, closestEndPos))
+ return 0;
+
+ float portalApex[3], portalLeft[3], portalRight[3];
+
+ if (pathSize > 1)
+ {
+ vcopy(portalApex, closestStartPos);
+ vcopy(portalLeft, portalApex);
+ vcopy(portalRight, portalApex);
+ int apexIndex = 0;
+ int leftIndex = 0;
+ int rightIndex = 0;
+
+ for (int i = 0; i < pathSize; ++i)
+ {
+ float left[3], right[3];
+ if (i < pathSize-1)
+ {
+ // Next portal.
+ if (!getPortalPoints(path[i], path[i+1], left, right))
+ {
+ if (!closestPointToPoly(path[i], endPos, closestEndPos))
+ return 0;
+ vcopy(&straightPath[straightPathSize*3], closestEndPos);
+ straightPathSize++;
+ return straightPathSize;
+ }
+ }
+ else
+ {
+ // End of the path.
+ vcopy(left, closestEndPos);
+ vcopy(right, closestEndPos);
+ }
+
+ // Right vertex.
+ if (vequal(portalApex, portalRight))
+ {
+ vcopy(portalRight, right);
+ rightIndex = i;
+ }
+ else
+ {
+ if (triArea2D(portalApex, portalRight, right) <= 0.0f)
+ {
+ if (triArea2D(portalApex, portalLeft, right) > 0.0f)
+ {
+ vcopy(portalRight, right);
+ rightIndex = i;
+ }
+ else
+ {
+ vcopy(portalApex, portalLeft);
+ apexIndex = leftIndex;
+
+ if (!vequal(&straightPath[(straightPathSize-1)*3], portalApex))
+ {
+ vcopy(&straightPath[straightPathSize*3], portalApex);
+ straightPathSize++;
+ if (straightPathSize >= maxStraightPathSize)
+ return straightPathSize;
+ }
+
+ vcopy(portalLeft, portalApex);
+ vcopy(portalRight, portalApex);
+ leftIndex = apexIndex;
+ rightIndex = apexIndex;
+
+ // Restart
+ i = apexIndex;
+
+ continue;
+ }
+ }
+ }
+
+ // Left vertex.
+ if (vequal(portalApex, portalLeft))
+ {
+ vcopy(portalLeft, left);
+ leftIndex = i;
+ }
+ else
+ {
+ if (triArea2D(portalApex, portalLeft, left) >= 0.0f)
+ {
+ if (triArea2D(portalApex, portalRight, left) < 0.0f)
+ {
+ vcopy(portalLeft, left);
+ leftIndex = i;
+ }
+ else
+ {
+ vcopy(portalApex, portalRight);
+ apexIndex = rightIndex;
+
+ if (!vequal(&straightPath[(straightPathSize-1)*3], portalApex))
+ {
+ vcopy(&straightPath[straightPathSize*3], portalApex);
+ straightPathSize++;
+ if (straightPathSize >= maxStraightPathSize)
+ return straightPathSize;
+ }
+
+ vcopy(portalLeft, portalApex);
+ vcopy(portalRight, portalApex);
+ leftIndex = apexIndex;
+ rightIndex = apexIndex;
+
+ // Restart
+ i = apexIndex;
+
+ continue;
+ }
+ }
+ }
+ }
+ }
+
+ // Add end point.
+ vcopy(&straightPath[straightPathSize*3], closestEndPos);
+ straightPathSize++;
+
+ return straightPathSize;
+}
+
+// Returns portal points between two polygons.
+bool dtTiledNavMesh::getPortalPoints(dtTilePolyRef from, dtTilePolyRef to, float* left, float* right) const
+{
+ unsigned int salt, it, ip;
+ dtDecodeTileId(from, salt, it, ip);
+ if (it >= DT_MAX_TILES) return false;
+ if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return false;
+ if (ip >= (unsigned int)m_tiles[it].header->npolys) return false;
+ const dtTileHeader* fromHeader = m_tiles[it].header;
+ const dtTilePoly* fromPoly = &fromHeader->polys[ip];
+
+ for (int i = 0; i < fromPoly->nlinks; ++i)
+ {
+ const dtTileLink* link = &fromHeader->links[fromPoly->links+i];
+ if (link->ref == to)
+ {
+ // Find portal vertices.
+ const int v0 = fromPoly->v[link->e];
+ const int v1 = fromPoly->v[(link->e+1) % fromPoly->nv];
+ vcopy(left, &fromHeader->verts[v0*3]);
+ vcopy(right, &fromHeader->verts[v1*3]);
+ // If the link is at tile boundary, clamp the vertices to
+ // the link width.
+ if (link->side == 0 || link->side == 2)
+ {
+ // Unpack portal limits.
+ const float smin = min(left[2],right[2]);
+ const float smax = max(left[2],right[2]);
+ const float s = (smax-smin) / 255.0f;
+ const float lmin = smin + link->bmin*s;
+ const float lmax = smin + link->bmax*s;
+ left[2] = max(left[2],lmin);
+ left[2] = min(left[2],lmax);
+ right[2] = max(right[2],lmin);
+ right[2] = min(right[2],lmax);
+ }
+ else if (link->side == 1 || link->side == 3)
+ {
+ // Unpack portal limits.
+ const float smin = min(left[0],right[0]);
+ const float smax = max(left[0],right[0]);
+ const float s = (smax-smin) / 255.0f;
+ const float lmin = smin + link->bmin*s;
+ const float lmax = smin + link->bmax*s;
+ left[0] = max(left[0],lmin);
+ left[0] = min(left[0],lmax);
+ right[0] = max(right[0],lmin);
+ right[0] = min(right[0],lmax);
+ }
+ return true;
+ }
+ }
+ return false;
+}
+
+// Returns edge mid point between two polygons.
+bool dtTiledNavMesh::getEdgeMidPoint(dtTilePolyRef from, dtTilePolyRef to, float* mid) const
+{
+ float left[3], right[3];
+ if (!getPortalPoints(from, to, left,right)) return false;
+ mid[0] = (left[0]+right[0])*0.5f;
+ mid[1] = (left[1]+right[1])*0.5f;
+ mid[2] = (left[2]+right[2])*0.5f;
+ return true;
+}
+
+int dtTiledNavMesh::raycast(dtTilePolyRef centerRef, const float* startPos, const float* endPos,
+ float& t, dtTilePolyRef* path, const int pathSize)
+{
+ t = 0;
+
+ if (!centerRef || !getPolyByRef(centerRef))
+ return 0;
+
+ dtTilePolyRef curRef = centerRef;
+ float verts[DT_TILE_VERTS_PER_POLYGON*3];
+ int n = 0;
+
+ while (curRef)
+ {
+ // Cast ray against current polygon.
+
+ // The API input has been cheked already, skip checking internal data.
+ unsigned int salt, it, ip;
+ dtDecodeTileId(curRef, salt, it, ip);
+ const dtTileHeader* header = m_tiles[it].header;
+ const dtTilePoly* poly = &header->polys[ip];
+
+ // Collect vertices.
+ int nv = 0;
+ for (int i = 0; i < (int)poly->nv; ++i)
+ {
+ vcopy(&verts[nv*3], &header->verts[poly->v[i]*3]);
+ nv++;
+ }
+ if (nv < 3)
+ {
+ // Hit bad polygon, report hit.
+ return n;
+ }
+
+ float tmin, tmax;
+ int segMin, segMax;
+ if (!intersectSegmentPoly2D(startPos, endPos, verts, nv, tmin, tmax, segMin, segMax))
+ {
+ // Could not hit the polygon, keep the old t and report hit.
+ return n;
+ }
+ // Keep track of furthest t so far.
+ if (tmax > t)
+ t = tmax;
+
+ if (n < pathSize)
+ path[n++] = curRef;
+
+ // Follow neighbours.
+ dtTilePolyRef nextRef = 0;
+ for (int i = 0; i < poly->nlinks; ++i)
+ {
+ const dtTileLink* link = &header->links[poly->links+i];
+ if ((int)link->e == segMax)
+ {
+ // If the link is internal, just return the ref.
+ if (link->side == 0xff)
+ {
+ nextRef = link->ref;
+ break;
+ }
+
+ // If the link is at tile boundary,
+ const int v0 = poly->v[link->e];
+ const int v1 = poly->v[(link->e+1) % poly->nv];
+ const float* left = &header->verts[v0*3];
+ const float* right = &header->verts[v1*3];
+
+ // Check that the intersection lies inside the link portal.
+ if (link->side == 0 || link->side == 2)
+ {
+ // Calculate link size.
+ const float smin = min(left[2],right[2]);
+ const float smax = max(left[2],right[2]);
+ const float s = (smax-smin) / 255.0f;
+ const float lmin = smin + link->bmin*s;
+ const float lmax = smin + link->bmax*s;
+ // Find Z intersection.
+ float z = startPos[2] + (endPos[2]-startPos[2])*tmax;
+ if (z >= lmin && z <= lmax)
+ {
+ nextRef = link->ref;
+ break;
+ }
+ }
+ else if (link->side == 1 || link->side == 3)
+ {
+ // Calculate link size.
+ const float smin = min(left[0],right[0]);
+ const float smax = max(left[0],right[0]);
+ const float s = (smax-smin) / 255.0f;
+ const float lmin = smin + link->bmin*s;
+ const float lmax = smin + link->bmax*s;
+ // Find X intersection.
+ float x = startPos[0] + (endPos[0]-startPos[0])*tmax;
+ if (x >= lmin && x <= lmax)
+ {
+ nextRef = link->ref;
+ break;
+ }
+ }
+ }
+ }
+
+ if (!nextRef)
+ {
+ // No neighbour, we hit a wall.
+ return n;
+ }
+
+ // No hit, advance to neighbour polygon.
+ curRef = nextRef;
+ }
+
+ return n;
+}
+
+int dtTiledNavMesh::findPolysAround(dtTilePolyRef centerRef, const float* centerPos, float radius,
+ dtTilePolyRef* resultRef, dtTilePolyRef* resultParent, float* resultCost,
+ const int maxResult)
+{
+ if (!centerRef) return 0;
+ if (!getPolyByRef(centerRef)) return 0;
+ if (!m_nodePool || !m_openList) return 0;
+
+ m_nodePool->clear();
+ m_openList->clear();
+
+ dtNode* startNode = m_nodePool->getNode(centerRef);
+ startNode->pidx = 0;
+ startNode->cost = 0;
+ startNode->total = 0;
+ startNode->id = centerRef;
+ startNode->flags = DT_NODE_OPEN;
+ m_openList->push(startNode);
+
+ int n = 0;
+ if (n < maxResult)
+ {
+ if (resultRef)
+ resultRef[n] = startNode->id;
+ if (resultParent)
+ resultParent[n] = 0;
+ if (resultCost)
+ resultCost[n] = 0;
+ ++n;
+ }
+
+ const float radiusSqr = sqr(radius);
+
+ while (!m_openList->empty())
+ {
+ dtNode* bestNode = m_openList->pop();
+
+ // Get poly and tile.
+ unsigned int salt, it, ip;
+ dtDecodeTileId(bestNode->id, salt, it, ip);
+ // The API input has been cheked already, skip checking internal data.
+ const dtTileHeader* header = m_tiles[it].header;
+ const dtTilePoly* poly = &header->polys[ip];
+
+ for (int i = 0; i < poly->nlinks; ++i)
+ {
+ const dtTileLink* link = &header->links[poly->links+i];
+ dtTilePolyRef neighbour = link->ref;
+ if (neighbour)
+ {
+ // Skip parent node.
+ if (bestNode->pidx && m_nodePool->getNodeAtIdx(bestNode->pidx)->id == neighbour)
+ continue;
+
+ // Calc distance to the edge.
+ const float* va = &header->verts[poly->v[link->e]*3];
+ const float* vb = &header->verts[poly->v[(link->e+1)%poly->nv]*3];
+ float tseg;
+ float distSqr = distancePtSegSqr2D(centerPos, va, vb, tseg);
+
+ // If the circle is not touching the next polygon, skip it.
+ if (distSqr > radiusSqr)
+ continue;
+
+ dtNode* parent = bestNode;
+ dtNode newNode;
+ newNode.pidx = m_nodePool->getNodeIdx(parent);
+ newNode.id = neighbour;
+
+ // Cost
+ float p0[3], p1[3];
+ if (!parent->pidx)
+ vcopy(p0, centerPos);
+ else
+ getEdgeMidPoint(m_nodePool->getNodeAtIdx(parent->pidx)->id, parent->id, p0);
+ getEdgeMidPoint(parent->id, newNode.id, p1);
+ newNode.total = parent->total + vdist(p0,p1);
+
+ dtNode* actualNode = m_nodePool->getNode(newNode.id);
+ if (!actualNode)
+ continue;
+
+ if (!((actualNode->flags & DT_NODE_OPEN) && newNode.total > actualNode->total) &&
+ !((actualNode->flags & DT_NODE_CLOSED) && newNode.total > actualNode->total))
+ {
+ actualNode->flags &= ~DT_NODE_CLOSED;
+ actualNode->pidx = newNode.pidx;
+ actualNode->total = newNode.total;
+
+ if (actualNode->flags & DT_NODE_OPEN)
+ {
+ m_openList->modify(actualNode);
+ }
+ else
+ {
+ if (n < maxResult)
+ {
+ if (resultRef)
+ resultRef[n] = actualNode->id;
+ if (resultParent)
+ resultParent[n] = m_nodePool->getNodeAtIdx(actualNode->pidx)->id;
+ if (resultCost)
+ resultCost[n] = actualNode->total;
+ ++n;
+ }
+ actualNode->flags = DT_NODE_OPEN;
+ m_openList->push(actualNode);
+ }
+ }
+ }
+ }
+ }
+
+ return n;
+}
+
+float dtTiledNavMesh::findDistanceToWall(dtTilePolyRef centerRef, const float* centerPos, float maxRadius,
+ float* hitPos, float* hitNormal)
+{
+ if (!centerRef) return 0;
+ if (!getPolyByRef(centerRef)) return 0;
+ if (!m_nodePool || !m_openList) return 0;
+
+ m_nodePool->clear();
+ m_openList->clear();
+
+ dtNode* startNode = m_nodePool->getNode(centerRef);
+ startNode->pidx = 0;
+ startNode->cost = 0;
+ startNode->total = 0;
+ startNode->id = centerRef;
+ startNode->flags = DT_NODE_OPEN;
+ m_openList->push(startNode);
+
+ float radiusSqr = sqr(maxRadius);
+
+ while (!m_openList->empty())
+ {
+ dtNode* bestNode = m_openList->pop();
+
+ // Get poly and tile.
+ unsigned int salt, it, ip;
+ dtDecodeTileId(bestNode->id, salt, it, ip);
+ // The API input has been cheked already, skip checking internal data.
+ const dtTileHeader* header = m_tiles[it].header;
+ const dtTilePoly* poly = &header->polys[ip];
+
+ // Hit test walls.
+ for (int i = 0, j = (int)poly->nv-1; i < (int)poly->nv; j = i++)
+ {
+ // Skip non-solid edges.
+ if (poly->n[j] & 0x8000)
+ {
+ // Tile border.
+ bool solid = true;
+ for (int i = 0; i < poly->nlinks; ++i)
+ {
+ const dtTileLink* link = &header->links[poly->links+i];
+ if (link->e == j && link->ref != 0)
+ {
+ solid = false;
+ break;
+ }
+ }
+ if (!solid) continue;
+ }
+ else if (poly->n[j])
+ {
+ // Internal edge
+ continue;
+ }
+
+ // Calc distance to the edge.
+ const float* vj = &header->verts[poly->v[j]*3];
+ const float* vi = &header->verts[poly->v[i]*3];
+ float tseg;
+ float distSqr = distancePtSegSqr2D(centerPos, vj, vi, tseg);
+
+ // Edge is too far, skip.
+ if (distSqr > radiusSqr)
+ continue;
+
+ // Hit wall, update radius.
+ radiusSqr = distSqr;
+ // Calculate hit pos.
+ hitPos[0] = vj[0] + (vi[0] - vj[0])*tseg;
+ hitPos[1] = vj[1] + (vi[1] - vj[1])*tseg;
+ hitPos[2] = vj[2] + (vi[2] - vj[2])*tseg;
+ }
+
+ for (int i = 0; i < poly->nlinks; ++i)
+ {
+ const dtTileLink* link = &header->links[poly->links+i];
+ dtTilePolyRef neighbour = link->ref;
+ if (neighbour)
+ {
+ // Skip parent node.
+ if (bestNode->pidx && m_nodePool->getNodeAtIdx(bestNode->pidx)->id == neighbour)
+ continue;
+
+ // Calc distance to the edge.
+ const float* va = &header->verts[poly->v[link->e]*3];
+ const float* vb = &header->verts[poly->v[(link->e+1)%poly->nv]*3];
+ float tseg;
+ float distSqr = distancePtSegSqr2D(centerPos, va, vb, tseg);
+
+ // If the circle is not touching the next polygon, skip it.
+ if (distSqr > radiusSqr)
+ continue;
+
+ dtNode* parent = bestNode;
+ dtNode newNode;
+ newNode.pidx = m_nodePool->getNodeIdx(parent);
+ newNode.id = neighbour;
+
+ float p0[3], p1[3];
+ if (!parent->pidx)
+ vcopy(p0, centerPos);
+ else
+ getEdgeMidPoint(m_nodePool->getNodeAtIdx(parent->pidx)->id, parent->id, p0);
+ getEdgeMidPoint(parent->id, newNode.id, p1);
+ newNode.total = parent->total + vdist(p0,p1);
+
+ dtNode* actualNode = m_nodePool->getNode(newNode.id);
+ if (!actualNode)
+ continue;
+
+ if (!((actualNode->flags & DT_NODE_OPEN) && newNode.total > actualNode->total) &&
+ !((actualNode->flags & DT_NODE_CLOSED) && newNode.total > actualNode->total))
+ {
+ actualNode->flags &= ~DT_NODE_CLOSED;
+ actualNode->pidx = newNode.pidx;
+ actualNode->total = newNode.total;
+
+ if (actualNode->flags & DT_NODE_OPEN)
+ {
+ m_openList->modify(actualNode);
+ }
+ else
+ {
+ actualNode->flags = DT_NODE_OPEN;
+ m_openList->push(actualNode);
+ }
+ }
+ }
+ }
+ }
+
+ // Calc hit normal.
+ vsub(hitNormal, centerPos, hitPos);
+ vnormalize(hitNormal);
+
+ return sqrtf(radiusSqr);
+}
+
+const dtTilePoly* dtTiledNavMesh::getPolyByRef(dtTilePolyRef ref) const
+{
+ unsigned int salt, it, ip;
+ dtDecodeTileId(ref, salt, it, ip);
+ if (it >= DT_MAX_TILES) return 0;
+ if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return 0;
+ if (ip >= (unsigned int)m_tiles[it].header->npolys) return 0;
+ return &m_tiles[it].header->polys[ip];
+}
+
+const float* dtTiledNavMesh::getPolyVertsByRef(dtTilePolyRef ref) const
+{
+ unsigned int salt, it, ip;
+ dtDecodeTileId(ref, salt, it, ip);
+ if (it >= DT_MAX_TILES) return 0;
+ if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return 0;
+ if (ip >= (unsigned int)m_tiles[it].header->npolys) return 0;
+ return m_tiles[it].header->verts;
+}
+
+const dtTileLink* dtTiledNavMesh::getPolyLinksByRef(dtTilePolyRef ref) const
+{
+ unsigned int salt, it, ip;
+ dtDecodeTileId(ref, salt, it, ip);
+ if (it >= DT_MAX_TILES) return 0;
+ if (m_tiles[it].salt != salt || m_tiles[it].header == 0) return 0;
+ if (ip >= (unsigned int)m_tiles[it].header->npolys) return 0;
+ return m_tiles[it].header->links;
+}
diff --git a/extern/recastnavigation/Detour/Source/DetourTileNavMeshBuilder.cpp b/extern/recastnavigation/Detour/Source/DetourTileNavMeshBuilder.cpp
new file mode 100644
index 00000000000..95dd34b04f6
--- /dev/null
+++ b/extern/recastnavigation/Detour/Source/DetourTileNavMeshBuilder.cpp
@@ -0,0 +1,213 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "DetourTileNavMesh.h"
+#include "DetourCommon.h"
+
+bool dtCreateNavMeshTileData(const unsigned short* verts, const int nverts,
+ const unsigned short* polys, const int npolys, const int nvp,
+ const unsigned short* dmeshes, const float* dverts, const int ndverts,
+ const unsigned char* dtris, const int ndtris,
+ const float* bmin, const float* bmax, float cs, float ch, int tileSize, int walkableClimb,
+ unsigned char** outData, int* outDataSize)
+{
+ if (nvp != DT_TILE_VERTS_PER_POLYGON)
+ return false;
+ if (nverts >= 0xffff)
+ return false;
+
+ if (!nverts)
+ return false;
+ if (!npolys)
+ return false;
+ if (!dmeshes || !dverts || ! dtris)
+ return false;
+
+ // Find portal edges which are at tile borders.
+ int nedges = 0;
+ int nportals = 0;
+ for (int i = 0; i < npolys; ++i)
+ {
+ const unsigned short* p = &polys[i*2*nvp];
+ for (int j = 0; j < nvp; ++j)
+ {
+ if (p[j] == 0xffff) break;
+ int nj = j+1;
+ if (nj >= nvp || p[nj] == 0xffff) nj = 0;
+ const unsigned short* va = &verts[p[j]*3];
+ const unsigned short* vb = &verts[p[nj]*3];
+
+ nedges++;
+
+ if (va[0] == tileSize && vb[0] == tileSize)
+ nportals++; // x+
+ else if (va[2] == tileSize && vb[2] == tileSize)
+ nportals++; // z+
+ else if (va[0] == 0 && vb[0] == 0)
+ nportals++; // x-
+ else if (va[2] == 0 && vb[2] == 0)
+ nportals++; // z-
+ }
+ }
+
+ const int maxLinks = nedges + nportals*2;
+
+
+ // Find unique detail vertices.
+ int uniqueDetailVerts = 0;
+ if (dmeshes)
+ {
+ for (int i = 0; i < npolys; ++i)
+ {
+ const unsigned short* p = &polys[i*nvp*2];
+ int ndv = dmeshes[i*4+1];
+ int nv = 0;
+ for (int j = 0; j < nvp; ++j)
+ {
+ if (p[j] == 0xffff) break;
+ nv++;
+ }
+ ndv -= nv;
+ uniqueDetailVerts += ndv;
+ }
+ }
+
+ // Calculate data size
+ const int headerSize = sizeof(dtTileHeader);
+ const int vertsSize = sizeof(float)*3*nverts;
+ const int polysSize = sizeof(dtTilePoly)*npolys;
+ const int linksSize = sizeof(dtTileLink)*maxLinks;
+ const int detailMeshesSize = sizeof(dtTilePolyDetail)*npolys;
+ const int detailVertsSize = sizeof(float)*3*uniqueDetailVerts;
+ const int detailTrisSize = sizeof(unsigned char)*4*ndtris;
+
+ const int dataSize = headerSize + vertsSize + polysSize + linksSize +
+ detailMeshesSize + detailVertsSize + detailTrisSize;
+ unsigned char* data = new unsigned char[dataSize];
+ if (!data)
+ return false;
+ memset(data, 0, dataSize);
+
+ unsigned char* d = data;
+ dtTileHeader* header = (dtTileHeader*)d; d += headerSize;
+ float* navVerts = (float*)d; d += vertsSize;
+ dtTilePoly* navPolys = (dtTilePoly*)d; d += polysSize;
+ d += linksSize;
+ dtTilePolyDetail* navDMeshes = (dtTilePolyDetail*)d; d += detailMeshesSize;
+ float* navDVerts = (float*)d; d += detailVertsSize;
+ unsigned char* navDTris = (unsigned char*)d; d += detailTrisSize;
+
+
+ // Store header
+ header->magic = DT_TILE_NAVMESH_MAGIC;
+ header->version = DT_TILE_NAVMESH_VERSION;
+ header->npolys = npolys;
+ header->nverts = nverts;
+ header->maxlinks = maxLinks;
+ header->bmin[0] = bmin[0];
+ header->bmin[1] = bmin[1];
+ header->bmin[2] = bmin[2];
+ header->bmax[0] = bmax[0];
+ header->bmax[1] = bmax[1];
+ header->bmax[2] = bmax[2];
+ header->ndmeshes = npolys;
+ header->ndverts = uniqueDetailVerts;
+ header->ndtris = ndtris;
+
+ // Store vertices
+ for (int i = 0; i < nverts; ++i)
+ {
+ const unsigned short* iv = &verts[i*3];
+ float* v = &navVerts[i*3];
+ v[0] = bmin[0] + iv[0] * cs;
+ v[1] = bmin[1] + iv[1] * ch;
+ v[2] = bmin[2] + iv[2] * cs;
+ }
+
+ // Store polygons
+ const unsigned short* src = polys;
+ for (int i = 0; i < npolys; ++i)
+ {
+ dtTilePoly* p = &navPolys[i];
+ p->nv = 0;
+ for (int j = 0; j < nvp; ++j)
+ {
+ if (src[j] == 0xffff) break;
+ p->v[j] = src[j];
+ p->n[j] = (src[nvp+j]+1) & 0xffff;
+ p->nv++;
+ }
+ src += nvp*2;
+ }
+
+ // Store portal edges.
+ for (int i = 0; i < npolys; ++i)
+ {
+ dtTilePoly* poly = &navPolys[i];
+ for (int j = 0; j < poly->nv; ++j)
+ {
+ int nj = j+1;
+ if (nj >= poly->nv) nj = 0;
+
+ const unsigned short* va = &verts[poly->v[j]*3];
+ const unsigned short* vb = &verts[poly->v[nj]*3];
+
+ if (va[0] == tileSize && vb[0] == tileSize) // x+
+ poly->n[j] = 0x8000 | 0;
+ else if (va[2] == tileSize && vb[2] == tileSize) // z+
+ poly->n[j] = 0x8000 | 1;
+ else if (va[0] == 0 && vb[0] == 0) // x-
+ poly->n[j] = 0x8000 | 2;
+ else if (va[2] == 0 && vb[2] == 0) // z-
+ poly->n[j] = 0x8000 | 3;
+ }
+ }
+
+ // Store detail meshes and vertices.
+ // The nav polygon vertices are stored as the first vertices on each mesh.
+ // We compress the mesh data by skipping them and using the navmesh coordinates.
+ unsigned short vbase = 0;
+ for (int i = 0; i < npolys; ++i)
+ {
+ dtTilePolyDetail& dtl = navDMeshes[i];
+ const int vb = dmeshes[i*4+0];
+ const int ndv = dmeshes[i*4+1];
+ const int nv = navPolys[i].nv;
+ dtl.vbase = vbase;
+ dtl.nverts = ndv-nv;
+ dtl.tbase = dmeshes[i*4+2];
+ dtl.ntris = dmeshes[i*4+3];
+ // Copy vertices except the first 'nv' verts which are equal to nav poly verts.
+ if (ndv-nv)
+ {
+ memcpy(&navDVerts[vbase*3], &dverts[(vb+nv)*3], sizeof(float)*3*(ndv-nv));
+ vbase += ndv-nv;
+ }
+ }
+ // Store triangles.
+ memcpy(navDTris, dtris, sizeof(unsigned char)*4*ndtris);
+
+ *outData = data;
+ *outDataSize = dataSize;
+
+ return true;
+}
diff --git a/extern/recastnavigation/License.txt b/extern/recastnavigation/License.txt
new file mode 100644
index 00000000000..95f4bfc9654
--- /dev/null
+++ b/extern/recastnavigation/License.txt
@@ -0,0 +1,18 @@
+Copyright (c) 2009 Mikko Mononen memon@inside.org
+
+This software is provided 'as-is', without any express or implied
+warranty. In no event will the authors be held liable for any damages
+arising from the use of this software.
+
+Permission is granted to anyone to use this software for any purpose,
+including commercial applications, and to alter it and redistribute it
+freely, subject to the following restrictions:
+
+1. The origin of this software must not be misrepresented; you must not
+claim that you wrote the original software. If you use this software
+in a product, an acknowledgment in the product documentation would be
+appreciated but is not required.
+2. Altered source versions must be plainly marked as such, and must not be
+misrepresented as being the original software.
+3. This notice may not be removed or altered from any source distribution.
+
diff --git a/extern/recastnavigation/Readme.txt b/extern/recastnavigation/Readme.txt
new file mode 100644
index 00000000000..0c2f7b1675f
--- /dev/null
+++ b/extern/recastnavigation/Readme.txt
@@ -0,0 +1,120 @@
+
+Recast & Detour Version 1.4
+
+
+Recast
+
+Recast is state of the art navigation mesh construction toolset for games.
+
+ * It is automatic, which means that you can throw any level geometry
+ at it and you will get robust mesh out
+ * It is fast which means swift turnaround times for level designers
+ * It is open source so it comes with full source and you can
+ customize it to your hearts content.
+
+The Recast process starts with constructing a voxel mold from a level geometry
+and then casting a navigation mesh over it. The process consists of three steps,
+building the voxel mold, partitioning the mold into simple regions, peeling off
+the regions as simple polygons.
+
+ 1. The voxel mold is build from the input triangle mesh by rasterizing
+ the triangles into a multi-layer heightfield. Some simple filters are
+ then applied to the mold to prune out locations where the character
+ would not be able to move.
+ 2. The walkable areas described by the mold are divided into simple
+ overlayed 2D regions. The resulting regions have only one non-overlapping
+ contour, which simplifies the final step of the process tremendously.
+ 3. The navigation polygons are peeled off from the regions by first tracing
+ the boundaries and then simplifying them. The resulting polygons are
+ finally converted to convex polygons which makes them perfect for
+ pathfinding and spatial reasoning about the level.
+
+The toolset code is located in the Recast folder and demo application using the Recast
+toolset is located in the RecastDemo folder.
+
+The project files with this distribution can be compiled with Microsoft Visual C++ 2008
+(you can download it for free) and XCode 3.1.
+
+
+Detour
+
+Recast is accompanied with Detour, path-finding and spatial reasoning toolkit. You can use any navigation mesh with Detour, but of course the data generated with Recast fits perfectly.
+
+Detour offers simple static navigation mesh which is suitable for many simple cases, as well as tiled navigation mesh which allows you to plug in and out pieces of the mesh. The tiled mesh allows to create systems where you stream new navigation data in and out as the player progresses the level, or you may regenerate tiles as the world changes.
+
+
+Latest code available at http://code.google.com/p/recastnavigation/
+
+
+--
+
+Release Notes
+
+----------------
+* Recast 1.4
+ Released August 24th, 2009
+
+- Added detail height mesh generation (RecastDetailMesh.cpp) for single,
+ tiled statmeshes as well as tilemesh.
+- Added feature to contour tracing which detects extra vertices along
+ tile edges which should be removed later.
+- Changed the tiled stat mesh preprocess, so that it first generated
+ polymeshes per tile and finally combines them.
+- Fixed bug in the GUI code where invisible buttons could be pressed.
+
+----------------
+* Recast 1.31
+ Released July 24th, 2009
+
+- Better cost and heuristic functions.
+- Fixed tile navmesh raycast on tile borders.
+
+----------------
+* Recast 1.3
+ Released July 14th, 2009
+
+- Added dtTileNavMesh which allows to dynamically add and remove navmesh pieces at runtime.
+- Renamed stat navmesh types to dtStat* (i.e. dtPoly is now dtStatPoly).
+- Moved common code used by tile and stat navmesh to DetourNode.h/cpp and DetourCommon.h/cpp.
+- Refactores the demo code.
+
+----------------
+* Recast 1.2
+ Released June 17th, 2009
+
+- Added tiled mesh generation. The tiled generation allows to generate navigation for
+ much larger worlds, it removes some of the artifacts that comes from distance fields
+ in open areas, and allows later streaming and dynamic runtime generation
+- Improved and added some debug draw modes
+- API change: The helper function rcBuildNavMesh does not exists anymore,
+ had to change few internal things to cope with the tiled processing,
+ similar API functionality will be added later once the tiled process matures
+- The demo is getting way too complicated, need to split demos
+- Fixed several filtering functions so that the mesh is tighter to the geometry,
+ sometimes there could be up error up to tow voxel units close to walls,
+ now it should be just one.
+
+----------------
+* Recast 1.1
+ Released April 11th, 2009
+
+This is the first release of Detour.
+
+----------------
+* Recast 1.0
+ Released March 29th, 2009
+
+This is the first release of Recast.
+
+The process is not always as robust as I would wish. The watershed phase sometimes swallows tiny islands
+which are close to edges. These droppings are handled in rcBuildContours, but the code is not
+particularly robust either.
+
+Another non-robust case is when portal contours (contours shared between two regions) are always
+assumed to be straight. That can lead to overlapping contours specially when the level has
+large open areas.
+
+
+
+Mikko Mononen
+memon@inside.org
diff --git a/extern/recastnavigation/Recast/Include/Recast.h b/extern/recastnavigation/Recast/Include/Recast.h
new file mode 100644
index 00000000000..f25ab47f8fa
--- /dev/null
+++ b/extern/recastnavigation/Recast/Include/Recast.h
@@ -0,0 +1,501 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#ifndef RECAST_H
+#define RECAST_H
+
+// The units of the parameters are specified in parenthesis as follows:
+// (vx) voxels, (wu) world units
+struct rcConfig
+{
+ int width, height; // Dimensions of the rasterized heighfield (vx)
+ int tileSize; // Width and Height of a tile (vx)
+ int borderSize; // Non-navigable Border around the heightfield (vx)
+ float cs, ch; // Grid cell size and height (wu)
+ float bmin[3], bmax[3]; // Grid bounds (wu)
+ float walkableSlopeAngle; // Maximum walkble slope angle in degrees.
+ int walkableHeight; // Minimum height where the agent can still walk (vx)
+ int walkableClimb; // Maximum height between grid cells the agent can climb (vx)
+ int walkableRadius; // Radius of the agent in cells (vx)
+ int maxEdgeLen; // Maximum contour edge length (vx)
+ float maxSimplificationError; // Maximum distance error from contour to cells (vx)
+ int minRegionSize; // Minimum regions size. Smaller regions will be deleted (vx)
+ int mergeRegionSize; // Minimum regions size. Smaller regions will be merged (vx)
+ int maxVertsPerPoly; // Max number of vertices per polygon
+ float detailSampleDist; // Detail mesh sample spacing.
+ float detailSampleMaxError; // Detail mesh simplification max sample error.
+};
+
+// Heightfield span.
+struct rcSpan
+{
+ unsigned int smin : 15; // Span min height.
+ unsigned int smax : 15; // Span max height.
+ unsigned int flags : 2; // Span flags.
+ rcSpan* next; // Next span in column.
+};
+
+static const int RC_SPANS_PER_POOL = 2048;
+
+// Memory pool used for quick span allocation.
+struct rcSpanPool
+{
+ rcSpanPool* next; // Pointer to next pool.
+ rcSpan items[1]; // Array of spans (size RC_SPANS_PER_POOL).
+};
+
+// Dynamic span-heightfield.
+struct rcHeightfield
+{
+ inline rcHeightfield() : width(0), height(0), spans(0), pools(0), freelist(0) {}
+ inline ~rcHeightfield()
+ {
+ // Delete span array.
+ delete [] spans;
+ // Delete span pools.
+ while (pools)
+ {
+ rcSpanPool* next = pools->next;
+ delete [] reinterpret_cast<unsigned char*>(pools);
+ pools = next;
+ }
+ }
+ int width, height; // Dimension of the heightfield.
+ float bmin[3], bmax[3]; // Bounding box of the heightfield
+ float cs, ch; // Cell size and height.
+ rcSpan** spans; // Heightfield of spans (width*height).
+ rcSpanPool* pools; // Linked list of span pools.
+ rcSpan* freelist; // Pointer to next free span.
+};
+
+struct rcCompactCell
+{
+ unsigned int index : 24; // Index to first span in column.
+ unsigned int count : 8; // Number of spans in this column.
+};
+
+struct rcCompactSpan
+{
+ unsigned short y; // Bottom coordinate of the span.
+ unsigned short reg; // Region ID
+ unsigned short dist; // Distance to border
+ unsigned short con; // Connections to neighbour cells.
+ unsigned char h; // Height of the span.
+ unsigned char flags; // Flags.
+};
+
+// Compact static heightfield.
+struct rcCompactHeightfield
+{
+ inline rcCompactHeightfield() : maxDistance(0), maxRegions(0), cells(0), spans(0) {}
+ inline ~rcCompactHeightfield() { delete [] cells; delete [] spans; }
+ int width, height; // Width and height of the heighfield.
+ int spanCount; // Number of spans in the heightfield.
+ int walkableHeight, walkableClimb; // Agent properties.
+ unsigned short maxDistance; // Maximum distance value stored in heightfield.
+ unsigned short maxRegions; // Maximum Region Id stored in heightfield.
+ float bmin[3], bmax[3]; // Bounding box of the heightfield.
+ float cs, ch; // Cell size and height.
+ rcCompactCell* cells; // Pointer to width*height cells.
+ rcCompactSpan* spans; // Pointer to spans.
+};
+
+struct rcContour
+{
+ inline rcContour() : verts(0), nverts(0), rverts(0), nrverts(0) { }
+ inline ~rcContour() { delete [] verts; delete [] rverts; }
+ int* verts; // Vertex coordinates, each vertex contains 4 components.
+ int nverts; // Number of vertices.
+ int* rverts; // Raw vertex coordinates, each vertex contains 4 components.
+ int nrverts; // Number of raw vertices.
+ unsigned short reg; // Region ID of the contour.
+};
+
+struct rcContourSet
+{
+ inline rcContourSet() : conts(0), nconts(0) {}
+ inline ~rcContourSet() { delete [] conts; }
+ rcContour* conts; // Pointer to all contours.
+ int nconts; // Number of contours.
+ float bmin[3], bmax[3]; // Bounding box of the heightfield.
+ float cs, ch; // Cell size and height.
+};
+
+// Polymesh store a connected mesh of polygons.
+// The polygons are store in an array where each polygons takes
+// 'nvp*2' elements. The first 'nvp' elements are indices to vertices
+// and the second 'nvp' elements are indices to neighbour polygons.
+// If a polygona has less than 'bvp' vertices, the remaining indices
+// are set os 0xffff. If an polygon edge does not have a neighbour
+// the neighbour index is set to 0xffff.
+// Vertices can be transformed into world space as follows:
+// x = bmin[0] + verts[i*3+0]*cs;
+// y = bmin[1] + verts[i*3+1]*ch;
+// z = bmin[2] + verts[i*3+2]*cs;
+struct rcPolyMesh
+{
+ inline rcPolyMesh() : verts(0), polys(0), regs(0), nverts(0), npolys(0), nvp(3) {}
+ inline ~rcPolyMesh() { delete [] verts; delete [] polys; delete [] regs; }
+ unsigned short* verts; // Vertices of the mesh, 3 elements per vertex.
+ unsigned short* polys; // Polygons of the mesh, nvp*2 elements per polygon.
+ unsigned short* regs; // Regions of the polygons.
+ int nverts; // Number of vertices.
+ int npolys; // Number of polygons.
+ int nvp; // Max number of vertices per polygon.
+ float bmin[3], bmax[3]; // Bounding box of the mesh.
+ float cs, ch; // Cell size and height.
+};
+
+// Detail mesh generated from a rcPolyMesh.
+// Each submesh represents a polygon in the polymesh and they are stored in
+// excatly same order. Each submesh is described as 4 values:
+// base vertex, vertex count, base triangle, triangle count. That is,
+// const unsigned char* t = &dtl.tris[(tbase+i)*3]; and
+// const float* v = &dtl.verts[(vbase+t[j])*3];
+// If the input polygon has 'n' vertices, those vertices are first in the
+// submesh vertex list. This allows to compres the mesh by not storing the
+// first vertices and using the polymesh vertices instead.
+
+struct rcPolyMeshDetail
+{
+ inline rcPolyMeshDetail() :
+ meshes(0), verts(0), tris(0),
+ nmeshes(0), nverts(0), ntris(0) {}
+ inline ~rcPolyMeshDetail()
+ {
+ delete [] meshes; delete [] verts; delete [] tris;
+ }
+
+ unsigned short* meshes; // Pointer to all mesh data.
+ float* verts; // Pointer to all vertex data.
+ unsigned char* tris; // Pointer to all triangle data.
+ int nmeshes; // Number of meshes.
+ int nverts; // Number of total vertices.
+ int ntris; // Number of triangles.
+};
+
+
+// Simple dynamic array ints.
+class rcIntArray
+{
+ int* m_data;
+ int m_size, m_cap;
+public:
+ inline rcIntArray() : m_data(0), m_size(0), m_cap(0) {}
+ inline rcIntArray(int n) : m_data(0), m_size(0), m_cap(n) { m_data = new int[n]; }
+ inline ~rcIntArray() { delete [] m_data; }
+ void resize(int n);
+ inline void push(int item) { resize(m_size+1); m_data[m_size-1] = item; }
+ inline int pop() { if (m_size > 0) m_size--; return m_data[m_size]; }
+ inline const int& operator[](int i) const { return m_data[i]; }
+ inline int& operator[](int i) { return m_data[i]; }
+ inline int size() const { return m_size; }
+};
+
+enum rcSpanFlags
+{
+ RC_WALKABLE = 0x01,
+ RC_REACHABLE = 0x02,
+};
+
+// If heightfield region ID has the following bit set, the region is on border area
+// and excluded from many calculations.
+static const unsigned short RC_BORDER_REG = 0x8000;
+
+// If contour region ID has the following bit set, the vertex will be later
+// removed in order to match the segments and vertices at tile boundaries.
+static const int RC_BORDER_VERTEX = 0x10000;
+
+// Compact span neighbour helpers.
+inline int rcGetCon(const rcCompactSpan& s, int dir)
+{
+ return (s.con >> (dir*4)) & 0xf;
+}
+
+inline int rcGetDirOffsetX(int dir)
+{
+ const int offset[4] = { -1, 0, 1, 0, };
+ return offset[dir&0x03];
+}
+
+inline int rcGetDirOffsetY(int dir)
+{
+ const int offset[4] = { 0, 1, 0, -1 };
+ return offset[dir&0x03];
+}
+
+// Common helper functions
+template<class T> inline void rcSwap(T& a, T& b) { T t = a; a = b; b = t; }
+template<class T> inline T rcMin(T a, T b) { return a < b ? a : b; }
+template<class T> inline T rcMax(T a, T b) { return a > b ? a : b; }
+template<class T> inline T rcAbs(T a) { return a < 0 ? -a : a; }
+template<class T> inline T rcSqr(T a) { return a*a; }
+template<class T> inline T rcClamp(T v, T mn, T mx) { return v < mn ? mn : (v > mx ? mx : v); }
+
+// Common vector helper functions.
+inline void vcross(float* dest, const float* v1, const float* v2)
+{
+ dest[0] = v1[1]*v2[2] - v1[2]*v2[1];
+ dest[1] = v1[2]*v2[0] - v1[0]*v2[2];
+ dest[2] = v1[0]*v2[1] - v1[1]*v2[0];
+}
+
+inline float vdot(const float* v1, const float* v2)
+{
+ return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
+}
+
+inline void vmad(float* dest, const float* v1, const float* v2, const float s)
+{
+ dest[0] = v1[0]+v2[0]*s;
+ dest[1] = v1[1]+v2[1]*s;
+ dest[2] = v1[2]+v2[2]*s;
+}
+
+inline void vadd(float* dest, const float* v1, const float* v2)
+{
+ dest[0] = v1[0]+v2[0];
+ dest[1] = v1[1]+v2[1];
+ dest[2] = v1[2]+v2[2];
+}
+
+inline void vsub(float* dest, const float* v1, const float* v2)
+{
+ dest[0] = v1[0]-v2[0];
+ dest[1] = v1[1]-v2[1];
+ dest[2] = v1[2]-v2[2];
+}
+
+inline void vmin(float* mn, const float* v)
+{
+ mn[0] = rcMin(mn[0], v[0]);
+ mn[1] = rcMin(mn[1], v[1]);
+ mn[2] = rcMin(mn[2], v[2]);
+}
+
+inline void vmax(float* mx, const float* v)
+{
+ mx[0] = rcMax(mx[0], v[0]);
+ mx[1] = rcMax(mx[1], v[1]);
+ mx[2] = rcMax(mx[2], v[2]);
+}
+
+inline void vcopy(float* dest, const float* v)
+{
+ dest[0] = v[0];
+ dest[1] = v[1];
+ dest[2] = v[2];
+}
+
+inline float vdist(const float* v1, const float* v2)
+{
+ float dx = v2[0] - v1[0];
+ float dy = v2[1] - v1[1];
+ float dz = v2[2] - v1[2];
+ return sqrtf(dx*dx + dy*dy + dz*dz);
+}
+
+inline float vdistSqr(const float* v1, const float* v2)
+{
+ float dx = v2[0] - v1[0];
+ float dy = v2[1] - v1[1];
+ float dz = v2[2] - v1[2];
+ return dx*dx + dy*dy + dz*dz;
+}
+
+inline void vnormalize(float* v)
+{
+ float d = 1.0f / sqrtf(rcSqr(v[0]) + rcSqr(v[1]) + rcSqr(v[2]));
+ v[0] *= d;
+ v[1] *= d;
+ v[2] *= d;
+}
+
+inline bool vequal(const float* p0, const float* p1)
+{
+ static const float thr = rcSqr(1.0f/16384.0f);
+ const float d = vdistSqr(p0, p1);
+ return d < thr;
+}
+
+
+// Calculated bounding box of array of vertices.
+// Params:
+// verts - (in) array of vertices
+// nv - (in) vertex count
+// bmin, bmax - (out) bounding box
+void rcCalcBounds(const float* verts, int nv, float* bmin, float* bmax);
+
+// Calculates grid size based on bounding box and grid cell size.
+// Params:
+// bmin, bmax - (in) bounding box
+// cs - (in) grid cell size
+// w - (out) grid width
+// h - (out) grid height
+void rcCalcGridSize(const float* bmin, const float* bmax, float cs, int* w, int* h);
+
+// Creates and initializes new heightfield.
+// Params:
+// hf - (in/out) heightfield to initialize.
+// width - (in) width of the heightfield.
+// height - (in) height of the heightfield.
+// bmin, bmax - (in) bounding box of the heightfield
+// cs - (in) grid cell size
+// ch - (in) grid cell height
+bool rcCreateHeightfield(rcHeightfield& hf, int width, int height,
+ const float* bmin, const float* bmax,
+ float cs, float ch);
+
+// Sets the WALKABLE flag for every triangle whose slope is below
+// the maximun walkable slope angle.
+// Params:
+// walkableSlopeAngle - (in) maximun slope angle in degrees.
+// verts - (in) array of vertices
+// nv - (in) vertex count
+// tris - (in) array of triangle vertex indices
+// nt - (in) triangle count
+// flags - (out) array of triangle flags
+void rcMarkWalkableTriangles(const float walkableSlopeAngle,
+ const float* verts, int nv,
+ const int* tris, int nt,
+ unsigned char* flags);
+
+// Rasterizes a triangle into heightfield spans.
+// Params:
+// v0,v1,v2 - (in) the vertices of the triangle.
+// flags - (in) triangle flags (uses WALKABLE)
+// solid - (in) heighfield where the triangle is rasterized
+void rcRasterizeTriangle(const float* v0, const float* v1, const float* v2,
+ unsigned char flags, rcHeightfield& solid);
+
+// Rasterizes the triangles into heightfield spans.
+// Params:
+// verts - (in) array of vertices
+// nv - (in) vertex count
+// tris - (in) array of triangle vertex indices
+// norms - (in) array of triangle normals
+// flags - (in) array of triangle flags (uses WALKABLE)
+// nt - (in) triangle count
+// solid - (in) heighfield where the triangles are rasterized
+void rcRasterizeTriangles(const float* verts, int nv,
+ const int* tris, const unsigned char* flags, int nt,
+ rcHeightfield& solid);
+
+// Removes WALKABLE flag from all spans that are at ledges. This filtering
+// removes possible overestimation of the conservative voxelization so that
+// the resulting mesh will not have regions hanging in air over ledges.
+// Params:
+// walkableHeight - (in) minimum height where the agent can still walk
+// walkableClimb - (in) maximum height between grid cells the agent can climb
+// solid - (in/out) heightfield describing the solid space
+void rcFilterLedgeSpans(const int walkableHeight,
+ const int walkableClimb,
+ rcHeightfield& solid);
+
+// Removes WALKABLE flag from all spans which have smaller than
+// 'walkableHeight' clearane above them.
+// Params:
+// walkableHeight - (in) minimum height where the agent can still walk
+// solid - (in/out) heightfield describing the solid space
+void rcFilterWalkableLowHeightSpans(int walkableHeight,
+ rcHeightfield& solid);
+
+// Marks spans which are reachable from any of the topmost spans.
+// Params:
+// walkableHeight - (in) minimum height where the agent can still walk
+// walkableClimb - (in) maximum height between grid cells the agent can climb
+// solid - (in/out) heightfield describing the solid space
+// Returns false if operation ran out of memory.
+bool rcMarkReachableSpans(const int walkableHeight,
+ const int walkableClimb,
+ rcHeightfield& solid);
+
+// Builds compact representation of the heightfield.
+// Params:
+// walkableHeight - (in) minimum height where the agent can still walk
+// walkableClimb - (in) maximum height between grid cells the agent can climb
+// hf - (in) heightfield to be compacted
+// chf - (out) compact heightfield representing the open space.
+// Returns false if operation ran out of memory.
+bool rcBuildCompactHeightfield(const int walkableHeight, const int walkableClimb,
+ unsigned char flags,
+ rcHeightfield& hf,
+ rcCompactHeightfield& chf);
+
+// Builds distance field and stores it into the combat heightfield.
+// Params:
+// chf - (in/out) compact heightfield representing the open space.
+// Returns false if operation ran out of memory.
+bool rcBuildDistanceField(rcCompactHeightfield& chf);
+
+// Divides the walkable heighfied into simple regions.
+// Each region has only one contour and no overlaps.
+// The regions are stored in the compact heightfield 'reg' field.
+// The regions will be shrinked by the radius of the agent.
+// The process sometimes creates small regions. The parameter
+// 'minRegionSize' specifies the smallest allowed regions size.
+// If the area of a regions is smaller than allowed, the regions is
+// removed or merged to neighbour region.
+// Params:
+// chf - (in/out) compact heightfield representing the open space.
+// walkableRadius - (in) the radius of the agent.
+// minRegionSize - (in) the smallest allowed regions size.
+// maxMergeRegionSize - (in) the largest allowed regions size which can be merged.
+// Returns false if operation ran out of memory.
+bool rcBuildRegions(rcCompactHeightfield& chf,
+ int walkableRadius, int borderSize,
+ int minRegionSize, int mergeRegionSize);
+
+// Builds simplified contours from the regions outlines.
+// Params:
+// chf - (in) compact heightfield which has regions set.
+// maxError - (in) maximum allowed distance between simplified countour and cells.
+// maxEdgeLen - (in) maximum allowed contour edge length in cells.
+// cset - (out) Resulting contour set.
+// Returns false if operation ran out of memory.
+bool rcBuildContours(rcCompactHeightfield& chf,
+ const float maxError, const int maxEdgeLen,
+ rcContourSet& cset);
+
+// Builds connected convex polygon mesh from contour polygons.
+// Params:
+// cset - (in) contour set.
+// nvp - (in) maximum number of vertices per polygon.
+// mesh - (out) poly mesh.
+// Returns false if operation ran out of memory.
+bool rcBuildPolyMesh(rcContourSet& cset, int nvp, rcPolyMesh& mesh);
+
+bool rcMergePolyMeshes(rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh);
+
+// Builds detail triangle mesh for each polygon in the poly mesh.
+// Params:
+// mesh - (in) poly mesh to detail.
+// chf - (in) compacy height field, used to query height for new vertices.
+// sampleDist - (in) spacing between height samples used to generate more detail into mesh.
+// sampleMaxError - (in) maximum allowed distance between simplified detail mesh and height sample.
+// pmdtl - (out) detail mesh.
+// Returns false if operation ran out of memory.
+bool rcBuildPolyMeshDetail(const rcPolyMesh& mesh, const rcCompactHeightfield& chf,
+ const float sampleDist, const float sampleMaxError,
+ rcPolyMeshDetail& dmesh);
+
+bool rcMergePolyMeshDetails(rcPolyMeshDetail** meshes, const int nmeshes, rcPolyMeshDetail& mesh);
+
+bool buildMeshAdjacency(unsigned short* polys, const int npolys, const int nverts, const int vertsPerPoly);
+
+#endif // RECAST_H
diff --git a/extern/recastnavigation/Recast/Include/RecastLog.h b/extern/recastnavigation/Recast/Include/RecastLog.h
new file mode 100644
index 00000000000..026ef73a3aa
--- /dev/null
+++ b/extern/recastnavigation/Recast/Include/RecastLog.h
@@ -0,0 +1,80 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#ifndef RECAST_LOG_H
+#define RECAST_LOG_H
+
+enum rcLogCategory
+{
+ RC_LOG_PROGRESS = 1,
+ RC_LOG_WARNING,
+ RC_LOG_ERROR,
+};
+
+class rcLog
+{
+public:
+ rcLog();
+ ~rcLog();
+
+ void log(rcLogCategory category, const char* format, ...);
+ inline void clear() { m_messageCount = 0; m_textPoolSize = 0; }
+ inline int getMessageCount() const { return m_messageCount; }
+ inline char getMessageType(int i) const { return *m_messages[i]; }
+ inline const char* getMessageText(int i) const { return m_messages[i]+1; }
+
+private:
+ static const int MAX_MESSAGES = 1000;
+ const char* m_messages[MAX_MESSAGES];
+ int m_messageCount;
+ static const int TEXT_POOL_SIZE = 8000;
+ char m_textPool[TEXT_POOL_SIZE];
+ int m_textPoolSize;
+};
+
+struct rcBuildTimes
+{
+ int rasterizeTriangles;
+ int buildCompact;
+ int buildContours;
+ int buildContoursTrace;
+ int buildContoursSimplify;
+ int filterBorder;
+ int filterWalkable;
+ int filterMarkReachable;
+ int buildPolymesh;
+ int buildDistanceField;
+ int buildDistanceFieldDist;
+ int buildDistanceFieldBlur;
+ int buildRegions;
+ int buildRegionsReg;
+ int buildRegionsExp;
+ int buildRegionsFlood;
+ int buildRegionsFilter;
+ int buildDetailMesh;
+ int mergePolyMesh;
+ int mergePolyMeshDetail;
+};
+
+void rcSetLog(rcLog* log);
+rcLog* rcGetLog();
+
+void rcSetBuildTimes(rcBuildTimes* btimes);
+rcBuildTimes* rcGetBuildTimes();
+
+#endif // RECAST_LOG_H
diff --git a/extern/recastnavigation/Recast/Include/RecastTimer.h b/extern/recastnavigation/Recast/Include/RecastTimer.h
new file mode 100644
index 00000000000..d4f21e58776
--- /dev/null
+++ b/extern/recastnavigation/Recast/Include/RecastTimer.h
@@ -0,0 +1,31 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+#ifndef RECAST_TIMER_H
+#define RECAST_TIMER_H
+
+#ifdef __GNUC__
+#include <stdint.h>
+typedef int64_t rcTimeVal;
+#else
+typedef __int64 rcTimeVal;
+#endif
+
+rcTimeVal rcGetPerformanceTimer();
+int rcGetDeltaTimeUsec(rcTimeVal start, rcTimeVal end);
+
+#endif // RECAST_TIMER_H
diff --git a/extern/recastnavigation/Recast/Source/Recast.cpp b/extern/recastnavigation/Recast/Source/Recast.cpp
new file mode 100644
index 00000000000..4bd8b7a12a9
--- /dev/null
+++ b/extern/recastnavigation/Recast/Source/Recast.cpp
@@ -0,0 +1,272 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#include <float.h>
+#define _USE_MATH_DEFINES
+#include <math.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include "Recast.h"
+#include "RecastLog.h"
+#include "RecastTimer.h"
+
+
+void rcIntArray::resize(int n)
+{
+ if (n > m_cap)
+ {
+ if (!m_cap) m_cap = 8;
+ while (m_cap < n) m_cap *= 2;
+ int* newData = new int[m_cap];
+ if (m_size && newData) memcpy(newData, m_data, m_size*sizeof(int));
+ delete [] m_data;
+ m_data = newData;
+ }
+ m_size = n;
+}
+
+void rcCalcBounds(const float* verts, int nv, float* bmin, float* bmax)
+{
+ // Calculate bounding box.
+ vcopy(bmin, verts);
+ vcopy(bmax, verts);
+ for (int i = 1; i < nv; ++i)
+ {
+ const float* v = &verts[i*3];
+ vmin(bmin, v);
+ vmax(bmax, v);
+ }
+}
+
+void rcCalcGridSize(const float* bmin, const float* bmax, float cs, int* w, int* h)
+{
+ *w = (int)((bmax[0] - bmin[0])/cs+0.5f);
+ *h = (int)((bmax[2] - bmin[2])/cs+0.5f);
+}
+
+bool rcCreateHeightfield(rcHeightfield& hf, int width, int height,
+ const float* bmin, const float* bmax,
+ float cs, float ch)
+{
+ hf.width = width;
+ hf.height = height;
+ hf.spans = new rcSpan*[hf.width*hf.height];
+ vcopy(hf.bmin, bmin);
+ vcopy(hf.bmax, bmax);
+ hf.cs = cs;
+ hf.ch = ch;
+ if (!hf.spans)
+ return false;
+ memset(hf.spans, 0, sizeof(rcSpan*)*hf.width*hf.height);
+ return true;
+}
+
+static void calcTriNormal(const float* v0, const float* v1, const float* v2, float* norm)
+{
+ float e0[3], e1[3];
+ vsub(e0, v1, v0);
+ vsub(e1, v2, v0);
+ vcross(norm, e0, e1);
+ vnormalize(norm);
+}
+
+void rcMarkWalkableTriangles(const float walkableSlopeAngle,
+ const float* verts, int nv,
+ const int* tris, int nt,
+ unsigned char* flags)
+{
+ const float walkableThr = cosf(walkableSlopeAngle/180.0f*(float)M_PI);
+
+ float norm[3];
+
+ for (int i = 0; i < nt; ++i)
+ {
+ const int* tri = &tris[i*3];
+ calcTriNormal(&verts[tri[0]*3], &verts[tri[1]*3], &verts[tri[2]*3], norm);
+ // Check if the face is walkable.
+ if (norm[1] > walkableThr)
+ flags[i] |= RC_WALKABLE;
+ }
+}
+
+static int getSpanCount(unsigned char flags, rcHeightfield& hf)
+{
+ const int w = hf.width;
+ const int h = hf.height;
+ int spanCount = 0;
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ for (rcSpan* s = hf.spans[x + y*w]; s; s = s->next)
+ {
+ if (s->flags == flags)
+ spanCount++;
+ }
+ }
+ }
+ return spanCount;
+}
+
+inline void setCon(rcCompactSpan& s, int dir, int i)
+{
+ s.con &= ~(0xf << (dir*4));
+ s.con |= (i&0xf) << (dir*4);
+}
+
+bool rcBuildCompactHeightfield(const int walkableHeight, const int walkableClimb,
+ unsigned char flags, rcHeightfield& hf,
+ rcCompactHeightfield& chf)
+{
+ rcTimeVal startTime = rcGetPerformanceTimer();
+
+ const int w = hf.width;
+ const int h = hf.height;
+ const int spanCount = getSpanCount(flags, hf);
+
+ // Fill in header.
+ chf.width = w;
+ chf.height = h;
+ chf.spanCount = spanCount;
+ chf.walkableHeight = walkableHeight;
+ chf.walkableClimb = walkableClimb;
+ chf.maxRegions = 0;
+ vcopy(chf.bmin, hf.bmin);
+ vcopy(chf.bmax, hf.bmax);
+ chf.bmax[1] += walkableHeight*hf.ch;
+ chf.cs = hf.cs;
+ chf.ch = hf.ch;
+ chf.cells = new rcCompactCell[w*h];
+ if (!chf.cells)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Out of memory 'chf.cells' (%d)", w*h);
+ return false;
+ }
+ memset(chf.cells, 0, sizeof(rcCompactCell)*w*h);
+ chf.spans = new rcCompactSpan[spanCount];
+ if (!chf.spans)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Out of memory 'chf.spans' (%d)", spanCount);
+ return false;
+ }
+ memset(chf.spans, 0, sizeof(rcCompactSpan)*spanCount);
+
+ const int MAX_HEIGHT = 0xffff;
+
+ // Fill in cells and spans.
+ int idx = 0;
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ const rcSpan* s = hf.spans[x + y*w];
+ // If there are no spans at this cell, just leave the data to index=0, count=0.
+ if (!s) continue;
+ rcCompactCell& c = chf.cells[x+y*w];
+ c.index = idx;
+ c.count = 0;
+ while (s)
+ {
+ if (s->flags == flags)
+ {
+ const int bot = (int)s->smax;
+ const int top = (int)s->next ? (int)s->next->smin : MAX_HEIGHT;
+ chf.spans[idx].y = (unsigned short)rcClamp(bot, 0, 0xffff);
+ chf.spans[idx].h = (unsigned char)rcClamp(top - bot, 0, 0xff);
+ idx++;
+ c.count++;
+ }
+ s = s->next;
+ }
+ }
+ }
+
+ // Find neighbour connections.
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ const rcCompactCell& c = chf.cells[x+y*w];
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
+ {
+ rcCompactSpan& s = chf.spans[i];
+ for (int dir = 0; dir < 4; ++dir)
+ {
+ setCon(s, dir, 0xf);
+ const int nx = x + rcGetDirOffsetX(dir);
+ const int ny = y + rcGetDirOffsetY(dir);
+ // First check that the neighbour cell is in bounds.
+ if (nx < 0 || ny < 0 || nx >= w || ny >= h)
+ continue;
+ // Iterate over all neighbour spans and check if any of the is
+ // accessible from current cell.
+ const rcCompactCell& nc = chf.cells[nx+ny*w];
+ for (int k = (int)nc.index, nk = (int)(nc.index+nc.count); k < nk; ++k)
+ {
+ const rcCompactSpan& ns = chf.spans[k];
+ const int bot = rcMax(s.y, ns.y);
+ const int top = rcMin(s.y+s.h, ns.y+ns.h);
+
+ // Check that the gap between the spans is walkable,
+ // and that the climb height between the gaps is not too high.
+ if ((top - bot) >= walkableHeight && rcAbs((int)ns.y - (int)s.y) <= walkableClimb)
+ {
+ // Mark direction as walkable.
+ setCon(s, dir, k - (int)nc.index);
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ rcTimeVal endTime = rcGetPerformanceTimer();
+
+ if (rcGetBuildTimes())
+ rcGetBuildTimes()->buildCompact += rcGetDeltaTimeUsec(startTime, endTime);
+
+ return true;
+}
+
+static int getHeightfieldMemoryUsage(const rcHeightfield& hf)
+{
+ int size = 0;
+ size += sizeof(hf);
+ size += hf.width * hf.height * sizeof(rcSpan*);
+
+ rcSpanPool* pool = hf.pools;
+ while (pool)
+ {
+ size += (sizeof(rcSpanPool) - sizeof(rcSpan)) + sizeof(rcSpan)*RC_SPANS_PER_POOL;
+ pool = pool->next;
+ }
+ return size;
+}
+
+static int getCompactHeightFieldMemoryusage(const rcCompactHeightfield& chf)
+{
+ int size = 0;
+ size += sizeof(rcCompactHeightfield);
+ size += sizeof(rcCompactSpan) * chf.spanCount;
+ size += sizeof(rcCompactCell) * chf.width * chf.height;
+ return size;
+}
diff --git a/extern/recastnavigation/Recast/Source/RecastContour.cpp b/extern/recastnavigation/Recast/Source/RecastContour.cpp
new file mode 100644
index 00000000000..96f763a18f3
--- /dev/null
+++ b/extern/recastnavigation/Recast/Source/RecastContour.cpp
@@ -0,0 +1,732 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#define _USE_MATH_DEFINES
+#include <math.h>
+#include <string.h>
+#include <stdio.h>
+#include "Recast.h"
+#include "RecastLog.h"
+#include "RecastTimer.h"
+
+
+static int getCornerHeight(int x, int y, int i, int dir,
+ const rcCompactHeightfield& chf,
+ bool& isBorderVertex)
+{
+ const rcCompactSpan& s = chf.spans[i];
+ int ch = (int)s.y;
+ int dirp = (dir+1) & 0x3;
+
+ unsigned short regs[4] = {0,0,0,0};
+
+ regs[0] = s.reg;
+
+ if (rcGetCon(s, dir) != 0xf)
+ {
+ const int ax = x + rcGetDirOffsetX(dir);
+ const int ay = y + rcGetDirOffsetY(dir);
+ const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(s, dir);
+ const rcCompactSpan& as = chf.spans[ai];
+ ch = rcMax(ch, (int)as.y);
+ regs[1] = as.reg;
+ if (rcGetCon(as, dirp) != 0xf)
+ {
+ const int ax2 = ax + rcGetDirOffsetX(dirp);
+ const int ay2 = ay + rcGetDirOffsetY(dirp);
+ const int ai2 = (int)chf.cells[ax2+ay2*chf.width].index + rcGetCon(as, dirp);
+ const rcCompactSpan& as2 = chf.spans[ai2];
+ ch = rcMax(ch, (int)as2.y);
+ regs[2] = as2.reg;
+ }
+ }
+ if (rcGetCon(s, dirp) != 0xf)
+ {
+ const int ax = x + rcGetDirOffsetX(dirp);
+ const int ay = y + rcGetDirOffsetY(dirp);
+ const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(s, dirp);
+ const rcCompactSpan& as = chf.spans[ai];
+ ch = rcMax(ch, (int)as.y);
+ regs[3] = as.reg;
+ if (rcGetCon(as, dir) != 0xf)
+ {
+ const int ax2 = ax + rcGetDirOffsetX(dir);
+ const int ay2 = ay + rcGetDirOffsetY(dir);
+ const int ai2 = (int)chf.cells[ax2+ay2*chf.width].index + rcGetCon(as, dir);
+ const rcCompactSpan& as2 = chf.spans[ai2];
+ ch = rcMax(ch, (int)as2.y);
+ regs[2] = as2.reg;
+ }
+ }
+
+ // Check if the vertex is special edge vertex, these vertices will be removed later.
+ for (int j = 0; j < 4; ++j)
+ {
+ const int a = j;
+ const int b = (j+1) & 0x3;
+ const int c = (j+2) & 0x3;
+ const int d = (j+3) & 0x3;
+
+ // The vertex is a border vertex there are two same exterior cells in a row,
+ // followed by two interior cells and none of the regions are out of bounds.
+ const bool twoSameExts = (regs[a] & regs[b] & RC_BORDER_REG) != 0 && regs[a] == regs[b];
+ const bool twoInts = ((regs[c] | regs[d]) & RC_BORDER_REG) == 0;
+ const bool noZeros = regs[a] != 0 && regs[b] != 0 && regs[c] != 0 && regs[d] != 0;
+ if (twoSameExts && twoInts && noZeros)
+ {
+ isBorderVertex = true;
+ break;
+ }
+ }
+
+ return ch;
+}
+
+static void walkContour(int x, int y, int i,
+ rcCompactHeightfield& chf,
+ unsigned char* flags, rcIntArray& points)
+{
+ // Choose the first non-connected edge
+ unsigned char dir = 0;
+ while ((flags[i] & (1 << dir)) == 0)
+ dir++;
+
+ unsigned char startDir = dir;
+ int starti = i;
+
+ int iter = 0;
+ while (++iter < 40000)
+ {
+ if (flags[i] & (1 << dir))
+ {
+ // Choose the edge corner
+ bool isBorderVertex = false;
+ int px = x;
+ int py = getCornerHeight(x, y, i, dir, chf, isBorderVertex);
+ int pz = y;
+ switch(dir)
+ {
+ case 0: pz++; break;
+ case 1: px++; pz++; break;
+ case 2: px++; break;
+ }
+ int r = 0;
+ const rcCompactSpan& s = chf.spans[i];
+ if (rcGetCon(s, dir) != 0xf)
+ {
+ const int ax = x + rcGetDirOffsetX(dir);
+ const int ay = y + rcGetDirOffsetY(dir);
+ const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(s, dir);
+ const rcCompactSpan& as = chf.spans[ai];
+ r = (int)as.reg;
+ }
+ if (isBorderVertex)
+ r |= RC_BORDER_VERTEX;
+ points.push(px);
+ points.push(py);
+ points.push(pz);
+ points.push(r);
+
+ flags[i] &= ~(1 << dir); // Remove visited edges
+ dir = (dir+1) & 0x3; // Rotate CW
+ }
+ else
+ {
+ int ni = -1;
+ const int nx = x + rcGetDirOffsetX(dir);
+ const int ny = y + rcGetDirOffsetY(dir);
+ const rcCompactSpan& s = chf.spans[i];
+ if (rcGetCon(s, dir) != 0xf)
+ {
+ const rcCompactCell& nc = chf.cells[nx+ny*chf.width];
+ ni = (int)nc.index + rcGetCon(s, dir);
+ }
+ if (ni == -1)
+ {
+ // Should not happen.
+ return;
+ }
+ x = nx;
+ y = ny;
+ i = ni;
+ dir = (dir+3) & 0x3; // Rotate CCW
+ }
+
+ if (starti == i && startDir == dir)
+ {
+ break;
+ }
+ }
+}
+
+static float distancePtSeg(int x, int y, int z,
+ int px, int py, int pz,
+ int qx, int qy, int qz)
+{
+/* float pqx = (float)(qx - px);
+ float pqy = (float)(qy - py);
+ float pqz = (float)(qz - pz);
+ float dx = (float)(x - px);
+ float dy = (float)(y - py);
+ float dz = (float)(z - pz);
+ float d = pqx*pqx + pqy*pqy + pqz*pqz;
+ float t = pqx*dx + pqy*dy + pqz*dz;
+ if (d > 0)
+ t /= d;
+ if (t < 0)
+ t = 0;
+ else if (t > 1)
+ t = 1;
+
+ dx = px + t*pqx - x;
+ dy = py + t*pqy - y;
+ dz = pz + t*pqz - z;
+
+ return dx*dx + dy*dy + dz*dz;*/
+
+ float pqx = (float)(qx - px);
+ float pqz = (float)(qz - pz);
+ float dx = (float)(x - px);
+ float dz = (float)(z - pz);
+ float d = pqx*pqx + pqz*pqz;
+ float t = pqx*dx + pqz*dz;
+ if (d > 0)
+ t /= d;
+ if (t < 0)
+ t = 0;
+ else if (t > 1)
+ t = 1;
+
+ dx = px + t*pqx - x;
+ dz = pz + t*pqz - z;
+
+ return dx*dx + dz*dz;
+}
+
+static void simplifyContour(rcIntArray& points, rcIntArray& simplified, float maxError, int maxEdgeLen)
+{
+ // Add initial points.
+ bool noConnections = true;
+ for (int i = 0; i < points.size(); i += 4)
+ {
+ if ((points[i+3] & 0xffff) != 0)
+ {
+ noConnections = false;
+ break;
+ }
+ }
+
+ if (noConnections)
+ {
+ // If there is no connections at all,
+ // create some initial points for the simplification process.
+ // Find lower-left and upper-right vertices of the contour.
+ int llx = points[0];
+ int lly = points[1];
+ int llz = points[2];
+ int lli = 0;
+ int urx = points[0];
+ int ury = points[1];
+ int urz = points[2];
+ int uri = 0;
+ for (int i = 0; i < points.size(); i += 4)
+ {
+ int x = points[i+0];
+ int y = points[i+1];
+ int z = points[i+2];
+ if (x < llx || (x == llx && z < llz))
+ {
+ llx = x;
+ lly = y;
+ llz = z;
+ lli = i/4;
+ }
+ if (x >= urx || (x == urx && z > urz))
+ {
+ urx = x;
+ ury = y;
+ urz = z;
+ uri = i/4;
+ }
+ }
+ simplified.push(llx);
+ simplified.push(lly);
+ simplified.push(llz);
+ simplified.push(lli);
+
+ simplified.push(urx);
+ simplified.push(ury);
+ simplified.push(urz);
+ simplified.push(uri);
+ }
+ else
+ {
+ // The contour has some portals to other regions.
+ // Add a new point to every location where the region changes.
+ for (int i = 0, ni = points.size()/4; i < ni; ++i)
+ {
+ int ii = (i+1) % ni;
+ if ((points[i*4+3] & 0xffff) != (points[ii*4+3] & 0xffff))
+ {
+ simplified.push(points[i*4+0]);
+ simplified.push(points[i*4+1]);
+ simplified.push(points[i*4+2]);
+ simplified.push(i);
+ }
+ }
+ }
+
+ // Add points until all raw points are within
+ // error tolerance to the simplified shape.
+ const int pn = points.size()/4;
+ for (int i = 0; i < simplified.size()/4; )
+ {
+ int ii = (i+1) % (simplified.size()/4);
+
+ int ax = simplified[i*4+0];
+ int ay = simplified[i*4+1];
+ int az = simplified[i*4+2];
+ int ai = simplified[i*4+3];
+
+ int bx = simplified[ii*4+0];
+ int by = simplified[ii*4+1];
+ int bz = simplified[ii*4+2];
+ int bi = simplified[ii*4+3];
+
+ // Find maximum deviation from the segment.
+ float maxd = 0;
+ int maxi = -1;
+ int ci = (ai+1) % pn;
+
+ // Tesselate only outer edges.
+ if ((points[ci*4+3] & 0xffff) == 0)
+ {
+ while (ci != bi)
+ {
+ float d = distancePtSeg(points[ci*4+0], points[ci*4+1]/4, points[ci*4+2],
+ ax, ay/4, az, bx, by/4, bz);
+ if (d > maxd)
+ {
+ maxd = d;
+ maxi = ci;
+ }
+ ci = (ci+1) % pn;
+ }
+ }
+
+
+ // If the max deviation is larger than accepted error,
+ // add new point, else continue to next segment.
+ if (maxi != -1 && maxd > (maxError*maxError))
+ {
+ // Add space for the new point.
+ simplified.resize(simplified.size()+4);
+ int n = simplified.size()/4;
+ for (int j = n-1; j > i; --j)
+ {
+ simplified[j*4+0] = simplified[(j-1)*4+0];
+ simplified[j*4+1] = simplified[(j-1)*4+1];
+ simplified[j*4+2] = simplified[(j-1)*4+2];
+ simplified[j*4+3] = simplified[(j-1)*4+3];
+ }
+ // Add the point.
+ simplified[(i+1)*4+0] = points[maxi*4+0];
+ simplified[(i+1)*4+1] = points[maxi*4+1];
+ simplified[(i+1)*4+2] = points[maxi*4+2];
+ simplified[(i+1)*4+3] = maxi;
+ }
+ else
+ {
+ ++i;
+ }
+ }
+
+ // Split too long edges.
+ if (maxEdgeLen > 0)
+ {
+ for (int i = 0; i < simplified.size()/4; )
+ {
+ int ii = (i+1) % (simplified.size()/4);
+
+ int ax = simplified[i*4+0];
+ int az = simplified[i*4+2];
+ int ai = simplified[i*4+3];
+
+ int bx = simplified[ii*4+0];
+ int bz = simplified[ii*4+2];
+ int bi = simplified[ii*4+3];
+
+ // Find maximum deviation from the segment.
+ int maxi = -1;
+ int ci = (ai+1) % pn;
+
+ // Tesselate only outer edges.
+ if ((points[ci*4+3] & 0xffff) == 0)
+ {
+ int dx = bx - ax;
+ int dz = bz - az;
+ if (dx*dx + dz*dz > maxEdgeLen*maxEdgeLen)
+ {
+ int n = bi < ai ? (bi+pn - ai) : (bi - ai);
+ maxi = (ai + n/2) % pn;
+ }
+ }
+
+ // If the max deviation is larger than accepted error,
+ // add new point, else continue to next segment.
+ if (maxi != -1)
+ {
+ // Add space for the new point.
+ simplified.resize(simplified.size()+4);
+ int n = simplified.size()/4;
+ for (int j = n-1; j > i; --j)
+ {
+ simplified[j*4+0] = simplified[(j-1)*4+0];
+ simplified[j*4+1] = simplified[(j-1)*4+1];
+ simplified[j*4+2] = simplified[(j-1)*4+2];
+ simplified[j*4+3] = simplified[(j-1)*4+3];
+ }
+ // Add the point.
+ simplified[(i+1)*4+0] = points[maxi*4+0];
+ simplified[(i+1)*4+1] = points[maxi*4+1];
+ simplified[(i+1)*4+2] = points[maxi*4+2];
+ simplified[(i+1)*4+3] = maxi;
+ }
+ else
+ {
+ ++i;
+ }
+ }
+ }
+
+ for (int i = 0; i < simplified.size()/4; ++i)
+ {
+ // The edge vertex flag is take from the current raw point,
+ // and the neighbour region is take from the next raw point.
+ const int ai = (simplified[i*4+3]+1) % pn;
+ const int bi = simplified[i*4+3];
+ simplified[i*4+3] = (points[ai*4+3] & 0xffff) | (points[bi*4+3] & RC_BORDER_VERTEX);
+ }
+
+}
+
+static void removeDegenerateSegments(rcIntArray& simplified)
+{
+ // Remove adjacent vertices which are equal on xz-plane,
+ // or else the triangulator will get confused.
+ for (int i = 0; i < simplified.size()/4; ++i)
+ {
+ int ni = i+1;
+ if (ni >= (simplified.size()/4))
+ ni = 0;
+
+ if (simplified[i*4+0] == simplified[ni*4+0] &&
+ simplified[i*4+2] == simplified[ni*4+2])
+ {
+ // Degenerate segment, remove.
+ for (int j = i; j < simplified.size()/4-1; ++j)
+ {
+ simplified[j*4+0] = simplified[(j+1)*4+0];
+ simplified[j*4+1] = simplified[(j+1)*4+1];
+ simplified[j*4+2] = simplified[(j+1)*4+2];
+ simplified[j*4+3] = simplified[(j+1)*4+3];
+ }
+ simplified.pop();
+ }
+ }
+}
+
+static int calcAreaOfPolygon2D(const int* verts, const int nverts)
+{
+ int area = 0;
+ for (int i = 0, j = nverts-1; i < nverts; j=i++)
+ {
+ const int* vi = &verts[i*4];
+ const int* vj = &verts[j*4];
+ area += vi[0] * vj[2] - vj[0] * vi[2];
+ }
+ return (area+1) / 2;
+}
+
+static void getClosestIndices(const int* vertsa, const int nvertsa,
+ const int* vertsb, const int nvertsb,
+ int& ia, int& ib)
+{
+ int closestDist = 0xfffffff;
+ for (int i = 0; i < nvertsa; ++i)
+ {
+ const int* va = &vertsa[i*4];
+ for (int j = 0; j < nvertsb; ++j)
+ {
+ const int* vb = &vertsb[j*4];
+ const int dx = vb[0] - va[0];
+ const int dz = vb[2] - va[2];
+ const int d = dx*dx + dz*dz;
+ if (d < closestDist)
+ {
+ ia = i;
+ ib = j;
+ closestDist = d;
+ }
+ }
+ }
+}
+
+static bool mergeContours(rcContour& ca, rcContour& cb, int ia, int ib)
+{
+ const int maxVerts = ca.nverts + cb.nverts + 2;
+ int* verts = new int[maxVerts*4];
+ if (!verts)
+ return false;
+
+ int nv = 0;
+
+ // Copy contour A.
+ for (int i = 0; i <= ca.nverts; ++i)
+ {
+ int* dst = &verts[nv*4];
+ const int* src = &ca.verts[((ia+i)%ca.nverts)*4];
+ dst[0] = src[0];
+ dst[1] = src[1];
+ dst[2] = src[2];
+ dst[3] = src[3];
+ nv++;
+ }
+
+ // Copy contour B
+ for (int i = 0; i <= cb.nverts; ++i)
+ {
+ int* dst = &verts[nv*4];
+ const int* src = &cb.verts[((ib+i)%cb.nverts)*4];
+ dst[0] = src[0];
+ dst[1] = src[1];
+ dst[2] = src[2];
+ dst[3] = src[3];
+ nv++;
+ }
+
+ delete [] ca.verts;
+ ca.verts = verts;
+ ca.nverts = nv;
+
+ delete [] cb.verts;
+ cb.verts = 0;
+ cb.nverts = 0;
+
+ return true;
+}
+
+bool rcBuildContours(rcCompactHeightfield& chf,
+ const float maxError, const int maxEdgeLen,
+ rcContourSet& cset)
+{
+ const int w = chf.width;
+ const int h = chf.height;
+
+ rcTimeVal startTime = rcGetPerformanceTimer();
+
+ vcopy(cset.bmin, chf.bmin);
+ vcopy(cset.bmax, chf.bmax);
+ cset.cs = chf.cs;
+ cset.ch = chf.ch;
+
+ const int maxContours = chf.maxRegions*2;
+ cset.conts = new rcContour[maxContours];
+ if (!cset.conts)
+ return false;
+ cset.nconts = 0;
+
+ unsigned char* flags = new unsigned char[chf.spanCount];
+ if (!flags)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildContours: Out of memory 'flags'.");
+ return false;
+ }
+
+ rcTimeVal traceStartTime = rcGetPerformanceTimer();
+
+
+ // Mark boundaries.
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ const rcCompactCell& c = chf.cells[x+y*w];
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
+ {
+ unsigned char res = 0;
+ const rcCompactSpan& s = chf.spans[i];
+ if (!s.reg || (s.reg & RC_BORDER_REG))
+ {
+ flags[i] = 0;
+ continue;
+ }
+ for (int dir = 0; dir < 4; ++dir)
+ {
+ unsigned short r = 0;
+ if (rcGetCon(s, dir) != 0xf)
+ {
+ const int ax = x + rcGetDirOffsetX(dir);
+ const int ay = y + rcGetDirOffsetY(dir);
+ const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir);
+ const rcCompactSpan& as = chf.spans[ai];
+ r = as.reg;
+ }
+ if (r == s.reg)
+ res |= (1 << dir);
+ }
+ flags[i] = res ^ 0xf; // Inverse, mark non connected edges.
+ }
+ }
+ }
+
+ rcTimeVal traceEndTime = rcGetPerformanceTimer();
+
+ rcTimeVal simplifyStartTime = rcGetPerformanceTimer();
+
+ rcIntArray verts(256);
+ rcIntArray simplified(64);
+
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ const rcCompactCell& c = chf.cells[x+y*w];
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
+ {
+ if (flags[i] == 0 || flags[i] == 0xf)
+ {
+ flags[i] = 0;
+ continue;
+ }
+ unsigned short reg = chf.spans[i].reg;
+ if (!reg || (reg & RC_BORDER_REG))
+ continue;
+
+ verts.resize(0);
+ simplified.resize(0);
+ walkContour(x, y, i, chf, flags, verts);
+ simplifyContour(verts, simplified, maxError, maxEdgeLen);
+ removeDegenerateSegments(simplified);
+
+ // Store region->contour remap info.
+ // Create contour.
+ if (simplified.size()/4 >= 3)
+ {
+ if (cset.nconts >= maxContours)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildContours: Too many contours %d, max %d.", cset.nconts, maxContours);
+ return false;
+ }
+
+ rcContour* cont = &cset.conts[cset.nconts++];
+
+ cont->nverts = simplified.size()/4;
+ cont->verts = new int[cont->nverts*4];
+ memcpy(cont->verts, &simplified[0], sizeof(int)*cont->nverts*4);
+
+ cont->nrverts = verts.size()/4;
+ cont->rverts = new int[cont->nrverts*4];
+ memcpy(cont->rverts, &verts[0], sizeof(int)*cont->nrverts*4);
+
+/* cont->cx = cont->cy = cont->cz = 0;
+ for (int i = 0; i < cont->nverts; ++i)
+ {
+ cont->cx += cont->verts[i*4+0];
+ cont->cy += cont->verts[i*4+1];
+ cont->cz += cont->verts[i*4+2];
+ }
+ cont->cx /= cont->nverts;
+ cont->cy /= cont->nverts;
+ cont->cz /= cont->nverts;*/
+
+ cont->reg = reg;
+ }
+ }
+ }
+ }
+
+ // Check and merge droppings.
+ // Sometimes the previous algorithms can fail and create several countours
+ // per area. This pass will try to merge the holes into the main region.
+ for (int i = 0; i < cset.nconts; ++i)
+ {
+ rcContour& cont = cset.conts[i];
+ // Check if the contour is would backwards.
+ if (calcAreaOfPolygon2D(cont.verts, cont.nverts) < 0)
+ {
+ // Find another contour which has the same region ID.
+ int mergeIdx = -1;
+ for (int j = 0; j < cset.nconts; ++j)
+ {
+ if (i == j) continue;
+ if (cset.conts[j].nverts && cset.conts[j].reg == cont.reg)
+ {
+ // Make sure the polygon is correctly oriented.
+ if (calcAreaOfPolygon2D(cset.conts[j].verts, cset.conts[j].nverts))
+ {
+ mergeIdx = j;
+ break;
+ }
+ }
+ }
+ if (mergeIdx == -1)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_WARNING, "rcBuildContours: Could not find merge target for bad contour %d.", i);
+ }
+ else
+ {
+ rcContour& mcont = cset.conts[mergeIdx];
+ // Merge by closest points.
+ int ia, ib;
+ getClosestIndices(mcont.verts, mcont.nverts, cont.verts, cont.nverts, ia, ib);
+ if (!mergeContours(mcont, cont, ia, ib))
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_WARNING, "rcBuildContours: Failed to merge contours %d and %d.", i, mergeIdx);
+ }
+ }
+ }
+ }
+
+
+ delete [] flags;
+
+ rcTimeVal simplifyEndTime = rcGetPerformanceTimer();
+
+ rcTimeVal endTime = rcGetPerformanceTimer();
+
+// if (rcGetLog())
+// {
+// rcGetLog()->log(RC_LOG_PROGRESS, "Create contours: %.3f ms", rcGetDeltaTimeUsec(startTime, endTime)/1000.0f);
+// rcGetLog()->log(RC_LOG_PROGRESS, " - boundary: %.3f ms", rcGetDeltaTimeUsec(boundaryStartTime, boundaryEndTime)/1000.0f);
+// rcGetLog()->log(RC_LOG_PROGRESS, " - contour: %.3f ms", rcGetDeltaTimeUsec(contourStartTime, contourEndTime)/1000.0f);
+// }
+
+ if (rcGetBuildTimes())
+ {
+ rcGetBuildTimes()->buildContours += rcGetDeltaTimeUsec(startTime, endTime);
+ rcGetBuildTimes()->buildContoursTrace += rcGetDeltaTimeUsec(traceStartTime, traceEndTime);
+ rcGetBuildTimes()->buildContoursSimplify += rcGetDeltaTimeUsec(simplifyStartTime, simplifyEndTime);
+ }
+
+ return true;
+}
diff --git a/extern/recastnavigation/Recast/Source/RecastFilter.cpp b/extern/recastnavigation/Recast/Source/RecastFilter.cpp
new file mode 100644
index 00000000000..3421ea1899e
--- /dev/null
+++ b/extern/recastnavigation/Recast/Source/RecastFilter.cpp
@@ -0,0 +1,249 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#define _USE_MATH_DEFINES
+#include <math.h>
+#include <stdio.h>
+#include "Recast.h"
+#include "RecastLog.h"
+#include "RecastTimer.h"
+
+
+void rcFilterLedgeSpans(const int walkableHeight,
+ const int walkableClimb,
+ rcHeightfield& solid)
+{
+ rcTimeVal startTime = rcGetPerformanceTimer();
+
+ const int w = solid.width;
+ const int h = solid.height;
+ const int MAX_HEIGHT = 0xffff;
+
+ // Mark border spans.
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ for (rcSpan* s = solid.spans[x + y*w]; s; s = s->next)
+ {
+ // Skip non walkable spans.
+ if ((s->flags & RC_WALKABLE) == 0)
+ continue;
+
+ const int bot = (int)s->smax;
+ const int top = (int)s->next ? (int)s->next->smin : MAX_HEIGHT;
+
+ // Find neighbours minimum height.
+ int minh = MAX_HEIGHT;
+
+ for (int dir = 0; dir < 4; ++dir)
+ {
+ int dx = x + rcGetDirOffsetX(dir);
+ int dy = y + rcGetDirOffsetY(dir);
+ // Skip neighbours which are out of bounds.
+ if (dx < 0 || dy < 0 || dx >= w || dy >= h)
+ {
+ minh = rcMin(minh, -walkableClimb - bot);
+ continue;
+ }
+
+ // From minus infinity to the first span.
+ rcSpan* ns = solid.spans[dx + dy*w];
+ int nbot = -walkableClimb;
+ int ntop = ns ? (int)ns->smin : MAX_HEIGHT;
+ // Skip neightbour if the gap between the spans is too small.
+ if (rcMin(top,ntop) - rcMax(bot,nbot) > walkableHeight)
+ minh = rcMin(minh, nbot - bot);
+
+ // Rest of the spans.
+ for (ns = solid.spans[dx + dy*w]; ns; ns = ns->next)
+ {
+ nbot = (int)ns->smax;
+ ntop = (int)ns->next ? (int)ns->next->smin : MAX_HEIGHT;
+ // Skip neightbour if the gap between the spans is too small.
+ if (rcMin(top,ntop) - rcMax(bot,nbot) > walkableHeight)
+ minh = rcMin(minh, nbot - bot);
+ }
+ }
+
+ // The current span is close to a ledge if the drop to any
+ // neighbour span is less than the walkableClimb.
+ if (minh < -walkableClimb)
+ s->flags &= ~RC_WALKABLE;
+
+ }
+ }
+ }
+
+ rcTimeVal endTime = rcGetPerformanceTimer();
+// if (rcGetLog())
+// rcGetLog()->log(RC_LOG_PROGRESS, "Filter border: %.3f ms", rcGetDeltaTimeUsec(startTime, endTime)/1000.0f);
+ if (rcGetBuildTimes())
+ rcGetBuildTimes()->filterBorder += rcGetDeltaTimeUsec(startTime, endTime);
+}
+
+void rcFilterWalkableLowHeightSpans(int walkableHeight,
+ rcHeightfield& solid)
+{
+ rcTimeVal startTime = rcGetPerformanceTimer();
+
+ const int w = solid.width;
+ const int h = solid.height;
+ const int MAX_HEIGHT = 0xffff;
+
+ // Remove walkable flag from spans which do not have enough
+ // space above them for the agent to stand there.
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ for (rcSpan* s = solid.spans[x + y*w]; s; s = s->next)
+ {
+ const int bot = (int)s->smax;
+ const int top = (int)s->next ? (int)s->next->smin : MAX_HEIGHT;
+ if ((top - bot) <= walkableHeight)
+ s->flags &= ~RC_WALKABLE;
+ }
+ }
+ }
+
+ rcTimeVal endTime = rcGetPerformanceTimer();
+
+// if (rcGetLog())
+// rcGetLog()->log(RC_LOG_PROGRESS, "Filter walkable: %.3f ms", rcGetDeltaTimeUsec(startTime, endTime)/1000.0f);
+ if (rcGetBuildTimes())
+ rcGetBuildTimes()->filterWalkable += rcGetDeltaTimeUsec(startTime, endTime);
+}
+
+
+struct rcReachableSeed
+{
+ inline void set(int ix, int iy, rcSpan* is)
+ {
+ x = (unsigned short)ix;
+ y = (unsigned short)iy;
+ s = is;
+ }
+ unsigned short x, y;
+ rcSpan* s;
+};
+
+bool rcMarkReachableSpans(const int walkableHeight,
+ const int walkableClimb,
+ rcHeightfield& solid)
+{
+ const int w = solid.width;
+ const int h = solid.height;
+ const int MAX_HEIGHT = 0xffff;
+
+ rcTimeVal startTime = rcGetPerformanceTimer();
+
+ // Build navigable space.
+ const int MAX_SEEDS = w*h;
+ rcReachableSeed* stack = new rcReachableSeed[MAX_SEEDS];
+ if (!stack)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcMarkReachableSpans: Out of memory 'stack' (%d).", MAX_SEEDS);
+ return false;
+ }
+ int stackSize = 0;
+
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ rcSpan* topSpan = solid.spans[x + y*w];
+ if (!topSpan)
+ continue;
+ while (topSpan->next)
+ topSpan = topSpan->next;
+
+ // If the span is not walkable, skip it.
+ if ((topSpan->flags & RC_WALKABLE) == 0)
+ continue;
+ // If the span has been visited already, skip it.
+ if (topSpan->flags & RC_REACHABLE)
+ continue;
+
+ // Start flood fill.
+ topSpan->flags |= RC_REACHABLE;
+ stackSize = 0;
+ stack[stackSize].set(x, y, topSpan);
+ stackSize++;
+
+ while (stackSize)
+ {
+ // Pop a seed from the stack.
+ stackSize--;
+ rcReachableSeed cur = stack[stackSize];
+
+ const int bot = (int)cur.s->smax;
+ const int top = (int)cur.s->next ? (int)cur.s->next->smin : MAX_HEIGHT;
+
+ // Visit neighbours in all 4 directions.
+ for (int dir = 0; dir < 4; ++dir)
+ {
+ int dx = (int)cur.x + rcGetDirOffsetX(dir);
+ int dy = (int)cur.y + rcGetDirOffsetY(dir);
+ // Skip neighbour which are out of bounds.
+ if (dx < 0 || dy < 0 || dx >= w || dy >= h)
+ continue;
+ for (rcSpan* ns = solid.spans[dx + dy*w]; ns; ns = ns->next)
+ {
+ // Skip neighbour if it is not walkable.
+ if ((ns->flags & RC_WALKABLE) == 0)
+ continue;
+ // Skip the neighbour if it has been visited already.
+ if (ns->flags & RC_REACHABLE)
+ continue;
+
+ const int nbot = (int)ns->smax;
+ const int ntop = (int)ns->next ? (int)ns->next->smin : MAX_HEIGHT;
+ // Skip neightbour if the gap between the spans is too small.
+ if (rcMin(top,ntop) - rcMax(bot,nbot) < walkableHeight)
+ continue;
+ // Skip neightbour if the climb height to the neighbour is too high.
+ if (rcAbs(nbot - bot) >= walkableClimb)
+ continue;
+
+ // This neighbour has not been visited yet.
+ // Mark it as reachable and add it to the seed stack.
+ ns->flags |= RC_REACHABLE;
+ if (stackSize < MAX_SEEDS)
+ {
+ stack[stackSize].set(dx, dy, ns);
+ stackSize++;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ delete [] stack;
+
+ rcTimeVal endTime = rcGetPerformanceTimer();
+
+// if (rcGetLog())
+// rcGetLog()->log(RC_LOG_PROGRESS, "Mark reachable: %.3f ms", rcGetDeltaTimeUsec(startTime, endTime)/1000.0f);
+ if (rcGetBuildTimes())
+ rcGetBuildTimes()->filterMarkReachable += rcGetDeltaTimeUsec(startTime, endTime);
+
+ return true;
+}
diff --git a/extern/recastnavigation/Recast/Source/RecastLog.cpp b/extern/recastnavigation/Recast/Source/RecastLog.cpp
new file mode 100644
index 00000000000..27868042a1c
--- /dev/null
+++ b/extern/recastnavigation/Recast/Source/RecastLog.cpp
@@ -0,0 +1,77 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#include "RecastLog.h"
+#include <stdio.h>
+#include <stdarg.h>
+
+static rcLog* g_log = 0;
+static rcBuildTimes* g_btimes = 0;
+
+rcLog::rcLog() :
+ m_messageCount(0),
+ m_textPoolSize(0)
+{
+}
+
+rcLog::~rcLog()
+{
+ if (g_log == this)
+ g_log = 0;
+}
+
+void rcLog::log(rcLogCategory category, const char* format, ...)
+{
+ if (m_messageCount >= MAX_MESSAGES)
+ return;
+ char* dst = &m_textPool[m_textPoolSize];
+ int n = TEXT_POOL_SIZE - m_textPoolSize;
+ if (n < 2)
+ return;
+ // Store category
+ *dst = (char)category;
+ n--;
+ // Store message
+ va_list ap;
+ va_start(ap, format);
+ int ret = vsnprintf(dst+1, n-1, format, ap);
+ va_end(ap);
+ if (ret > 0)
+ m_textPoolSize += ret+2;
+ m_messages[m_messageCount++] = dst;
+}
+
+void rcSetLog(rcLog* log)
+{
+ g_log = log;
+}
+
+rcLog* rcGetLog()
+{
+ return g_log;
+}
+
+void rcSetBuildTimes(rcBuildTimes* btimes)
+{
+ g_btimes = btimes;
+}
+
+rcBuildTimes* rcGetBuildTimes()
+{
+ return g_btimes;
+}
diff --git a/extern/recastnavigation/Recast/Source/RecastMesh.cpp b/extern/recastnavigation/Recast/Source/RecastMesh.cpp
new file mode 100644
index 00000000000..45ed4a2fdc6
--- /dev/null
+++ b/extern/recastnavigation/Recast/Source/RecastMesh.cpp
@@ -0,0 +1,1212 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#define _USE_MATH_DEFINES
+#include <math.h>
+#include <string.h>
+#include <stdio.h>
+#include "Recast.h"
+#include "RecastLog.h"
+#include "RecastTimer.h"
+
+
+struct rcEdge
+{
+ unsigned short vert[2];
+ unsigned short polyEdge[2];
+ unsigned short poly[2];
+};
+
+/*static */bool buildMeshAdjacency(unsigned short* polys, const int npolys,
+ const int nverts, const int vertsPerPoly)
+{
+ // Based on code by Eric Lengyel from:
+ // http://www.terathon.com/code/edges.php
+
+ int maxEdgeCount = npolys*vertsPerPoly;
+ unsigned short* firstEdge = new unsigned short[nverts + maxEdgeCount];
+ if (!firstEdge)
+ return false;
+ unsigned short* nextEdge = firstEdge + nverts;
+ int edgeCount = 0;
+
+ rcEdge* edges = new rcEdge[maxEdgeCount];
+ if (!edges)
+ return false;
+
+ for (int i = 0; i < nverts; i++)
+ firstEdge[i] = 0xffff;
+
+ // Invalida indices are marked as 0xffff, the following code
+ // handles them just fine.
+
+ for (int i = 0; i < npolys; ++i)
+ {
+ unsigned short* t = &polys[i*vertsPerPoly*2];
+ for (int j = 0; j < vertsPerPoly; ++j)
+ {
+ unsigned short v0 = t[j];
+ unsigned short v1 = (j+1 >= vertsPerPoly || t[j+1] == 0xffff) ? t[0] : t[j+1];
+ if (v0 < v1)
+ {
+ rcEdge& edge = edges[edgeCount];
+ edge.vert[0] = v0;
+ edge.vert[1] = v1;
+ edge.poly[0] = (unsigned short)i;
+ edge.polyEdge[0] = (unsigned short)j;
+ edge.poly[1] = (unsigned short)i;
+ edge.polyEdge[1] = 0;
+ // Insert edge
+ nextEdge[edgeCount] = firstEdge[v0];
+ firstEdge[v0] = edgeCount;
+ edgeCount++;
+ }
+ }
+ }
+
+ for (int i = 0; i < npolys; ++i)
+ {
+ unsigned short* t = &polys[i*vertsPerPoly*2];
+ for (int j = 0; j < vertsPerPoly; ++j)
+ {
+ unsigned short v0 = t[j];
+ unsigned short v1 = (j+1 >= vertsPerPoly || t[j+1] == 0xffff) ? t[0] : t[j+1];
+ if (v0 > v1)
+ {
+ for (unsigned short e = firstEdge[v1]; e != 0xffff; e = nextEdge[e])
+ {
+ rcEdge& edge = edges[e];
+ if (edge.vert[1] == v0 && edge.poly[0] == edge.poly[1])
+ {
+ edge.poly[1] = (unsigned short)i;
+ edge.polyEdge[1] = (unsigned short)j;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ // Store adjacency
+ for (int i = 0; i < edgeCount; ++i)
+ {
+ const rcEdge& e = edges[i];
+ if (e.poly[0] != e.poly[1])
+ {
+ unsigned short* p0 = &polys[e.poly[0]*vertsPerPoly*2];
+ unsigned short* p1 = &polys[e.poly[1]*vertsPerPoly*2];
+ p0[vertsPerPoly + e.polyEdge[0]] = e.poly[1];
+ p1[vertsPerPoly + e.polyEdge[1]] = e.poly[0];
+ }
+ }
+
+ delete [] firstEdge;
+ delete [] edges;
+
+ return true;
+}
+
+
+static const int VERTEX_BUCKET_COUNT = (1<<12);
+
+inline int computeVertexHash(int x, int y, int z)
+{
+ const unsigned int h1 = 0x8da6b343; // Large multiplicative constants;
+ const unsigned int h2 = 0xd8163841; // here arbitrarily chosen primes
+ const unsigned int h3 = 0xcb1ab31f;
+ unsigned int n = h1 * x + h2 * y + h3 * z;
+ return (int)(n & (VERTEX_BUCKET_COUNT-1));
+}
+
+static int addVertex(unsigned short x, unsigned short y, unsigned short z,
+ unsigned short* verts, int* firstVert, int* nextVert, int& nv)
+{
+ int bucket = computeVertexHash(x, 0, z);
+ int i = firstVert[bucket];
+
+ while (i != -1)
+ {
+ const unsigned short* v = &verts[i*3];
+ if (v[0] == x && (rcAbs(v[1] - y) <= 2) && v[2] == z)
+ return i;
+ i = nextVert[i]; // next
+ }
+
+ // Could not find, create new.
+ i = nv; nv++;
+ unsigned short* v = &verts[i*3];
+ v[0] = x;
+ v[1] = y;
+ v[2] = z;
+ nextVert[i] = firstVert[bucket];
+ firstVert[bucket] = i;
+
+ return i;
+}
+
+inline int prev(int i, int n) { return i-1 >= 0 ? i-1 : n-1; }
+inline int next(int i, int n) { return i+1 < n ? i+1 : 0; }
+
+inline int area2(const int* a, const int* b, const int* c)
+{
+ return (b[0] - a[0]) * (c[2] - a[2]) - (c[0] - a[0]) * (b[2] - a[2]);
+}
+
+// Exclusive or: true iff exactly one argument is true.
+// The arguments are negated to ensure that they are 0/1
+// values. Then the bitwise Xor operator may apply.
+// (This idea is due to Michael Baldwin.)
+inline bool xorb(bool x, bool y)
+{
+ return !x ^ !y;
+}
+
+// Returns true iff c is strictly to the left of the directed
+// line through a to b.
+inline bool left(const int* a, const int* b, const int* c)
+{
+ return area2(a, b, c) < 0;
+}
+
+inline bool leftOn(const int* a, const int* b, const int* c)
+{
+ return area2(a, b, c) <= 0;
+}
+
+inline bool collinear(const int* a, const int* b, const int* c)
+{
+ return area2(a, b, c) == 0;
+}
+
+// Returns true iff ab properly intersects cd: they share
+// a point interior to both segments. The properness of the
+// intersection is ensured by using strict leftness.
+bool intersectProp(const int* a, const int* b, const int* c, const int* d)
+{
+ // Eliminate improper cases.
+ if (collinear(a,b,c) || collinear(a,b,d) ||
+ collinear(c,d,a) || collinear(c,d,b))
+ return false;
+
+ return xorb(left(a,b,c), left(a,b,d)) && xorb(left(c,d,a), left(c,d,b));
+}
+
+// Returns T iff (a,b,c) are collinear and point c lies
+// on the closed segement ab.
+static bool between(const int* a, const int* b, const int* c)
+{
+ if (!collinear(a, b, c))
+ return false;
+ // If ab not vertical, check betweenness on x; else on y.
+ if (a[0] != b[0])
+ return ((a[0] <= c[0]) && (c[0] <= b[0])) || ((a[0] >= c[0]) && (c[0] >= b[0]));
+ else
+ return ((a[2] <= c[2]) && (c[2] <= b[2])) || ((a[2] >= c[2]) && (c[2] >= b[2]));
+}
+
+// Returns true iff segments ab and cd intersect, properly or improperly.
+static bool intersect(const int* a, const int* b, const int* c, const int* d)
+{
+ if (intersectProp(a, b, c, d))
+ return true;
+ else if (between(a, b, c) || between(a, b, d) ||
+ between(c, d, a) || between(c, d, b))
+ return true;
+ else
+ return false;
+}
+
+static bool vequal(const int* a, const int* b)
+{
+ return a[0] == b[0] && a[2] == b[2];
+}
+
+// Returns T iff (v_i, v_j) is a proper internal *or* external
+// diagonal of P, *ignoring edges incident to v_i and v_j*.
+static bool diagonalie(int i, int j, int n, const int* verts, int* indices)
+{
+ const int* d0 = &verts[(indices[i] & 0x0fffffff) * 4];
+ const int* d1 = &verts[(indices[j] & 0x0fffffff) * 4];
+
+ // For each edge (k,k+1) of P
+ for (int k = 0; k < n; k++)
+ {
+ int k1 = next(k, n);
+ // Skip edges incident to i or j
+ if (!((k == i) || (k1 == i) || (k == j) || (k1 == j)))
+ {
+ const int* p0 = &verts[(indices[k] & 0x0fffffff) * 4];
+ const int* p1 = &verts[(indices[k1] & 0x0fffffff) * 4];
+
+ if (vequal(d0, p0) || vequal(d1, p0) || vequal(d0, p1) || vequal(d1, p1))
+ continue;
+
+ if (intersect(d0, d1, p0, p1))
+ return false;
+ }
+ }
+ return true;
+}
+
+// Returns true iff the diagonal (i,j) is strictly internal to the
+// polygon P in the neighborhood of the i endpoint.
+static bool inCone(int i, int j, int n, const int* verts, int* indices)
+{
+ const int* pi = &verts[(indices[i] & 0x0fffffff) * 4];
+ const int* pj = &verts[(indices[j] & 0x0fffffff) * 4];
+ const int* pi1 = &verts[(indices[next(i, n)] & 0x0fffffff) * 4];
+ const int* pin1 = &verts[(indices[prev(i, n)] & 0x0fffffff) * 4];
+
+ // If P[i] is a convex vertex [ i+1 left or on (i-1,i) ].
+ if (leftOn(pin1, pi, pi1))
+ return left(pi, pj, pin1) && left(pj, pi, pi1);
+ // Assume (i-1,i,i+1) not collinear.
+ // else P[i] is reflex.
+ return !(leftOn(pi, pj, pi1) && leftOn(pj, pi, pin1));
+}
+
+// Returns T iff (v_i, v_j) is a proper internal
+// diagonal of P.
+static bool diagonal(int i, int j, int n, const int* verts, int* indices)
+{
+ return inCone(i, j, n, verts, indices) && diagonalie(i, j, n, verts, indices);
+}
+
+int triangulate(int n, const int* verts, int* indices, int* tris)
+{
+ int ntris = 0;
+ int* dst = tris;
+
+ // The last bit of the index is used to indicate if the vertex can be removed.
+ for (int i = 0; i < n; i++)
+ {
+ int i1 = next(i, n);
+ int i2 = next(i1, n);
+ if (diagonal(i, i2, n, verts, indices))
+ indices[i1] |= 0x80000000;
+ }
+
+ while (n > 3)
+ {
+ int minLen = -1;
+ int mini = -1;
+ for (int i = 0; i < n; i++)
+ {
+ int i1 = next(i, n);
+ if (indices[i1] & 0x80000000)
+ {
+ const int* p0 = &verts[(indices[i] & 0x0fffffff) * 4];
+ const int* p2 = &verts[(indices[next(i1, n)] & 0x0fffffff) * 4];
+
+ int dx = p2[0] - p0[0];
+ int dy = p2[2] - p0[2];
+ int len = dx*dx + dy*dy;
+
+ if (minLen < 0 || len < minLen)
+ {
+ minLen = len;
+ mini = i;
+ }
+ }
+ }
+
+ if (mini == -1)
+ {
+ // Should not happen.
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_WARNING, "triangulate: Failed to triangulate polygon.");
+/* printf("mini == -1 ntris=%d n=%d\n", ntris, n);
+ for (int i = 0; i < n; i++)
+ {
+ printf("%d ", indices[i] & 0x0fffffff);
+ }
+ printf("\n");*/
+ return -ntris;
+ }
+
+ int i = mini;
+ int i1 = next(i, n);
+ int i2 = next(i1, n);
+
+ *dst++ = indices[i] & 0x0fffffff;
+ *dst++ = indices[i1] & 0x0fffffff;
+ *dst++ = indices[i2] & 0x0fffffff;
+ ntris++;
+
+ // Removes P[i1] by copying P[i+1]...P[n-1] left one index.
+ n--;
+ for (int k = i1; k < n; k++)
+ indices[k] = indices[k+1];
+
+ if (i1 >= n) i1 = 0;
+ i = prev(i1,n);
+ // Update diagonal flags.
+ if (diagonal(prev(i, n), i1, n, verts, indices))
+ indices[i] |= 0x80000000;
+ else
+ indices[i] &= 0x0fffffff;
+
+ if (diagonal(i, next(i1, n), n, verts, indices))
+ indices[i1] |= 0x80000000;
+ else
+ indices[i1] &= 0x0fffffff;
+ }
+
+ // Append the remaining triangle.
+ *dst++ = indices[0] & 0x0fffffff;
+ *dst++ = indices[1] & 0x0fffffff;
+ *dst++ = indices[2] & 0x0fffffff;
+ ntris++;
+
+ return ntris;
+}
+
+static int countPolyVerts(const unsigned short* p, const int nvp)
+{
+ for (int i = 0; i < nvp; ++i)
+ if (p[i] == 0xffff)
+ return i;
+ return nvp;
+}
+
+inline bool uleft(const unsigned short* a, const unsigned short* b, const unsigned short* c)
+{
+ return ((int)b[0] - (int)a[0]) * ((int)c[2] - (int)a[2]) -
+ ((int)c[0] - (int)a[0]) * ((int)b[2] - (int)a[2]) < 0;
+}
+
+static int getPolyMergeValue(unsigned short* pa, unsigned short* pb,
+ const unsigned short* verts, int& ea, int& eb,
+ const int nvp)
+{
+ const int na = countPolyVerts(pa, nvp);
+ const int nb = countPolyVerts(pb, nvp);
+
+ // If the merged polygon would be too big, do not merge.
+ if (na+nb-2 > nvp)
+ return -1;
+
+ // Check if the polygons share an edge.
+ ea = -1;
+ eb = -1;
+
+ for (int i = 0; i < na; ++i)
+ {
+ unsigned short va0 = pa[i];
+ unsigned short va1 = pa[(i+1) % na];
+ if (va0 > va1)
+ rcSwap(va0, va1);
+ for (int j = 0; j < nb; ++j)
+ {
+ unsigned short vb0 = pb[j];
+ unsigned short vb1 = pb[(j+1) % nb];
+ if (vb0 > vb1)
+ rcSwap(vb0, vb1);
+ if (va0 == vb0 && va1 == vb1)
+ {
+ ea = i;
+ eb = j;
+ break;
+ }
+ }
+ }
+
+ // No common edge, cannot merge.
+ if (ea == -1 || eb == -1)
+ return -1;
+
+ // Check to see if the merged polygon would be convex.
+ unsigned short va, vb, vc;
+
+ va = pa[(ea+na-1) % na];
+ vb = pa[ea];
+ vc = pb[(eb+2) % nb];
+ if (!uleft(&verts[va*3], &verts[vb*3], &verts[vc*3]))
+ return -1;
+
+ va = pb[(eb+nb-1) % nb];
+ vb = pb[eb];
+ vc = pa[(ea+2) % na];
+ if (!uleft(&verts[va*3], &verts[vb*3], &verts[vc*3]))
+ return -1;
+
+ va = pa[ea];
+ vb = pa[(ea+1)%na];
+
+ int dx = (int)verts[va*3+0] - (int)verts[vb*3+0];
+ int dy = (int)verts[va*3+2] - (int)verts[vb*3+2];
+
+ return dx*dx + dy*dy;
+}
+
+static void mergePolys(unsigned short* pa, unsigned short* pb,
+ const unsigned short* verts, int ea, int eb,
+ unsigned short* tmp, const int nvp)
+{
+ const int na = countPolyVerts(pa, nvp);
+ const int nb = countPolyVerts(pb, nvp);
+
+ // Merge polygons.
+ memset(tmp, 0xff, sizeof(unsigned short)*nvp);
+ int n = 0;
+ // Add pa
+ for (int i = 0; i < na-1; ++i)
+ tmp[n++] = pa[(ea+1+i) % na];
+ // Add pb
+ for (int i = 0; i < nb-1; ++i)
+ tmp[n++] = pb[(eb+1+i) % nb];
+
+ memcpy(pa, tmp, sizeof(unsigned short)*nvp);
+}
+
+static void pushFront(int v, int* arr, int& an)
+{
+ an++;
+ for (int i = an-1; i > 0; --i) arr[i] = arr[i-1];
+ arr[0] = v;
+}
+
+static void pushBack(int v, int* arr, int& an)
+{
+ arr[an] = v;
+ an++;
+}
+
+static bool removeVertex(rcPolyMesh& mesh, const unsigned short rem, const int maxTris)
+{
+ static const int nvp = mesh.nvp;
+
+ int* edges = 0;
+ int nedges = 0;
+ int* hole = 0;
+ int nhole = 0;
+ int* hreg = 0;
+ int nhreg = 0;
+ int* tris = 0;
+ int* tverts = 0;
+ int* thole = 0;
+ unsigned short* polys = 0;
+ unsigned short* pregs = 0;
+ int npolys = 0;
+
+ // Count number of polygons to remove.
+ int nrem = 0;
+ for (int i = 0; i < mesh.npolys; ++i)
+ {
+ unsigned short* p = &mesh.polys[i*nvp*2];
+ for (int j = 0; j < nvp; ++j)
+ if (p[j] == rem) { nrem++; break; }
+ }
+
+ edges = new int[nrem*nvp*3];
+ if (!edges)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_WARNING, "removeVertex: Out of memory 'edges' (%d).", nrem*nvp*3);
+ goto failure;
+ }
+
+ hole = new int[nrem*nvp];
+ if (!hole)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_WARNING, "removeVertex: Out of memory 'hole' (%d).", nrem*nvp);
+ goto failure;
+ }
+ hreg = new int[nrem*nvp];
+ if (!hreg)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_WARNING, "removeVertex: Out of memory 'hreg' (%d).", nrem*nvp);
+ goto failure;
+ }
+
+
+ for (int i = 0; i < mesh.npolys; ++i)
+ {
+ unsigned short* p = &mesh.polys[i*nvp*2];
+ const int nv = countPolyVerts(p, nvp);
+ bool hasRem = false;
+ for (int j = 0; j < nv; ++j)
+ if (p[j] == rem) hasRem = true;
+ if (hasRem)
+ {
+ // Collect edges which does not touch the removed vertex.
+ for (int j = 0, k = nv-1; j < nv; k = j++)
+ {
+ if (p[j] != rem && p[k] != rem)
+ {
+ int* e = &edges[nedges*3];
+ e[0] = p[k];
+ e[1] = p[j];
+ e[2] = mesh.regs[i];
+ nedges++;
+ }
+ }
+ // Remove the polygon.
+ unsigned short* p2 = &mesh.polys[(mesh.npolys-1)*nvp*2];
+ memcpy(p,p2,sizeof(unsigned short)*nvp);
+ mesh.regs[i] = mesh.regs[mesh.npolys-1];
+ mesh.npolys--;
+ --i;
+ }
+ }
+
+ // Remove vertex.
+ for (int i = (int)rem; i < mesh.nverts; ++i)
+ {
+ mesh.verts[i*3+0] = mesh.verts[(i+1)*3+0];
+ mesh.verts[i*3+1] = mesh.verts[(i+1)*3+1];
+ mesh.verts[i*3+2] = mesh.verts[(i+1)*3+2];
+ }
+ mesh.nverts--;
+
+ // Adjust indices to match the removed vertex layout.
+ for (int i = 0; i < mesh.npolys; ++i)
+ {
+ unsigned short* p = &mesh.polys[i*nvp*2];
+ const int nv = countPolyVerts(p, nvp);
+ for (int j = 0; j < nv; ++j)
+ if (p[j] > rem) p[j]--;
+ }
+ for (int i = 0; i < nedges; ++i)
+ {
+ if (edges[i*3+0] > rem) edges[i*3+0]--;
+ if (edges[i*3+1] > rem) edges[i*3+1]--;
+ }
+
+ if (nedges == 0)
+ return true;
+
+ hole[nhole] = edges[0];
+ hreg[nhole] = edges[2];
+ nhole++;
+
+ while (nedges)
+ {
+ bool match = false;
+
+ for (int i = 0; i < nedges; ++i)
+ {
+ const int ea = edges[i*3+0];
+ const int eb = edges[i*3+1];
+ const int r = edges[i*3+2];
+ bool add = false;
+ if (hole[0] == eb)
+ {
+ pushFront(ea, hole, nhole);
+ pushFront(r, hreg, nhreg);
+ add = true;
+ }
+ else if (hole[nhole-1] == ea)
+ {
+ pushBack(eb, hole, nhole);
+ pushBack(r, hreg, nhreg);
+ add = true;
+ }
+ if (add)
+ {
+ // Remove edge.
+ edges[i*3+0] = edges[(nedges-1)*3+0];
+ edges[i*3+1] = edges[(nedges-1)*3+1];
+ edges[i*3+2] = edges[(nedges-1)*3+2];
+ --nedges;
+ match = true;
+ --i;
+ }
+ }
+
+ if (!match)
+ break;
+ }
+
+ tris = new int[nhole*3];
+ if (!tris)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_WARNING, "removeVertex: Out of memory 'tris' (%d).", nhole*3);
+ goto failure;
+ }
+
+ tverts = new int[nhole*4];
+ if (!tverts)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_WARNING, "removeVertex: Out of memory 'tverts' (%d).", nhole*4);
+ goto failure;
+ }
+
+ thole = new int[nhole];
+ if (!tverts)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_WARNING, "removeVertex: Out of memory 'thole' (%d).", nhole);
+ goto failure;
+ }
+
+ // Generate temp vertex array for triangulation.
+ for (int i = 0; i < nhole; ++i)
+ {
+ const int pi = hole[i];
+ tverts[i*4+0] = mesh.verts[pi*3+0];
+ tverts[i*4+1] = mesh.verts[pi*3+1];
+ tverts[i*4+2] = mesh.verts[pi*3+2];
+ tverts[i*4+3] = 0;
+ thole[i] = i;
+ }
+
+ // Triangulate the hole.
+ int ntris = triangulate(nhole, &tverts[0], &thole[0], tris);
+
+ // Merge the hole triangles back to polygons.
+ polys = new unsigned short[(ntris+1)*nvp];
+ if (!polys)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_WARNING, "removeVertex: Out of memory 'polys' (%d).", (ntris+1)*nvp);
+ goto failure;
+ }
+ pregs = new unsigned short[ntris];
+ if (!pregs)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_WARNING, "removeVertex: Out of memory 'pregs' (%d).", ntris);
+ goto failure;
+ }
+
+ unsigned short* tmpPoly = &polys[ntris*nvp];
+
+ // Build initial polygons.
+ memset(polys, 0xff, ntris*nvp*sizeof(unsigned short));
+ for (int j = 0; j < ntris; ++j)
+ {
+ int* t = &tris[j*3];
+ if (t[0] != t[1] && t[0] != t[2] && t[1] != t[2])
+ {
+ polys[npolys*nvp+0] = (unsigned short)hole[t[0]];
+ polys[npolys*nvp+1] = (unsigned short)hole[t[1]];
+ polys[npolys*nvp+2] = (unsigned short)hole[t[2]];
+ pregs[npolys] = hreg[t[0]];
+ npolys++;
+ }
+ }
+ if (!npolys)
+ return true;
+
+ // Merge polygons.
+ if (nvp > 3)
+ {
+ while (true)
+ {
+ // Find best polygons to merge.
+ int bestMergeVal = 0;
+ int bestPa, bestPb, bestEa, bestEb;
+
+ for (int j = 0; j < npolys-1; ++j)
+ {
+ unsigned short* pj = &polys[j*nvp];
+ for (int k = j+1; k < npolys; ++k)
+ {
+ unsigned short* pk = &polys[k*nvp];
+ int ea, eb;
+ int v = getPolyMergeValue(pj, pk, mesh.verts, ea, eb, nvp);
+ if (v > bestMergeVal)
+ {
+ bestMergeVal = v;
+ bestPa = j;
+ bestPb = k;
+ bestEa = ea;
+ bestEb = eb;
+ }
+ }
+ }
+
+ if (bestMergeVal > 0)
+ {
+ // Found best, merge.
+ unsigned short* pa = &polys[bestPa*nvp];
+ unsigned short* pb = &polys[bestPb*nvp];
+ mergePolys(pa, pb, mesh.verts, bestEa, bestEb, tmpPoly, nvp);
+ memcpy(pb, &polys[(npolys-1)*nvp], sizeof(unsigned short)*nvp);
+ pregs[bestPb] = pregs[npolys-1];
+ npolys--;
+ }
+ else
+ {
+ // Could not merge any polygons, stop.
+ break;
+ }
+ }
+ }
+
+ // Store polygons.
+ for (int i = 0; i < npolys; ++i)
+ {
+ if (mesh.npolys >= maxTris) break;
+ unsigned short* p = &mesh.polys[mesh.npolys*nvp*2];
+ memset(p,0xff,sizeof(unsigned short)*nvp*2);
+ for (int j = 0; j < nvp; ++j)
+ p[j] = polys[i*nvp+j];
+ mesh.regs[mesh.npolys] = pregs[i];
+ mesh.npolys++;
+ }
+
+ delete [] edges;
+ delete [] hole;
+ delete [] hreg;
+ delete [] tris;
+ delete [] thole;
+ delete [] tverts;
+ delete [] polys;
+ delete [] pregs;
+
+ return true;
+
+failure:
+ delete [] edges;
+ delete [] hole;
+ delete [] hreg;
+ delete [] tris;
+ delete [] thole;
+ delete [] tverts;
+ delete [] polys;
+ delete [] pregs;
+
+ return false;
+}
+
+
+bool rcBuildPolyMesh(rcContourSet& cset, int nvp, rcPolyMesh& mesh)
+{
+ rcTimeVal startTime = rcGetPerformanceTimer();
+
+ vcopy(mesh.bmin, cset.bmin);
+ vcopy(mesh.bmax, cset.bmax);
+ mesh.cs = cset.cs;
+ mesh.ch = cset.ch;
+
+ int maxVertices = 0;
+ int maxTris = 0;
+ int maxVertsPerCont = 0;
+ for (int i = 0; i < cset.nconts; ++i)
+ {
+ maxVertices += cset.conts[i].nverts;
+ maxTris += cset.conts[i].nverts - 2;
+ maxVertsPerCont = rcMax(maxVertsPerCont, cset.conts[i].nverts);
+ }
+
+ if (maxVertices >= 0xfffe)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Too many vertices %d.", maxVertices);
+ return false;
+ }
+
+ unsigned char* vflags = 0;
+ int* nextVert = 0;
+ int* firstVert = 0;
+ int* indices = 0;
+ int* tris = 0;
+ unsigned short* polys = 0;
+
+ vflags = new unsigned char[maxVertices];
+ if (!vflags)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.verts' (%d).", maxVertices);
+ goto failure;
+ }
+ memset(vflags, 0, maxVertices);
+
+ mesh.verts = new unsigned short[maxVertices*3];
+ if (!mesh.verts)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.verts' (%d).", maxVertices);
+ goto failure;
+ }
+ mesh.polys = new unsigned short[maxTris*nvp*2];
+ if (!mesh.polys)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.polys' (%d).", maxTris*nvp*2);
+ goto failure;
+ }
+ mesh.regs = new unsigned short[maxTris];
+ if (!mesh.regs)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'mesh.regs' (%d).", maxTris);
+ goto failure;
+ }
+ mesh.nverts = 0;
+ mesh.npolys = 0;
+ mesh.nvp = nvp;
+
+ memset(mesh.verts, 0, sizeof(unsigned short)*maxVertices*3);
+ memset(mesh.polys, 0xff, sizeof(unsigned short)*maxTris*nvp*2);
+ memset(mesh.regs, 0, sizeof(unsigned short)*maxTris);
+
+ nextVert = new int[maxVertices];
+ if (!nextVert)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'nextVert' (%d).", maxVertices);
+ goto failure;
+ }
+ memset(nextVert, 0, sizeof(int)*maxVertices);
+
+ firstVert = new int[VERTEX_BUCKET_COUNT];
+ if (!firstVert)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'firstVert' (%d).", VERTEX_BUCKET_COUNT);
+ goto failure;
+ }
+ for (int i = 0; i < VERTEX_BUCKET_COUNT; ++i)
+ firstVert[i] = -1;
+
+ indices = new int[maxVertsPerCont];
+ if (!indices)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'indices' (%d).", maxVertsPerCont);
+ goto failure;
+ }
+ tris = new int[maxVertsPerCont*3];
+ if (!tris)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'tris' (%d).", maxVertsPerCont*3);
+ goto failure;
+ }
+ polys = new unsigned short[(maxVertsPerCont+1)*nvp];
+ if (!polys)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Out of memory 'polys' (%d).", maxVertsPerCont*nvp);
+ goto failure;
+ }
+ unsigned short* tmpPoly = &polys[maxVertsPerCont*nvp];
+
+ for (int i = 0; i < cset.nconts; ++i)
+ {
+ rcContour& cont = cset.conts[i];
+
+ // Skip empty contours.
+ if (cont.nverts < 3)
+ continue;
+
+ // Triangulate contour
+ for (int j = 0; j < cont.nverts; ++j)
+ indices[j] = j;
+
+ int ntris = triangulate(cont.nverts, cont.verts, &indices[0], &tris[0]);
+ if (ntris <= 0)
+ {
+ // Bad triangulation, should not happen.
+/* for (int k = 0; k < cont.nverts; ++k)
+ {
+ const int* v = &cont.verts[k*4];
+ printf("\t\t%d,%d,%d,%d,\n", v[0], v[1], v[2], v[3]);
+ if (nBadPos < 100)
+ {
+ badPos[nBadPos*3+0] = v[0];
+ badPos[nBadPos*3+1] = v[1];
+ badPos[nBadPos*3+2] = v[2];
+ nBadPos++;
+ }
+ }*/
+ ntris = -ntris;
+ }
+ // Add and merge vertices.
+ for (int j = 0; j < cont.nverts; ++j)
+ {
+ const int* v = &cont.verts[j*4];
+ indices[j] = addVertex((unsigned short)v[0], (unsigned short)v[1], (unsigned short)v[2],
+ mesh.verts, firstVert, nextVert, mesh.nverts);
+ if (v[3] & RC_BORDER_VERTEX)
+ {
+ // This vertex should be removed.
+ vflags[indices[j]] = 1;
+ }
+ }
+
+ // Build initial polygons.
+ int npolys = 0;
+ memset(polys, 0xff, maxVertsPerCont*nvp*sizeof(unsigned short));
+ for (int j = 0; j < ntris; ++j)
+ {
+ int* t = &tris[j*3];
+ if (t[0] != t[1] && t[0] != t[2] && t[1] != t[2])
+ {
+ polys[npolys*nvp+0] = (unsigned short)indices[t[0]];
+ polys[npolys*nvp+1] = (unsigned short)indices[t[1]];
+ polys[npolys*nvp+2] = (unsigned short)indices[t[2]];
+ npolys++;
+ }
+ }
+ if (!npolys)
+ continue;
+
+ // Merge polygons.
+ if (nvp > 3)
+ {
+ while (true)
+ {
+ // Find best polygons to merge.
+ int bestMergeVal = 0;
+ int bestPa, bestPb, bestEa, bestEb;
+
+ for (int j = 0; j < npolys-1; ++j)
+ {
+ unsigned short* pj = &polys[j*nvp];
+ for (int k = j+1; k < npolys; ++k)
+ {
+ unsigned short* pk = &polys[k*nvp];
+ int ea, eb;
+ int v = getPolyMergeValue(pj, pk, mesh.verts, ea, eb, nvp);
+ if (v > bestMergeVal)
+ {
+ bestMergeVal = v;
+ bestPa = j;
+ bestPb = k;
+ bestEa = ea;
+ bestEb = eb;
+ }
+ }
+ }
+
+ if (bestMergeVal > 0)
+ {
+ // Found best, merge.
+ unsigned short* pa = &polys[bestPa*nvp];
+ unsigned short* pb = &polys[bestPb*nvp];
+ mergePolys(pa, pb, mesh.verts, bestEa, bestEb, tmpPoly, nvp);
+ memcpy(pb, &polys[(npolys-1)*nvp], sizeof(unsigned short)*nvp);
+ npolys--;
+ }
+ else
+ {
+ // Could not merge any polygons, stop.
+ break;
+ }
+ }
+ }
+
+
+ // Store polygons.
+ for (int j = 0; j < npolys; ++j)
+ {
+ unsigned short* p = &mesh.polys[mesh.npolys*nvp*2];
+ unsigned short* q = &polys[j*nvp];
+ for (int k = 0; k < nvp; ++k)
+ p[k] = q[k];
+ mesh.regs[mesh.npolys] = cont.reg;
+ mesh.npolys++;
+ }
+ }
+
+
+ // Remove edge vertices.
+ for (int i = 0; i < mesh.nverts; ++i)
+ {
+ if (vflags[i])
+ {
+ if (!removeVertex(mesh, i, maxTris))
+ goto failure;
+ for (int j = i; j < mesh.nverts-1; ++j)
+ vflags[j] = vflags[j+1];
+ --i;
+ }
+ }
+
+ delete [] vflags;
+ delete [] firstVert;
+ delete [] nextVert;
+ delete [] indices;
+ delete [] tris;
+
+ // Calculate adjacency.
+ if (!buildMeshAdjacency(mesh.polys, mesh.npolys, mesh.nverts, nvp))
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMesh: Adjacency failed.");
+ return false;
+ }
+
+ rcTimeVal endTime = rcGetPerformanceTimer();
+
+// if (rcGetLog())
+// rcGetLog()->log(RC_LOG_PROGRESS, "Build polymesh: %.3f ms", rcGetDeltaTimeUsec(startTime, endTime)/1000.0f);
+ if (rcGetBuildTimes())
+ rcGetBuildTimes()->buildPolymesh += rcGetDeltaTimeUsec(startTime, endTime);
+
+ return true;
+
+failure:
+ delete [] vflags;
+ delete [] tmpPoly;
+ delete [] firstVert;
+ delete [] nextVert;
+ delete [] indices;
+ delete [] tris;
+
+ return false;
+}
+
+bool rcMergePolyMeshes(rcPolyMesh** meshes, const int nmeshes, rcPolyMesh& mesh)
+{
+ if (!nmeshes || !meshes)
+ return true;
+
+ rcTimeVal startTime = rcGetPerformanceTimer();
+
+ int* nextVert = 0;
+ int* firstVert = 0;
+ unsigned short* vremap = 0;
+
+ mesh.nvp = meshes[0]->nvp;
+ mesh.cs = meshes[0]->cs;
+ mesh.ch = meshes[0]->ch;
+ vcopy(mesh.bmin, meshes[0]->bmin);
+ vcopy(mesh.bmax, meshes[0]->bmax);
+
+ int maxVerts = 0;
+ int maxPolys = 0;
+ int maxVertsPerMesh = 0;
+ for (int i = 0; i < nmeshes; ++i)
+ {
+ vmin(mesh.bmin, meshes[i]->bmin);
+ vmax(mesh.bmax, meshes[i]->bmax);
+ maxVertsPerMesh = rcMax(maxVertsPerMesh, meshes[i]->nverts);
+ maxVerts += meshes[i]->nverts;
+ maxPolys += meshes[i]->npolys;
+ }
+
+ mesh.nverts = 0;
+ mesh.verts = new unsigned short[maxVerts*3];
+ if (!mesh.verts)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'mesh.verts' (%d).", maxVerts*3);
+ return false;
+ }
+
+ mesh.npolys = 0;
+ mesh.polys = new unsigned short[maxPolys*2*mesh.nvp];
+ if (!mesh.polys)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'mesh.polys' (%d).", maxPolys*2*mesh.nvp);
+ return false;
+ }
+ memset(mesh.polys, 0xff, sizeof(unsigned short)*maxPolys*2*mesh.nvp);
+
+ mesh.regs = new unsigned short[maxPolys];
+ if (!mesh.regs)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'mesh.regs' (%d).", maxPolys);
+ return false;
+ }
+ memset(mesh.regs, 0, sizeof(unsigned short)*maxPolys);
+
+ nextVert = new int[maxVerts];
+ if (!nextVert)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'nextVert' (%d).", maxVerts);
+ goto failure;
+ }
+ memset(nextVert, 0, sizeof(int)*maxVerts);
+
+ firstVert = new int[VERTEX_BUCKET_COUNT];
+ if (!firstVert)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'firstVert' (%d).", VERTEX_BUCKET_COUNT);
+ goto failure;
+ }
+ for (int i = 0; i < VERTEX_BUCKET_COUNT; ++i)
+ firstVert[i] = -1;
+
+ vremap = new unsigned short[maxVertsPerMesh];
+ if (!vremap)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcMergePolyMeshes: Out of memory 'vremap' (%d).", maxVertsPerMesh);
+ goto failure;
+ }
+ memset(nextVert, 0, sizeof(int)*maxVerts);
+
+ for (int i = 0; i < nmeshes; ++i)
+ {
+ const rcPolyMesh* pmesh = meshes[i];
+
+ const unsigned short ox = (unsigned short)floorf((pmesh->bmin[0]-mesh.bmin[0])/mesh.cs+0.5f);
+ const unsigned short oz = (unsigned short)floorf((pmesh->bmin[2]-mesh.bmin[2])/mesh.cs+0.5f);
+
+ for (int j = 0; j < pmesh->nverts; ++j)
+ {
+ unsigned short* v = &pmesh->verts[j*3];
+ vremap[j] = addVertex(v[0]+ox, v[1], v[2]+oz,
+ mesh.verts, firstVert, nextVert, mesh.nverts);
+ }
+
+ for (int j = 0; j < pmesh->npolys; ++j)
+ {
+ unsigned short* tgt = &mesh.polys[mesh.npolys*2*mesh.nvp];
+ unsigned short* src = &pmesh->polys[j*2*mesh.nvp];
+ mesh.regs[mesh.npolys] = pmesh->regs[j];
+ mesh.npolys++;
+ for (int k = 0; k < mesh.nvp; ++k)
+ {
+ if (src[k] == 0xffff) break;
+ tgt[k] = vremap[src[k]];
+ }
+ }
+ }
+
+ // Calculate adjacency.
+ if (!buildMeshAdjacency(mesh.polys, mesh.npolys, mesh.nverts, mesh.nvp))
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcMergePolyMeshes: Adjacency failed.");
+ return false;
+ }
+
+
+ delete [] firstVert;
+ delete [] nextVert;
+ delete [] vremap;
+
+ rcTimeVal endTime = rcGetPerformanceTimer();
+
+ if (rcGetBuildTimes())
+ rcGetBuildTimes()->mergePolyMesh += rcGetDeltaTimeUsec(startTime, endTime);
+
+ return true;
+
+failure:
+ delete [] firstVert;
+ delete [] nextVert;
+ delete [] vremap;
+
+ return false;
+}
diff --git a/extern/recastnavigation/Recast/Source/RecastMeshDetail.cpp b/extern/recastnavigation/Recast/Source/RecastMeshDetail.cpp
new file mode 100644
index 00000000000..ef17531d7bc
--- /dev/null
+++ b/extern/recastnavigation/Recast/Source/RecastMeshDetail.cpp
@@ -0,0 +1,981 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#include <float.h>
+#define _USE_MATH_DEFINES
+#include <math.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include "Recast.h"
+#include "RecastLog.h"
+#include "RecastTimer.h"
+
+
+struct rcHeightPatch
+{
+ inline rcHeightPatch() : data(0) {}
+ inline ~rcHeightPatch() { delete [] data; }
+ unsigned short* data;
+ int xmin, ymin, width, height;
+};
+
+
+static int circumCircle(const float xp, const float yp,
+ const float x1, const float y1,
+ const float x2, const float y2,
+ const float x3, const float y3,
+ float& xc, float& yc, float& rsqr)
+{
+ static const float EPSILON = 1e-6f;
+
+ const float fabsy1y2 = rcAbs(y1-y2);
+ const float fabsy2y3 = rcAbs(y2-y3);
+
+ /* Check for coincident points */
+ if (fabsy1y2 < EPSILON && fabsy2y3 < EPSILON)
+ return 0;
+
+ if (fabsy1y2 < EPSILON)
+ {
+ const float m2 = - (x3-x2) / (y3-y2);
+ const float mx2 = (x2 + x3) / 2.0f;
+ const float my2 = (y2 + y3) / 2.0f;
+ xc = (x2 + x1) / 2.0f;
+ yc = m2 * (xc - mx2) + my2;
+ }
+ else if (fabsy2y3 < EPSILON)
+ {
+ const float m1 = - (x2-x1) / (y2-y1);
+ const float mx1 = (x1 + x2) / 2.0f;
+ const float my1 = (y1 + y2) / 2.0f;
+ xc = (x3 + x2) / 2.0f;
+ yc = m1 * (xc - mx1) + my1;
+ }
+ else
+ {
+ const float m1 = - (x2-x1) / (y2-y1);
+ const float m2 = - (x3-x2) / (y3-y2);
+ const float mx1 = (x1 + x2) / 2.0f;
+ const float mx2 = (x2 + x3) / 2.0f;
+ const float my1 = (y1 + y2) / 2.0f;
+ const float my2 = (y2 + y3) / 2.0f;
+ xc = (m1 * mx1 - m2 * mx2 + my2 - my1) / (m1 - m2);
+ if (fabsy1y2 > fabsy2y3)
+ yc = m1 * (xc - mx1) + my1;
+ else
+ yc = m2 * (xc - mx2) + my2;
+ }
+
+ float dx,dy;
+
+ dx = x2 - xc;
+ dy = y2 - yc;
+ rsqr = dx*dx + dy*dy;
+
+ dx = xp - xc;
+ dy = yp - yc;
+ const float drsqr = dx*dx + dy*dy;
+
+ return (drsqr <= rsqr) ? 1 : 0;
+}
+
+static int ptcmp(void* up, const void *v1, const void *v2)
+{
+ const float* verts = (const float*)up;
+ const float* p1 = &verts[(*(const int*)v1)*3];
+ const float* p2 = &verts[(*(const int*)v2)*3];
+ if (p1[0] < p2[0])
+ return -1;
+ else if (p1[0] > p2[0])
+ return 1;
+ else
+ return 0;
+}
+
+// Based on Paul Bourke's triangulate.c
+// http://astronomy.swin.edu.au/~pbourke/terrain/triangulate/triangulate.c
+static void delaunay(const int nv, float *verts, rcIntArray& idx, rcIntArray& tris, rcIntArray& edges)
+{
+ // Sort vertices
+ idx.resize(nv);
+ for (int i = 0; i < nv; ++i)
+ idx[i] = i;
+#ifdef WIN32
+ qsort_s(&idx[0], idx.size(), sizeof(int), ptcmp, verts);
+#else
+ qsort_r(&idx[0], idx.size(), sizeof(int), verts, ptcmp);
+#endif
+
+ // Find the maximum and minimum vertex bounds.
+ // This is to allow calculation of the bounding triangle
+ float xmin = verts[0];
+ float ymin = verts[2];
+ float xmax = xmin;
+ float ymax = ymin;
+ for (int i = 1; i < nv; ++i)
+ {
+ xmin = rcMin(xmin, verts[i*3+0]);
+ xmax = rcMax(xmax, verts[i*3+0]);
+ ymin = rcMin(ymin, verts[i*3+2]);
+ ymax = rcMax(ymax, verts[i*3+2]);
+ }
+ float dx = xmax - xmin;
+ float dy = ymax - ymin;
+ float dmax = (dx > dy) ? dx : dy;
+ float xmid = (xmax + xmin) / 2.0f;
+ float ymid = (ymax + ymin) / 2.0f;
+
+ // Set up the supertriangle
+ // This is a triangle which encompasses all the sample points.
+ // The supertriangle coordinates are added to the end of the
+ // vertex list. The supertriangle is the first triangle in
+ // the triangle list.
+ float sv[3*3];
+
+ sv[0] = xmid - 20 * dmax;
+ sv[1] = 0;
+ sv[2] = ymid - dmax;
+
+ sv[3] = xmid;
+ sv[4] = 0;
+ sv[5] = ymid + 20 * dmax;
+
+ sv[6] = xmid + 20 * dmax;
+ sv[7] = 0;
+ sv[8] = ymid - dmax;
+
+ tris.push(-3);
+ tris.push(-2);
+ tris.push(-1);
+ tris.push(0); // not completed
+
+ for (int i = 0; i < nv; ++i)
+ {
+ const float xp = verts[idx[i]*3+0];
+ const float yp = verts[idx[i]*3+2];
+
+ edges.resize(0);
+
+ // Set up the edge buffer.
+ // If the point (xp,yp) lies inside the circumcircle then the
+ // three edges of that triangle are added to the edge buffer
+ // and that triangle is removed.
+ for (int j = 0; j < tris.size()/4; ++j)
+ {
+ int* t = &tris[j*4];
+ if (t[3]) // completed?
+ continue;
+ const float* v1 = t[0] < 0 ? &sv[(t[0]+3)*3] : &verts[idx[t[0]]*3];
+ const float* v2 = t[1] < 0 ? &sv[(t[1]+3)*3] : &verts[idx[t[1]]*3];
+ const float* v3 = t[2] < 0 ? &sv[(t[2]+3)*3] : &verts[idx[t[2]]*3];
+ float xc,yc,rsqr;
+ int inside = circumCircle(xp,yp, v1[0],v1[2], v2[0],v2[2], v3[0],v3[2], xc,yc,rsqr);
+ if (xc < xp && rcSqr(xp-xc) > rsqr)
+ t[3] = 1;
+ if (inside)
+ {
+ // Collect triangle edges.
+ edges.push(t[0]);
+ edges.push(t[1]);
+ edges.push(t[1]);
+ edges.push(t[2]);
+ edges.push(t[2]);
+ edges.push(t[0]);
+ // Remove triangle j.
+ t[0] = tris[tris.size()-4];
+ t[1] = tris[tris.size()-3];
+ t[2] = tris[tris.size()-2];
+ t[3] = tris[tris.size()-1];
+ tris.resize(tris.size()-4);
+ j--;
+ }
+ }
+
+ // Remove duplicate edges.
+ const int ne = edges.size()/2;
+ for (int j = 0; j < ne-1; ++j)
+ {
+ for (int k = j+1; k < ne; ++k)
+ {
+ // Dupe?, make null.
+ if ((edges[j*2+0] == edges[k*2+1]) && (edges[j*2+1] == edges[k*2+0]))
+ {
+ edges[j*2+0] = 0;
+ edges[j*2+1] = 0;
+ edges[k*2+0] = 0;
+ edges[k*2+1] = 0;
+ }
+ }
+ }
+
+ // Form new triangles for the current point
+ // Skipping over any null.
+ // All edges are arranged in clockwise order.
+ for (int j = 0; j < ne; ++j)
+ {
+ if (edges[j*2+0] == edges[j*2+1]) continue;
+ tris.push(edges[j*2+0]);
+ tris.push(edges[j*2+1]);
+ tris.push(i);
+ tris.push(0); // not completed
+ }
+ }
+
+ // Remove triangles with supertriangle vertices
+ // These are triangles which have a vertex number greater than nv
+ for (int i = 0; i < tris.size()/4; ++i)
+ {
+ int* t = &tris[i*4];
+ if (t[0] < 0 || t[1] < 0 || t[2] < 0)
+ {
+ t[0] = tris[tris.size()-4];
+ t[1] = tris[tris.size()-3];
+ t[2] = tris[tris.size()-2];
+ t[3] = tris[tris.size()-1];
+ tris.resize(tris.size()-4);
+ i--;
+ }
+ }
+ // Triangle vertices are pointing to sorted vertices, remap indices.
+ for (int i = 0; i < tris.size(); ++i)
+ tris[i] = idx[tris[i]];
+}
+
+inline float vdot2(const float* a, const float* b)
+{
+ return a[0]*b[0] + a[2]*b[2];
+}
+
+static float distPtTri(const float* p, const float* a, const float* b, const float* c)
+{
+ float v0[3], v1[3], v2[3];
+ vsub(v0, c,a);
+ vsub(v1, b,a);
+ vsub(v2, p,a);
+
+ const float dot00 = vdot2(v0, v0);
+ const float dot01 = vdot2(v0, v1);
+ const float dot02 = vdot2(v0, v2);
+ const float dot11 = vdot2(v1, v1);
+ const float dot12 = vdot2(v1, v2);
+
+ // Compute barycentric coordinates
+ float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01);
+ float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
+ float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
+
+ // If point lies inside the triangle, return interpolated y-coord.
+ static const float EPS = 1e-4f;
+ if (u >= -EPS && v >= -EPS && (u+v) <= 1+EPS)
+ {
+ float y = a[1] + v0[1]*u + v1[1]*v;
+ return fabsf(y-p[1]);
+ }
+ return FLT_MAX;
+}
+
+static float distancePtSeg(const float* pt, const float* p, const float* q)
+{
+ float pqx = q[0] - p[0];
+ float pqy = q[1] - p[1];
+ float pqz = q[2] - p[2];
+ float dx = pt[0] - p[0];
+ float dy = pt[1] - p[1];
+ float dz = pt[2] - p[2];
+ float d = pqx*pqx + pqy*pqy + pqz*pqz;
+ float t = pqx*dx + pqy*dy + pqz*dz;
+ if (d > 0)
+ t /= d;
+ if (t < 0)
+ t = 0;
+ else if (t > 1)
+ t = 1;
+
+ dx = p[0] + t*pqx - pt[0];
+ dy = p[1] + t*pqy - pt[1];
+ dz = p[2] + t*pqz - pt[2];
+
+ return dx*dx + dy*dy + dz*dz;
+}
+
+static float distancePtSeg2d(const float* pt, const float* p, const float* q)
+{
+ float pqx = q[0] - p[0];
+ float pqz = q[2] - p[2];
+ float dx = pt[0] - p[0];
+ float dz = pt[2] - p[2];
+ float d = pqx*pqx + pqz*pqz;
+ float t = pqx*dx + pqz*dz;
+ if (d > 0)
+ t /= d;
+ if (t < 0)
+ t = 0;
+ else if (t > 1)
+ t = 1;
+
+ dx = p[0] + t*pqx - pt[0];
+ dz = p[2] + t*pqz - pt[2];
+
+ return dx*dx + dz*dz;
+}
+
+static float distToTriMesh(const float* p, const float* verts, int nverts, const int* tris, int ntris)
+{
+ float dmin = FLT_MAX;
+ for (int i = 0; i < ntris; ++i)
+ {
+ const float* va = &verts[tris[i*4+0]*3];
+ const float* vb = &verts[tris[i*4+1]*3];
+ const float* vc = &verts[tris[i*4+2]*3];
+ float d = distPtTri(p, va,vb,vc);
+ if (d < dmin)
+ dmin = d;
+ }
+ if (dmin == FLT_MAX) return -1;
+ return dmin;
+}
+
+static float distToPoly(int nvert, const float* verts, const float* p)
+{
+
+ float dmin = FLT_MAX;
+ int i, j, c = 0;
+ for (i = 0, j = nvert-1; i < nvert; j = i++)
+ {
+ const float* vi = &verts[i*3];
+ const float* vj = &verts[j*3];
+ if (((vi[2] > p[2]) != (vj[2] > p[2])) &&
+ (p[0] < (vj[0]-vi[0]) * (p[2]-vi[2]) / (vj[2]-vi[2]) + vi[0]) )
+ c = !c;
+ dmin = rcMin(dmin, distancePtSeg2d(p, vj, vi));
+ }
+ return c ? -dmin : dmin;
+}
+
+
+static unsigned short getHeight(const float* pos, const float* bmin, const float ics, const rcHeightPatch& hp)
+{
+ int ix = (int)floorf((pos[0]-bmin[0])*ics + 0.01f);
+ int iz = (int)floorf((pos[2]-bmin[2])*ics + 0.01f);
+ ix = rcClamp(ix-hp.xmin, 0, hp.width);
+ iz = rcClamp(iz-hp.ymin, 0, hp.height);
+ unsigned short h = hp.data[ix+iz*hp.width];
+ return h;
+}
+
+static bool buildPolyDetail(const float* in, const int nin, unsigned short reg,
+ const float sampleDist, const float sampleMaxError,
+ const rcCompactHeightfield& chf, const rcHeightPatch& hp,
+ float* verts, int& nverts, rcIntArray& tris,
+ rcIntArray& edges, rcIntArray& idx, rcIntArray& samples)
+{
+ static const int MAX_VERTS = 256;
+ static const int MAX_EDGE = 64;
+ float edge[(MAX_EDGE+1)*3];
+
+ nverts = 0;
+
+ for (int i = 0; i < nin; ++i)
+ vcopy(&verts[i*3], &in[i*3]);
+ nverts = nin;
+
+ const float ics = 1.0f/chf.cs;
+
+ // Tesselate outlines.
+ // This is done in separate pass in order to ensure
+ // seamless height values across the ply boundaries.
+ if (sampleDist > 0)
+ {
+ for (int i = 0, j = nin-1; i < nin; j=i++)
+ {
+ const float* vj = &in[j*3];
+ const float* vi = &in[i*3];
+ // Make sure the segments are always handled in same order
+ // using lexological sort or else there will be seams.
+ if (fabsf(vj[0]-vi[0]) < 1e-6f)
+ {
+ if (vj[2] > vi[2])
+ rcSwap(vj,vi);
+ }
+ else
+ {
+ if (vj[0] > vi[0])
+ rcSwap(vj,vi);
+ }
+ // Create samples along the edge.
+ float dx = vi[0] - vj[0];
+ float dy = vi[1] - vj[1];
+ float dz = vi[2] - vj[2];
+ float d = sqrtf(dx*dx + dz*dz);
+ int nn = 1 + (int)floorf(d/sampleDist);
+ if (nn > MAX_EDGE) nn = MAX_EDGE;
+ if (nverts+nn >= MAX_VERTS)
+ nn = MAX_VERTS-1-nverts;
+ for (int k = 0; k <= nn; ++k)
+ {
+ float u = (float)k/(float)nn;
+ float* pos = &edge[k*3];
+ pos[0] = vj[0] + dx*u;
+ pos[1] = vj[1] + dy*u;
+ pos[2] = vj[2] + dz*u;
+ pos[1] = chf.bmin[1] + getHeight(pos, chf.bmin, ics, hp)*chf.ch;
+ }
+ // Simplify samples.
+ int idx[MAX_EDGE] = {0,nn};
+ int nidx = 2;
+ for (int k = 0; k < nidx-1; )
+ {
+ const int a = idx[k];
+ const int b = idx[k+1];
+ const float* va = &edge[a*3];
+ const float* vb = &edge[b*3];
+ // Find maximum deviation along the segment.
+ float maxd = 0;
+ int maxi = -1;
+ for (int m = a+1; m < b; ++m)
+ {
+ float d = distancePtSeg(&edge[m*3],va,vb);
+ if (d > maxd)
+ {
+ maxd = d;
+ maxi = m;
+ }
+ }
+ // If the max deviation is larger than accepted error,
+ // add new point, else continue to next segment.
+ if (maxi != -1 && maxd > rcSqr(sampleMaxError))
+ {
+ for (int m = nidx; m > k; --m)
+ idx[m] = idx[m-1];
+ idx[k+1] = maxi;
+ nidx++;
+ }
+ else
+ {
+ ++k;
+ }
+ }
+ // Add new vertices.
+ for (int k = 1; k < nidx-1; ++k)
+ {
+ vcopy(&verts[nverts*3], &edge[idx[k]*3]);
+ nverts++;
+ }
+ }
+ }
+
+ // Tesselate the base mesh.
+ edges.resize(0);
+ tris.resize(0);
+ idx.resize(0);
+ delaunay(nverts, verts, idx, tris, edges);
+
+ if (sampleDist > 0)
+ {
+ // Create sample locations in a grid.
+ float bmin[3], bmax[3];
+ vcopy(bmin, in);
+ vcopy(bmax, in);
+ for (int i = 1; i < nin; ++i)
+ {
+ vmin(bmin, &in[i*3]);
+ vmax(bmax, &in[i*3]);
+ }
+ int x0 = (int)floorf(bmin[0]/sampleDist);
+ int x1 = (int)ceilf(bmax[0]/sampleDist);
+ int z0 = (int)floorf(bmin[2]/sampleDist);
+ int z1 = (int)ceilf(bmax[2]/sampleDist);
+ samples.resize(0);
+ for (int z = z0; z < z1; ++z)
+ {
+ for (int x = x0; x < x1; ++x)
+ {
+ float pt[3];
+ pt[0] = x*sampleDist;
+ pt[2] = z*sampleDist;
+ // Make sure the samples are not too close to the edges.
+ if (distToPoly(nin,in,pt) > -sampleDist/2) continue;
+ samples.push(x);
+ samples.push(getHeight(pt, chf.bmin, ics, hp));
+ samples.push(z);
+ }
+ }
+
+ // Add the samples starting from the one that has the most
+ // error. The procedure stops when all samples are added
+ // or when the max error is within treshold.
+ const int nsamples = samples.size()/3;
+ for (int iter = 0; iter < nsamples; ++iter)
+ {
+ // Find sample with most error.
+ float bestpt[3];
+ float bestd = 0;
+ for (int i = 0; i < nsamples; ++i)
+ {
+ float pt[3];
+ pt[0] = samples[i*3+0]*sampleDist;
+ pt[1] = chf.bmin[1] + samples[i*3+1]*chf.ch;
+ pt[2] = samples[i*3+2]*sampleDist;
+ float d = distToTriMesh(pt, verts, nverts, &tris[0], tris.size()/4);
+ if (d < 0) continue; // did not hit the mesh.
+ if (d > bestd)
+ {
+ bestd = d;
+ vcopy(bestpt,pt);
+ }
+ }
+ // If the max error is within accepted threshold, stop tesselating.
+ if (bestd <= sampleMaxError)
+ break;
+
+ // Add the new sample point.
+ vcopy(&verts[nverts*3],bestpt);
+ nverts++;
+
+ // Create new triangulation.
+ // TODO: Incremental add instead of full rebuild.
+ edges.resize(0);
+ tris.resize(0);
+ idx.resize(0);
+ delaunay(nverts, verts, idx, tris, edges);
+
+ if (nverts >= MAX_VERTS)
+ break;
+ }
+ }
+
+ return true;
+}
+
+static void getHeightData(const rcCompactHeightfield& chf,
+ const unsigned short* poly, const int npoly,
+ const unsigned short* verts,
+ rcHeightPatch& hp, rcIntArray& stack)
+{
+ // Floodfill the heightfield to get 2D height data,
+ // starting at vertex locations as seeds.
+
+ memset(hp.data, 0xff, sizeof(unsigned short)*hp.width*hp.height);
+
+ stack.resize(0);
+
+ // Use poly vertices as seed points for the flood fill.
+ for (int j = 0; j < npoly; ++j)
+ {
+ const int ax = (int)verts[poly[j]*3+0];
+ const int ay = (int)verts[poly[j]*3+1];
+ const int az = (int)verts[poly[j]*3+2];
+ if (ax < hp.xmin || ax >= hp.xmin+hp.width ||
+ az < hp.ymin || az >= hp.ymin+hp.height)
+ continue;
+
+ const rcCompactCell& c = chf.cells[ax+az*chf.width];
+ int dmin = 0xffff;
+ int ai = -1;
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
+ {
+ const rcCompactSpan& s = chf.spans[i];
+ int d = rcAbs(ay - (int)s.y);
+ if (d < dmin)
+ {
+ ai = i;
+ dmin = d;
+ }
+ }
+ if (ai != -1)
+ {
+ stack.push(ax);
+ stack.push(az);
+ stack.push(ai);
+ }
+ }
+
+ while (stack.size() > 0)
+ {
+ int ci = stack.pop();
+ int cy = stack.pop();
+ int cx = stack.pop();
+
+ // Skip already visited locations.
+ int idx = cx-hp.xmin+(cy-hp.ymin)*hp.width;
+ if (hp.data[idx] != 0xffff)
+ continue;
+
+ const rcCompactSpan& cs = chf.spans[ci];
+ hp.data[idx] = cs.y;
+
+ for (int dir = 0; dir < 4; ++dir)
+ {
+ if (rcGetCon(cs, dir) == 0xf) continue;
+
+ const int ax = cx + rcGetDirOffsetX(dir);
+ const int ay = cy + rcGetDirOffsetY(dir);
+
+ if (ax < hp.xmin || ax >= (hp.xmin+hp.width) ||
+ ay < hp.ymin || ay >= (hp.ymin+hp.height))
+ continue;
+
+ if (hp.data[ax-hp.xmin+(ay-hp.ymin)*hp.width] != 0xffff)
+ continue;
+
+ const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(cs, dir);
+
+ stack.push(ax);
+ stack.push(ay);
+ stack.push(ai);
+ }
+ }
+}
+
+static unsigned char getEdgeFlags(const float* va, const float* vb,
+ const float* vpoly, const int npoly)
+{
+ // Return true if edge (va,vb) is part of the polygon.
+ static const float thrSqr = rcSqr(0.001f);
+ for (int i = 0, j = npoly-1; i < npoly; j=i++)
+ {
+ if (distancePtSeg2d(va, &vpoly[j*3], &vpoly[i*3]) < thrSqr &&
+ distancePtSeg2d(vb, &vpoly[j*3], &vpoly[i*3]) < thrSqr)
+ return 1;
+ }
+ return 0;
+}
+
+static unsigned char getTriFlags(const float* va, const float* vb, const float* vc,
+ const float* vpoly, const int npoly)
+{
+ unsigned char flags = 0;
+ flags |= getEdgeFlags(va,vb,vpoly,npoly) << 0;
+ flags |= getEdgeFlags(vb,vc,vpoly,npoly) << 2;
+ flags |= getEdgeFlags(vc,va,vpoly,npoly) << 4;
+ return flags;
+}
+
+
+
+bool rcBuildPolyMeshDetail(const rcPolyMesh& mesh, const rcCompactHeightfield& chf,
+ const float sampleDist, const float sampleMaxError,
+ rcPolyMeshDetail& dmesh)
+{
+ rcTimeVal startTime = rcGetPerformanceTimer();
+
+ if (mesh.nverts == 0 || mesh.npolys == 0)
+ return true;
+
+ const int nvp = mesh.nvp;
+ const float cs = mesh.cs;
+ const float ch = mesh.ch;
+ const float* orig = mesh.bmin;
+
+ rcIntArray edges(64);
+ rcIntArray tris(512);
+ rcIntArray idx(512);
+ rcIntArray stack(512);
+ rcIntArray samples(512);
+ float verts[256*3];
+ float* poly = 0;
+ int* bounds = 0;
+ rcHeightPatch hp;
+ int nPolyVerts = 0;
+ int maxhw = 0, maxhh = 0;
+
+ bounds = new int[mesh.npolys*4];
+ if (!bounds)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'bounds' (%d).", mesh.npolys*4);
+ goto failure;
+ }
+ poly = new float[nvp*3];
+ if (!bounds)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'poly' (%d).", nvp*3);
+ goto failure;
+ }
+
+ // Find max size for a polygon area.
+ for (int i = 0; i < mesh.npolys; ++i)
+ {
+ const unsigned short* p = &mesh.polys[i*nvp*2];
+ int& xmin = bounds[i*4+0];
+ int& xmax = bounds[i*4+1];
+ int& ymin = bounds[i*4+2];
+ int& ymax = bounds[i*4+3];
+ xmin = chf.width;
+ xmax = 0;
+ ymin = chf.height;
+ ymax = 0;
+ for (int j = 0; j < nvp; ++j)
+ {
+ if(p[j] == 0xffff) break;
+ const unsigned short* v = &mesh.verts[p[j]*3];
+ xmin = rcMin(xmin, (int)v[0]);
+ xmax = rcMax(xmax, (int)v[0]);
+ ymin = rcMin(ymin, (int)v[2]);
+ ymax = rcMax(ymax, (int)v[2]);
+ nPolyVerts++;
+ }
+ xmin = rcMax(0,xmin-1);
+ xmax = rcMin(chf.width,xmax+1);
+ ymin = rcMax(0,ymin-1);
+ ymax = rcMin(chf.height,ymax+1);
+ if (xmin >= xmax || ymin >= ymax) continue;
+ maxhw = rcMax(maxhw, xmax-xmin);
+ maxhh = rcMax(maxhh, ymax-ymin);
+ }
+
+ hp.data = new unsigned short[maxhw*maxhh];
+ if (!hp.data)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'hp.data' (%d).", maxhw*maxhh);
+ goto failure;
+ }
+
+ dmesh.nmeshes = mesh.npolys;
+ dmesh.nverts = 0;
+ dmesh.ntris = 0;
+ dmesh.meshes = new unsigned short[dmesh.nmeshes*4];
+ if (!dmesh.meshes)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.meshes' (%d).", dmesh.nmeshes*4);
+ goto failure;
+ }
+
+ int vcap = nPolyVerts+nPolyVerts/2;
+ int tcap = vcap*2;
+
+ dmesh.nverts = 0;
+ dmesh.verts = new float[vcap*3];
+ if (!dmesh.verts)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.verts' (%d).", vcap*3);
+ goto failure;
+ }
+ dmesh.ntris = 0;
+ dmesh.tris = new unsigned char[tcap*4];
+ if (!dmesh.tris)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.tris' (%d).", tcap*4);
+ goto failure;
+ }
+
+ for (int i = 0; i < mesh.npolys; ++i)
+ {
+ const unsigned short* p = &mesh.polys[i*nvp*2];
+
+ // Find polygon bounding box.
+ int npoly = 0;
+ for (int j = 0; j < nvp; ++j)
+ {
+ if(p[j] == 0xffff) break;
+ const unsigned short* v = &mesh.verts[p[j]*3];
+ poly[j*3+0] = orig[0] + v[0]*cs;
+ poly[j*3+1] = orig[1] + v[1]*ch;
+ poly[j*3+2] = orig[2] + v[2]*cs;
+ npoly++;
+ }
+
+ // Get the height data from the area of the polygon.
+ hp.xmin = bounds[i*4+0];
+ hp.ymin = bounds[i*4+2];
+ hp.width = bounds[i*4+1]-bounds[i*4+0];
+ hp.height = bounds[i*4+3]-bounds[i*4+2];
+ getHeightData(chf, p, npoly, mesh.verts, hp, stack);
+
+ // Build detail mesh.
+ int nverts = 0;
+ if (!buildPolyDetail(poly, npoly, mesh.regs[i],
+ sampleDist, sampleMaxError,
+ chf, hp, verts, nverts, tris,
+ edges, idx, samples))
+ {
+ goto failure;
+ }
+
+ // Offset detail vertices, unnecassary?
+ for (int j = 0; j < nverts; ++j)
+ verts[j*3+1] += chf.ch;
+
+ // Store detail submesh.
+ const int ntris = tris.size()/4;
+
+ dmesh.meshes[i*4+0] = dmesh.nverts;
+ dmesh.meshes[i*4+1] = (unsigned short)nverts;
+ dmesh.meshes[i*4+2] = dmesh.ntris;
+ dmesh.meshes[i*4+3] = (unsigned short)ntris;
+
+ // Store vertices, allocate more memory if necessary.
+ if (dmesh.nverts+nverts > vcap)
+ {
+ while (dmesh.nverts+nverts > vcap)
+ vcap += 256;
+
+ float* newv = new float[vcap*3];
+ if (!newv)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'newv' (%d).", vcap*3);
+ goto failure;
+ }
+ if (dmesh.nverts)
+ memcpy(newv, dmesh.verts, sizeof(float)*3*dmesh.nverts);
+ delete [] dmesh.verts;
+ dmesh.verts = newv;
+ }
+ for (int j = 0; j < nverts; ++j)
+ {
+ dmesh.verts[dmesh.nverts*3+0] = verts[j*3+0];
+ dmesh.verts[dmesh.nverts*3+1] = verts[j*3+1];
+ dmesh.verts[dmesh.nverts*3+2] = verts[j*3+2];
+ dmesh.nverts++;
+ }
+
+ // Store triangles, allocate more memory if necessary.
+ if (dmesh.ntris+ntris > tcap)
+ {
+ while (dmesh.ntris+ntris > tcap)
+ tcap += 256;
+ unsigned char* newt = new unsigned char[tcap*4];
+ if (!newt)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'newt' (%d).", tcap*4);
+ goto failure;
+ }
+ if (dmesh.ntris)
+ memcpy(newt, dmesh.tris, sizeof(unsigned char)*4*dmesh.ntris);
+ delete [] dmesh.tris;
+ dmesh.tris = newt;
+ }
+ for (int j = 0; j < ntris; ++j)
+ {
+ const int* t = &tris[j*4];
+ dmesh.tris[dmesh.ntris*4+0] = (unsigned char)t[0];
+ dmesh.tris[dmesh.ntris*4+1] = (unsigned char)t[1];
+ dmesh.tris[dmesh.ntris*4+2] = (unsigned char)t[2];
+ dmesh.tris[dmesh.ntris*4+3] = getTriFlags(&verts[t[0]*3], &verts[t[1]*3], &verts[t[2]*3], poly, npoly);
+ dmesh.ntris++;
+ }
+ }
+
+ delete [] bounds;
+ delete [] poly;
+
+ rcTimeVal endTime = rcGetPerformanceTimer();
+
+ if (rcGetBuildTimes())
+ rcGetBuildTimes()->buildDetailMesh += rcGetDeltaTimeUsec(startTime, endTime);
+
+ return true;
+
+failure:
+
+ delete [] bounds;
+ delete [] poly;
+
+ return false;
+}
+
+bool rcMergePolyMeshDetails(rcPolyMeshDetail** meshes, const int nmeshes, rcPolyMeshDetail& mesh)
+{
+ rcTimeVal startTime = rcGetPerformanceTimer();
+
+ int maxVerts = 0;
+ int maxTris = 0;
+ int maxMeshes = 0;
+
+ for (int i = 0; i < nmeshes; ++i)
+ {
+ if (!meshes[i]) continue;
+ maxVerts += meshes[i]->nverts;
+ maxTris += meshes[i]->ntris;
+ maxMeshes += meshes[i]->nmeshes;
+ }
+
+ mesh.nmeshes = 0;
+ mesh.meshes = new unsigned short[maxMeshes*4];
+ if (!mesh.meshes)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'pmdtl.meshes' (%d).", maxMeshes*4);
+ return false;
+ }
+
+ mesh.ntris = 0;
+ mesh.tris = new unsigned char[maxTris*4];
+ if (!mesh.tris)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.tris' (%d).", maxTris*4);
+ return false;
+ }
+
+ mesh.nverts = 0;
+ mesh.verts = new float[maxVerts*3];
+ if (!mesh.verts)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildPolyMeshDetail: Out of memory 'dmesh.verts' (%d).", maxVerts*3);
+ return false;
+ }
+
+ // Merge datas.
+ for (int i = 0; i < nmeshes; ++i)
+ {
+ rcPolyMeshDetail* dm = meshes[i];
+ if (!dm) continue;
+ for (int j = 0; j < dm->nmeshes; ++j)
+ {
+ unsigned short* dst = &mesh.meshes[mesh.nmeshes*4];
+ unsigned short* src = &dm->meshes[j*4];
+ dst[0] = mesh.nverts+src[0];
+ dst[1] = src[1];
+ dst[2] = mesh.ntris+src[2];
+ dst[3] = src[3];
+ mesh.nmeshes++;
+ }
+
+ for (int k = 0; k < dm->nverts; ++k)
+ {
+ vcopy(&mesh.verts[mesh.nverts*3], &dm->verts[k*3]);
+ mesh.nverts++;
+ }
+ for (int k = 0; k < dm->ntris; ++k)
+ {
+ mesh.tris[mesh.ntris*4+0] = dm->tris[k*4+0];
+ mesh.tris[mesh.ntris*4+1] = dm->tris[k*4+1];
+ mesh.tris[mesh.ntris*4+2] = dm->tris[k*4+2];
+ mesh.tris[mesh.ntris*4+3] = dm->tris[k*4+3];
+ mesh.ntris++;
+ }
+ }
+
+ rcTimeVal endTime = rcGetPerformanceTimer();
+
+ if (rcGetBuildTimes())
+ rcGetBuildTimes()->mergePolyMeshDetail += rcGetDeltaTimeUsec(startTime, endTime);
+
+ return true;
+}
+
diff --git a/extern/recastnavigation/Recast/Source/RecastRasterization.cpp b/extern/recastnavigation/Recast/Source/RecastRasterization.cpp
new file mode 100644
index 00000000000..658b0e1fb51
--- /dev/null
+++ b/extern/recastnavigation/Recast/Source/RecastRasterization.cpp
@@ -0,0 +1,308 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#define _USE_MATH_DEFINES
+#include <math.h>
+#include <stdio.h>
+#include "Recast.h"
+#include "RecastTimer.h"
+#include "RecastLog.h"
+
+inline bool overlapBounds(const float* amin, const float* amax, const float* bmin, const float* bmax)
+{
+ bool overlap = true;
+ overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap;
+ overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap;
+ overlap = (amin[2] > bmax[2] || amax[2] < bmin[2]) ? false : overlap;
+ return overlap;
+}
+
+inline bool overlapInterval(unsigned short amin, unsigned short amax,
+ unsigned short bmin, unsigned short bmax)
+{
+ if (amax < bmin) return false;
+ if (amin > bmax) return false;
+ return true;
+}
+
+
+static rcSpan* allocSpan(rcHeightfield& hf)
+{
+ // If running out of memory, allocate new page and update the freelist.
+ if (!hf.freelist || !hf.freelist->next)
+ {
+ // Create new page.
+ // Allocate memory for the new pool.
+ const int size = (sizeof(rcSpanPool)-sizeof(rcSpan)) + sizeof(rcSpan)*RC_SPANS_PER_POOL;
+ rcSpanPool* pool = reinterpret_cast<rcSpanPool*>(new unsigned char[size]);
+ if (!pool) return 0;
+ pool->next = 0;
+ // Add the pool into the list of pools.
+ pool->next = hf.pools;
+ hf.pools = pool;
+ // Add new items to the free list.
+ rcSpan* freelist = hf.freelist;
+ rcSpan* head = &pool->items[0];
+ rcSpan* it = &pool->items[RC_SPANS_PER_POOL];
+ do
+ {
+ --it;
+ it->next = freelist;
+ freelist = it;
+ }
+ while (it != head);
+ hf.freelist = it;
+ }
+
+ // Pop item from in front of the free list.
+ rcSpan* it = hf.freelist;
+ hf.freelist = hf.freelist->next;
+ return it;
+}
+
+static void freeSpan(rcHeightfield& hf, rcSpan* ptr)
+{
+ if (!ptr) return;
+ // Add the node in front of the free list.
+ ptr->next = hf.freelist;
+ hf.freelist = ptr;
+}
+
+static void addSpan(rcHeightfield& hf, int x, int y,
+ unsigned short smin, unsigned short smax,
+ unsigned short flags)
+{
+ int idx = x + y*hf.width;
+
+ rcSpan* s = allocSpan(hf);
+ s->smin = smin;
+ s->smax = smax;
+ s->flags = flags;
+ s->next = 0;
+
+ // Empty cell, add he first span.
+ if (!hf.spans[idx])
+ {
+ hf.spans[idx] = s;
+ return;
+ }
+ rcSpan* prev = 0;
+ rcSpan* cur = hf.spans[idx];
+
+ // Insert and merge spans.
+ while (cur)
+ {
+ if (cur->smin > s->smax)
+ {
+ // Current span is further than the new span, break.
+ break;
+ }
+ else if (cur->smax < s->smin)
+ {
+ // Current span is before the new span advance.
+ prev = cur;
+ cur = cur->next;
+ }
+ else
+ {
+ // Merge spans.
+ if (cur->smin < s->smin)
+ s->smin = cur->smin;
+ if (cur->smax > s->smax)
+ s->smax = cur->smax;
+
+ // Merge flags.
+// if (s->smax == cur->smax)
+ if (rcAbs((int)s->smax - (int)cur->smax) <= 1)
+ s->flags |= cur->flags;
+
+ // Remove current span.
+ rcSpan* next = cur->next;
+ freeSpan(hf, cur);
+ if (prev)
+ prev->next = next;
+ else
+ hf.spans[idx] = next;
+ cur = next;
+ }
+ }
+
+ // Insert new span.
+ if (prev)
+ {
+ s->next = prev->next;
+ prev->next = s;
+ }
+ else
+ {
+ s->next = hf.spans[idx];
+ hf.spans[idx] = s;
+ }
+}
+
+static int clipPoly(const float* in, int n, float* out, float pnx, float pnz, float pd)
+{
+ float d[12];
+ for (int i = 0; i < n; ++i)
+ d[i] = pnx*in[i*3+0] + pnz*in[i*3+2] + pd;
+
+ int m = 0;
+ for (int i = 0, j = n-1; i < n; j=i, ++i)
+ {
+ bool ina = d[j] >= 0;
+ bool inb = d[i] >= 0;
+ if (ina != inb)
+ {
+ float s = d[j] / (d[j] - d[i]);
+ out[m*3+0] = in[j*3+0] + (in[i*3+0] - in[j*3+0])*s;
+ out[m*3+1] = in[j*3+1] + (in[i*3+1] - in[j*3+1])*s;
+ out[m*3+2] = in[j*3+2] + (in[i*3+2] - in[j*3+2])*s;
+ m++;
+ }
+ if (inb)
+ {
+ out[m*3+0] = in[i*3+0];
+ out[m*3+1] = in[i*3+1];
+ out[m*3+2] = in[i*3+2];
+ m++;
+ }
+ }
+ return m;
+}
+
+static void rasterizeTri(const float* v0, const float* v1, const float* v2,
+ unsigned char flags, rcHeightfield& hf,
+ const float* bmin, const float* bmax,
+ const float cs, const float ics, const float ich)
+{
+ const int w = hf.width;
+ const int h = hf.height;
+ float tmin[3], tmax[3];
+ const float by = bmax[1] - bmin[1];
+
+ // Calculate the bounding box of the triangle.
+ vcopy(tmin, v0);
+ vcopy(tmax, v0);
+ vmin(tmin, v1);
+ vmin(tmin, v2);
+ vmax(tmax, v1);
+ vmax(tmax, v2);
+
+ // If the triangle does not touch the bbox of the heightfield, skip the triagle.
+ if (!overlapBounds(bmin, bmax, tmin, tmax))
+ return;
+
+ // Calculate the footpring of the triangle on the grid.
+ int x0 = (int)((tmin[0] - bmin[0])*ics);
+ int y0 = (int)((tmin[2] - bmin[2])*ics);
+ int x1 = (int)((tmax[0] - bmin[0])*ics);
+ int y1 = (int)((tmax[2] - bmin[2])*ics);
+ x0 = rcClamp(x0, 0, w-1);
+ y0 = rcClamp(y0, 0, h-1);
+ x1 = rcClamp(x1, 0, w-1);
+ y1 = rcClamp(y1, 0, h-1);
+
+ // Clip the triangle into all grid cells it touches.
+ float in[7*3], out[7*3], inrow[7*3];
+
+ for (int y = y0; y <= y1; ++y)
+ {
+ // Clip polygon to row.
+ vcopy(&in[0], v0);
+ vcopy(&in[1*3], v1);
+ vcopy(&in[2*3], v2);
+ int nvrow = 3;
+ const float cz = bmin[2] + y*cs;
+ nvrow = clipPoly(in, nvrow, out, 0, 1, -cz);
+ if (nvrow < 3) continue;
+ nvrow = clipPoly(out, nvrow, inrow, 0, -1, cz+cs);
+ if (nvrow < 3) continue;
+
+ for (int x = x0; x <= x1; ++x)
+ {
+ // Clip polygon to column.
+ int nv = nvrow;
+ const float cx = bmin[0] + x*cs;
+ nv = clipPoly(inrow, nv, out, 1, 0, -cx);
+ if (nv < 3) continue;
+ nv = clipPoly(out, nv, in, -1, 0, cx+cs);
+ if (nv < 3) continue;
+
+ // Calculate min and max of the span.
+ float smin = in[1], smax = in[1];
+ for (int i = 1; i < nv; ++i)
+ {
+ smin = rcMin(smin, in[i*3+1]);
+ smax = rcMax(smax, in[i*3+1]);
+ }
+ smin -= bmin[1];
+ smax -= bmin[1];
+ // Skip the span if it is outside the heightfield bbox
+ if (smax < 0.0f) continue;
+ if (smin > by) continue;
+ // Clamp the span to the heightfield bbox.
+ if (smin < 0.0f) smin = bmin[1];
+ if (smax > by) smax = bmax[1];
+
+ // Snap the span to the heightfield height grid.
+ unsigned short ismin = (unsigned short)rcClamp((int)floorf(smin * ich), 0, 0x7fff);
+ unsigned short ismax = (unsigned short)rcClamp((int)ceilf(smax * ich), 0, 0x7fff);
+
+ addSpan(hf, x, y, ismin, ismax, flags);
+ }
+ }
+}
+
+void rcRasterizeTriangle(const float* v0, const float* v1, const float* v2,
+ unsigned char flags, rcHeightfield& solid)
+{
+ rcTimeVal startTime = rcGetPerformanceTimer();
+
+ const float ics = 1.0f/solid.cs;
+ const float ich = 1.0f/solid.ch;
+ rasterizeTri(v0, v1, v2, flags, solid, solid.bmin, solid.bmax, solid.cs, ics, ich);
+
+ rcTimeVal endTime = rcGetPerformanceTimer();
+
+ if (rcGetBuildTimes())
+ rcGetBuildTimes()->rasterizeTriangles += rcGetDeltaTimeUsec(startTime, endTime);
+}
+
+void rcRasterizeTriangles(const float* verts, int nv,
+ const int* tris, const unsigned char* flags, int nt,
+ rcHeightfield& solid)
+{
+ rcTimeVal startTime = rcGetPerformanceTimer();
+
+ const float ics = 1.0f/solid.cs;
+ const float ich = 1.0f/solid.ch;
+ // Rasterize triangles.
+ for (int i = 0; i < nt; ++i)
+ {
+ const float* v0 = &verts[tris[i*3+0]*3];
+ const float* v1 = &verts[tris[i*3+1]*3];
+ const float* v2 = &verts[tris[i*3+2]*3];
+ // Rasterize.
+ rasterizeTri(v0, v1, v2, flags[i], solid, solid.bmin, solid.bmax, solid.cs, ics, ich);
+ }
+
+ rcTimeVal endTime = rcGetPerformanceTimer();
+
+ if (rcGetBuildTimes())
+ rcGetBuildTimes()->rasterizeTriangles += rcGetDeltaTimeUsec(startTime, endTime);
+}
diff --git a/extern/recastnavigation/Recast/Source/RecastRegion.cpp b/extern/recastnavigation/Recast/Source/RecastRegion.cpp
new file mode 100644
index 00000000000..5c557cf0681
--- /dev/null
+++ b/extern/recastnavigation/Recast/Source/RecastRegion.cpp
@@ -0,0 +1,1081 @@
+//
+// Copyright (c) 2009 Mikko Mononen memon@inside.org
+//
+// This software is provided 'as-is', without any express or implied
+// warranty. In no event will the authors be held liable for any damages
+// arising from the use of this software.
+// Permission is granted to anyone to use this software for any purpose,
+// including commercial applications, and to alter it and redistribute it
+// freely, subject to the following restrictions:
+// 1. The origin of this software must not be misrepresented; you must not
+// claim that you wrote the original software. If you use this software
+// in a product, an acknowledgment in the product documentation would be
+// appreciated but is not required.
+// 2. Altered source versions must be plainly marked as such, and must not be
+// misrepresented as being the original software.
+// 3. This notice may not be removed or altered from any source distribution.
+//
+
+#include <float.h>
+#define _USE_MATH_DEFINES
+#include <math.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include "Recast.h"
+#include "RecastLog.h"
+#include "RecastTimer.h"
+
+
+static unsigned short* calculateDistanceField(rcCompactHeightfield& chf,
+ unsigned short* src, unsigned short* dst,
+ unsigned short& maxDist)
+{
+ const int w = chf.width;
+ const int h = chf.height;
+
+ // Init distance and points.
+ for (int i = 0; i < chf.spanCount; ++i)
+ src[i] = 0xffff;
+
+ // Mark boundary cells.
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ const rcCompactCell& c = chf.cells[x+y*w];
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
+ {
+ const rcCompactSpan& s = chf.spans[i];
+ int nc = 0;
+ for (int dir = 0; dir < 4; ++dir)
+ {
+ if (rcGetCon(s, dir) != 0xf)
+ nc++;
+ }
+ if (nc != 4)
+ src[i] = 0;
+ }
+ }
+ }
+
+ // Pass 1
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ const rcCompactCell& c = chf.cells[x+y*w];
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
+ {
+ const rcCompactSpan& s = chf.spans[i];
+
+ if (rcGetCon(s, 0) != 0xf)
+ {
+ // (-1,0)
+ const int ax = x + rcGetDirOffsetX(0);
+ const int ay = y + rcGetDirOffsetY(0);
+ const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 0);
+ const rcCompactSpan& as = chf.spans[ai];
+ if (src[ai]+2 < src[i])
+ src[i] = src[ai]+2;
+
+ // (-1,-1)
+ if (rcGetCon(as, 3) != 0xf)
+ {
+ const int aax = ax + rcGetDirOffsetX(3);
+ const int aay = ay + rcGetDirOffsetY(3);
+ const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 3);
+ if (src[aai]+3 < src[i])
+ src[i] = src[aai]+3;
+ }
+ }
+ if (rcGetCon(s, 3) != 0xf)
+ {
+ // (0,-1)
+ const int ax = x + rcGetDirOffsetX(3);
+ const int ay = y + rcGetDirOffsetY(3);
+ const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 3);
+ const rcCompactSpan& as = chf.spans[ai];
+ if (src[ai]+2 < src[i])
+ src[i] = src[ai]+2;
+
+ // (1,-1)
+ if (rcGetCon(as, 2) != 0xf)
+ {
+ const int aax = ax + rcGetDirOffsetX(2);
+ const int aay = ay + rcGetDirOffsetY(2);
+ const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 2);
+ if (src[aai]+3 < src[i])
+ src[i] = src[aai]+3;
+ }
+ }
+ }
+ }
+ }
+
+ // Pass 2
+ for (int y = h-1; y >= 0; --y)
+ {
+ for (int x = w-1; x >= 0; --x)
+ {
+ const rcCompactCell& c = chf.cells[x+y*w];
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
+ {
+ const rcCompactSpan& s = chf.spans[i];
+
+ if (rcGetCon(s, 2) != 0xf)
+ {
+ // (1,0)
+ const int ax = x + rcGetDirOffsetX(2);
+ const int ay = y + rcGetDirOffsetY(2);
+ const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 2);
+ const rcCompactSpan& as = chf.spans[ai];
+ if (src[ai]+2 < src[i])
+ src[i] = src[ai]+2;
+
+ // (1,1)
+ if (rcGetCon(as, 1) != 0xf)
+ {
+ const int aax = ax + rcGetDirOffsetX(1);
+ const int aay = ay + rcGetDirOffsetY(1);
+ const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 1);
+ if (src[aai]+3 < src[i])
+ src[i] = src[aai]+3;
+ }
+ }
+ if (rcGetCon(s, 1) != 0xf)
+ {
+ // (0,1)
+ const int ax = x + rcGetDirOffsetX(1);
+ const int ay = y + rcGetDirOffsetY(1);
+ const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 1);
+ const rcCompactSpan& as = chf.spans[ai];
+ if (src[ai]+2 < src[i])
+ src[i] = src[ai]+2;
+
+ // (-1,1)
+ if (rcGetCon(as, 0) != 0xf)
+ {
+ const int aax = ax + rcGetDirOffsetX(0);
+ const int aay = ay + rcGetDirOffsetY(0);
+ const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 0);
+ if (src[aai]+3 < src[i])
+ src[i] = src[aai]+3;
+ }
+ }
+ }
+ }
+ }
+
+ maxDist = 0;
+ for (int i = 0; i < chf.spanCount; ++i)
+ maxDist = rcMax(src[i], maxDist);
+
+ return src;
+
+}
+
+static unsigned short* boxBlur(rcCompactHeightfield& chf, int thr,
+ unsigned short* src, unsigned short* dst)
+{
+ const int w = chf.width;
+ const int h = chf.height;
+
+ thr *= 2;
+
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ const rcCompactCell& c = chf.cells[x+y*w];
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
+ {
+ const rcCompactSpan& s = chf.spans[i];
+ int cd = (int)src[i];
+ if (cd <= thr)
+ {
+ dst[i] = cd;
+ continue;
+ }
+
+ int d = cd;
+ for (int dir = 0; dir < 4; ++dir)
+ {
+ if (rcGetCon(s, dir) != 0xf)
+ {
+ const int ax = x + rcGetDirOffsetX(dir);
+ const int ay = y + rcGetDirOffsetY(dir);
+ const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir);
+ d += (int)src[ai];
+
+ const rcCompactSpan& as = chf.spans[ai];
+ const int dir2 = (dir+1) & 0x3;
+ if (rcGetCon(as, dir2) != 0xf)
+ {
+ const int ax2 = ax + rcGetDirOffsetX(dir2);
+ const int ay2 = ay + rcGetDirOffsetY(dir2);
+ const int ai2 = (int)chf.cells[ax2+ay2*w].index + rcGetCon(as, dir2);
+ d += (int)src[ai2];
+ }
+ else
+ {
+ d += cd;
+ }
+ }
+ else
+ {
+ d += cd*2;
+ }
+ }
+ dst[i] = (unsigned short)((d+5)/9);
+ }
+ }
+ }
+ return dst;
+}
+
+
+static bool floodRegion(int x, int y, int i,
+ unsigned short level, unsigned short minLevel, unsigned short r,
+ rcCompactHeightfield& chf,
+ unsigned short* src,
+ rcIntArray& stack)
+{
+ const int w = chf.width;
+
+ // Flood fill mark region.
+ stack.resize(0);
+ stack.push((int)x);
+ stack.push((int)y);
+ stack.push((int)i);
+ src[i*2] = r;
+ src[i*2+1] = 0;
+
+ unsigned short lev = level >= minLevel+2 ? level-2 : minLevel;
+ int count = 0;
+
+ while (stack.size() > 0)
+ {
+ int ci = stack.pop();
+ int cy = stack.pop();
+ int cx = stack.pop();
+
+ const rcCompactSpan& cs = chf.spans[ci];
+
+ // Check if any of the neighbours already have a valid region set.
+ unsigned short ar = 0;
+ for (int dir = 0; dir < 4; ++dir)
+ {
+ // 8 connected
+ if (rcGetCon(cs, dir) != 0xf)
+ {
+ const int ax = cx + rcGetDirOffsetX(dir);
+ const int ay = cy + rcGetDirOffsetY(dir);
+ const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(cs, dir);
+ unsigned short nr = src[ai*2];
+ if (nr != 0 && nr != r)
+ ar = nr;
+
+ const rcCompactSpan& as = chf.spans[ai];
+
+ const int dir2 = (dir+1) & 0x3;
+ if (rcGetCon(as, dir2) != 0xf)
+ {
+ const int ax2 = ax + rcGetDirOffsetX(dir2);
+ const int ay2 = ay + rcGetDirOffsetY(dir2);
+ const int ai2 = (int)chf.cells[ax2+ay2*w].index + rcGetCon(as, dir2);
+
+ unsigned short nr = src[ai2*2];
+ if (nr != 0 && nr != r)
+ ar = nr;
+ }
+ }
+ }
+ if (ar != 0)
+ {
+ src[ci*2] = 0;
+ continue;
+ }
+ count++;
+
+ // Expand neighbours.
+ for (int dir = 0; dir < 4; ++dir)
+ {
+ if (rcGetCon(cs, dir) != 0xf)
+ {
+ const int ax = cx + rcGetDirOffsetX(dir);
+ const int ay = cy + rcGetDirOffsetY(dir);
+ const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(cs, dir);
+ if (chf.spans[ai].dist >= lev)
+ {
+ if (src[ai*2] == 0)
+ {
+ src[ai*2] = r;
+ src[ai*2+1] = 0;
+ stack.push(ax);
+ stack.push(ay);
+ stack.push(ai);
+ }
+ }
+ }
+ }
+ }
+
+ return count > 0;
+}
+
+static unsigned short* expandRegions(int maxIter, unsigned short level,
+ rcCompactHeightfield& chf,
+ unsigned short* src,
+ unsigned short* dst,
+ rcIntArray& stack)
+{
+ const int w = chf.width;
+ const int h = chf.height;
+
+ // Find cells revealed by the raised level.
+ stack.resize(0);
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ const rcCompactCell& c = chf.cells[x+y*w];
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
+ {
+ if (chf.spans[i].dist >= level && src[i*2] == 0)
+ {
+ stack.push(x);
+ stack.push(y);
+ stack.push(i);
+ }
+ }
+ }
+ }
+
+ int iter = 0;
+ while (stack.size() > 0)
+ {
+ int failed = 0;
+
+ memcpy(dst, src, sizeof(unsigned short)*chf.spanCount*2);
+
+ for (int j = 0; j < stack.size(); j += 3)
+ {
+ int x = stack[j+0];
+ int y = stack[j+1];
+ int i = stack[j+2];
+ if (i < 0)
+ {
+ failed++;
+ continue;
+ }
+
+ unsigned short r = src[i*2];
+ unsigned short d2 = 0xffff;
+ const rcCompactSpan& s = chf.spans[i];
+ for (int dir = 0; dir < 4; ++dir)
+ {
+ if (rcGetCon(s, dir) == 0xf) continue;
+ const int ax = x + rcGetDirOffsetX(dir);
+ const int ay = y + rcGetDirOffsetY(dir);
+ const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir);
+ if (src[ai*2] > 0 && (src[ai*2] & RC_BORDER_REG) == 0)
+ {
+ if ((int)src[ai*2+1]+2 < (int)d2)
+ {
+ r = src[ai*2];
+ d2 = src[ai*2+1]+2;
+ }
+ }
+ }
+ if (r)
+ {
+ stack[j+2] = -1; // mark as used
+ dst[i*2] = r;
+ dst[i*2+1] = d2;
+ }
+ else
+ {
+ failed++;
+ }
+ }
+
+ // rcSwap source and dest.
+ rcSwap(src, dst);
+
+ if (failed*3 == stack.size())
+ break;
+
+ if (level > 0)
+ {
+ ++iter;
+ if (iter >= maxIter)
+ break;
+ }
+ }
+
+ return src;
+}
+
+
+struct rcRegion
+{
+ inline rcRegion() : count(0), id(0), remap(false) {}
+
+ int count;
+ unsigned short id;
+ bool remap;
+ rcIntArray connections;
+ rcIntArray floors;
+};
+
+static void removeAdjacentNeighbours(rcRegion& reg)
+{
+ // Remove adjacent duplicates.
+ for (int i = 0; i < reg.connections.size() && reg.connections.size() > 1; )
+ {
+ int ni = (i+1) % reg.connections.size();
+ if (reg.connections[i] == reg.connections[ni])
+ {
+ // Remove duplicate
+ for (int j = i; j < reg.connections.size()-1; ++j)
+ reg.connections[j] = reg.connections[j+1];
+ reg.connections.pop();
+ }
+ else
+ ++i;
+ }
+}
+
+static void replaceNeighbour(rcRegion& reg, unsigned short oldId, unsigned short newId)
+{
+ bool neiChanged = false;
+ for (int i = 0; i < reg.connections.size(); ++i)
+ {
+ if (reg.connections[i] == oldId)
+ {
+ reg.connections[i] = newId;
+ neiChanged = true;
+ }
+ }
+ for (int i = 0; i < reg.floors.size(); ++i)
+ {
+ if (reg.floors[i] == oldId)
+ reg.floors[i] = newId;
+ }
+ if (neiChanged)
+ removeAdjacentNeighbours(reg);
+}
+
+static bool canMergeWithRegion(rcRegion& reg, unsigned short id)
+{
+ int n = 0;
+ for (int i = 0; i < reg.connections.size(); ++i)
+ {
+ if (reg.connections[i] == id)
+ n++;
+ }
+ if (n > 1)
+ return false;
+ for (int i = 0; i < reg.floors.size(); ++i)
+ {
+ if (reg.floors[i] == id)
+ return false;
+ }
+ return true;
+}
+
+static void addUniqueFloorRegion(rcRegion& reg, unsigned short n)
+{
+ for (int i = 0; i < reg.floors.size(); ++i)
+ if (reg.floors[i] == n)
+ return;
+ reg.floors.push(n);
+}
+
+static bool mergeRegions(rcRegion& rega, rcRegion& regb)
+{
+ unsigned short aid = rega.id;
+ unsigned short bid = regb.id;
+
+ // Duplicate current neighbourhood.
+ rcIntArray acon;
+ acon.resize(rega.connections.size());
+ for (int i = 0; i < rega.connections.size(); ++i)
+ acon[i] = rega.connections[i];
+ rcIntArray& bcon = regb.connections;
+
+ // Find insertion point on A.
+ int insa = -1;
+ for (int i = 0; i < acon.size(); ++i)
+ {
+ if (acon[i] == bid)
+ {
+ insa = i;
+ break;
+ }
+ }
+ if (insa == -1)
+ return false;
+
+ // Find insertion point on B.
+ int insb = -1;
+ for (int i = 0; i < bcon.size(); ++i)
+ {
+ if (bcon[i] == aid)
+ {
+ insb = i;
+ break;
+ }
+ }
+ if (insb == -1)
+ return false;
+
+ // Merge neighbours.
+ rega.connections.resize(0);
+ for (int i = 0, ni = acon.size(); i < ni-1; ++i)
+ rega.connections.push(acon[(insa+1+i) % ni]);
+
+ for (int i = 0, ni = bcon.size(); i < ni-1; ++i)
+ rega.connections.push(bcon[(insb+1+i) % ni]);
+
+ removeAdjacentNeighbours(rega);
+
+ for (int j = 0; j < regb.floors.size(); ++j)
+ addUniqueFloorRegion(rega, regb.floors[j]);
+ rega.count += regb.count;
+ regb.count = 0;
+ regb.connections.resize(0);
+
+ return true;
+}
+
+static bool isRegionConnectedToBorder(const rcRegion& reg)
+{
+ // Region is connected to border if
+ // one of the neighbours is null id.
+ for (int i = 0; i < reg.connections.size(); ++i)
+ {
+ if (reg.connections[i] == 0)
+ return true;
+ }
+ return false;
+}
+
+static bool isSolidEdge(rcCompactHeightfield& chf, unsigned short* src,
+ int x, int y, int i, int dir)
+{
+ const rcCompactSpan& s = chf.spans[i];
+ unsigned short r = 0;
+ if (rcGetCon(s, dir) != 0xf)
+ {
+ const int ax = x + rcGetDirOffsetX(dir);
+ const int ay = y + rcGetDirOffsetY(dir);
+ const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(s, dir);
+ r = src[ai*2];
+ }
+ if (r == src[i*2])
+ return false;
+ return true;
+}
+
+static void walkContour(int x, int y, int i, int dir,
+ rcCompactHeightfield& chf,
+ unsigned short* src,
+ rcIntArray& cont)
+{
+ int startDir = dir;
+ int starti = i;
+
+ const rcCompactSpan& ss = chf.spans[i];
+ unsigned short curReg = 0;
+ if (rcGetCon(ss, dir) != 0xf)
+ {
+ const int ax = x + rcGetDirOffsetX(dir);
+ const int ay = y + rcGetDirOffsetY(dir);
+ const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(ss, dir);
+ curReg = src[ai*2];
+ }
+ cont.push(curReg);
+
+ int iter = 0;
+ while (++iter < 40000)
+ {
+ const rcCompactSpan& s = chf.spans[i];
+
+ if (isSolidEdge(chf, src, x, y, i, dir))
+ {
+ // Choose the edge corner
+ unsigned short r = 0;
+ if (rcGetCon(s, dir) != 0xf)
+ {
+ const int ax = x + rcGetDirOffsetX(dir);
+ const int ay = y + rcGetDirOffsetY(dir);
+ const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(s, dir);
+ r = src[ai*2];
+ }
+ if (r != curReg)
+ {
+ curReg = r;
+ cont.push(curReg);
+ }
+
+ dir = (dir+1) & 0x3; // Rotate CW
+ }
+ else
+ {
+ int ni = -1;
+ const int nx = x + rcGetDirOffsetX(dir);
+ const int ny = y + rcGetDirOffsetY(dir);
+ if (rcGetCon(s, dir) != 0xf)
+ {
+ const rcCompactCell& nc = chf.cells[nx+ny*chf.width];
+ ni = (int)nc.index + rcGetCon(s, dir);
+ }
+ if (ni == -1)
+ {
+ // Should not happen.
+ return;
+ }
+ x = nx;
+ y = ny;
+ i = ni;
+ dir = (dir+3) & 0x3; // Rotate CCW
+ }
+
+ if (starti == i && startDir == dir)
+ {
+ break;
+ }
+ }
+
+ // Remove adjacent duplicates.
+ if (cont.size() > 1)
+ {
+ for (int i = 0; i < cont.size(); )
+ {
+ int ni = (i+1) % cont.size();
+ if (cont[i] == cont[ni])
+ {
+ for (int j = i; j < cont.size()-1; ++j)
+ cont[j] = cont[j+1];
+ cont.pop();
+ }
+ else
+ ++i;
+ }
+ }
+}
+
+static bool filterSmallRegions(int minRegionSize, int mergeRegionSize,
+ unsigned short& maxRegionId,
+ rcCompactHeightfield& chf,
+ unsigned short* src)
+{
+ const int w = chf.width;
+ const int h = chf.height;
+
+ int nreg = maxRegionId+1;
+ rcRegion* regions = new rcRegion[nreg];
+ if (!regions)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "filterSmallRegions: Out of memory 'regions' (%d).", nreg);
+ return false;
+ }
+
+ for (int i = 0; i < nreg; ++i)
+ regions[i].id = (unsigned short)i;
+
+ // Find edge of a region and find connections around the contour.
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ const rcCompactCell& c = chf.cells[x+y*w];
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
+ {
+ unsigned short r = src[i*2];
+ if (r == 0 || r >= nreg)
+ continue;
+
+ rcRegion& reg = regions[r];
+ reg.count++;
+
+
+ // Update floors.
+ for (int j = (int)c.index; j < ni; ++j)
+ {
+ if (i == j) continue;
+ unsigned short floorId = src[j*2];
+ if (floorId == 0 || floorId >= nreg)
+ continue;
+ addUniqueFloorRegion(reg, floorId);
+ }
+
+ // Have found contour
+ if (reg.connections.size() > 0)
+ continue;
+
+ // Check if this cell is next to a border.
+ int ndir = -1;
+ for (int dir = 0; dir < 4; ++dir)
+ {
+ if (isSolidEdge(chf, src, x, y, i, dir))
+ {
+ ndir = dir;
+ break;
+ }
+ }
+
+ if (ndir != -1)
+ {
+ // The cell is at border.
+ // Walk around the contour to find all the neighbours.
+ walkContour(x, y, i, ndir, chf, src, reg.connections);
+ }
+ }
+ }
+ }
+
+ // Remove too small unconnected regions.
+ for (int i = 0; i < nreg; ++i)
+ {
+ rcRegion& reg = regions[i];
+ if (reg.id == 0 || (reg.id & RC_BORDER_REG))
+ continue;
+ if (reg.count == 0)
+ continue;
+
+ if (reg.connections.size() == 1 && reg.connections[0] == 0)
+ {
+ if (reg.count < minRegionSize)
+ {
+ // Non-connected small region, remove.
+ reg.count = 0;
+ reg.id = 0;
+ }
+ }
+ }
+
+
+ // Merge too small regions to neighbour regions.
+ int mergeCount = 0 ;
+ do
+ {
+ mergeCount = 0;
+ for (int i = 0; i < nreg; ++i)
+ {
+ rcRegion& reg = regions[i];
+ if (reg.id == 0 || (reg.id & RC_BORDER_REG))
+ continue;
+ if (reg.count == 0)
+ continue;
+
+ // Check to see if the region should be merged.
+ if (reg.count > mergeRegionSize && isRegionConnectedToBorder(reg))
+ continue;
+
+ // Small region with more than 1 connection.
+ // Or region which is not connected to a border at all.
+ // Find smallest neighbour region that connects to this one.
+ int smallest = 0xfffffff;
+ unsigned short mergeId = reg.id;
+ for (int j = 0; j < reg.connections.size(); ++j)
+ {
+ if (reg.connections[j] & RC_BORDER_REG) continue;
+ rcRegion& mreg = regions[reg.connections[j]];
+ if (mreg.id == 0 || (mreg.id & RC_BORDER_REG)) continue;
+ if (mreg.count < smallest &&
+ canMergeWithRegion(reg, mreg.id) &&
+ canMergeWithRegion(mreg, reg.id))
+ {
+ smallest = mreg.count;
+ mergeId = mreg.id;
+ }
+ }
+ // Found new id.
+ if (mergeId != reg.id)
+ {
+ unsigned short oldId = reg.id;
+ rcRegion& target = regions[mergeId];
+
+ // Merge neighbours.
+ if (mergeRegions(target, reg))
+ {
+ // Fixup regions pointing to current region.
+ for (int j = 0; j < nreg; ++j)
+ {
+ if (regions[j].id == 0 || (regions[j].id & RC_BORDER_REG)) continue;
+ // If another region was already merged into current region
+ // change the nid of the previous region too.
+ if (regions[j].id == oldId)
+ regions[j].id = mergeId;
+ // Replace the current region with the new one if the
+ // current regions is neighbour.
+ replaceNeighbour(regions[j], oldId, mergeId);
+ }
+ mergeCount++;
+ }
+ }
+ }
+ }
+ while (mergeCount > 0);
+
+ // Compress region Ids.
+ for (int i = 0; i < nreg; ++i)
+ {
+ regions[i].remap = false;
+ if (regions[i].id == 0) continue; // Skip nil regions.
+ if (regions[i].id & RC_BORDER_REG) continue; // Skip external regions.
+ regions[i].remap = true;
+ }
+
+ unsigned short regIdGen = 0;
+ for (int i = 0; i < nreg; ++i)
+ {
+ if (!regions[i].remap)
+ continue;
+ unsigned short oldId = regions[i].id;
+ unsigned short newId = ++regIdGen;
+ for (int j = i; j < nreg; ++j)
+ {
+ if (regions[j].id == oldId)
+ {
+ regions[j].id = newId;
+ regions[j].remap = false;
+ }
+ }
+ }
+ maxRegionId = regIdGen;
+
+ // Remap regions.
+ for (int i = 0; i < chf.spanCount; ++i)
+ {
+ if ((src[i*2] & RC_BORDER_REG) == 0)
+ src[i*2] = regions[src[i*2]].id;
+ }
+
+ delete [] regions;
+
+ return true;
+}
+
+bool rcBuildDistanceField(rcCompactHeightfield& chf)
+{
+ rcTimeVal startTime = rcGetPerformanceTimer();
+
+ unsigned short* dist0 = new unsigned short[chf.spanCount];
+ if (!dist0)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildDistanceField: Out of memory 'dist0' (%d).", chf.spanCount);
+ return false;
+ }
+ unsigned short* dist1 = new unsigned short[chf.spanCount];
+ if (!dist1)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildDistanceField: Out of memory 'dist1' (%d).", chf.spanCount);
+ delete [] dist0;
+ return false;
+ }
+
+ unsigned short* src = dist0;
+ unsigned short* dst = dist1;
+
+ unsigned short maxDist = 0;
+
+ rcTimeVal distStartTime = rcGetPerformanceTimer();
+
+ if (calculateDistanceField(chf, src, dst, maxDist) != src)
+ rcSwap(src, dst);
+
+ chf.maxDistance = maxDist;
+
+ rcTimeVal distEndTime = rcGetPerformanceTimer();
+
+ rcTimeVal blurStartTime = rcGetPerformanceTimer();
+
+ // Blur
+ if (boxBlur(chf, 1, src, dst) != src)
+ rcSwap(src, dst);
+
+ // Store distance.
+ for (int i = 0; i < chf.spanCount; ++i)
+ chf.spans[i].dist = src[i];
+
+ rcTimeVal blurEndTime = rcGetPerformanceTimer();
+
+ delete [] dist0;
+ delete [] dist1;
+
+ rcTimeVal endTime = rcGetPerformanceTimer();
+
+/* if (rcGetLog())
+ {
+ rcGetLog()->log(RC_LOG_PROGRESS, "Build distance field: %.3f ms", rcGetDeltaTimeUsec(startTime, endTime)/1000.0f);
+ rcGetLog()->log(RC_LOG_PROGRESS, " - dist: %.3f ms", rcGetDeltaTimeUsec(distStartTime, distEndTime)/1000.0f);
+ rcGetLog()->log(RC_LOG_PROGRESS, " - blur: %.3f ms", rcGetDeltaTimeUsec(blurStartTime, blurEndTime)/1000.0f);
+ }*/
+ if (rcGetBuildTimes())
+ {
+ rcGetBuildTimes()->buildDistanceField += rcGetDeltaTimeUsec(startTime, endTime);
+ rcGetBuildTimes()->buildDistanceFieldDist += rcGetDeltaTimeUsec(distStartTime, distEndTime);
+ rcGetBuildTimes()->buildDistanceFieldBlur += rcGetDeltaTimeUsec(blurStartTime, blurEndTime);
+ }
+
+ return true;
+}
+
+static void paintRectRegion(int minx, int maxx, int miny, int maxy,
+ unsigned short regId, unsigned short minLevel,
+ rcCompactHeightfield& chf, unsigned short* src)
+{
+ const int w = chf.width;
+ for (int y = miny; y < maxy; ++y)
+ {
+ for (int x = minx; x < maxx; ++x)
+ {
+ const rcCompactCell& c = chf.cells[x+y*w];
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
+ {
+ if (chf.spans[i].dist >= minLevel)
+ src[i*2] = regId;
+ }
+ }
+ }
+}
+
+bool rcBuildRegions(rcCompactHeightfield& chf,
+ int walkableRadius, int borderSize,
+ int minRegionSize, int mergeRegionSize)
+{
+ rcTimeVal startTime = rcGetPerformanceTimer();
+
+ const int w = chf.width;
+ const int h = chf.height;
+
+ unsigned short* tmp1 = new unsigned short[chf.spanCount*2];
+ if (!tmp1)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildDistanceField: Out of memory 'tmp1' (%d).", chf.spanCount*2);
+ return false;
+ }
+ unsigned short* tmp2 = new unsigned short[chf.spanCount*2];
+ if (!tmp2)
+ {
+ if (rcGetLog())
+ rcGetLog()->log(RC_LOG_ERROR, "rcBuildDistanceField: Out of memory 'tmp2' (%d).", chf.spanCount*2);
+ delete [] tmp1;
+ return false;
+ }
+
+ rcTimeVal regStartTime = rcGetPerformanceTimer();
+
+ rcIntArray stack(1024);
+ rcIntArray visited(1024);
+
+ unsigned short* src = tmp1;
+ unsigned short* dst = tmp2;
+
+ memset(src, 0, sizeof(unsigned short) * chf.spanCount*2);
+
+ unsigned short regionId = 1;
+ unsigned short level = (chf.maxDistance+1) & ~1;
+
+ unsigned short minLevel = (unsigned short)(walkableRadius*2);
+
+ const int expandIters = 4 + walkableRadius * 2;
+
+ // Mark border regions.
+ paintRectRegion(0, borderSize, 0, h, regionId|RC_BORDER_REG, minLevel, chf, src); regionId++;
+ paintRectRegion(w-borderSize, w, 0, h, regionId|RC_BORDER_REG, minLevel, chf, src); regionId++;
+ paintRectRegion(0, w, 0, borderSize, regionId|RC_BORDER_REG, minLevel, chf, src); regionId++;
+ paintRectRegion(0, w, h-borderSize, h, regionId|RC_BORDER_REG, minLevel, chf, src); regionId++;
+
+ rcTimeVal expTime = 0;
+ rcTimeVal floodTime = 0;
+
+ while (level > minLevel)
+ {
+ level = level >= 2 ? level-2 : 0;
+
+ rcTimeVal expStartTime = rcGetPerformanceTimer();
+
+ // Expand current regions until no empty connected cells found.
+ if (expandRegions(expandIters, level, chf, src, dst, stack) != src)
+ rcSwap(src, dst);
+
+ expTime += rcGetPerformanceTimer() - expStartTime;
+
+ rcTimeVal floodStartTime = rcGetPerformanceTimer();
+
+ // Mark new regions with IDs.
+ for (int y = 0; y < h; ++y)
+ {
+ for (int x = 0; x < w; ++x)
+ {
+ const rcCompactCell& c = chf.cells[x+y*w];
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
+ {
+ if (chf.spans[i].dist < level || src[i*2] != 0)
+ continue;
+
+ if (floodRegion(x, y, i, minLevel, level, regionId, chf, src, stack))
+ regionId++;
+ }
+ }
+ }
+
+ floodTime += rcGetPerformanceTimer() - floodStartTime;
+
+ }
+
+ // Expand current regions until no empty connected cells found.
+ if (expandRegions(expandIters*8, minLevel, chf, src, dst, stack) != src)
+ rcSwap(src, dst);
+
+ rcTimeVal regEndTime = rcGetPerformanceTimer();
+
+ rcTimeVal filterStartTime = rcGetPerformanceTimer();
+
+ // Filter out small regions.
+ chf.maxRegions = regionId;
+ if (!filterSmallRegions(minRegionSize, mergeRegionSize, chf.maxRegions, chf, src))
+ return false;
+
+ rcTimeVal filterEndTime = rcGetPerformanceTimer();
+
+ // Write the result out.
+ for (int i = 0; i < chf.spanCount; ++i)
+ chf.spans[i].reg = src[i*2];
+
+ delete [] tmp1;
+ delete [] tmp2;
+
+ rcTimeVal endTime = rcGetPerformanceTimer();
+
+/* if (rcGetLog())
+ {
+ rcGetLog()->log(RC_LOG_PROGRESS, "Build regions: %.3f ms", rcGetDeltaTimeUsec(startTime, endTime)/1000.0f);
+ rcGetLog()->log(RC_LOG_PROGRESS, " - reg: %.3f ms", rcGetDeltaTimeUsec(regStartTime, regEndTime)/1000.0f);
+ rcGetLog()->log(RC_LOG_PROGRESS, " - exp: %.3f ms", rcGetDeltaTimeUsec(0, expTime)/1000.0f);
+ rcGetLog()->log(RC_LOG_PROGRESS, " - flood: %.3f ms", rcGetDeltaTimeUsec(0, floodTime)/1000.0f);
+ rcGetLog()->log(RC_LOG_PROGRESS, " - filter: %.3f ms", rcGetDeltaTimeUsec(filterStartTime, filterEndTime)/1000.0f);
+ }
+*/
+ if (rcGetBuildTimes())
+ {
+ rcGetBuildTimes()->buildRegions += rcGetDeltaTimeUsec(startTime, endTime);
+ rcGetBuildTimes()->buildRegionsReg += rcGetDeltaTimeUsec(regStartTime, regEndTime);
+ rcGetBuildTimes()->buildRegionsExp += rcGetDeltaTimeUsec(0, expTime);
+ rcGetBuildTimes()->buildRegionsFlood += rcGetDeltaTimeUsec(0, floodTime);
+ rcGetBuildTimes()->buildRegionsFilter += rcGetDeltaTimeUsec(filterStartTime, filterEndTime);
+ }
+
+ return true;
+}
+
+
diff --git a/extern/recastnavigation/Recast/Source/RecastTimer.cpp b/extern/recastnavigation/Recast/Source/RecastTimer.cpp
new file mode 100644
index 00000000000..51ffb7d3160
--- /dev/null
+++ b/extern/recastnavigation/Recast/Source/RecastTimer.cpp
@@ -0,0 +1,58 @@
+#include "RecastTimer.h"
+
+#if defined(WIN32)
+
+// Win32
+#include <windows.h>
+
+rcTimeVal rcGetPerformanceTimer()
+{
+ __int64 count;
+ QueryPerformanceCounter((LARGE_INTEGER*)&count);
+ return count;
+}
+
+int rcGetDeltaTimeUsec(rcTimeVal start, rcTimeVal end)
+{
+ static __int64 freq = 0;
+ if (freq == 0)
+ QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
+ __int64 elapsed = end - start;
+ return (int)(elapsed*1000000 / freq);
+}
+
+#elif defined(__MACH__)
+
+// OSX
+#include <mach/mach_time.h>
+
+rcTimeVal rcGetPerformanceTimer()
+{
+ return mach_absolute_time();
+}
+
+int rcGetDeltaTimeUsec(rcTimeVal start, rcTimeVal end)
+{
+ static mach_timebase_info_data_t timebaseInfo;
+ if (timebaseInfo.denom == 0)
+ mach_timebase_info(&timebaseInfo);
+ uint64_t elapsed = end - start;
+ uint64_t nanosec = elapsed * timebaseInfo.numer / timebaseInfo.denom;
+ return (int)(nanosec / 1000);
+}
+
+#else
+
+// TODO: Linux, etc
+
+rcTimeVal rcGetPerformanceTimer()
+{
+ return 0;
+}
+
+int rcGetDeltaTimeUsec(rcTimeVal start, rcTimeVal end)
+{
+ return 0;
+}
+
+#endif \ No newline at end of file
diff --git a/extern/recastnavigation/SConscript b/extern/recastnavigation/SConscript
new file mode 100644
index 00000000000..be10c33c327
--- /dev/null
+++ b/extern/recastnavigation/SConscript
@@ -0,0 +1,11 @@
+#!/usr/bin/python
+
+Import('env')
+
+sources = env.Glob('Recast/Source/*.cpp') + env.Glob('Detour/Source/*.cpp')
+
+incs = 'Recast/Include Detour/Include'
+
+env.BlenderLib ( 'extern_recastnavigation', sources, Split(incs), [],
+ libtype=['extern','player'],
+ priority=[10,185])
diff --git a/intern/ghost/intern/GHOST_SystemPathsCocoa.mm b/intern/ghost/intern/GHOST_SystemPathsCocoa.mm
index 238ce1a46c7..e9ce1ea46db 100644
--- a/intern/ghost/intern/GHOST_SystemPathsCocoa.mm
+++ b/intern/ghost/intern/GHOST_SystemPathsCocoa.mm
@@ -1,121 +1,121 @@
-/**
- * $Id$
- *
- * ***** BEGIN GPL LICENSE BLOCK *****
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * The Original Code is Copyright (C) 2010 Blender Foundation.
- * All rights reserved.
- *
- *
- * Contributor(s): Damien Plisson 2010
- *
- * ***** END GPL LICENSE BLOCK *****
- */
-
-#import <Cocoa/Cocoa.h>
-
-/*For the currently not ported to Cocoa keyboard layout functions (64bit & 10.6 compatible)*/
-#include <Carbon/Carbon.h>
-
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/sysctl.h>
-
-#include "GHOST_SystemPathsCocoa.h"
-
-
-#pragma mark initialization/finalization
-
-
-GHOST_SystemPathsCocoa::GHOST_SystemPathsCocoa()
-{
-}
-
-GHOST_SystemPathsCocoa::~GHOST_SystemPathsCocoa()
-{
-}
-
-
-#pragma mark Base directories retrieval
-
-const GHOST_TUns8* GHOST_SystemPathsCocoa::getSystemDir() const
-{
- static GHOST_TUns8 tempPath[512] = "";
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- NSString *basePath;
- NSArray *paths;
-
- paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSLocalDomainMask, YES);
-
- if ([paths count] > 0)
- basePath = [paths objectAtIndex:0];
- else {
- [pool drain];
- return NULL;
- }
-
- strcpy((char*)tempPath, [basePath cStringUsingEncoding:NSASCIIStringEncoding]);
-
- [pool drain];
- return tempPath;
-}
-
-const GHOST_TUns8* GHOST_SystemPathsCocoa::getUserDir() const
-{
- static GHOST_TUns8 tempPath[512] = "";
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- NSString *basePath;
- NSArray *paths;
-
- paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
-
- if ([paths count] > 0)
- basePath = [paths objectAtIndex:0];
- else {
- [pool drain];
- return NULL;
- }
-
- strcpy((char*)tempPath, [basePath cStringUsingEncoding:NSASCIIStringEncoding]);
-
- [pool drain];
- return tempPath;
-}
-
-const GHOST_TUns8* GHOST_SystemPathsCocoa::getBinaryDir() const
-{
- static GHOST_TUns8 tempPath[512] = "";
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- NSString *basePath;
-
- basePath = [[NSBundle mainBundle] bundlePath];
-
- if (basePath == nil) {
- [pool drain];
- return NULL;
- }
-
- strcpy((char*)tempPath, [basePath cStringUsingEncoding:NSASCIIStringEncoding]);
-
- [pool drain];
- return tempPath;
-}
-
-void GHOST_SystemPathsCocoa::addToSystemRecentFiles(const char* filename) const
-{
- /* XXXXX TODO: Implementation for X11 if possible */
-}
+/**
+ * $Id: GHOST_SystemPathsCocoa.mm 34106 2011-01-05 19:19:49Z gsrb3d $
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2010 Blender Foundation.
+ * All rights reserved.
+ *
+ *
+ * Contributor(s): Damien Plisson 2010
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#import <Cocoa/Cocoa.h>
+
+/*For the currently not ported to Cocoa keyboard layout functions (64bit & 10.6 compatible)*/
+#include <Carbon/Carbon.h>
+
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/sysctl.h>
+
+#include "GHOST_SystemPathsCocoa.h"
+
+
+#pragma mark initialization/finalization
+
+
+GHOST_SystemPathsCocoa::GHOST_SystemPathsCocoa()
+{
+}
+
+GHOST_SystemPathsCocoa::~GHOST_SystemPathsCocoa()
+{
+}
+
+
+#pragma mark Base directories retrieval
+
+const GHOST_TUns8* GHOST_SystemPathsCocoa::getSystemDir() const
+{
+ static GHOST_TUns8 tempPath[512] = "";
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+ NSString *basePath;
+ NSArray *paths;
+
+ paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSLocalDomainMask, YES);
+
+ if ([paths count] > 0)
+ basePath = [paths objectAtIndex:0];
+ else {
+ [pool drain];
+ return NULL;
+ }
+
+ strcpy((char*)tempPath, [basePath cStringUsingEncoding:NSASCIIStringEncoding]);
+
+ [pool drain];
+ return tempPath;
+}
+
+const GHOST_TUns8* GHOST_SystemPathsCocoa::getUserDir() const
+{
+ static GHOST_TUns8 tempPath[512] = "";
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+ NSString *basePath;
+ NSArray *paths;
+
+ paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
+
+ if ([paths count] > 0)
+ basePath = [paths objectAtIndex:0];
+ else {
+ [pool drain];
+ return NULL;
+ }
+
+ strcpy((char*)tempPath, [basePath cStringUsingEncoding:NSASCIIStringEncoding]);
+
+ [pool drain];
+ return tempPath;
+}
+
+const GHOST_TUns8* GHOST_SystemPathsCocoa::getBinaryDir() const
+{
+ static GHOST_TUns8 tempPath[512] = "";
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+ NSString *basePath;
+
+ basePath = [[NSBundle mainBundle] bundlePath];
+
+ if (basePath == nil) {
+ [pool drain];
+ return NULL;
+ }
+
+ strcpy((char*)tempPath, [basePath cStringUsingEncoding:NSASCIIStringEncoding]);
+
+ [pool drain];
+ return tempPath;
+}
+
+void GHOST_SystemPathsCocoa::addToSystemRecentFiles(const char* filename) const
+{
+ /* XXXXX TODO: Implementation for X11 if possible */
+}
diff --git a/release/freedesktop/icons/48x48/apps/blender.png b/release/freedesktop/icons/48x48/apps/blender.png
deleted file mode 100644
index 9caed661fba..00000000000
--- a/release/freedesktop/icons/48x48/apps/blender.png
+++ /dev/null
Binary files differ
diff --git a/release/scripts/presets/framerate/23.98.py b/release/scripts/presets/framerate/23.98.py
deleted file mode 100644
index 498a5180d73..00000000000
--- a/release/scripts/presets/framerate/23.98.py
+++ /dev/null
@@ -1,3 +0,0 @@
-import bpy
-bpy.context.scene.render.fps = 24
-bpy.context.scene.render.fps_base = 1.001
diff --git a/release/scripts/presets/framerate/24.py b/release/scripts/presets/framerate/24.py
deleted file mode 100644
index 6fcdcb1284a..00000000000
--- a/release/scripts/presets/framerate/24.py
+++ /dev/null
@@ -1,3 +0,0 @@
-import bpy
-bpy.context.scene.render.fps = 24
-bpy.context.scene.render.fps_base = 1
diff --git a/release/scripts/presets/framerate/25.py b/release/scripts/presets/framerate/25.py
deleted file mode 100644
index 20973919720..00000000000
--- a/release/scripts/presets/framerate/25.py
+++ /dev/null
@@ -1,3 +0,0 @@
-import bpy
-bpy.context.scene.render.fps = 25
-bpy.context.scene.render.fps_base = 1
diff --git a/release/scripts/presets/framerate/29.97.py b/release/scripts/presets/framerate/29.97.py
deleted file mode 100644
index 8f7ad67a7ee..00000000000
--- a/release/scripts/presets/framerate/29.97.py
+++ /dev/null
@@ -1,3 +0,0 @@
-import bpy
-bpy.context.scene.render.fps = 30
-bpy.context.scene.render.fps_base = 1.001
diff --git a/release/scripts/presets/framerate/30.py b/release/scripts/presets/framerate/30.py
deleted file mode 100644
index bc83df19ac8..00000000000
--- a/release/scripts/presets/framerate/30.py
+++ /dev/null
@@ -1,3 +0,0 @@
-import bpy
-bpy.context.scene.render.fps = 30
-bpy.context.scene.render.fps_base = 1
diff --git a/release/scripts/presets/framerate/50.py b/release/scripts/presets/framerate/50.py
deleted file mode 100644
index 08dfaa89c6e..00000000000
--- a/release/scripts/presets/framerate/50.py
+++ /dev/null
@@ -1,3 +0,0 @@
-import bpy
-bpy.context.scene.render.fps = 50
-bpy.context.scene.render.fps_base = 1
diff --git a/release/scripts/presets/framerate/59.94.py b/release/scripts/presets/framerate/59.94.py
deleted file mode 100644
index 5c44841a9ef..00000000000
--- a/release/scripts/presets/framerate/59.94.py
+++ /dev/null
@@ -1,3 +0,0 @@
-import bpy
-bpy.context.scene.render.fps = 60
-bpy.context.scene.render.fps_base = 1.001
diff --git a/release/scripts/presets/framerate/60.py b/release/scripts/presets/framerate/60.py
deleted file mode 100644
index 30f2db7cdad..00000000000
--- a/release/scripts/presets/framerate/60.py
+++ /dev/null
@@ -1,3 +0,0 @@
-import bpy
-bpy.context.scene.render.fps = 60
-bpy.context.scene.render.fps_base = 1
diff --git a/release/scripts/presets/framerate/Custom.py b/release/scripts/presets/framerate/Custom.py
deleted file mode 100644
index 001d19a2280..00000000000
--- a/release/scripts/presets/framerate/Custom.py
+++ /dev/null
@@ -1 +0,0 @@
-import bpy
diff --git a/release/scripts/startup/bl_ui/properties_data_modifier.py b/release/scripts/startup/bl_ui/properties_data_modifier.py
index 179921c3c85..dc6b5401168 100644
--- a/release/scripts/startup/bl_ui/properties_data_modifier.py
+++ b/release/scripts/startup/bl_ui/properties_data_modifier.py
@@ -379,6 +379,14 @@ class DATA_PT_modifiers(ModifierButtonsPanel, Panel):
col.label(text="Mirror Object:")
col.prop(md, "mirror_object", text="")
+ def NAVMESH(self, layout, ob, md):
+ split = layout.split()
+ if ob.mode == 'EDIT':
+ col = split.column()
+ col.operator("object.assign_navpolygon", text="Assign poly idx")
+ col = split.column()
+ col.operator("object.assign_new_navpolygon", text="Assign new poly idx")
+
def MULTIRES(self, layout, ob, md):
layout.row().prop(md, "subdivision_type", expand=True)
diff --git a/release/scripts/startup/bl_ui/properties_game.py b/release/scripts/startup/bl_ui/properties_game.py
index 161e4b10cff..16c7adbe5ff 100644
--- a/release/scripts/startup/bl_ui/properties_game.py
+++ b/release/scripts/startup/bl_ui/properties_game.py
@@ -195,6 +195,31 @@ class PHYSICS_PT_game_collision_bounds(PhysicsButtonsPanel, Panel):
row.prop(game, "collision_margin", text="Margin", slider=True)
row.prop(game, "use_collision_compound", text="Compound")
+class PHYSICS_PT_game_obstacles(PhysicsButtonsPanel, bpy.types.Panel):
+ bl_label = "Create obstacle"
+ COMPAT_ENGINES = {'BLENDER_GAME'}
+
+ @classmethod
+ def poll(self, context):
+ game = context.object.game
+ rd = context.scene.render
+ return (game.physics_type in ('DYNAMIC', 'RIGID_BODY', 'SENSOR', 'SOFT_BODY', 'STATIC')) and (rd.engine in cls.COMPAT_ENGINES)
+
+ def draw_header(self, context):
+ game = context.active_object.game
+
+ self.layout.prop(game, "create_obstacle", text="")
+
+ def draw(self, context):
+ layout = self.layout
+
+ game = context.active_object.game
+
+ layout.active = game.create_obstacle
+
+ split = layout.split()
+ col = split.column()
+ col.prop(game, "obstacle_radius", text="Radius")
class RenderButtonsPanel():
bl_space_type = 'PROPERTIES'
@@ -487,5 +512,19 @@ class WORLD_PT_game_physics(WorldButtonsPanel, Panel):
col.label(text="Logic Steps:")
col.prop(gs, "logic_step_max", text="Max")
+class WORLD_PT_game_physics_obstacles(WorldButtonsPanel, bpy.types.Panel):
+ bl_label = "Obstacle simulation"
+ COMPAT_ENGINES = {'BLENDER_GAME'}
+
+ def draw(self, context):
+ layout = self.layout
+
+ gs = context.scene.game_settings
+
+ layout.prop(gs, "obstacle_simulation", text = "Type")
+ if gs.obstacle_simulation != 'None':
+ layout.prop(gs, "level_height")
+ layout.prop(gs, "show_obstacle_simulation")
+
if __name__ == "__main__": # only for live edit.
bpy.utils.register_module(__name__)
diff --git a/release/scripts/startup/bl_ui/properties_scene.py b/release/scripts/startup/bl_ui/properties_scene.py
index 66f967bb6e1..001897c222f 100644
--- a/release/scripts/startup/bl_ui/properties_scene.py
+++ b/release/scripts/startup/bl_ui/properties_scene.py
@@ -331,5 +331,76 @@ class ANIM_OT_keying_set_export(Operator):
wm.fileselect_add(self)
return {'RUNNING_MODAL'}
+class SCENE_PT_navmesh(SceneButtonsPanel, bpy.types.Panel):
+ bl_label = "Navmesh"
+ bl_default_closed = True
+ COMPAT_ENGINES = {'BLENDER_GAME'}
+
+ def draw(self, context):
+ layout = self.layout
+
+ rd = context.scene.game_settings.recast_data
+
+ layout.operator("object.create_navmesh", text='Build navigation mesh')
+
+ layout.label(text="Rasterization:")
+ split = layout.split()
+
+ col = split.column()
+ col.prop(rd, "cell_size")
+ col = split.column()
+ col.prop(rd, "cell_height")
+
+ layout.separator()
+
+ layout.label(text="Agent:")
+ split = layout.split()
+
+ col = split.column()
+ row = col.row()
+ row.prop(rd, "agent_height")
+ row = col.row()
+ row.prop(rd, "agent_radius")
+
+ col = split.column()
+ row = col.row()
+ row.prop(rd, "max_slope")
+ row = col.row()
+ row.prop(rd, "max_climb")
+
+ layout.separator()
+
+ layout.label(text="Region:")
+ split = layout.split()
+ col = split.column()
+ col.prop(rd, "region_min_size")
+
+ col = split.column()
+ col.prop(rd, "region_merge_size")
+
+ layout.separator()
+
+ layout.label(text="Polygonization:")
+ split = layout.split()
+ col = split.column()
+ row = col.row()
+ row.prop(rd, "edge_max_len")
+ row = col.row()
+ row.prop(rd, "edge_max_error")
+
+ col = split.column()
+ row = col.row()
+ row.prop(rd, "verts_per_poly")
+
+ layout.separator()
+
+ layout.label(text="Detail Mesh:")
+ split = layout.split()
+ col = split.column()
+ col.prop(rd, "sample_dist")
+
+ col = split.column()
+ col.prop(rd, "sample_max_error")
+
if __name__ == "__main__": # only for live edit.
bpy.utils.register_module(__name__)
diff --git a/source/blender/blenkernel/intern/customdata.c b/source/blender/blenkernel/intern/customdata.c
index c342bbc917f..883f67c3061 100644
--- a/source/blender/blenkernel/intern/customdata.c
+++ b/source/blender/blenkernel/intern/customdata.c
@@ -867,7 +867,9 @@ static const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
{sizeof(MCol)*4, "MCol", 4, "TexturedCol", NULL, NULL, layerInterp_mcol,
layerSwap_mcol, layerDefault_mcol},
/* 23: CD_CLOTH_ORCO */
- {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL}
+ {sizeof(float)*3, "", 0, NULL, NULL, NULL, NULL, NULL, NULL},
+ /* 24: CD_RECAST */
+ {sizeof(MRecast), "MRecast", 1,"Recast",NULL,NULL,NULL,NULL}
};
static const char *LAYERTYPENAMES[CD_NUMTYPES] = {
@@ -875,7 +877,7 @@ static const char *LAYERTYPENAMES[CD_NUMTYPES] = {
/* 5-9 */ "CDMTFace", "CDMCol", "CDOrigIndex", "CDNormal", "CDFlags",
/* 10-14 */ "CDMFloatProperty", "CDMIntProperty","CDMStringProperty", "CDOrigSpace", "CDOrco",
/* 15-19 */ "CDMTexPoly", "CDMLoopUV", "CDMloopCol", "CDTangent", "CDMDisps",
- /* 20-23 */"CDWeightMCol", "CDIDMCol", "CDTextureMCol", "CDClothOrco"
+ /* 20-24 */"CDWeightMCol", "CDIDMCol", "CDTextureMCol", "CDClothOrco", "CDMRecast"
};
const CustomDataMask CD_MASK_BAREMESH =
@@ -883,14 +885,14 @@ const CustomDataMask CD_MASK_BAREMESH =
const CustomDataMask CD_MASK_MESH =
CD_MASK_MVERT | CD_MASK_MEDGE | CD_MASK_MFACE |
CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE | CD_MASK_MCOL |
- CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR | CD_MASK_MDISPS;
+ CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR | CD_MASK_MDISPS | CD_MASK_RECAST;
const CustomDataMask CD_MASK_EDITMESH =
CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE |
- CD_MASK_MCOL|CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR | CD_MASK_MDISPS;
+ CD_MASK_MCOL|CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR | CD_MASK_MDISPS | CD_MASK_RECAST;
const CustomDataMask CD_MASK_DERIVEDMESH =
CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_MTFACE |
CD_MASK_MCOL | CD_MASK_ORIGINDEX | CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_CLOTH_ORCO |
- CD_MASK_PROP_STR | CD_MASK_ORIGSPACE | CD_MASK_ORCO | CD_MASK_TANGENT | CD_MASK_WEIGHT_MCOL;
+ CD_MASK_PROP_STR | CD_MASK_ORIGSPACE | CD_MASK_ORCO | CD_MASK_TANGENT | CD_MASK_WEIGHT_MCOL | CD_MASK_RECAST;
const CustomDataMask CD_MASK_BMESH =
CD_MASK_MSTICKY | CD_MASK_MDEFORMVERT | CD_MASK_PROP_FLT | CD_MASK_PROP_INT | CD_MASK_PROP_STR;
const CustomDataMask CD_MASK_FACECORNERS =
diff --git a/source/blender/blenkernel/intern/object.c b/source/blender/blenkernel/intern/object.c
index 0dc83084c90..898cec7fa55 100644
--- a/source/blender/blenkernel/intern/object.c
+++ b/source/blender/blenkernel/intern/object.c
@@ -1082,6 +1082,7 @@ Object *add_only_object(int type, const char *name)
ob->state=1;
/* ob->pad3 == Contact Processing Threshold */
ob->m_contactProcessingThreshold = 1.;
+ ob->obstacleRad = 1.;
/* NT fluid sim defaults */
ob->fluidsimFlag = 0;
diff --git a/source/blender/blenkernel/intern/sca.c b/source/blender/blenkernel/intern/sca.c
index 0d523599598..d529a6d94c9 100644
--- a/source/blender/blenkernel/intern/sca.c
+++ b/source/blender/blenkernel/intern/sca.c
@@ -396,6 +396,7 @@ void init_actuator(bActuator *act)
bObjectActuator *oa;
bRandomActuator *ra;
bSoundActuator *sa;
+ bSteeringActuator *sta;
if(act->data) MEM_freeN(act->data);
act->data= NULL;
@@ -470,6 +471,16 @@ void init_actuator(bActuator *act)
case ACT_ARMATURE:
act->data = MEM_callocN(sizeof( bArmatureActuator ), "armature act");
break;
+ case ACT_STEERING:
+ act->data = MEM_callocN(sizeof( bSteeringActuator), "steering act");
+ sta = act->data;
+ sta->acceleration = 3.f;
+ sta->turnspeed = 120.f;
+ sta->dist = 1.f;
+ sta->velocity= 3.f;
+ sta->flag = ACT_STEERING_AUTOMATICFACING;
+ sta->facingaxis = 1;
+ break;
default:
; /* this is very severe... I cannot make any memory for this */
/* logic brick... */
@@ -595,6 +606,11 @@ void set_sca_new_poins_ob(Object *ob)
bPropertyActuator *pa= act->data;
ID_NEW(pa->ob);
}
+ else if(act->type==ACT_STEERING) {
+ bSteeringActuator *sta = act->data;
+ ID_NEW(sta->navmesh);
+ ID_NEW(sta->target);
+ }
}
act= act->next;
}
diff --git a/source/blender/blenkernel/intern/scene.c b/source/blender/blenkernel/intern/scene.c
index d6003a44a7d..1454b3f0ce3 100644
--- a/source/blender/blenkernel/intern/scene.c
+++ b/source/blender/blenkernel/intern/scene.c
@@ -514,6 +514,23 @@ Scene *add_scene(const char *name)
sce->gm.flag = GAME_DISPLAY_LISTS;
sce->gm.matmode = GAME_MAT_MULTITEX;
+ sce->gm.obstacleSimulation= OBSTSIMULATION_NONE;
+ sce->gm.levelHeight = 2.f;
+
+ sce->gm.recastData.cellsize = 0.3f;
+ sce->gm.recastData.cellheight = 0.2f;
+ sce->gm.recastData.agentmaxslope = M_PI/2;
+ sce->gm.recastData.agentmaxclimb = 0.9f;
+ sce->gm.recastData.agentheight = 2.0f;
+ sce->gm.recastData.agentradius = 0.6f;
+ sce->gm.recastData.edgemaxlen = 12.0f;
+ sce->gm.recastData.edgemaxerror = 1.3f;
+ sce->gm.recastData.regionminsize = 50.f;
+ sce->gm.recastData.regionmergesize = 20.f;
+ sce->gm.recastData.vertsperpoly = 6;
+ sce->gm.recastData.detailsampledist = 6.0f;
+ sce->gm.recastData.detailsamplemaxerror = 1.0f;
+
sound_create_scene(sce);
return sce;
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 66e38f1c5f8..f15d7a7b772 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -3895,6 +3895,11 @@ static void lib_link_object(FileData *fd, Main *main)
arma->target= newlibadr(fd, ob->id.lib, arma->target);
arma->subtarget= newlibadr(fd, ob->id.lib, arma->subtarget);
}
+ else if(act->type==ACT_STEERING) {
+ bSteeringActuator *steeringa = act->data;
+ steeringa->target = newlibadr(fd, ob->id.lib, steeringa->target);
+ steeringa->navmesh = newlibadr(fd, ob->id.lib, steeringa->navmesh);
+ }
act= act->next;
}
@@ -11607,6 +11612,23 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
}
}
+ // init facing axis property of steering actuators
+ {
+ Object *ob;
+ for(ob = main->object.first; ob; ob = ob->id.next) {
+ bActuator *act;
+ for(act= ob->actuators.first; act; act= act->next) {
+ if(act->type==ACT_STEERING) {
+ bSteeringActuator* stact = act->data;
+ if (stact->facingaxis==0)
+ {
+ stact->facingaxis=1;
+ }
+ }
+ }
+ }
+ }
+
if (main->versionfile < 256) {
bScreen *sc;
ScrArea *sa;
@@ -11999,6 +12021,43 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
}
+ //set defaults for obstacle avoidance, recast data
+ {
+ Scene *sce;
+ for(sce = main->scene.first; sce; sce = sce->id.next)
+ {
+ if (sce->gm.levelHeight == 0.f)
+ sce->gm.levelHeight = 2.f;
+
+ if(sce->gm.recastData.cellsize == 0.0f)
+ sce->gm.recastData.cellsize = 0.3f;
+ if(sce->gm.recastData.cellheight == 0.0f)
+ sce->gm.recastData.cellheight = 0.2f;
+ if(sce->gm.recastData.agentmaxslope == 0.0f)
+ sce->gm.recastData.agentmaxslope = M_PI/4;
+ if(sce->gm.recastData.agentmaxclimb == 0.0f)
+ sce->gm.recastData.agentmaxclimb = 0.9f;
+ if(sce->gm.recastData.agentheight == 0.0f)
+ sce->gm.recastData.agentheight = 2.0f;
+ if(sce->gm.recastData.agentradius == 0.0f)
+ sce->gm.recastData.agentradius = 0.6f;
+ if(sce->gm.recastData.edgemaxlen == 0.0f)
+ sce->gm.recastData.edgemaxlen = 12.0f;
+ if(sce->gm.recastData.edgemaxerror == 0.0f)
+ sce->gm.recastData.edgemaxerror = 1.3f;
+ if(sce->gm.recastData.regionminsize == 0.0f)
+ sce->gm.recastData.regionminsize = 50.f;
+ if(sce->gm.recastData.regionmergesize == 0.0f)
+ sce->gm.recastData.regionmergesize = 20.f;
+ if(sce->gm.recastData.vertsperpoly<3)
+ sce->gm.recastData.vertsperpoly = 6;
+ if(sce->gm.recastData.detailsampledist == 0.0f)
+ sce->gm.recastData.detailsampledist = 6.0f;
+ if(sce->gm.recastData.detailsamplemaxerror == 0.0f)
+ sce->gm.recastData.detailsamplemaxerror = 1.0f;
+ }
+ }
+
/* WATCH IT!!!: pointers from libdata have not been converted yet here! */
/* WATCH IT 2!: Userdef struct init has to be in editors/interface/resources.c! */
@@ -12903,6 +12962,11 @@ static void expand_object(FileData *fd, Main *mainvar, Object *ob)
bArmatureActuator *arma= act->data;
expand_doit(fd, mainvar, arma->target);
}
+ else if(act->type==ACT_STEERING) {
+ bSteeringActuator *sta= act->data;
+ expand_doit(fd, mainvar, sta->target);
+ expand_doit(fd, mainvar, sta->navmesh);
+ }
act= act->next;
}
diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c
index b982630ec41..9275852d424 100644
--- a/source/blender/blenloader/intern/writefile.c
+++ b/source/blender/blenloader/intern/writefile.c
@@ -1113,6 +1113,9 @@ static void write_actuators(WriteData *wd, ListBase *lb)
case ACT_ARMATURE:
writestruct(wd, DATA, "bArmatureActuator", 1, act->data);
break;
+ case ACT_STEERING:
+ writestruct(wd, DATA, "bSteeringActuator", 1, act->data);
+ break;
default:
; /* error: don't know how to write this file */
}
diff --git a/source/blender/editors/include/ED_navmesh_conversion.h b/source/blender/editors/include/ED_navmesh_conversion.h
new file mode 100644
index 00000000000..28922142f5c
--- /dev/null
+++ b/source/blender/editors/include/ED_navmesh_conversion.h
@@ -0,0 +1,96 @@
+/**
+* $Id$
+*
+* ***** BEGIN GPL LICENSE BLOCK *****
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software Foundation,
+* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+* All rights reserved.
+*
+* The Original Code is: all of this file.
+*
+* Contributor(s): none yet.
+*
+* ***** END GPL LICENSE BLOCK *****
+*/
+
+#ifndef NAVMESH_CONVERSION_H
+#define NAVMESH_CONVERSION_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct DerivedMesh;
+
+/* navmesh_conversion.cpp */
+bool buildNavMeshDataByDerivedMesh(DerivedMesh *dm, int& vertsPerPoly,
+ int &nverts, float *&verts,
+ int &ndtris, unsigned short *&dtris,
+ int& npolys, unsigned short *&dmeshes,
+ unsigned short*& polys, int *&dtrisToPolysMap,
+ int *&dtrisToTrisMap, int *&trisToFacesMap);
+
+bool buildRawVertIndicesData(DerivedMesh* dm, int &nverts, float *&verts,
+ int &ntris, unsigned short *&tris, int *&trisToFacesMap,
+ int *&recastData);
+
+bool buildNavMeshData(const int nverts, const float* verts,
+ const int ntris, const unsigned short *tris,
+ const int* recastData, const int* trisToFacesMap,
+ int &ndtris, unsigned short *&dtris,
+ int &npolys, unsigned short *&dmeshes, unsigned short *&polys,
+ int &vertsPerPoly, int *&dtrisToPolysMap, int *&dtrisToTrisMap);
+
+bool buildPolygonsByDetailedMeshes(const int vertsPerPoly, const int npolys,
+ unsigned short* polys, const unsigned short* dmeshes,
+ const float* verts, const unsigned short* dtris,
+ const int* dtrisToPolysMap);
+
+int polyNumVerts(const unsigned short* p, const int vertsPerPoly);
+bool polyIsConvex(const unsigned short* p, const int vertsPerPoly, const float* verts);
+int polyFindVertex(const unsigned short* p, const int vertsPerPoly, unsigned short vertexIdx);
+float distPointToSegmentSq(const float* point, const float* a, const float* b);
+
+
+inline int bit(int a, int b)
+{
+ return (a & (1 << b)) >> b;
+}
+
+inline void intToCol(int i, float* col)
+{
+ int r = bit(i, 0) + bit(i, 3) * 2 + 1;
+ int g = bit(i, 1) + bit(i, 4) * 2 + 1;
+ int b = bit(i, 2) + bit(i, 5) * 2 + 1;
+ col[0] = 1 - r*63.0f/255.0f;
+ col[1] = 1 - g*63.0f/255.0f;
+ col[2] = 1 - b*63.0f/255.0f;
+}
+
+inline float area2(const float* a, const float* b, const float* c)
+{
+ return (b[0] - a[0]) * (c[2] - a[2]) - (c[0] - a[0]) * (b[2] - a[2]);
+}
+inline bool left(const float* a, const float* b, const float* c)
+{
+ return area2(a, b, c) < 0;
+}
+
+#ifdef __cplusplus
+}
+#endif
+#endif //NAVMESH_CONVERSION_H \ No newline at end of file
diff --git a/source/blender/editors/object/CMakeLists.txt b/source/blender/editors/object/CMakeLists.txt
index 14b40d55f11..de74a0321c7 100644
--- a/source/blender/editors/object/CMakeLists.txt
+++ b/source/blender/editors/object/CMakeLists.txt
@@ -33,6 +33,7 @@ set(INC
../../render/extern/include
../../windowmanager
../../../../intern/guardedalloc
+ ../../../../extern/recastnavigation/Recast/Include
)
set(INC_SYS
@@ -48,6 +49,7 @@ set(SRC
object_hook.c
object_lattice.c
object_modifier.c
+ object_navmesh.cpp
object_ops.c
object_relations.c
object_select.c
diff --git a/source/blender/editors/object/SConscript b/source/blender/editors/object/SConscript
index ca048cb59f9..900c96044a5 100644
--- a/source/blender/editors/object/SConscript
+++ b/source/blender/editors/object/SConscript
@@ -1,12 +1,13 @@
#!/usr/bin/python
Import ('env')
-sources = env.Glob('*.c')
+sources = env.Glob('*.c') + env.Glob('*.cpp')
incs = '../include ../../blenlib ../../blenkernel ../../makesdna ../../imbuf'
incs += ' ../../windowmanager #/intern/guardedalloc ../../blenloader'
incs += ' ../../makesrna ../../python ../../ikplugin'
incs += ' ../../render/extern/include ../../gpu' # for object_bake.c
+incs += ' #extern/recastnavigation/Recast/Include'
defs = []
diff --git a/source/blender/editors/object/object_intern.h b/source/blender/editors/object/object_intern.h
index c308d36f838..3da0723ec18 100644
--- a/source/blender/editors/object/object_intern.h
+++ b/source/blender/editors/object/object_intern.h
@@ -223,5 +223,10 @@ void OBJECT_OT_group_remove(struct wmOperatorType *ot);
/* object_bake.c */
void OBJECT_OT_bake_image(wmOperatorType *ot);
+/* object_navmesh.cpp */
+void OBJECT_OT_create_navmesh(struct wmOperatorType *ot);
+void OBJECT_OT_assign_navpolygon(struct wmOperatorType *ot);
+void OBJECT_OT_assign_new_navpolygon(struct wmOperatorType *ot);
+
#endif /* ED_OBJECT_INTERN_H */
diff --git a/source/blender/editors/object/object_navmesh.cpp b/source/blender/editors/object/object_navmesh.cpp
new file mode 100644
index 00000000000..8ee63296c70
--- /dev/null
+++ b/source/blender/editors/object/object_navmesh.cpp
@@ -0,0 +1,629 @@
+/**
+* $Id$
+*
+* ***** BEGIN GPL LICENSE BLOCK *****
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software Foundation,
+* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+* The Original Code is Copyright (C) 2004 by Blender Foundation
+* All rights reserved.
+*
+* The Original Code is: all of this file.
+*
+* Contributor(s): none yet.
+*
+* ***** END GPL LICENSE BLOCK *****
+*/
+
+#include <math.h>
+#include "Recast.h"
+
+extern "C"
+{
+#include "MEM_guardedalloc.h"
+
+#include "DNA_scene_types.h"
+#include "DNA_object_types.h"
+#include "DNA_meshdata_types.h"
+#include "DNA_modifier_types.h"
+#include "DNA_ID.h"
+
+#include "BKE_library.h"
+#include "BKE_depsgraph.h"
+#include "BKE_context.h"
+#include "BKE_mesh.h"
+#include "BKE_modifier.h"
+#include "BKE_scene.h"
+#include "BKE_DerivedMesh.h"
+#include "BKE_cdderivedmesh.h"
+#include "BLI_editVert.h"
+#include "BLI_listbase.h"
+#include "BLI_utildefines.h"
+#include "ED_object.h"
+#include "BLI_math_vector.h"
+
+#include "RNA_access.h"
+
+#include "ED_mesh.h"
+
+/*mesh/mesh_intern.h */
+extern struct EditVert *addvertlist(EditMesh *em, float *vec, struct EditVert *example);
+extern struct EditFace *addfacelist(EditMesh *em, struct EditVert *v1, struct EditVert *v2, struct EditVert *v3, struct EditVert *v4, struct EditFace *example, struct EditFace *exampleEdges);
+extern void free_vertlist(EditMesh *em, ListBase *edve);
+extern void free_edgelist(EditMesh *em, ListBase *lb);
+extern void free_facelist(EditMesh *em, ListBase *lb);
+
+#include "WM_api.h"
+#include "WM_types.h"
+
+static void createVertsTrisData(bContext *C, LinkNode* obs, int& nverts, float*& verts, int &ntris, int*& tris)
+{
+ MVert *mvert;
+ int nfaces = 0, *tri, i, curnverts, basenverts, curnfaces;
+ MFace *mface;
+ float co[3], wco[3];
+ Object *ob;
+ LinkNode *oblink, *dmlink;
+ DerivedMesh *dm;
+ Scene* scene = CTX_data_scene(C);
+ LinkNode* dms = NULL;
+
+ nverts = 0;
+ ntris = 0;
+ //calculate number of verts and tris
+ for (oblink = obs; oblink; oblink = oblink->next)
+ {
+ ob = (Object*) oblink->link;
+ DerivedMesh *dm = mesh_create_derived_no_virtual(scene, ob, NULL, CD_MASK_MESH);
+ BLI_linklist_append(&dms, (void*)dm);
+
+ nverts += dm->getNumVerts(dm);
+ nfaces = dm->getNumFaces(dm);
+ ntris += nfaces;
+
+ //resolve quad faces
+ mface = dm->getFaceArray(dm);
+ for (i=0; i<nfaces; i++)
+ {
+ MFace* mf = &mface[i];
+ if (mf->v4)
+ ntris+=1;
+ }
+ }
+
+ //create data
+ verts = (float*) MEM_mallocN(sizeof(float)*3*nverts, "verts");
+ tris = (int*) MEM_mallocN(sizeof(int)*3*ntris, "faces");
+
+ basenverts = 0;
+ tri = tris;
+ for (oblink = obs, dmlink = dms; oblink && dmlink;
+ oblink = oblink->next, dmlink = dmlink->next)
+ {
+ ob = (Object*) oblink->link;
+ dm = (DerivedMesh*) dmlink->link;
+
+ curnverts = dm->getNumVerts(dm);
+ mvert = dm->getVertArray(dm);
+ //copy verts
+ for (i=0; i<curnverts; i++)
+ {
+ MVert *v = &mvert[i];
+ copy_v3_v3(co, v->co);
+ mul_v3_m4v3(wco, ob->obmat, co);
+ verts[3*(basenverts+i)+0] = wco[0];
+ verts[3*(basenverts+i)+1] = wco[2];
+ verts[3*(basenverts+i)+2] = wco[1];
+ }
+
+ //create tris
+ curnfaces = dm->getNumFaces(dm);
+ mface = dm->getFaceArray(dm);
+ for (i=0; i<curnfaces; i++)
+ {
+ MFace* mf = &mface[i];
+ tri[0]= basenverts + mf->v1; tri[1]= basenverts + mf->v3; tri[2]= basenverts + mf->v2;
+ tri += 3;
+ if (mf->v4)
+ {
+ tri[0]= basenverts + mf->v1; tri[1]= basenverts + mf->v4; tri[2]= basenverts + mf->v3;
+ tri += 3;
+ }
+ }
+ basenverts += curnverts;
+ }
+
+ //release derived mesh
+ for (dmlink = dms; dmlink; dmlink = dmlink->next)
+ {
+ dm = (DerivedMesh*) dmlink->link;
+ dm->release(dm);
+ }
+ BLI_linklist_free(dms, NULL);
+}
+
+static bool buildNavMesh(const RecastData& recastParams, int nverts, float* verts, int ntris, int* tris,
+ rcPolyMesh*& pmesh, rcPolyMeshDetail*& dmesh)
+{
+ float bmin[3], bmax[3];
+ rcHeightfield* solid;
+ unsigned char *triflags;
+ rcCompactHeightfield* chf;
+ rcContourSet *cset;
+
+ rcCalcBounds(verts, nverts, bmin, bmax);
+
+ //
+ // Step 1. Initialize build config.
+ //
+ rcConfig cfg;
+ memset(&cfg, 0, sizeof(cfg));
+ {
+/*
+ float cellsize = 0.3f;
+ float cellheight = 0.2f;
+ float agentmaxslope = M_PI/4;
+ float agentmaxclimb = 0.9f;
+ float agentheight = 2.0f;
+ float agentradius = 0.6f;
+ float edgemaxlen = 12.0f;
+ float edgemaxerror = 1.3f;
+ float regionminsize = 50.f;
+ float regionmergesize = 20.f;
+ int vertsperpoly = 6;
+ float detailsampledist = 6.0f;
+ float detailsamplemaxerror = 1.0f;
+ cfg.cs = cellsize;
+ cfg.ch = cellheight;
+ cfg.walkableSlopeAngle = agentmaxslope/M_PI*180.f;
+ cfg.walkableHeight = (int)ceilf(agentheight/ cfg.ch);
+ cfg.walkableClimb = (int)floorf(agentmaxclimb / cfg.ch);
+ cfg.walkableRadius = (int)ceilf(agentradius / cfg.cs);
+ cfg.maxEdgeLen = (int)(edgemaxlen/cellsize);
+ cfg.maxSimplificationError = edgemaxerror;
+ cfg.minRegionSize = (int)rcSqr(regionminsize);
+ cfg.mergeRegionSize = (int)rcSqr(regionmergesize);
+ cfg.maxVertsPerPoly = vertsperpoly;
+ cfg.detailSampleDist = detailsampledist< 0.9f ? 0 : cellsize * detailsampledist;
+ cfg.detailSampleMaxError = cellheight * detailsamplemaxerror;
+*/
+ cfg.cs = recastParams.cellsize;
+ cfg.ch = recastParams.cellheight;
+ cfg.walkableSlopeAngle = recastParams.agentmaxslope/((float)M_PI)*180.f;
+ cfg.walkableHeight = (int)ceilf(recastParams.agentheight/ cfg.ch);
+ cfg.walkableClimb = (int)floorf(recastParams.agentmaxclimb / cfg.ch);
+ cfg.walkableRadius = (int)ceilf(recastParams.agentradius / cfg.cs);
+ cfg.maxEdgeLen = (int)(recastParams.edgemaxlen/recastParams.cellsize);
+ cfg.maxSimplificationError = recastParams.edgemaxerror;
+ cfg.minRegionSize = (int)rcSqr(recastParams.regionminsize);
+ cfg.mergeRegionSize = (int)rcSqr(recastParams.regionmergesize);
+ cfg.maxVertsPerPoly = recastParams.vertsperpoly;
+ cfg.detailSampleDist = recastParams.detailsampledist< 0.9f ? 0 :
+ recastParams.cellsize * recastParams.detailsampledist;
+ cfg.detailSampleMaxError = recastParams.cellheight * recastParams.detailsamplemaxerror;
+
+ }
+
+ // Set the area where the navigation will be build.
+ vcopy(cfg.bmin, bmin);
+ vcopy(cfg.bmax, bmax);
+ rcCalcGridSize(cfg.bmin, cfg.bmax, cfg.cs, &cfg.width, &cfg.height);
+
+ //
+ // Step 2. Rasterize input polygon soup.
+ //
+ // Allocate voxel heightfield where we rasterize our input data to.
+ solid = new rcHeightfield;
+ if (!solid)
+ return false;
+
+ if (!rcCreateHeightfield(*solid, cfg.width, cfg.height, cfg.bmin, cfg.bmax, cfg.cs, cfg.ch))
+ return false;
+
+ // Allocate array that can hold triangle flags.
+ triflags = (unsigned char*) MEM_mallocN(sizeof(unsigned char)*ntris, "triflags");
+ if (!triflags)
+ return false;
+ // Find triangles which are walkable based on their slope and rasterize them.
+ memset(triflags, 0, ntris*sizeof(unsigned char));
+ rcMarkWalkableTriangles(cfg.walkableSlopeAngle, verts, nverts, tris, ntris, triflags);
+ rcRasterizeTriangles(verts, nverts, tris, triflags, ntris, *solid);
+ MEM_freeN(triflags);
+ MEM_freeN(verts);
+ MEM_freeN(tris);
+
+ //
+ // Step 3. Filter walkables surfaces.
+ //
+ rcFilterLedgeSpans(cfg.walkableHeight, cfg.walkableClimb, *solid);
+ rcFilterWalkableLowHeightSpans(cfg.walkableHeight, *solid);
+
+ //
+ // Step 4. Partition walkable surface to simple regions.
+ //
+
+ chf = new rcCompactHeightfield;
+ if (!chf)
+ return false;
+ if (!rcBuildCompactHeightfield(cfg.walkableHeight, cfg.walkableClimb, RC_WALKABLE, *solid, *chf))
+ return false;
+
+ delete solid;
+
+ // Prepare for region partitioning, by calculating distance field along the walkable surface.
+ if (!rcBuildDistanceField(*chf))
+ return false;
+
+ // Partition the walkable surface into simple regions without holes.
+ if (!rcBuildRegions(*chf, cfg.walkableRadius, cfg.borderSize, cfg.minRegionSize, cfg.mergeRegionSize))
+ return false;
+
+ //
+ // Step 5. Trace and simplify region contours.
+ //
+ // Create contours.
+ cset = new rcContourSet;
+ if (!cset)
+ return false;
+
+ if (!rcBuildContours(*chf, cfg.maxSimplificationError, cfg.maxEdgeLen, *cset))
+ return false;
+
+ //
+ // Step 6. Build polygons mesh from contours.
+ //
+ pmesh = new rcPolyMesh;
+ if (!pmesh)
+ return false;
+ if (!rcBuildPolyMesh(*cset, cfg.maxVertsPerPoly, *pmesh))
+ return false;
+
+
+ //
+ // Step 7. Create detail mesh which allows to access approximate height on each polygon.
+ //
+
+ dmesh = new rcPolyMeshDetail;
+ if (!dmesh)
+ return false;
+
+ if (!rcBuildPolyMeshDetail(*pmesh, *chf, cfg.detailSampleDist, cfg.detailSampleMaxError, *dmesh))
+ return false;
+
+ delete chf;
+ delete cset;
+
+ return true;
+}
+
+static Object* createRepresentation(bContext *C, rcPolyMesh*& pmesh, rcPolyMeshDetail*& dmesh, Base* base)
+{
+ float co[3], rot[3];
+ EditMesh *em;
+ int i,j, k, polyverts;
+ unsigned short* v;
+ int face[3];
+ Main *bmain = CTX_data_main(C);
+ Scene *scene= CTX_data_scene(C);
+ Object* obedit;
+ int createob = base==NULL;
+ zero_v3(co);
+ zero_v3(rot);
+ if (createob)
+ {
+ //create new object
+ obedit = ED_object_add_type(C, OB_MESH, co, rot, FALSE, 1);
+ }
+ else
+ {
+ obedit = base->object;
+ scene_select_base(scene, base);
+ copy_v3_v3(obedit->loc, co);
+ copy_v3_v3(obedit->rot, rot);
+ }
+
+ ED_object_enter_editmode(C, EM_DO_UNDO|EM_IGNORE_LAYER);
+ em = BKE_mesh_get_editmesh(((Mesh *)obedit->data));
+
+ if (!createob)
+ {
+ //clear
+ if(em->verts.first) free_vertlist(em, &em->verts);
+ if(em->edges.first) free_edgelist(em, &em->edges);
+ if(em->faces.first) free_facelist(em, &em->faces);
+ if(em->selected.first) BLI_freelistN(&(em->selected));
+ }
+
+ //create verts for polygon mesh
+ for(i = 0; i < pmesh->nverts; i++) {
+ v = &pmesh->verts[3*i];
+ co[0] = pmesh->bmin[0] + v[0]*pmesh->cs;
+ co[1] = pmesh->bmin[1] + v[1]*pmesh->ch;
+ co[2] = pmesh->bmin[2] + v[2]*pmesh->cs;
+ SWAP(float, co[1], co[2]);
+ addvertlist(em, co, NULL);
+ }
+ polyverts = pmesh->nverts;
+
+ //create custom data layer to save polygon idx
+ CustomData_add_layer_named(&em->fdata, CD_RECAST, CD_CALLOC, NULL, 0, "recastData");
+
+ //create verts and faces for detailed mesh
+ for (i=0; i<dmesh->nmeshes; i++)
+ {
+ int uniquevbase = em->totvert;
+ unsigned short vbase = dmesh->meshes[4*i+0];
+ unsigned short ndv = dmesh->meshes[4*i+1];
+ unsigned short tribase = dmesh->meshes[4*i+2];
+ unsigned short trinum = dmesh->meshes[4*i+3];
+ const unsigned short* p = &pmesh->polys[i*pmesh->nvp*2];
+ int nv = 0;
+ for (j = 0; j < pmesh->nvp; ++j)
+ {
+ if (p[j] == 0xffff) break;
+ nv++;
+ }
+ //create unique verts
+ for (j=nv; j<ndv; j++)
+ {
+ copy_v3_v3(co, &dmesh->verts[3*(vbase + j)]);
+ SWAP(float, co[1], co[2]);
+ addvertlist(em, co, NULL);
+ }
+
+ EM_init_index_arrays(em, 1, 0, 0);
+
+ //create faces
+ for (j=0; j<trinum; j++)
+ {
+ unsigned char* tri = &dmesh->tris[4*(tribase+j)];
+ EditFace* newFace;
+ for (k=0; k<3; k++)
+ {
+ if (tri[k]<nv)
+ face[k] = p[tri[k]]; //shared vertex
+ else
+ face[k] = uniquevbase+tri[k]-nv; //unique vertex
+ }
+ newFace = addfacelist(em, EM_get_vert_for_index(face[0]), EM_get_vert_for_index(face[2]),
+ EM_get_vert_for_index(face[1]), NULL, NULL, NULL);
+
+ //set navigation polygon idx to the custom layer
+ int* polygonIdx = (int*)CustomData_em_get(&em->fdata, newFace->data, CD_RECAST);
+ *polygonIdx = i+1; //add 1 to avoid zero idx
+ }
+
+ EM_free_index_arrays();
+ }
+
+ delete pmesh; pmesh = NULL;
+ delete dmesh; dmesh = NULL;
+
+ BKE_mesh_end_editmesh((Mesh*)obedit->data, em);
+
+ DAG_id_tag_update((ID*)obedit->data, OB_RECALC_DATA);
+ WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
+
+
+ ED_object_exit_editmode(C, EM_FREEDATA);
+ WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, obedit);
+
+ if (createob)
+ {
+ obedit->gameflag &= ~OB_COLLISION;
+ obedit->gameflag |= OB_NAVMESH;
+ obedit->body_type = OB_BODY_TYPE_NAVMESH;
+ rename_id((ID *)obedit, "Navmesh");
+ }
+
+ ModifierData *md= modifiers_findByType(obedit, eModifierType_NavMesh);
+ if (!md)
+ {
+ ED_object_modifier_add(NULL, bmain, scene, obedit, NULL, eModifierType_NavMesh);
+ }
+
+ return obedit;
+}
+
+static int create_navmesh_exec(bContext *C, wmOperator *op)
+{
+ Scene* scene = CTX_data_scene(C);
+ int nverts, ntris;
+ float* verts;
+ int* tris;
+ rcPolyMesh* pmesh;
+ rcPolyMeshDetail* dmesh;
+ LinkNode* obs = NULL;
+ Base* navmeshBase = NULL;
+ //CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) //expand macros to avoid error in convertion from void*
+ {
+ ListBase ctx_data_list;
+ CollectionPointerLink *ctx_link;
+ CTX_data_selected_editable_bases(C, &ctx_data_list);
+ for(ctx_link = (CollectionPointerLink *)ctx_data_list.first;
+ ctx_link; ctx_link = (CollectionPointerLink *)ctx_link->next) {
+ Base* base= (Base*)ctx_link->ptr.data;
+ {
+ if (base->object->body_type==OB_BODY_TYPE_NAVMESH)
+ {
+ if (!navmeshBase || base==CTX_data_active_base(C))
+ navmeshBase = base;
+ }
+ else
+ BLI_linklist_append(&obs, (void*)base->object);
+ }
+ CTX_DATA_END;
+ createVertsTrisData(C, obs, nverts, verts, ntris, tris);
+ BLI_linklist_free(obs, NULL);
+ buildNavMesh(scene->gm.recastData, nverts, verts, ntris, tris, pmesh, dmesh);
+ createRepresentation(C, pmesh, dmesh, navmeshBase);
+
+ return OPERATOR_FINISHED;
+}
+
+void OBJECT_OT_create_navmesh(wmOperatorType *ot)
+{
+ /* identifiers */
+ ot->name= "Create navigation mesh";
+ ot->description= "Create navigation mesh for selected objects";
+ ot->idname= "OBJECT_OT_create_navmesh";
+
+ /* api callbacks */
+ ot->exec= create_navmesh_exec;
+
+ /* flags */
+ ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+static int assign_navpolygon_poll(bContext *C)
+{
+ Object *ob= (Object *)CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
+ if (!ob || !ob->data)
+ return 0;
+ return (((Mesh*)ob->data)->edit_mesh != NULL);
+}
+
+static int assign_navpolygon_exec(bContext *C, wmOperator *op)
+{
+ Object *obedit= CTX_data_edit_object(C);
+ EditMesh *em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
+
+ //do work here
+ int targetPolyIdx = -1;
+ EditFace *ef, *efa;
+ efa = EM_get_actFace(em, 0);
+ if (efa)
+ {
+ if (CustomData_has_layer(&em->fdata, CD_RECAST))
+ {
+ targetPolyIdx = *(int*)CustomData_em_get(&em->fdata, efa->data, CD_RECAST);
+ targetPolyIdx = targetPolyIdx>=0? targetPolyIdx : -targetPolyIdx;
+ if (targetPolyIdx>0)
+ {
+ //set target poly idx to other selected faces
+ ef = (EditFace*)em->faces.last;
+ while(ef)
+ {
+ if((ef->f & SELECT )&& ef!=efa)
+ {
+ int* recastDataBlock = (int*)CustomData_em_get(&em->fdata, ef->data, CD_RECAST);
+ *recastDataBlock = targetPolyIdx;
+ }
+ ef = ef->prev;
+ }
+ }
+ }
+ }
+
+ DAG_id_tag_update((ID*)obedit->data, OB_RECALC_DATA);
+ WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
+
+ BKE_mesh_end_editmesh((Mesh*)obedit->data, em);
+ return OPERATOR_FINISHED;
+}
+
+void OBJECT_OT_assign_navpolygon(struct wmOperatorType *ot)
+{
+ /* identifiers */
+ ot->name= "Assign polygon index ";
+ ot->description= "Assign polygon index to face by active face";
+ ot->idname= "OBJECT_OT_assign_navpolygon";
+
+ /* api callbacks */
+ ot->poll = assign_navpolygon_poll;
+ ot->exec= assign_navpolygon_exec;
+
+ /* flags */
+ ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+static int compare(const void * a, const void * b){
+ return ( *(int*)a - *(int*)b );
+}
+static int findFreeNavPolyIndex(EditMesh* em)
+{
+ //construct vector of indices
+ int numfaces = em->totface;
+ int* indices = new int[numfaces];
+ EditFace* ef = (EditFace*)em->faces.last;
+ int idx = 0;
+ while(ef)
+ {
+ int polyIdx = *(int*)CustomData_em_get(&em->fdata, ef->data, CD_RECAST);
+ indices[idx] = polyIdx;
+ idx++;
+ ef = ef->prev;
+ }
+ qsort(indices, numfaces, sizeof(int), compare);
+ //search first free index
+ int freeIdx = 1;
+ for (int i=0; i<numfaces; i++)
+ {
+ if (indices[i]==freeIdx)
+ freeIdx++;
+ else if (indices[i]>freeIdx)
+ break;
+ }
+ delete indices;
+ return freeIdx;
+}
+
+static int assign_new_navpolygon_exec(bContext *C, wmOperator *op)
+{
+ Object *obedit= CTX_data_edit_object(C);
+ EditMesh *em= BKE_mesh_get_editmesh((Mesh *)obedit->data);
+
+ EditFace *ef;
+ if (CustomData_has_layer(&em->fdata, CD_RECAST))
+ {
+ int targetPolyIdx = findFreeNavPolyIndex(em);
+ if (targetPolyIdx>0)
+ {
+ //set target poly idx to selected faces
+ ef = (EditFace*)em->faces.last;
+ while(ef)
+ {
+ if(ef->f & SELECT )
+ {
+ int* recastDataBlock = (int*)CustomData_em_get(&em->fdata, ef->data, CD_RECAST);
+ *recastDataBlock = targetPolyIdx;
+ }
+ ef = ef->prev;
+ }
+ }
+ }
+
+ DAG_id_tag_update((ID*)obedit->data, OB_RECALC_DATA);
+ WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
+
+ BKE_mesh_end_editmesh((Mesh*)obedit->data, em);
+ return OPERATOR_FINISHED;
+}
+
+void OBJECT_OT_assign_new_navpolygon(struct wmOperatorType *ot)
+{
+ /* identifiers */
+ ot->name= "Assign new polygon index ";
+ ot->description= "Assign new polygon index to face";
+ ot->idname= "OBJECT_OT_assign_new_navpolygon";
+
+ /* api callbacks */
+ ot->poll = assign_navpolygon_poll;
+ ot->exec= assign_new_navpolygon_exec;
+
+ /* flags */
+ ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+}
diff --git a/source/blender/editors/object/object_ops.c b/source/blender/editors/object/object_ops.c
index 8f00f923b84..29d536c1dab 100644
--- a/source/blender/editors/object/object_ops.c
+++ b/source/blender/editors/object/object_ops.c
@@ -212,6 +212,10 @@ void ED_operatortypes_object(void)
WM_operatortype_append(OBJECT_OT_bake_image);
WM_operatortype_append(OBJECT_OT_drop_named_material);
+
+ WM_operatortype_append(OBJECT_OT_create_navmesh);
+ WM_operatortype_append(OBJECT_OT_assign_navpolygon);
+ WM_operatortype_append(OBJECT_OT_assign_new_navpolygon);
}
void ED_operatormacros_object(void)
diff --git a/source/blender/editors/space_logic/logic_window.c b/source/blender/editors/space_logic/logic_window.c
index 20b001965aa..43a8747e942 100644
--- a/source/blender/editors/space_logic/logic_window.c
+++ b/source/blender/editors/space_logic/logic_window.c
@@ -718,6 +718,8 @@ static const char *actuator_name(int type)
return "State";
case ACT_ARMATURE:
return "Armature";
+ case ACT_STEERING:
+ return "Steering";
}
return "unknown";
}
@@ -4345,6 +4347,48 @@ static void draw_actuator_visibility(uiLayout *layout, PointerRNA *ptr)
uiItemR(row, ptr, "apply_to_children", 0, NULL, ICON_NONE);
}
+static void draw_actuator_steering(uiLayout *layout, PointerRNA *ptr)
+{
+ uiLayout *row;
+ uiLayout *col;
+
+ uiItemR(layout, ptr, "mode", 0, NULL, 0);
+ uiItemR(layout, ptr, "target", 0, NULL, 0);
+ uiItemR(layout, ptr, "navmesh", 0, NULL, 0);
+
+ row = uiLayoutRow(layout, 0);
+ uiItemR(row, ptr, "distance", 0, NULL, 0);
+ uiItemR(row, ptr, "velocity", 0, NULL, 0);
+ row = uiLayoutRow(layout, 0);
+ uiItemR(row, ptr, "acceleration", 0, NULL, 0);
+ uiItemR(row, ptr, "turn_speed", 0, NULL, 0);
+
+ row = uiLayoutRow(layout, 0);
+ col = uiLayoutColumn(row, 0);
+ uiItemR(col, ptr, "facing", 0, NULL, 0);
+ col = uiLayoutColumn(row, 0);
+ uiItemR(col, ptr, "facing_axis", 0, NULL, 0);
+ if (!RNA_boolean_get(ptr, "facing"))
+ {
+ uiLayoutSetActive(col, 0);
+ }
+ col = uiLayoutColumn(row, 0);
+ uiItemR(col, ptr, "normal_up", 0, NULL, 0);
+ if (!RNA_pointer_get(ptr, "navmesh").data)
+ {
+ uiLayoutSetActive(col, 0);
+ }
+
+ row = uiLayoutRow(layout, 0);
+ uiItemR(row, ptr, "self_terminated", 0, NULL, 0);
+ if (RNA_enum_get(ptr, "mode")==ACT_STEERING_PATHFOLLOWING)
+ {
+ uiItemR(row, ptr, "update_period", 0, NULL, 0);
+ row = uiLayoutRow(layout, 0);
+ }
+ uiItemR(row, ptr, "show_visualization", 0, NULL, 0);
+}
+
static void draw_brick_actuator(uiLayout *layout, PointerRNA *ptr, bContext *C)
{
uiLayout *box;
@@ -4406,6 +4450,8 @@ static void draw_brick_actuator(uiLayout *layout, PointerRNA *ptr, bContext *C)
case ACT_VISIBILITY:
draw_actuator_visibility(box, ptr);
break;
+ case ACT_STEERING:
+ draw_actuator_steering(box, ptr);
}
}
diff --git a/source/blender/editors/util/CMakeLists.txt b/source/blender/editors/util/CMakeLists.txt
index 72f13c14f5d..da6e1990622 100644
--- a/source/blender/editors/util/CMakeLists.txt
+++ b/source/blender/editors/util/CMakeLists.txt
@@ -24,6 +24,7 @@ set(INC
../../blenkernel
../../blenlib
../../blenloader
+ ../../../../extern/recastnavigation/Recast/Include
../../makesdna
../../makesrna
../../windowmanager
@@ -39,6 +40,7 @@ set(SRC
editmode_undo.c
numinput.c
undo.c
+ navmesh_conversion.cpp
crazyspace.c
util_intern.h
@@ -62,6 +64,7 @@ set(SRC
../include/ED_markers.h
../include/ED_mball.h
../include/ED_mesh.h
+ ../include/ED_navmesh_conversion.h
../include/ED_node.h
../include/ED_numinput.h
../include/ED_object.h
diff --git a/source/blender/editors/util/SConscript b/source/blender/editors/util/SConscript
index a694b211ca4..0ee64fbf9f2 100644
--- a/source/blender/editors/util/SConscript
+++ b/source/blender/editors/util/SConscript
@@ -1,11 +1,12 @@
#!/usr/bin/python
Import ('env')
-sources = env.Glob('*.c')
+sources = env.Glob('*.c') + env.Glob('*.cpp')
incs = '../include ../../blenlib ../../blenkernel ../../makesdna ../../imbuf'
incs += ' ../../windowmanager #/intern/guardedalloc #/extern/glew/include'
incs += ' ../../makesrna'
+incs += ' #extern/recastnavigation/Recast/Include'
incs += ' ../../blenloader'
-env.BlenderLib ( 'bf_editors_util', sources, Split(incs), [], libtype=['core'], priority=[130] )
+env.BlenderLib ( 'bf_editors_util', sources, Split(incs), [], libtype=['core','player'], priority=[130,210] )
diff --git a/source/blender/editors/util/navmesh_conversion.cpp b/source/blender/editors/util/navmesh_conversion.cpp
new file mode 100644
index 00000000000..873660baa13
--- /dev/null
+++ b/source/blender/editors/util/navmesh_conversion.cpp
@@ -0,0 +1,444 @@
+/**
+* $Id$
+*
+* ***** BEGIN GPL LICENSE BLOCK *****
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software Foundation,
+* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+* All rights reserved.
+*
+* The Original Code is: all of this file.
+*
+* Contributor(s): none yet.
+*
+* ***** END GPL LICENSE BLOCK *****
+*/
+#include <math.h>
+#include "Recast.h"
+
+
+extern "C"{
+#include "ED_navmesh_conversion.h"
+
+#include "DNA_meshdata_types.h"
+#include "BKE_cdderivedmesh.h"
+#include "BLI_math.h"
+}
+
+int polyNumVerts(const unsigned short* p, const int vertsPerPoly)
+{
+ int nv = 0;
+ for (int i=0; i<vertsPerPoly; i++)
+ {
+ if (p[i]==0xffff)
+ break;
+ nv++;
+ }
+ return nv;
+}
+
+bool polyIsConvex(const unsigned short* p, const int vertsPerPoly, const float* verts)
+{
+ int nv = polyNumVerts(p, vertsPerPoly);
+ if (nv<3)
+ return false;
+ for (int j=0; j<nv; j++)
+ {
+ const float* v = &verts[3*p[j]];
+ const float* v_next = &verts[3*p[(j+1)%nv]];
+ const float* v_prev = &verts[3*p[(nv+j-1)%nv]];
+ if (!left(v_prev, v, v_next))
+ return false;
+
+ }
+ return true;
+}
+
+float distPointToSegmentSq(const float* point, const float* a, const float* b)
+{
+ float abx[3], dx[3];
+ vsub(abx, b,a);
+ vsub(dx, point,a);
+ float d = abx[0]*abx[0]+abx[2]*abx[2];
+ float t = abx[0]*dx[0]+abx[2]*dx[2];
+ if (d > 0)
+ t /= d;
+ if (t < 0)
+ t = 0;
+ else if (t > 1)
+ t = 1;
+ dx[0] = a[0] + t*abx[0] - point[0];
+ dx[2] = a[2] + t*abx[2] - point[2];
+ return dx[0]*dx[0] + dx[2]*dx[2];
+}
+
+bool buildRawVertIndicesData(DerivedMesh* dm, int &nverts, float *&verts,
+ int &ntris, unsigned short *&tris, int *&trisToFacesMap,
+ int *&recastData)
+{
+ nverts = dm->getNumVerts(dm);
+ if (nverts>=0xffff)
+ {
+ printf("Converting navmesh: Error! Too many vertices. Max number of vertices %d\n", 0xffff);
+ return false;
+ }
+ verts = new float[3*nverts];
+ dm->getVertCos(dm, (float(*)[3])verts);
+
+ //flip coordinates
+ for (int vi=0; vi<nverts; vi++)
+ {
+ SWAP(float, verts[3*vi+1], verts[3*vi+2]);
+ }
+
+ //calculate number of tris
+ int nfaces = dm->getNumFaces(dm);
+ MFace *faces = dm->getFaceArray(dm);
+ ntris = nfaces;
+ for (int fi=0; fi<nfaces; fi++)
+ {
+ MFace* face = &faces[fi];
+ if (face->v4)
+ ntris++;
+ }
+
+ //copy and transform to triangles (reorder on the run)
+ trisToFacesMap = new int[ntris];
+ tris = new unsigned short[3*ntris];
+ unsigned short* tri = tris;
+ int triIdx = 0;
+ for (int fi=0; fi<nfaces; fi++)
+ {
+ MFace* face = &faces[fi];
+ tri[3*triIdx+0] = (unsigned short) face->v1;
+ tri[3*triIdx+1] = (unsigned short) face->v3;
+ tri[3*triIdx+2] = (unsigned short) face->v2;
+ trisToFacesMap[triIdx++]=fi;
+ if (face->v4)
+ {
+ tri[3*triIdx+0] = (unsigned short) face->v1;
+ tri[3*triIdx+1] = (unsigned short) face->v4;
+ tri[3*triIdx+2] = (unsigned short) face->v3;
+ trisToFacesMap[triIdx++]=fi;
+ }
+ }
+
+ //carefully, recast data is just reference to data in derived mesh
+ recastData = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST);
+ return true;
+}
+
+bool buildPolygonsByDetailedMeshes(const int vertsPerPoly, const int npolys,
+ unsigned short* polys, const unsigned short* dmeshes,
+ const float* verts, const unsigned short* dtris,
+ const int* dtrisToPolysMap)
+{
+ bool res = false;
+ int capacity = vertsPerPoly;
+ unsigned short* newPoly = new unsigned short[capacity];
+ memset(newPoly, 0xff, sizeof(unsigned short)*capacity);
+ for (int polyidx=0; polyidx<npolys; polyidx++)
+ {
+ int nv = 0;
+ //search border
+ int btri = -1;
+ int bedge = -1;
+ int dtrisNum = dmeshes[polyidx*4+3];
+ int dtrisBase = dmeshes[polyidx*4+2];
+ unsigned char *traversedTris = new unsigned char[dtrisNum];
+ memset(traversedTris, 0, dtrisNum*sizeof(unsigned char));
+ for (int j=0; j<dtrisNum && btri==-1;j++)
+ {
+ int curpolytri = dtrisBase+j;
+ for (int k=0; k<3; k++)
+ {
+ unsigned short neighbortri = dtris[curpolytri*3*2+3+k];
+ if ( neighbortri==0xffff || dtrisToPolysMap[neighbortri]!=polyidx+1)
+ {
+ btri = curpolytri;
+ bedge = k;
+ break;
+ }
+ }
+ }
+ if (btri==-1 || bedge==-1)
+ {
+ //can't find triangle with border edge
+ return false;
+ }
+
+ newPoly[nv++] = dtris[btri*3*2+bedge];
+ int tri = btri;
+ int edge = (bedge+1)%3;
+ traversedTris[tri-dtrisBase] = 1;
+ while (tri!=btri || edge!=bedge)
+ {
+ int neighbortri = dtris[tri*3*2+3+edge];
+ if (neighbortri==0xffff || dtrisToPolysMap[neighbortri]!=polyidx+1)
+ {
+ if (nv==capacity)
+ {
+ capacity += vertsPerPoly;
+ unsigned short* newPolyBig = new unsigned short[capacity];
+ memset(newPolyBig, 0xff, sizeof(unsigned short)*capacity);
+ memcpy(newPolyBig, newPoly, sizeof(unsigned short)*nv);
+ delete newPoly;
+ newPoly = newPolyBig;
+ }
+ newPoly[nv++] = dtris[tri*3*2+edge];
+ //move to next edge
+ edge = (edge+1)%3;
+ }
+ else
+ {
+ //move to next tri
+ int twinedge = -1;
+ for (int k=0; k<3; k++)
+ {
+ if (dtris[neighbortri*3*2+3+k] == tri)
+ {
+ twinedge = k;
+ break;
+ }
+ }
+ if (twinedge==-1)
+ {
+ printf("Converting navmesh: Error! Can't find neighbor edge - invalid adjacency info\n");
+ goto returnLabel;
+ }
+ tri = neighbortri;
+ edge = (twinedge+1)%3;
+ traversedTris[tri-dtrisBase] = 1;
+ }
+ }
+
+ unsigned short* adjustedPoly = new unsigned short[nv];
+ int adjustedNv = 0;
+ for (size_t i=0; i<(size_t)nv; i++)
+ {
+ unsigned short prev = newPoly[(nv+i-1)%nv];
+ unsigned short cur = newPoly[i];
+ unsigned short next = newPoly[(i+1)%nv];
+ float distSq = distPointToSegmentSq(&verts[3*cur], &verts[3*prev], &verts[3*next]);
+ static const float tolerance = 0.001f;
+ if (distSq>tolerance)
+ adjustedPoly[adjustedNv++] = cur;
+ }
+ memcpy(newPoly, adjustedPoly, adjustedNv*sizeof(unsigned short));
+ delete adjustedPoly;
+ nv = adjustedNv;
+
+ bool allBorderTraversed = true;
+ for (size_t i=0; i<(size_t)dtrisNum; i++)
+ {
+ if (traversedTris[i]==0)
+ {
+ //check whether it has border edges
+ int curpolytri = dtrisBase+i;
+ for (int k=0; k<3; k++)
+ {
+ unsigned short neighbortri = dtris[curpolytri*3*2+3+k];
+ if ( neighbortri==0xffff || dtrisToPolysMap[neighbortri]!=polyidx+1)
+ {
+ allBorderTraversed = false;
+ break;
+ }
+ }
+ }
+ }
+
+ if (nv<=vertsPerPoly && allBorderTraversed)
+ {
+ for (int i=0; i<nv; i++)
+ {
+ polys[polyidx*vertsPerPoly*2+i] = newPoly[i];
+ }
+ }
+ }
+ res = true;
+
+returnLabel:
+ delete newPoly;
+ return true;
+}
+
+struct SortContext
+{
+ const int* recastData;
+ const int* trisToFacesMap;
+};
+static int compareByData(void* data, const void * a, const void * b){
+ SortContext* context = (SortContext*)data;
+ return ( context->recastData[context->trisToFacesMap[*(int*)a]] -
+ context->recastData[context->trisToFacesMap[*(int*)b]] );
+}
+
+bool buildNavMeshData(const int nverts, const float* verts,
+ const int ntris, const unsigned short *tris,
+ const int* recastData, const int* trisToFacesMap,
+ int &ndtris, unsigned short *&dtris,
+ int &npolys, unsigned short *&dmeshes, unsigned short *&polys,
+ int &vertsPerPoly, int *&dtrisToPolysMap, int *&dtrisToTrisMap)
+
+{
+ if (!recastData)
+ {
+ printf("Converting navmesh: Error! Can't find recast custom data\n");
+ return false;
+ }
+
+ //sort the triangles by polygon idx
+ int* trisMapping = new int[ntris];
+ for (int i=0; i<ntris; i++)
+ trisMapping[i]=i;
+ SortContext context;
+ context.recastData = recastData;
+ context.trisToFacesMap = trisToFacesMap;
+ qsort_s(trisMapping, ntris, sizeof(int), compareByData, &context);
+
+ //search first valid triangle - triangle of convex polygon
+ int validTriStart = -1;
+ for (int i=0; i< ntris; i++)
+ {
+ if (recastData[trisToFacesMap[trisMapping[i]]]>0)
+ {
+ validTriStart = i;
+ break;
+ }
+ }
+
+ if (validTriStart<0)
+ {
+ printf("Converting navmesh: Error! No valid polygons in mesh\n");
+ delete trisMapping;
+ return false;
+ }
+
+ ndtris = ntris-validTriStart;
+ //fill dtris to faces mapping
+ dtrisToTrisMap = new int[ndtris];
+ memcpy(dtrisToTrisMap, &trisMapping[validTriStart], ndtris*sizeof(int));
+ delete trisMapping; trisMapping=NULL;
+
+ //create detailed mesh triangles - copy only valid triangles
+ //and reserve memory for adjacency info
+ dtris = new unsigned short[3*2*ndtris];
+ memset(dtris, 0xffff, sizeof(unsigned short)*3*2*ndtris);
+ for (int i=0; i<ndtris; i++)
+ {
+ memcpy(dtris+3*2*i, tris+3*dtrisToTrisMap[i], sizeof(unsigned short)*3);
+ }
+ //create new recast data corresponded to dtris and renumber for continuous indices
+ int prevPolyIdx=-1, curPolyIdx, newPolyIdx=0;
+ dtrisToPolysMap = new int[ndtris];
+ for (int i=0; i<ndtris; i++)
+ {
+ curPolyIdx = recastData[trisToFacesMap[dtrisToTrisMap[i]]];
+ if (curPolyIdx!=prevPolyIdx)
+ {
+ newPolyIdx++;
+ prevPolyIdx=curPolyIdx;
+ }
+ dtrisToPolysMap[i] = newPolyIdx;
+ }
+
+
+ //build adjacency info for detailed mesh triangles
+ buildMeshAdjacency(dtris, ndtris, nverts, 3);
+
+ //create detailed mesh description for each navigation polygon
+ npolys = dtrisToPolysMap[ndtris-1];
+ dmeshes = new unsigned short[npolys*4];
+ memset(dmeshes, 0, npolys*4*sizeof(unsigned short));
+ unsigned short *dmesh = NULL;
+ int prevpolyidx = 0;
+ for (int i=0; i<ndtris; i++)
+ {
+ int curpolyidx = dtrisToPolysMap[i];
+ if (curpolyidx!=prevpolyidx)
+ {
+ if (curpolyidx!=prevpolyidx+1)
+ {
+ printf("Converting navmesh: Error! Wrong order of detailed mesh faces\n");
+ return false;
+ }
+ dmesh = dmesh==NULL ? dmeshes : dmesh+4;
+ dmesh[2] = (unsigned short)i; //tbase
+ dmesh[3] = 0; //tnum
+ prevpolyidx = curpolyidx;
+ }
+ dmesh[3]++;
+ }
+
+ //create navigation polygons
+ vertsPerPoly = 6;
+ polys = new unsigned short[npolys*vertsPerPoly*2];
+ memset(polys, 0xff, sizeof(unsigned short)*vertsPerPoly*2*npolys);
+
+ buildPolygonsByDetailedMeshes(vertsPerPoly, npolys, polys, dmeshes, verts, dtris, dtrisToPolysMap);
+
+ return true;
+}
+
+
+bool buildNavMeshDataByDerivedMesh(DerivedMesh *dm, int& vertsPerPoly,
+ int &nverts, float *&verts,
+ int &ndtris, unsigned short *&dtris,
+ int& npolys, unsigned short *&dmeshes,
+ unsigned short*& polys, int *&dtrisToPolysMap,
+ int *&dtrisToTrisMap, int *&trisToFacesMap)
+{
+ bool res = true;
+ int ntris =0, *recastData=NULL;
+ unsigned short *tris=NULL;
+ res = buildRawVertIndicesData(dm, nverts, verts, ntris, tris, trisToFacesMap, recastData);
+ if (!res)
+ {
+ printf("Converting navmesh: Error! Can't get vertices and indices from mesh\n");
+ goto exit;
+ }
+
+ res = buildNavMeshData(nverts, verts, ntris, tris, recastData, trisToFacesMap,
+ ndtris, dtris, npolys, dmeshes,polys, vertsPerPoly,
+ dtrisToPolysMap, dtrisToTrisMap);
+ if (!res)
+ {
+ printf("Converting navmesh: Error! Can't get vertices and indices from mesh\n");
+ goto exit;
+ }
+
+exit:
+ if (tris)
+ delete tris;
+
+ return res;
+}
+
+int polyFindVertex(const unsigned short* p, const int vertsPerPoly, unsigned short vertexIdx)
+{
+ int res = -1;
+ for(int i=0; i<vertsPerPoly; i++)
+ {
+ if (p[i]==0xffff)
+ break;
+ if (p[i]==vertexIdx)
+ {
+ res = i;
+ break;
+ }
+ }
+ return res;
+} \ No newline at end of file
diff --git a/source/blender/gpu/intern/gpu_shader_material.glsl b/source/blender/gpu/intern/gpu_shader_material.glsl
index 815b74a1bf4..06520755651 100644
--- a/source/blender/gpu/intern/gpu_shader_material.glsl
+++ b/source/blender/gpu/intern/gpu_shader_material.glsl
@@ -1,3 +1,1779 @@
+<<<<<<< .working
+
+float exp_blender(float f)
+{
+ return pow(2.71828182846, f);
+}
+
+void rgb_to_hsv(vec4 rgb, out vec4 outcol)
+{
+ float cmax, cmin, h, s, v, cdelta;
+ vec3 c;
+
+ cmax = max(rgb[0], max(rgb[1], rgb[2]));
+ cmin = min(rgb[0], min(rgb[1], rgb[2]));
+ cdelta = cmax-cmin;
+
+ v = cmax;
+ if (cmax!=0.0)
+ s = cdelta/cmax;
+ else {
+ s = 0.0;
+ h = 0.0;
+ }
+
+ if (s == 0.0) {
+ h = 0.0;
+ }
+ else {
+ c = (vec3(cmax, cmax, cmax) - rgb.xyz)/cdelta;
+
+ if (rgb.x==cmax) h = c[2] - c[1];
+ else if (rgb.y==cmax) h = 2.0 + c[0] - c[2];
+ else h = 4.0 + c[1] - c[0];
+
+ h /= 6.0;
+
+ if (h<0.0)
+ h += 1.0;
+ }
+
+ outcol = vec4(h, s, v, rgb.w);
+}
+
+void hsv_to_rgb(vec4 hsv, out vec4 outcol)
+{
+ float i, f, p, q, t, h, s, v;
+ vec3 rgb;
+
+ h = hsv[0];
+ s = hsv[1];
+ v = hsv[2];
+
+ if(s==0.0) {
+ rgb = vec3(v, v, v);
+ }
+ else {
+ if(h==1.0)
+ h = 0.0;
+
+ h *= 6.0;
+ i = floor(h);
+ f = h - i;
+ rgb = vec3(f, f, f);
+ p = v*(1.0-s);
+ q = v*(1.0-(s*f));
+ t = v*(1.0-(s*(1.0-f)));
+
+ if (i == 0.0) rgb = vec3(v, t, p);
+ else if (i == 1.0) rgb = vec3(q, v, p);
+ else if (i == 2.0) rgb = vec3(p, v, t);
+ else if (i == 3.0) rgb = vec3(p, q, v);
+ else if (i == 4.0) rgb = vec3(t, p, v);
+ else rgb = vec3(v, p, q);
+ }
+
+ outcol = vec4(rgb, hsv.w);
+}
+
+float srgb_to_linearrgb(float c)
+{
+ if(c < 0.04045)
+ return (c < 0.0)? 0.0: c * (1.0/12.92);
+ else
+ return pow((c + 0.055)*(1.0/1.055), 2.4);
+}
+
+float linearrgb_to_srgb(float c)
+{
+ if(c < 0.0031308)
+ return (c < 0.0)? 0.0: c * 12.92;
+ else
+ return 1.055 * pow(c, 1.0/2.4) - 0.055;
+}
+
+void srgb_to_linearrgb(vec4 col_from, out vec4 col_to)
+{
+ col_to.r = srgb_to_linearrgb(col_from.r);
+ col_to.g = srgb_to_linearrgb(col_from.g);
+ col_to.b = srgb_to_linearrgb(col_from.b);
+ col_to.a = col_from.a;
+}
+
+void linearrgb_to_srgb(vec4 col_from, out vec4 col_to)
+{
+ col_to.r = linearrgb_to_srgb(col_from.r);
+ col_to.g = linearrgb_to_srgb(col_from.g);
+ col_to.b = linearrgb_to_srgb(col_from.b);
+ col_to.a = col_from.a;
+}
+
+#define M_PI 3.14159265358979323846
+
+/*********** SHADER NODES ***************/
+
+void vcol_attribute(vec4 attvcol, out vec4 vcol)
+{
+ vcol = vec4(attvcol.x/255.0, attvcol.y/255.0, attvcol.z/255.0, 1.0);
+}
+
+void uv_attribute(vec2 attuv, out vec3 uv)
+{
+ uv = vec3(attuv*2.0 - vec2(1.0, 1.0), 0.0);
+}
+
+void geom(vec3 co, vec3 nor, mat4 viewinvmat, vec3 attorco, vec2 attuv, vec4 attvcol, out vec3 global, out vec3 local, out vec3 view, out vec3 orco, out vec3 uv, out vec3 normal, out vec4 vcol, out float frontback)
+{
+ local = co;
+ view = normalize(local);
+ global = (viewinvmat*vec4(local, 1.0)).xyz;
+ orco = attorco;
+ uv_attribute(attuv, uv);
+ normal = -normalize(nor); /* blender render normal is negated */
+ vcol_attribute(attvcol, vcol);
+ frontback = 1.0;
+}
+
+void mapping(vec3 vec, mat4 mat, vec3 minvec, vec3 maxvec, float domin, float domax, out vec3 outvec)
+{
+ outvec = (mat * vec4(vec, 1.0)).xyz;
+ if(domin == 1.0)
+ outvec = max(outvec, minvec);
+ if(domax == 1.0)
+ outvec = min(outvec, maxvec);
+}
+
+void camera(vec3 co, out vec3 outview, out float outdepth, out float outdist)
+{
+ outdepth = abs(co.z);
+ outdist = length(co);
+ outview = normalize(co);
+}
+
+void math_add(float val1, float val2, out float outval)
+{
+ outval = val1 + val2;
+}
+
+void math_subtract(float val1, float val2, out float outval)
+{
+ outval = val1 - val2;
+}
+
+void math_multiply(float val1, float val2, out float outval)
+{
+ outval = val1 * val2;
+}
+
+void math_divide(float val1, float val2, out float outval)
+{
+ if (val2 == 0.0)
+ outval = 0.0;
+ else
+ outval = val1 / val2;
+}
+
+void math_sine(float val, out float outval)
+{
+ outval = sin(val);
+}
+
+void math_cosine(float val, out float outval)
+{
+ outval = cos(val);
+}
+
+void math_tangent(float val, out float outval)
+{
+ outval = tan(val);
+}
+
+void math_asin(float val, out float outval)
+{
+ if (val <= 1.0 && val >= -1.0)
+ outval = asin(val);
+ else
+ outval = 0.0;
+}
+
+void math_acos(float val, out float outval)
+{
+ if (val <= 1.0 && val >= -1.0)
+ outval = acos(val);
+ else
+ outval = 0.0;
+}
+
+void math_atan(float val, out float outval)
+{
+ outval = atan(val);
+}
+
+void math_pow(float val1, float val2, out float outval)
+{
+ if (val1 >= 0.0)
+ outval = pow(val1, val2);
+ else
+ outval = 0.0;
+}
+
+void math_log(float val1, float val2, out float outval)
+{
+ if(val1 > 0.0 && val2 > 0.0)
+ outval= log2(val1) / log2(val2);
+ else
+ outval= 0.0;
+}
+
+void math_max(float val1, float val2, out float outval)
+{
+ outval = max(val1, val2);
+}
+
+void math_min(float val1, float val2, out float outval)
+{
+ outval = min(val1, val2);
+}
+
+void math_round(float val, out float outval)
+{
+ outval= floor(val + 0.5);
+}
+
+void math_less_than(float val1, float val2, out float outval)
+{
+ if(val1 < val2)
+ outval = 1.0;
+ else
+ outval = 0.0;
+}
+
+void math_greater_than(float val1, float val2, out float outval)
+{
+ if(val1 > val2)
+ outval = 1.0;
+ else
+ outval = 0.0;
+}
+
+void squeeze(float val, float width, float center, out float outval)
+{
+ outval = 1.0/(1.0 + pow(2.71828183, -((val-center)*width)));
+}
+
+void vec_math_add(vec3 v1, vec3 v2, out vec3 outvec, out float outval)
+{
+ outvec = v1 + v2;
+ outval = (abs(outvec[0]) + abs(outvec[1]) + abs(outvec[2]))/3.0;
+}
+
+void vec_math_sub(vec3 v1, vec3 v2, out vec3 outvec, out float outval)
+{
+ outvec = v1 - v2;
+ outval = (abs(outvec[0]) + abs(outvec[1]) + abs(outvec[2]))/3.0;
+}
+
+void vec_math_average(vec3 v1, vec3 v2, out vec3 outvec, out float outval)
+{
+ outvec = v1 + v2;
+ outval = length(outvec);
+ outvec = normalize(outvec);
+}
+
+void vec_math_dot(vec3 v1, vec3 v2, out vec3 outvec, out float outval)
+{
+ outvec = vec3(0, 0, 0);
+ outval = dot(v1, v2);
+}
+
+void vec_math_cross(vec3 v1, vec3 v2, out vec3 outvec, out float outval)
+{
+ outvec = cross(v1, v2);
+ outval = length(outvec);
+}
+
+void vec_math_normalize(vec3 v, out vec3 outvec, out float outval)
+{
+ outval = length(v);
+ outvec = normalize(v);
+}
+
+void vec_math_negate(vec3 v, out vec3 outv)
+{
+ outv = -v;
+}
+
+void normal(vec3 dir, vec3 nor, out vec3 outnor, out float outdot)
+{
+ outnor = dir;
+ outdot = -dot(dir, nor);
+}
+
+void curves_vec(float fac, vec3 vec, sampler1D curvemap, out vec3 outvec)
+{
+ outvec.x = texture1D(curvemap, (vec.x + 1.0)*0.5).x;
+ outvec.y = texture1D(curvemap, (vec.y + 1.0)*0.5).y;
+ outvec.z = texture1D(curvemap, (vec.z + 1.0)*0.5).z;
+
+ if (fac != 1.0)
+ outvec = (outvec*fac) + (vec*(1.0-fac));
+
+}
+
+void curves_rgb(float fac, vec4 col, sampler1D curvemap, out vec4 outcol)
+{
+ outcol.r = texture1D(curvemap, texture1D(curvemap, col.r).a).r;
+ outcol.g = texture1D(curvemap, texture1D(curvemap, col.g).a).g;
+ outcol.b = texture1D(curvemap, texture1D(curvemap, col.b).a).b;
+
+ if (fac != 1.0)
+ outcol = (outcol*fac) + (col*(1.0-fac));
+
+ outcol.a = col.a;
+}
+
+void set_value(float val, out float outval)
+{
+ outval = val;
+}
+
+void set_rgb(vec3 col, out vec3 outcol)
+{
+ outcol = col;
+}
+
+void set_rgba(vec4 col, out vec4 outcol)
+{
+ outcol = col;
+}
+
+void set_value_zero(out float outval)
+{
+ outval = 0.0;
+}
+
+void set_value_one(out float outval)
+{
+ outval = 1.0;
+}
+
+void set_rgb_zero(out vec3 outval)
+{
+ outval = vec3(0.0);
+}
+
+void set_rgba_zero(out vec4 outval)
+{
+ outval = vec4(0.0);
+}
+
+void mix_blend(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ outcol = mix(col1, col2, fac);
+ outcol.a = col1.a;
+}
+
+void mix_add(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ outcol = mix(col1, col1 + col2, fac);
+ outcol.a = col1.a;
+}
+
+void mix_mult(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ outcol = mix(col1, col1 * col2, fac);
+ outcol.a = col1.a;
+}
+
+void mix_screen(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ float facm = 1.0 - fac;
+
+ outcol = vec4(1.0) - (vec4(facm) + fac*(vec4(1.0) - col2))*(vec4(1.0) - col1);
+ outcol.a = col1.a;
+}
+
+void mix_overlay(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ float facm = 1.0 - fac;
+
+ outcol = col1;
+
+ if(outcol.r < 0.5)
+ outcol.r *= facm + 2.0*fac*col2.r;
+ else
+ outcol.r = 1.0 - (facm + 2.0*fac*(1.0 - col2.r))*(1.0 - outcol.r);
+
+ if(outcol.g < 0.5)
+ outcol.g *= facm + 2.0*fac*col2.g;
+ else
+ outcol.g = 1.0 - (facm + 2.0*fac*(1.0 - col2.g))*(1.0 - outcol.g);
+
+ if(outcol.b < 0.5)
+ outcol.b *= facm + 2.0*fac*col2.b;
+ else
+ outcol.b = 1.0 - (facm + 2.0*fac*(1.0 - col2.b))*(1.0 - outcol.b);
+}
+
+void mix_sub(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ outcol = mix(col1, col1 - col2, fac);
+ outcol.a = col1.a;
+}
+
+void mix_div(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ float facm = 1.0 - fac;
+
+ outcol = col1;
+
+ if(col2.r != 0.0) outcol.r = facm*outcol.r + fac*outcol.r/col2.r;
+ if(col2.g != 0.0) outcol.g = facm*outcol.g + fac*outcol.g/col2.g;
+ if(col2.b != 0.0) outcol.b = facm*outcol.b + fac*outcol.b/col2.b;
+}
+
+void mix_diff(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ outcol = mix(col1, abs(col1 - col2), fac);
+ outcol.a = col1.a;
+}
+
+void mix_dark(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ outcol.rgb = min(col1.rgb, col2.rgb*fac);
+ outcol.a = col1.a;
+}
+
+void mix_light(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ outcol.rgb = max(col1.rgb, col2.rgb*fac);
+ outcol.a = col1.a;
+}
+
+void mix_dodge(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ outcol = col1;
+
+ if(outcol.r != 0.0) {
+ float tmp = 1.0 - fac*col2.r;
+ if(tmp <= 0.0)
+ outcol.r = 1.0;
+ else if((tmp = outcol.r/tmp) > 1.0)
+ outcol.r = 1.0;
+ else
+ outcol.r = tmp;
+ }
+ if(outcol.g != 0.0) {
+ float tmp = 1.0 - fac*col2.g;
+ if(tmp <= 0.0)
+ outcol.g = 1.0;
+ else if((tmp = outcol.g/tmp) > 1.0)
+ outcol.g = 1.0;
+ else
+ outcol.g = tmp;
+ }
+ if(outcol.b != 0.0) {
+ float tmp = 1.0 - fac*col2.b;
+ if(tmp <= 0.0)
+ outcol.b = 1.0;
+ else if((tmp = outcol.b/tmp) > 1.0)
+ outcol.b = 1.0;
+ else
+ outcol.b = tmp;
+ }
+}
+
+void mix_burn(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ float tmp, facm = 1.0 - fac;
+
+ outcol = col1;
+
+ tmp = facm + fac*col2.r;
+ if(tmp <= 0.0)
+ outcol.r = 0.0;
+ else if((tmp = (1.0 - (1.0 - outcol.r)/tmp)) < 0.0)
+ outcol.r = 0.0;
+ else if(tmp > 1.0)
+ outcol.r = 1.0;
+ else
+ outcol.r = tmp;
+
+ tmp = facm + fac*col2.g;
+ if(tmp <= 0.0)
+ outcol.g = 0.0;
+ else if((tmp = (1.0 - (1.0 - outcol.g)/tmp)) < 0.0)
+ outcol.g = 0.0;
+ else if(tmp > 1.0)
+ outcol.g = 1.0;
+ else
+ outcol.g = tmp;
+
+ tmp = facm + fac*col2.b;
+ if(tmp <= 0.0)
+ outcol.b = 0.0;
+ else if((tmp = (1.0 - (1.0 - outcol.b)/tmp)) < 0.0)
+ outcol.b = 0.0;
+ else if(tmp > 1.0)
+ outcol.b = 1.0;
+ else
+ outcol.b = tmp;
+}
+
+void mix_hue(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ float facm = 1.0 - fac;
+
+ outcol = col1;
+
+ vec4 hsv, hsv2, tmp;
+ rgb_to_hsv(col2, hsv2);
+
+ if(hsv2.y != 0.0) {
+ rgb_to_hsv(outcol, hsv);
+ hsv.x = hsv2.x;
+ hsv_to_rgb(hsv, tmp);
+
+ outcol = mix(outcol, tmp, fac);
+ outcol.a = col1.a;
+ }
+}
+
+void mix_sat(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ float facm = 1.0 - fac;
+
+ outcol = col1;
+
+ vec4 hsv, hsv2;
+ rgb_to_hsv(outcol, hsv);
+
+ if(hsv.y != 0.0) {
+ rgb_to_hsv(col2, hsv2);
+
+ hsv.y = facm*hsv.y + fac*hsv2.y;
+ hsv_to_rgb(hsv, outcol);
+ }
+}
+
+void mix_val(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ float facm = 1.0 - fac;
+
+ vec4 hsv, hsv2;
+ rgb_to_hsv(col1, hsv);
+ rgb_to_hsv(col2, hsv2);
+
+ hsv.z = facm*hsv.z + fac*hsv2.z;
+ hsv_to_rgb(hsv, outcol);
+}
+
+void mix_color(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ float facm = 1.0 - fac;
+
+ outcol = col1;
+
+ vec4 hsv, hsv2, tmp;
+ rgb_to_hsv(col2, hsv2);
+
+ if(hsv2.y != 0.0) {
+ rgb_to_hsv(outcol, hsv);
+ hsv.x = hsv2.x;
+ hsv.y = hsv2.y;
+ hsv_to_rgb(hsv, tmp);
+
+ outcol = mix(outcol, tmp, fac);
+ outcol.a = col1.a;
+ }
+}
+
+void mix_soft(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+ float facm = 1.0 - fac;
+
+ vec4 one= vec4(1.0);
+ vec4 scr= one - (one - col2)*(one - col1);
+ outcol = facm*col1 + fac*((one - col1)*col2*col1 + col1*scr);
+}
+
+void mix_linear(float fac, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ fac = clamp(fac, 0.0, 1.0);
+
+ outcol = col1;
+
+ if(col2.r > 0.5)
+ outcol.r= col1.r + fac*(2.0*(col2.r - 0.5));
+ else
+ outcol.r= col1.r + fac*(2.0*(col2.r) - 1.0);
+
+ if(col2.g > 0.5)
+ outcol.g= col1.g + fac*(2.0*(col2.g - 0.5));
+ else
+ outcol.g= col1.g + fac*(2.0*(col2.g) - 1.0);
+
+ if(col2.b > 0.5)
+ outcol.b= col1.b + fac*(2.0*(col2.b - 0.5));
+ else
+ outcol.b= col1.b + fac*(2.0*(col2.b) - 1.0);
+}
+
+void valtorgb(float fac, sampler1D colormap, out vec4 outcol, out float outalpha)
+{
+ outcol = texture1D(colormap, fac);
+ outalpha = outcol.a;
+}
+
+void rgbtobw(vec4 color, out float outval)
+{
+ outval = color.r*0.35 + color.g*0.45 + color.b*0.2; /* keep these factors in sync with texture.h:RGBTOBW */
+}
+
+void invert(float fac, vec4 col, out vec4 outcol)
+{
+ outcol.xyz = mix(col.xyz, vec3(1.0, 1.0, 1.0) - col.xyz, fac);
+ outcol.w = col.w;
+}
+
+void hue_sat(float hue, float sat, float value, float fac, vec4 col, out vec4 outcol)
+{
+ vec4 hsv;
+
+ rgb_to_hsv(col, hsv);
+
+ hsv[0] += (hue - 0.5);
+ if(hsv[0]>1.0) hsv[0]-=1.0; else if(hsv[0]<0.0) hsv[0]+= 1.0;
+ hsv[1] *= sat;
+ if(hsv[1]>1.0) hsv[1]= 1.0; else if(hsv[1]<0.0) hsv[1]= 0.0;
+ hsv[2] *= value;
+ if(hsv[2]>1.0) hsv[2]= 1.0; else if(hsv[2]<0.0) hsv[2]= 0.0;
+
+ hsv_to_rgb(hsv, outcol);
+
+ outcol = mix(col, outcol, fac);
+}
+
+void separate_rgb(vec4 col, out float r, out float g, out float b)
+{
+ r = col.r;
+ g = col.g;
+ b = col.b;
+}
+
+void combine_rgb(float r, float g, float b, out vec4 col)
+{
+ col = vec4(r, g, b, 1.0);
+}
+
+void output_node(vec4 rgb, float alpha, out vec4 outrgb)
+{
+ outrgb = vec4(rgb.rgb, alpha);
+}
+
+/*********** TEXTURES ***************/
+
+void texture_flip_blend(vec3 vec, out vec3 outvec)
+{
+ outvec = vec.yxz;
+}
+
+void texture_blend_lin(vec3 vec, out float outval)
+{
+ outval = (1.0+vec.x)/2.0;
+}
+
+void texture_blend_quad(vec3 vec, out float outval)
+{
+ outval = max((1.0+vec.x)/2.0, 0.0);
+ outval *= outval;
+}
+
+void texture_wood_sin(vec3 vec, out float value, out vec4 color, out vec3 normal)
+{
+ float a = sqrt(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z)*20.0;
+ float wi = 0.5 + 0.5*sin(a);
+
+ value = wi;
+ color = vec4(wi, wi, wi, 1.0);
+ normal = vec3(0.0, 0.0, 0.0);
+}
+
+void texture_image(vec3 vec, sampler2D ima, out float value, out vec4 color, out vec3 normal)
+{
+ color = texture2D(ima, (vec.xy + vec2(1.0, 1.0))*0.5);
+ value = 1.0;
+
+ normal.x = 2.0*(color.r - 0.5);
+ normal.y = 2.0*(0.5 - color.g);
+ normal.z = 2.0*(color.b - 0.5);
+}
+
+/************* MTEX *****************/
+
+void texco_orco(vec3 attorco, out vec3 orco)
+{
+ orco = attorco;
+}
+
+void texco_uv(vec2 attuv, out vec3 uv)
+{
+ /* disabled for now, works together with leaving out mtex_2d_mapping
+ uv = vec3(attuv*2.0 - vec2(1.0, 1.0), 0.0); */
+ uv = vec3(attuv, 0.0);
+}
+
+void texco_norm(vec3 normal, out vec3 outnormal)
+{
+ /* corresponds to shi->orn, which is negated so cancels
+ out blender normal negation */
+ outnormal = normalize(normal);
+}
+
+void texco_tangent(vec3 tangent, out vec3 outtangent)
+{
+ outtangent = normalize(tangent);
+}
+
+void texco_global(mat4 viewinvmat, vec3 co, out vec3 global)
+{
+ global = (viewinvmat*vec4(co, 1.0)).xyz;
+}
+
+void texco_object(mat4 viewinvmat, mat4 obinvmat, vec3 co, out vec3 object)
+{
+ object = (obinvmat*(viewinvmat*vec4(co, 1.0))).xyz;
+}
+
+void texco_refl(vec3 vn, vec3 view, out vec3 ref)
+{
+ ref = view - 2.0*dot(vn, view)*vn;
+}
+
+void shade_norm(vec3 normal, out vec3 outnormal)
+{
+ /* blender render normal is negated */
+ outnormal = -normalize(normal);
+}
+
+void mtex_rgb_blend(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
+{
+ float facm;
+
+ fact *= facg;
+ facm = 1.0-fact;
+
+ incol = fact*texcol + facm*outcol;
+}
+
+void mtex_rgb_mul(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
+{
+ float facm;
+
+ fact *= facg;
+ facm = 1.0-facg;
+
+ incol = (facm + fact*texcol)*outcol;
+}
+
+void mtex_rgb_screen(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
+{
+ float facm;
+
+ fact *= facg;
+ facm = 1.0-facg;
+
+ incol = vec3(1.0) - (vec3(facm) + fact*(vec3(1.0) - texcol))*(vec3(1.0) - outcol);
+}
+
+void mtex_rgb_overlay(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
+{
+ float facm;
+
+ fact *= facg;
+ facm = 1.0-facg;
+
+ if(outcol.r < 0.5)
+ incol.r = outcol.r*(facm + 2.0*fact*texcol.r);
+ else
+ incol.r = 1.0 - (facm + 2.0*fact*(1.0 - texcol.r))*(1.0 - outcol.r);
+
+ if(outcol.g < 0.5)
+ incol.g = outcol.g*(facm + 2.0*fact*texcol.g);
+ else
+ incol.g = 1.0 - (facm + 2.0*fact*(1.0 - texcol.g))*(1.0 - outcol.g);
+
+ if(outcol.b < 0.5)
+ incol.b = outcol.b*(facm + 2.0*fact*texcol.b);
+ else
+ incol.b = 1.0 - (facm + 2.0*fact*(1.0 - texcol.b))*(1.0 - outcol.b);
+}
+
+void mtex_rgb_sub(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
+{
+ incol = -fact*facg*texcol + outcol;
+}
+
+void mtex_rgb_add(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
+{
+ incol = fact*facg*texcol + outcol;
+}
+
+void mtex_rgb_div(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
+{
+ float facm;
+
+ fact *= facg;
+ facm = 1.0-fact;
+
+ if(texcol.r != 0.0) incol.r = facm*outcol.r + fact*outcol.r/texcol.r;
+ if(texcol.g != 0.0) incol.g = facm*outcol.g + fact*outcol.g/texcol.g;
+ if(texcol.b != 0.0) incol.b = facm*outcol.b + fact*outcol.b/texcol.b;
+}
+
+void mtex_rgb_diff(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
+{
+ float facm;
+
+ fact *= facg;
+ facm = 1.0-fact;
+
+ incol = facm*outcol + fact*abs(texcol - outcol);
+}
+
+void mtex_rgb_dark(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
+{
+ float facm, col;
+
+ fact *= facg;
+ facm = 1.0-fact;
+
+ col = fact*texcol.r;
+ if(col < outcol.r) incol.r = col; else incol.r = outcol.r;
+ col = fact*texcol.g;
+ if(col < outcol.g) incol.g = col; else incol.g = outcol.g;
+ col = fact*texcol.b;
+ if(col < outcol.b) incol.b = col; else incol.b = outcol.b;
+}
+
+void mtex_rgb_light(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
+{
+ float facm, col;
+
+ fact *= facg;
+ facm = 1.0-fact;
+
+ col = fact*texcol.r;
+ if(col > outcol.r) incol.r = col; else incol.r = outcol.r;
+ col = fact*texcol.g;
+ if(col > outcol.g) incol.g = col; else incol.g = outcol.g;
+ col = fact*texcol.b;
+ if(col > outcol.b) incol.b = col; else incol.b = outcol.b;
+}
+
+void mtex_rgb_hue(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
+{
+ vec4 col;
+
+ mix_hue(fact*facg, vec4(outcol, 1.0), vec4(texcol, 1.0), col);
+ incol.rgb = col.rgb;
+}
+
+void mtex_rgb_sat(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
+{
+ vec4 col;
+
+ mix_sat(fact*facg, vec4(outcol, 1.0), vec4(texcol, 1.0), col);
+ incol.rgb = col.rgb;
+}
+
+void mtex_rgb_val(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
+{
+ vec4 col;
+
+ mix_val(fact*facg, vec4(outcol, 1.0), vec4(texcol, 1.0), col);
+ incol.rgb = col.rgb;
+}
+
+void mtex_rgb_color(vec3 outcol, vec3 texcol, float fact, float facg, out vec3 incol)
+{
+ vec4 col;
+
+ mix_color(fact*facg, vec4(outcol, 1.0), vec4(texcol, 1.0), col);
+ incol.rgb = col.rgb;
+}
+
+void mtex_value_vars(inout float fact, float facg, out float facm)
+{
+ fact *= abs(facg);
+ facm = 1.0-fact;
+
+ if(facg < 0.0) {
+ float tmp = fact;
+ fact = facm;
+ facm = tmp;
+ }
+}
+
+void mtex_value_blend(float outcol, float texcol, float fact, float facg, out float incol)
+{
+ float facm;
+ mtex_value_vars(fact, facg, facm);
+
+ incol = fact*texcol + facm*outcol;
+}
+
+void mtex_value_mul(float outcol, float texcol, float fact, float facg, out float incol)
+{
+ float facm;
+ mtex_value_vars(fact, facg, facm);
+
+ facm = 1.0 - facg;
+ incol = (facm + fact*texcol)*outcol;
+}
+
+void mtex_value_screen(float outcol, float texcol, float fact, float facg, out float incol)
+{
+ float facm;
+ mtex_value_vars(fact, facg, facm);
+
+ facm = 1.0 - facg;
+ incol = 1.0 - (facm + fact*(1.0 - texcol))*(1.0 - outcol);
+}
+
+void mtex_value_sub(float outcol, float texcol, float fact, float facg, out float incol)
+{
+ float facm;
+ mtex_value_vars(fact, facg, facm);
+
+ fact = -fact;
+ incol = fact*texcol + outcol;
+}
+
+void mtex_value_add(float outcol, float texcol, float fact, float facg, out float incol)
+{
+ float facm;
+ mtex_value_vars(fact, facg, facm);
+
+ fact = fact;
+ incol = fact*texcol + outcol;
+}
+
+void mtex_value_div(float outcol, float texcol, float fact, float facg, out float incol)
+{
+ float facm;
+ mtex_value_vars(fact, facg, facm);
+
+ if(texcol != 0.0)
+ incol = facm*outcol + fact*outcol/texcol;
+ else
+ incol = 0.0;
+}
+
+void mtex_value_diff(float outcol, float texcol, float fact, float facg, out float incol)
+{
+ float facm;
+ mtex_value_vars(fact, facg, facm);
+
+ incol = facm*outcol + fact*abs(texcol - outcol);
+}
+
+void mtex_value_dark(float outcol, float texcol, float fact, float facg, out float incol)
+{
+ float facm;
+ mtex_value_vars(fact, facg, facm);
+
+ float col = fact*texcol;
+ if(col < outcol) incol = col; else incol = outcol;
+}
+
+void mtex_value_light(float outcol, float texcol, float fact, float facg, out float incol)
+{
+ float facm;
+ mtex_value_vars(fact, facg, facm);
+
+ float col = fact*texcol;
+ if(col > outcol) incol = col; else incol = outcol;
+}
+
+void mtex_value_clamp_positive(float fac, out float outfac)
+{
+ outfac = max(fac, 0.0);
+}
+
+void mtex_value_clamp(float fac, out float outfac)
+{
+ outfac = clamp(fac, 0.0, 1.0);
+}
+
+void mtex_har_divide(float har, out float outhar)
+{
+ outhar = har/128.0;
+}
+
+void mtex_har_multiply_clamp(float har, out float outhar)
+{
+ har *= 128.0;
+
+ if(har < 1.0) outhar = 1.0;
+ else if(har > 511.0) outhar = 511.0;
+ else outhar = har;
+}
+
+void mtex_alpha_from_col(vec4 col, out float alpha)
+{
+ alpha = col.a;
+}
+
+void mtex_alpha_to_col(vec4 col, float alpha, out vec4 outcol)
+{
+ outcol = vec4(col.rgb, alpha);
+}
+
+void mtex_rgbtoint(vec4 rgb, out float intensity)
+{
+ intensity = dot(vec3(0.35, 0.45, 0.2), rgb.rgb);
+}
+
+void mtex_value_invert(float invalue, out float outvalue)
+{
+ outvalue = 1.0 - invalue;
+}
+
+void mtex_rgb_invert(vec4 inrgb, out vec4 outrgb)
+{
+ outrgb = vec4(vec3(1.0) - inrgb.rgb, inrgb.a);
+}
+
+void mtex_value_stencil(float stencil, float intensity, out float outstencil, out float outintensity)
+{
+ float fact = intensity;
+ outintensity = intensity*stencil;
+ outstencil = stencil*fact;
+}
+
+void mtex_rgb_stencil(float stencil, vec4 rgb, out float outstencil, out vec4 outrgb)
+{
+ float fact = rgb.a;
+ outrgb = vec4(rgb.rgb, rgb.a*stencil);
+ outstencil = stencil*fact;
+}
+
+void mtex_mapping_ofs(vec3 texco, vec3 ofs, out vec3 outtexco)
+{
+ outtexco = texco + ofs;
+}
+
+void mtex_mapping_size(vec3 texco, vec3 size, out vec3 outtexco)
+{
+ outtexco = size*texco;
+}
+
+void mtex_2d_mapping(vec3 vec, out vec3 outvec)
+{
+ outvec = vec3(vec.xy*0.5 + vec2(0.5, 0.5), vec.z);
+}
+
+void mtex_image(vec3 texco, sampler2D ima, out float value, out vec4 color)
+{
+ color = texture2D(ima, texco.xy);
+ value = 1.0;
+}
+
+void mtex_normal(vec3 texco, sampler2D ima, out vec3 normal)
+{
+ vec4 color = texture2D(ima, texco.xy);
+ normal = 2.0*(vec3(color.r, -color.g, color.b) - vec3(0.5, -0.5, 0.5));
+}
+
+void mtex_bump_normals_init( vec3 vN, out vec3 vNorg, out vec3 vNacc, out float fPrevMagnitude )
+{
+ vNorg = vN;
+ vNacc = vN;
+ fPrevMagnitude = 1.0;
+}
+
+/** helper method to extract the upper left 3x3 matrix from a 4x4 matrix */
+mat3 to_mat3(mat4 m4)
+{
+ mat3 m3;
+ m3[0] = m4[0].xyz;
+ m3[1] = m4[1].xyz;
+ m3[2] = m4[2].xyz;
+ return m3;
+}
+
+void mtex_bump_init_objspace( vec3 surf_pos, vec3 surf_norm,
+ mat4 mView, mat4 mViewInv, mat4 mObj, mat4 mObjInv,
+ float fPrevMagnitude_in, vec3 vNacc_in,
+ out float fPrevMagnitude_out, out vec3 vNacc_out,
+ out vec3 vR1, out vec3 vR2, out float fDet )
+{
+ mat3 obj2view = to_mat3(mView * mObj);
+ mat3 view2obj = to_mat3(mObjInv * mViewInv);
+
+ vec3 vSigmaS = view2obj * dFdx( surf_pos );
+ vec3 vSigmaT = view2obj * dFdy( surf_pos );
+ vec3 vN = normalize( surf_norm * obj2view );
+
+ vR1 = cross( vSigmaT, vN );
+ vR2 = cross( vN, vSigmaS ) ;
+ fDet = dot ( vSigmaS, vR1 );
+
+ /* pretransform vNacc (in mtex_bump_apply) using the inverse transposed */
+ vR1 = vR1 * view2obj;
+ vR2 = vR2 * view2obj;
+ vN = vN * view2obj;
+
+ float fMagnitude = abs(fDet) * length(vN);
+ vNacc_out = vNacc_in * (fMagnitude / fPrevMagnitude_in);
+ fPrevMagnitude_out = fMagnitude;
+}
+
+void mtex_bump_init_texturespace( vec3 surf_pos, vec3 surf_norm,
+ float fPrevMagnitude_in, vec3 vNacc_in,
+ out float fPrevMagnitude_out, out vec3 vNacc_out,
+ out vec3 vR1, out vec3 vR2, out float fDet )
+{
+ vec3 vSigmaS = dFdx( surf_pos );
+ vec3 vSigmaT = dFdy( surf_pos );
+ vec3 vN = surf_norm; /* normalized interpolated vertex normal */
+
+ vR1 = normalize( cross( vSigmaT, vN ) );
+ vR2 = normalize( cross( vN, vSigmaS ) );
+ fDet = sign( dot(vSigmaS, vR1) );
+
+ float fMagnitude = abs(fDet);
+ vNacc_out = vNacc_in * (fMagnitude / fPrevMagnitude_in);
+ fPrevMagnitude_out = fMagnitude;
+}
+
+void mtex_bump_init_viewspace( vec3 surf_pos, vec3 surf_norm,
+ float fPrevMagnitude_in, vec3 vNacc_in,
+ out float fPrevMagnitude_out, out vec3 vNacc_out,
+ out vec3 vR1, out vec3 vR2, out float fDet )
+{
+ vec3 vSigmaS = dFdx( surf_pos );
+ vec3 vSigmaT = dFdy( surf_pos );
+ vec3 vN = surf_norm; /* normalized interpolated vertex normal */
+
+ vR1 = cross( vSigmaT, vN );
+ vR2 = cross( vN, vSigmaS ) ;
+ fDet = dot ( vSigmaS, vR1 );
+
+ float fMagnitude = abs(fDet);
+ vNacc_out = vNacc_in * (fMagnitude / fPrevMagnitude_in);
+ fPrevMagnitude_out = fMagnitude;
+}
+
+void mtex_bump_tap3( vec3 texco, sampler2D ima, float hScale,
+ out float dBs, out float dBt )
+{
+ vec2 STll = texco.xy;
+ vec2 STlr = texco.xy + dFdx(texco.xy) ;
+ vec2 STul = texco.xy + dFdy(texco.xy) ;
+
+ float Hll,Hlr,Hul;
+ rgbtobw( texture2D(ima, STll), Hll );
+ rgbtobw( texture2D(ima, STlr), Hlr );
+ rgbtobw( texture2D(ima, STul), Hul );
+
+ dBs = hScale * (Hlr - Hll);
+ dBt = hScale * (Hul - Hll);
+}
+
+void mtex_bump_tap5( vec3 texco, sampler2D ima, float hScale,
+ out float dBs, out float dBt )
+{
+ vec2 TexDx = dFdx(texco.xy);
+ vec2 TexDy = dFdy(texco.xy);
+
+ vec2 STc = texco.xy;
+ vec2 STl = texco.xy - 0.5 * TexDx ;
+ vec2 STr = texco.xy + 0.5 * TexDx ;
+ vec2 STd = texco.xy - 0.5 * TexDy ;
+ vec2 STu = texco.xy + 0.5 * TexDy ;
+
+ float Hc,Hl,Hr,Hd,Hu;
+ rgbtobw( texture2D(ima, STc), Hc );
+ rgbtobw( texture2D(ima, STl), Hl );
+ rgbtobw( texture2D(ima, STr), Hr );
+ rgbtobw( texture2D(ima, STd), Hd );
+ rgbtobw( texture2D(ima, STu), Hu );
+
+ dBs = hScale * (Hr - Hl);
+ dBt = hScale * (Hu - Hd);
+}
+
+void mtex_bump_apply( float fDet, float dBs, float dBt, vec3 vR1, vec3 vR2, vec3 vNacc_in,
+ out vec3 vNacc_out, out vec3 perturbed_norm )
+{
+ vec3 vSurfGrad = sign(fDet) * ( dBs * vR1 + dBt * vR2 );
+
+ vNacc_out = vNacc_in - vSurfGrad;
+ perturbed_norm = normalize( vNacc_out );
+}
+
+void mtex_bump_apply_texspace( float fDet, float dBs, float dBt, vec3 vR1, vec3 vR2,
+ sampler2D ima, vec3 texco, float ima_x, float ima_y, vec3 vNacc_in,
+ out vec3 vNacc_out, out vec3 perturbed_norm )
+{
+ vec2 TexDx = dFdx(texco.xy);
+ vec2 TexDy = dFdy(texco.xy);
+
+ vec3 vSurfGrad = sign(fDet) * (
+ dBs / length( vec2(ima_x*TexDx.x, ima_y*TexDx.y) ) * vR1 +
+ dBt / length( vec2(ima_x*TexDy.x, ima_y*TexDy.y) ) * vR2 );
+
+ vNacc_out = vNacc_in - vSurfGrad;
+ perturbed_norm = normalize( vNacc_out );
+}
+
+void mtex_negate_texnormal(vec3 normal, out vec3 outnormal)
+{
+ outnormal = vec3(-normal.x, -normal.y, normal.z);
+}
+
+void mtex_nspace_tangent(vec3 tangent, vec3 normal, vec3 texnormal, out vec3 outnormal)
+{
+ tangent = normalize(tangent);
+ vec3 B = cross(normal, tangent);
+
+ outnormal = texnormal.x*tangent + texnormal.y*B + texnormal.z*normal;
+ outnormal = normalize(outnormal);
+}
+
+void mtex_blend_normal(float norfac, vec3 normal, vec3 newnormal, out vec3 outnormal)
+{
+ outnormal = (1.0 - norfac)*normal + norfac*newnormal;
+ outnormal = normalize(outnormal);
+}
+
+/******* MATERIAL *********/
+
+void lamp_visibility_sun_hemi(vec3 lampvec, out vec3 lv, out float dist, out float visifac)
+{
+ lv = lampvec;
+ dist = 1.0;
+ visifac = 1.0;
+}
+
+void lamp_visibility_other(vec3 co, vec3 lampco, out vec3 lv, out float dist, out float visifac)
+{
+ lv = co - lampco;
+ dist = length(lv);
+ lv = normalize(lv);
+ visifac = 1.0;
+}
+
+void lamp_falloff_invlinear(float lampdist, float dist, out float visifac)
+{
+ visifac = lampdist/(lampdist + dist);
+}
+
+void lamp_falloff_invsquare(float lampdist, float dist, out float visifac)
+{
+ visifac = lampdist/(lampdist + dist*dist);
+}
+
+void lamp_falloff_sliders(float lampdist, float ld1, float ld2, float dist, out float visifac)
+{
+ float lampdistkw = lampdist*lampdist;
+
+ visifac = lampdist/(lampdist + ld1*dist);
+ visifac *= lampdistkw/(lampdistkw + ld2*dist*dist);
+}
+
+void lamp_falloff_curve(float lampdist, sampler1D curvemap, float dist, out float visifac)
+{
+ visifac = texture1D(curvemap, dist/lampdist).x;
+}
+
+void lamp_visibility_sphere(float lampdist, float dist, float visifac, out float outvisifac)
+{
+ float t= lampdist - dist;
+
+ outvisifac= visifac*max(t, 0.0)/lampdist;
+}
+
+void lamp_visibility_spot_square(vec3 lampvec, mat4 lampimat, vec3 lv, out float inpr)
+{
+ if(dot(lv, lampvec) > 0.0) {
+ vec3 lvrot = (lampimat*vec4(lv, 0.0)).xyz;
+ float x = max(abs(lvrot.x/lvrot.z), abs(lvrot.y/lvrot.z));
+
+ inpr = 1.0/sqrt(1.0 + x*x);
+ }
+ else
+ inpr = 0.0;
+}
+
+void lamp_visibility_spot_circle(vec3 lampvec, vec3 lv, out float inpr)
+{
+ inpr = dot(lv, lampvec);
+}
+
+void lamp_visibility_spot(float spotsi, float spotbl, float inpr, float visifac, out float outvisifac)
+{
+ float t = spotsi;
+
+ if(inpr <= t) {
+ outvisifac = 0.0;
+ }
+ else {
+ t = inpr - t;
+
+ /* soft area */
+ if(spotbl != 0.0)
+ inpr *= smoothstep(0.0, 1.0, t/spotbl);
+
+ outvisifac = visifac*inpr;
+ }
+}
+
+void lamp_visibility_clamp(float visifac, out float outvisifac)
+{
+ outvisifac = (visifac < 0.001)? 0.0: visifac;
+}
+
+void shade_view(vec3 co, out vec3 view)
+{
+ /* handle perspective/orthographic */
+ view = (gl_ProjectionMatrix[3][3] == 0.0)? normalize(co): vec3(0.0, 0.0, -1.0);
+}
+
+void shade_tangent_v(vec3 lv, vec3 tang, out vec3 vn)
+{
+ vec3 c = cross(lv, tang);
+ vec3 vnor = cross(c, tang);
+
+ vn = -normalize(vnor);
+}
+
+void shade_inp(vec3 vn, vec3 lv, out float inp)
+{
+ inp = dot(vn, lv);
+}
+
+void shade_is_no_diffuse(out float is)
+{
+ is = 0.0;
+}
+
+void shade_is_hemi(float inp, out float is)
+{
+ is = 0.5*inp + 0.5;
+}
+
+float area_lamp_energy(mat4 area, vec3 co, vec3 vn)
+{
+ vec3 vec[4], c[4];
+ float rad[4], fac;
+
+ vec[0] = normalize(co - area[0].xyz);
+ vec[1] = normalize(co - area[1].xyz);
+ vec[2] = normalize(co - area[2].xyz);
+ vec[3] = normalize(co - area[3].xyz);
+
+ c[0] = normalize(cross(vec[0], vec[1]));
+ c[1] = normalize(cross(vec[1], vec[2]));
+ c[2] = normalize(cross(vec[2], vec[3]));
+ c[3] = normalize(cross(vec[3], vec[0]));
+
+ rad[0] = acos(dot(vec[0], vec[1]));
+ rad[1] = acos(dot(vec[1], vec[2]));
+ rad[2] = acos(dot(vec[2], vec[3]));
+ rad[3] = acos(dot(vec[3], vec[0]));
+
+ fac= rad[0]*dot(vn, c[0]);
+ fac+= rad[1]*dot(vn, c[1]);
+ fac+= rad[2]*dot(vn, c[2]);
+ fac+= rad[3]*dot(vn, c[3]);
+
+ return max(fac, 0.0);
+}
+
+void shade_inp_area(vec3 position, vec3 lampco, vec3 lampvec, vec3 vn, mat4 area, float areasize, float k, out float inp)
+{
+ vec3 co = position;
+ vec3 vec = co - lampco;
+
+ if(dot(vec, lampvec) < 0.0) {
+ inp = 0.0;
+ }
+ else {
+ float intens = area_lamp_energy(area, co, vn);
+
+ inp = pow(intens*areasize, k);
+ }
+}
+
+void shade_diffuse_oren_nayer(float nl, vec3 n, vec3 l, vec3 v, float rough, out float is)
+{
+ vec3 h = normalize(v + l);
+ float nh = max(dot(n, h), 0.0);
+ float nv = max(dot(n, v), 0.0);
+ float realnl = dot(n, l);
+
+ if(realnl < 0.0) {
+ is = 0.0;
+ }
+ else if(nl < 0.0) {
+ is = 0.0;
+ }
+ else {
+ float vh = max(dot(v, h), 0.0);
+ float Lit_A = acos(realnl);
+ float View_A = acos(nv);
+
+ vec3 Lit_B = normalize(l - realnl*n);
+ vec3 View_B = normalize(v - nv*n);
+
+ float t = max(dot(Lit_B, View_B), 0.0);
+
+ float a, b;
+
+ if(Lit_A > View_A) {
+ a = Lit_A;
+ b = View_A;
+ }
+ else {
+ a = View_A;
+ b = Lit_A;
+ }
+
+ float A = 1.0 - (0.5*((rough*rough)/((rough*rough) + 0.33)));
+ float B = 0.45*((rough*rough)/((rough*rough) + 0.09));
+
+ b *= 0.95;
+ is = nl*(A + (B * t * sin(a) * tan(b)));
+ }
+}
+
+void shade_diffuse_toon(vec3 n, vec3 l, vec3 v, float size, float tsmooth, out float is)
+{
+ float rslt = dot(n, l);
+ float ang = acos(rslt);
+
+ if(ang < size) is = 1.0;
+ else if(ang > (size + tsmooth) || tsmooth == 0.0) is = 0.0;
+ else is = 1.0 - ((ang - size)/tsmooth);
+}
+
+void shade_diffuse_minnaert(float nl, vec3 n, vec3 v, float darkness, out float is)
+{
+ if(nl <= 0.0) {
+ is = 0.0;
+ }
+ else {
+ float nv = max(dot(n, v), 0.0);
+
+ if(darkness <= 1.0)
+ is = nl*pow(max(nv*nl, 0.1), darkness - 1.0);
+ else
+ is = nl*pow(1.0001 - nv, darkness - 1.0);
+ }
+}
+
+float fresnel_fac(vec3 view, vec3 vn, float grad, float fac)
+{
+ float t1, t2;
+ float ffac;
+
+ if(fac==0.0) {
+ ffac = 1.0;
+ }
+ else {
+ t1= dot(view, vn);
+ if(t1>0.0) t2= 1.0+t1;
+ else t2= 1.0-t1;
+
+ t2= grad + (1.0-grad)*pow(t2, fac);
+
+ if(t2<0.0) ffac = 0.0;
+ else if(t2>1.0) ffac = 1.0;
+ else ffac = t2;
+ }
+
+ return ffac;
+}
+
+void shade_diffuse_fresnel(vec3 vn, vec3 lv, vec3 view, float fac_i, float fac, out float is)
+{
+ is = fresnel_fac(lv, vn, fac_i, fac);
+}
+
+void shade_cubic(float is, out float outis)
+{
+ if(is>0.0 && is<1.0)
+ outis= smoothstep(0.0, 1.0, is);
+ else
+ outis= is;
+}
+
+void shade_visifac(float i, float visifac, float refl, out float outi)
+{
+ /*if(i > 0.0)*/
+ outi = max(i*visifac*refl, 0.0);
+ /*else
+ outi = i;*/
+}
+
+void shade_tangent_v_spec(vec3 tang, out vec3 vn)
+{
+ vn = tang;
+}
+
+void shade_add_to_diffuse(float i, vec3 lampcol, vec3 col, out vec3 outcol)
+{
+ if(i > 0.0)
+ outcol = i*lampcol*col;
+ else
+ outcol = vec3(0.0, 0.0, 0.0);
+}
+
+void shade_hemi_spec(vec3 vn, vec3 lv, vec3 view, float spec, float hard, float visifac, out float t)
+{
+ lv += view;
+ lv = normalize(lv);
+
+ t = dot(vn, lv);
+ t = 0.5*t + 0.5;
+
+ t = visifac*spec*pow(t, hard);
+}
+
+void shade_phong_spec(vec3 n, vec3 l, vec3 v, float hard, out float specfac)
+{
+ vec3 h = normalize(l + v);
+ float rslt = max(dot(h, n), 0.0);
+
+ specfac = pow(rslt, hard);
+}
+
+void shade_cooktorr_spec(vec3 n, vec3 l, vec3 v, float hard, out float specfac)
+{
+ vec3 h = normalize(v + l);
+ float nh = dot(n, h);
+
+ if(nh < 0.0) {
+ specfac = 0.0;
+ }
+ else {
+ float nv = max(dot(n, v), 0.0);
+ float i = pow(nh, hard);
+
+ i = i/(0.1+nv);
+ specfac = i;
+ }
+}
+
+void shade_blinn_spec(vec3 n, vec3 l, vec3 v, float refrac, float spec_power, out float specfac)
+{
+ if(refrac < 1.0) {
+ specfac = 0.0;
+ }
+ else if(spec_power == 0.0) {
+ specfac = 0.0;
+ }
+ else {
+ if(spec_power<100.0)
+ spec_power= sqrt(1.0/spec_power);
+ else
+ spec_power= 10.0/spec_power;
+
+ vec3 h = normalize(v + l);
+ float nh = dot(n, h);
+ if(nh < 0.0) {
+ specfac = 0.0;
+ }
+ else {
+ float nv = max(dot(n, v), 0.01);
+ float nl = dot(n, l);
+ if(nl <= 0.01) {
+ specfac = 0.0;
+ }
+ else {
+ float vh = max(dot(v, h), 0.01);
+
+ float a = 1.0;
+ float b = (2.0*nh*nv)/vh;
+ float c = (2.0*nh*nl)/vh;
+
+ float g = 0.0;
+
+ if(a < b && a < c) g = a;
+ else if(b < a && b < c) g = b;
+ else if(c < a && c < b) g = c;
+
+ float p = sqrt(((refrac * refrac)+(vh*vh)-1.0));
+ float f = (((p-vh)*(p-vh))/((p+vh)*(p+vh)))*(1.0+((((vh*(p+vh))-1.0)*((vh*(p+vh))-1.0))/(((vh*(p-vh))+1.0)*((vh*(p-vh))+1.0))));
+ float ang = acos(nh);
+
+ specfac = max(f*g*exp_blender((-(ang*ang)/(2.0*spec_power*spec_power))), 0.0);
+ }
+ }
+ }
+}
+
+void shade_wardiso_spec(vec3 n, vec3 l, vec3 v, float rms, out float specfac)
+{
+ vec3 h = normalize(l + v);
+ float nh = max(dot(n, h), 0.001);
+ float nv = max(dot(n, v), 0.001);
+ float nl = max(dot(n, l), 0.001);
+ float angle = tan(acos(nh));
+ float alpha = max(rms, 0.001);
+
+ specfac= nl * (1.0/(4.0*M_PI*alpha*alpha))*(exp_blender(-(angle*angle)/(alpha*alpha))/(sqrt(nv*nl)));
+}
+
+void shade_toon_spec(vec3 n, vec3 l, vec3 v, float size, float tsmooth, out float specfac)
+{
+ vec3 h = normalize(l + v);
+ float rslt = dot(h, n);
+ float ang = acos(rslt);
+
+ if(ang < size) rslt = 1.0;
+ else if(ang >= (size + tsmooth) || tsmooth == 0.0) rslt = 0.0;
+ else rslt = 1.0 - ((ang - size)/tsmooth);
+
+ specfac = rslt;
+}
+
+void shade_spec_area_inp(float specfac, float inp, out float outspecfac)
+{
+ outspecfac = specfac*inp;
+}
+
+void shade_spec_t(float shadfac, float spec, float visifac, float specfac, out float t)
+{
+ t = shadfac*spec*visifac*specfac;
+}
+
+void shade_add_spec(float t, vec3 lampcol, vec3 speccol, out vec3 outcol)
+{
+ outcol = t*lampcol*speccol;
+}
+
+void shade_add(vec4 col1, vec4 col2, out vec4 outcol)
+{
+ outcol = col1 + col2;
+}
+
+void shade_madd(vec4 col, vec4 col1, vec4 col2, out vec4 outcol)
+{
+ outcol = col + col1*col2;
+}
+
+void shade_maddf(vec4 col, float f, vec4 col1, out vec4 outcol)
+{
+ outcol = col + f*col1;
+}
+
+void shade_mul(vec4 col1, vec4 col2, out vec4 outcol)
+{
+ outcol = col1*col2;
+}
+
+void shade_mul_value(float fac, vec4 col, out vec4 outcol)
+{
+ outcol = col*fac;
+}
+
+void shade_obcolor(vec4 col, vec4 obcol, out vec4 outcol)
+{
+ outcol = vec4(col.rgb*obcol.rgb, col.a);
+}
+
+void ramp_rgbtobw(vec3 color, out float outval)
+{
+ outval = color.r*0.3 + color.g*0.58 + color.b*0.12;
+}
+
+void shade_only_shadow(float i, float shadfac, float energy, out float outshadfac)
+{
+ outshadfac = i*energy*(1.0 - shadfac);
+}
+
+void shade_only_shadow_diffuse(float shadfac, vec3 rgb, vec4 diff, out vec4 outdiff)
+{
+ outdiff = diff - vec4(rgb*shadfac, 0.0);
+}
+
+void shade_only_shadow_specular(float shadfac, vec3 specrgb, vec4 spec, out vec4 outspec)
+{
+ outspec = spec - vec4(specrgb*shadfac, 0.0);
+}
+
+void test_shadowbuf(vec3 rco, sampler2DShadow shadowmap, mat4 shadowpersmat, float shadowbias, float inp, out float result)
+{
+ if(inp <= 0.0) {
+ result = 0.0;
+ }
+ else {
+ vec4 co = shadowpersmat*vec4(rco, 1.0);
+
+ //float bias = (1.5 - inp*inp)*shadowbias;
+ co.z -= shadowbias*co.w;
+
+ result = shadow2DProj(shadowmap, co).x;
+ }
+}
+
+void shade_exposure_correct(vec3 col, float linfac, float logfac, out vec3 outcol)
+{
+ outcol = linfac*(1.0 - exp(col*logfac));
+}
+
+void shade_mist_factor(vec3 co, float miststa, float mistdist, float misttype, float misi, out float outfac)
+{
+ float fac, zcor;
+
+ zcor = (gl_ProjectionMatrix[3][3] == 0.0)? length(co): -co[2];
+
+ fac = clamp((zcor-miststa)/mistdist, 0.0, 1.0);
+ if(misttype == 0.0) fac *= fac;
+ else if(misttype == 1.0);
+ else fac = sqrt(fac);
+
+ outfac = 1.0 - (1.0-fac)*(1.0-misi);
+}
+
+void shade_world_mix(vec3 hor, vec4 col, out vec4 outcol)
+{
+ float fac = clamp(col.a, 0.0, 1.0);
+ outcol = vec4(mix(hor, col.rgb, fac), col.a);
+}
+
+void shade_alpha_opaque(vec4 col, out vec4 outcol)
+{
+ outcol = vec4(col.rgb, 1.0);
+}
+
+void shade_alpha_obcolor(vec4 col, vec4 obcol, out vec4 outcol)
+{
+ outcol = vec4(col.rgb, col.a*obcol.a);
+}
+
+=======
float exp_blender(float f)
{
@@ -1802,3 +3578,4 @@ void shade_alpha_obcolor(vec4 col, vec4 obcol, out vec4 outcol)
outcol = vec4(col.rgb, col.a*obcol.a);
}
+>>>>>>> .merge-right.r36569
diff --git a/source/blender/gpu/intern/gpu_shader_vertex.glsl b/source/blender/gpu/intern/gpu_shader_vertex.glsl
index a86431bdd7a..68c8ed801bc 100644
--- a/source/blender/gpu/intern/gpu_shader_vertex.glsl
+++ b/source/blender/gpu/intern/gpu_shader_vertex.glsl
@@ -1,3 +1,17 @@
+<<<<<<< .working
+
+varying vec3 varposition;
+varying vec3 varnormal;
+
+void main()
+{
+ vec4 co = gl_ModelViewMatrix * gl_Vertex;
+
+ varposition = co.xyz;
+ varnormal = gl_NormalMatrix * gl_Normal;
+ gl_Position = gl_ProjectionMatrix * co;
+
+=======
varying vec3 varposition;
varying vec3 varnormal;
@@ -10,3 +24,4 @@ void main()
varnormal = normalize(gl_NormalMatrix * gl_Normal);
gl_Position = gl_ProjectionMatrix * co;
+>>>>>>> .merge-right.r36569
diff --git a/source/blender/makesdna/DNA_actuator_types.h b/source/blender/makesdna/DNA_actuator_types.h
index 93db8340aac..239903208ec 100644
--- a/source/blender/makesdna/DNA_actuator_types.h
+++ b/source/blender/makesdna/DNA_actuator_types.h
@@ -230,6 +230,20 @@ typedef struct bArmatureActuator {
struct Object *subtarget;
} bArmatureActuator;
+typedef struct bSteeringActuator {
+ char pad[5];
+ char flag;
+ short facingaxis;
+ int type; /* 0=seek, 1=flee, 2=path following */
+ float dist;
+ float velocity;
+ float acceleration;
+ float turnspeed;
+ int updateTime;
+ struct Object *target;
+ struct Object *navmesh;
+} bSteeringActuator;
+
typedef struct bActuator {
struct bActuator *next, *prev, *mynew;
short type;
@@ -295,6 +309,7 @@ typedef struct bActuator {
#define ACT_SHAPEACTION 21
#define ACT_STATE 22
#define ACT_ARMATURE 23
+#define ACT_STEERING 24
/* actuator flag */
#define ACT_SHOW 1
@@ -511,6 +526,16 @@ typedef struct bActuator {
#define ACT_CAMERA_X (float)'x'
#define ACT_CAMERA_Y (float)'y'
+/* steeringactuator->type */
+#define ACT_STEERING_SEEK 0
+#define ACT_STEERING_FLEE 1
+#define ACT_STEERING_PATHFOLLOWING 2
+/* steeringactuator->flag */
+#define ACT_STEERING_SELFTERMINATED 1
+#define ACT_STEERING_ENABLEVISUALIZATION 2
+#define ACT_STEERING_AUTOMATICFACING 4
+#define ACT_STEERING_NORMALUP 8
+
#endif
diff --git a/source/blender/makesdna/DNA_customdata_types.h b/source/blender/makesdna/DNA_customdata_types.h
index cdfcf465c6c..b721dc60e73 100644
--- a/source/blender/makesdna/DNA_customdata_types.h
+++ b/source/blender/makesdna/DNA_customdata_types.h
@@ -92,7 +92,8 @@ typedef struct CustomData {
#define CD_ID_MCOL 21
#define CD_TEXTURE_MCOL 22
#define CD_CLOTH_ORCO 23
-#define CD_NUMTYPES 24
+#define CD_RECAST 24
+#define CD_NUMTYPES 25
/* Bits for CustomDataMask */
#define CD_MASK_MVERT (1 << CD_MVERT)
@@ -117,6 +118,7 @@ typedef struct CustomData {
#define CD_MASK_MDISPS (1 << CD_MDISPS)
#define CD_MASK_WEIGHT_MCOL (1 << CD_WEIGHT_MCOL)
#define CD_MASK_CLOTH_ORCO (1 << CD_CLOTH_ORCO)
+#define CD_MASK_RECAST (1 << CD_RECAST)
/* CustomData.flag */
diff --git a/source/blender/makesdna/DNA_meshdata_types.h b/source/blender/makesdna/DNA_meshdata_types.h
index e3510b3a25a..61c4a660992 100644
--- a/source/blender/makesdna/DNA_meshdata_types.h
+++ b/source/blender/makesdna/DNA_meshdata_types.h
@@ -184,6 +184,10 @@ typedef struct PartialVisibility {
unsigned int totface, totedge, totvert, pad;
} PartialVisibility;
+typedef struct MRecast{
+ int i;
+} MRecast;
+
/* mvert->flag (1=SELECT) */
#define ME_SPHERETEST 2
#define ME_VERT_TMP_TAG 4
diff --git a/source/blender/makesdna/DNA_modifier_types.h b/source/blender/makesdna/DNA_modifier_types.h
index 053f3b38168..d41d23f1ee6 100644
--- a/source/blender/makesdna/DNA_modifier_types.h
+++ b/source/blender/makesdna/DNA_modifier_types.h
@@ -71,6 +71,7 @@ typedef enum ModifierType {
eModifierType_Solidify,
eModifierType_Screw,
eModifierType_Warp,
+ eModifierType_NavMesh,
NUM_MODIFIER_TYPES
} ModifierType;
@@ -747,6 +748,10 @@ typedef struct ScrewModifierData {
#define MOD_SCREW_OBJECT_OFFSET (1<<2)
// #define MOD_SCREW_OBJECT_ANGLE (1<<4)
+typedef struct NavMeshModifierData {
+ ModifierData modifier;
+} NavMeshModifierData;
+
typedef struct WarpModifierData {
ModifierData modifier;
diff --git a/source/blender/makesdna/DNA_object_types.h b/source/blender/makesdna/DNA_object_types.h
index ffa82092ef1..8750ee9e906 100644
--- a/source/blender/makesdna/DNA_object_types.h
+++ b/source/blender/makesdna/DNA_object_types.h
@@ -189,6 +189,8 @@ typedef struct Object {
float max_vel; /* clamp the maximum velocity 0.0 is disabled */
float min_vel; /* clamp the maximum velocity 0.0 is disabled */
float m_contactProcessingThreshold;
+ float obstacleRad;
+ char pad0[4];
short rotmode; /* rotation mode - uses defines set out in DNA_action_types.h for PoseChannel rotations... */
@@ -473,6 +475,8 @@ typedef struct DupliObject {
#define OB_SOFT_BODY 0x20000
#define OB_OCCLUDER 0x40000
#define OB_SENSOR 0x80000
+#define OB_NAVMESH 0x100000
+#define OB_HASOBSTACLE 0x200000
/* ob->gameflag2 */
#define OB_NEVER_DO_ACTIVITY_CULLING 1
@@ -493,6 +497,7 @@ typedef struct DupliObject {
#define OB_BODY_TYPE_SOFT 4
#define OB_BODY_TYPE_OCCLUDER 5
#define OB_BODY_TYPE_SENSOR 6
+#define OB_BODY_TYPE_NAVMESH 7
/* ob->scavisflag */
#define OB_VIS_SENS 1
diff --git a/source/blender/makesdna/DNA_scene_types.h b/source/blender/makesdna/DNA_scene_types.h
index be2a78ac774..be44b87d48e 100644
--- a/source/blender/makesdna/DNA_scene_types.h
+++ b/source/blender/makesdna/DNA_scene_types.h
@@ -426,6 +426,23 @@ typedef struct GameFraming {
#define SCE_GAMEFRAMING_EXTEND 1
#define SCE_GAMEFRAMING_SCALE 2
+typedef struct RecastData
+{
+ float cellsize;
+ float cellheight;
+ float agentmaxslope;
+ float agentmaxclimb;
+ float agentheight;
+ float agentradius;
+ float edgemaxlen;
+ float edgemaxerror;
+ float regionminsize;
+ float regionmergesize;
+ int vertsperpoly;
+ float detailsampledist;
+ float detailsamplemaxerror;
+} RecastData;
+
typedef struct GameData {
/* physics (it was in world)*/
@@ -440,10 +457,13 @@ typedef struct GameData {
* bit 3: (gameengine): Activity culling is enabled.
* bit 5: (gameengine) : enable Bullet DBVT tree for view frustrum culling
*/
- short mode, flag, matmode, pad[3];
+ short mode, flag, matmode, pad[2];
short occlusionRes; /* resolution of occlusion Z buffer in pixel */
short physicsEngine;
short ticrate, maxlogicstep, physubstep, maxphystep;
+ short obstacleSimulation;
+ float levelHeight;
+
/* standalone player */
struct GameFraming framing;
@@ -455,6 +475,7 @@ typedef struct GameData {
short stereoflag, stereomode;
short pad2, pad3;
float eyeseparation, pad1;
+ RecastData recastData;
} GameData;
#define STEREO_NOSTEREO 1
@@ -478,6 +499,11 @@ typedef struct GameData {
#define WOPHY_ODE 4
#define WOPHY_BULLET 5
+/* obstacleSimulation */
+#define OBSTSIMULATION_NONE 0
+#define OBSTSIMULATION_TOI_rays 1
+#define OBSTSIMULATION_TOI_cells 2
+
/* GameData.flag */
#define GAME_RESTRICT_ANIM_UPDATES (1 << 0)
#define GAME_ENABLE_ALL_FRAMES (1 << 1)
@@ -494,6 +520,7 @@ typedef struct GameData {
#define GAME_IGNORE_DEPRECATION_WARNINGS (1 << 12)
#define GAME_ENABLE_ANIMATION_RECORD (1 << 13)
#define GAME_SHOW_MOUSE (1 << 14)
+#define GAME_SHOW_OBSTACLE_SIMULATION (1 << 15)
#define GAME_GLSL_NO_COLOR_MANAGEMENT (1 << 15)
/* Note: GameData.flag is a short (max 16 flags). To add more flags, GameData.flag needs to be an int */
diff --git a/source/blender/makesrna/intern/rna_actuator.c b/source/blender/makesrna/intern/rna_actuator.c
index 3d0e177c94e..3214d31a53c 100644
--- a/source/blender/makesrna/intern/rna_actuator.c
+++ b/source/blender/makesrna/intern/rna_actuator.c
@@ -60,6 +60,7 @@ EnumPropertyItem actuator_type_items[] ={
{ACT_SOUND, "SOUND", 0, "Sound", ""},
{ACT_STATE, "STATE", 0, "State", ""},
{ACT_VISIBILITY, "VISIBILITY", 0, "Visibility", ""},
+ {ACT_STEERING, "STEERING", 0, "Steering", ""},
{0, NULL, 0, NULL, NULL}};
#ifdef RNA_RUNTIME
@@ -103,6 +104,8 @@ static StructRNA* rna_Actuator_refine(struct PointerRNA *ptr)
return &RNA_StateActuator;
case ACT_ARMATURE:
return &RNA_ArmatureActuator;
+ case ACT_STEERING:
+ return &RNA_SteeringActuator;
default:
return &RNA_Actuator;
}
@@ -435,6 +438,7 @@ EnumPropertyItem *rna_Actuator_type_itemf(bContext *C, PointerRNA *ptr, Property
RNA_enum_items_add_value(&item, &totitem, actuator_type_items, ACT_PROPERTY);
RNA_enum_items_add_value(&item, &totitem, actuator_type_items, ACT_RANDOM);
RNA_enum_items_add_value(&item, &totitem, actuator_type_items, ACT_SCENE);
+ RNA_enum_items_add_value(&item, &totitem, actuator_type_items, ACT_STEERING);
RNA_enum_items_add_value(&item, &totitem, actuator_type_items, ACT_SOUND);
RNA_enum_items_add_value(&item, &totitem, actuator_type_items, ACT_STATE);
@@ -480,6 +484,18 @@ static void rna_Actuator_Armature_update(Main *bmain, Scene *scene, PointerRNA *
constraint[0] = 0;
}
+static void rna_SteeringActuator_navmesh_set(PointerRNA *ptr, PointerRNA value)
+{
+ bActuator *act = (bActuator*)ptr->data;
+ bSteeringActuator *sa = (bSteeringActuator*) act->data;
+
+ Object* obj = value.data;
+ if (obj && obj->body_type==OB_BODY_TYPE_NAVMESH)
+ sa->navmesh = obj;
+ else
+ sa->navmesh = NULL;
+}
+
/* note: the following set functions exists only to avoid id refcounting */
static void rna_Actuator_editobject_mesh_set(PointerRNA *ptr, PointerRNA value)
{
@@ -1900,6 +1916,108 @@ static void rna_def_armature_actuator(BlenderRNA *brna)
RNA_def_property_update(prop, NC_LOGIC, NULL);
}
+static void rna_def_steering_actuator(BlenderRNA *brna)
+{
+ StructRNA *srna;
+ PropertyRNA *prop;
+
+ static EnumPropertyItem prop_type_items[] ={
+ {ACT_STEERING_SEEK, "SEEK", 0, "Seek", ""},
+ {ACT_STEERING_FLEE, "FLEE", 0, "Flee", ""},
+ {ACT_STEERING_PATHFOLLOWING, "PATHFOLLOWING", 0, "Path following", ""},
+ {0, NULL, 0, NULL, NULL}};
+
+ static EnumPropertyItem facingaxis_items[] ={
+ {1, "X", 0, "X", ""},
+ {2, "Y", 0, "Y", ""},
+ {3, "Z", 0, "Z", ""},
+ {4, "-X", 0, "-X", ""},
+ {5, "-Y", 0, "-Y", ""},
+ {6, "-Z", 0, "-Z", ""},
+ {0, NULL, 0, NULL, NULL}};
+
+ srna= RNA_def_struct(brna, "SteeringActuator", "Actuator");
+ RNA_def_struct_ui_text(srna, "Steering Actuator", "");
+ RNA_def_struct_sdna_from(srna, "bSteeringActuator", "data");
+
+ prop= RNA_def_property(srna, "mode", PROP_ENUM, PROP_NONE);
+ RNA_def_property_enum_sdna(prop, NULL, "type");
+ RNA_def_property_enum_items(prop, prop_type_items);
+ RNA_def_property_ui_text(prop, "Behavior", "");
+ RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+ prop= RNA_def_property(srna, "velocity", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "velocity");
+ RNA_def_property_range(prop, 0.0, 1000.0);
+ RNA_def_property_ui_text(prop, "Velocity", "Velocity magnitude");
+ RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+ prop= RNA_def_property(srna, "acceleration", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "acceleration");
+ RNA_def_property_range(prop, 0.0, 1000.0);
+ RNA_def_property_ui_text(prop, "Acceleration", "Max acceleration");
+ RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+ prop= RNA_def_property(srna, "turn_speed", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "turnspeed");
+ RNA_def_property_range(prop, 0.0, 720.0);
+ RNA_def_property_ui_text(prop, "Turn Speed", "Max turn speed");
+ RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+ prop= RNA_def_property(srna, "distance", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "dist");
+ RNA_def_property_range(prop, 0.0, 1000.0);
+ RNA_def_property_ui_text(prop, "Dist", "Relax distance");
+ RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+ prop= RNA_def_property(srna, "target", PROP_POINTER, PROP_NONE);
+ RNA_def_property_struct_type(prop, "Object");
+ RNA_def_property_pointer_sdna(prop, NULL, "target");
+ RNA_def_property_flag(prop, PROP_EDITABLE);
+ RNA_def_property_ui_text(prop, "Target Object", "Set target object");
+ RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+ prop= RNA_def_property(srna, "self_terminated", PROP_BOOLEAN, PROP_NONE);
+ RNA_def_property_boolean_sdna(prop, NULL, "flag", ACT_STEERING_SELFTERMINATED);
+ RNA_def_property_ui_text(prop, "Self Terminated", "Terminate when target is reached");
+ RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+ prop= RNA_def_property(srna, "show_visualization", PROP_BOOLEAN, PROP_NONE);
+ RNA_def_property_boolean_sdna(prop, NULL, "flag", ACT_STEERING_ENABLEVISUALIZATION);
+ RNA_def_property_ui_text(prop, "Visualize", "Enable debug visualization");
+ RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+ prop= RNA_def_property(srna, "update_period", PROP_INT, PROP_NONE);
+ RNA_def_property_int_sdna(prop, NULL, "updateTime");
+ RNA_def_property_ui_range(prop, -1, 100000, 1, 1);
+ RNA_def_property_ui_text(prop, "Update period", "Path update period");
+ RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+ prop= RNA_def_property(srna, "navmesh", PROP_POINTER, PROP_NONE);
+ RNA_def_property_struct_type(prop, "Object");
+ RNA_def_property_pointer_sdna(prop, NULL, "navmesh");
+ RNA_def_property_flag(prop, PROP_EDITABLE);
+ RNA_def_property_ui_text(prop, "NavMesh Object", "Navigation mesh");
+ RNA_def_property_pointer_funcs(prop, NULL, "rna_SteeringActuator_navmesh_set", NULL, NULL);
+ RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+ prop= RNA_def_property(srna, "facing", PROP_BOOLEAN, PROP_NONE);
+ RNA_def_property_boolean_sdna(prop, NULL, "flag", ACT_STEERING_AUTOMATICFACING);
+ RNA_def_property_ui_text(prop, "Facing", "Enable automatic facing");
+ RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+ prop= RNA_def_property(srna, "facing_axis", PROP_ENUM, PROP_NONE);
+ RNA_def_property_enum_sdna(prop, NULL, "facingaxis");
+ RNA_def_property_enum_items(prop, facingaxis_items);
+ RNA_def_property_ui_text(prop, "Axis", "Axis for automatic facing");
+ RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+ prop= RNA_def_property(srna, "normal_up", PROP_BOOLEAN, PROP_NONE);
+ RNA_def_property_boolean_sdna(prop, NULL, "flag", ACT_STEERING_NORMALUP);
+ RNA_def_property_ui_text(prop, "N", "Use normal of the navmesh to set \"UP\" vector");
+ RNA_def_property_update(prop, NC_LOGIC, NULL);
+}
+
void RNA_def_actuator(BlenderRNA *brna)
{
rna_def_actuator(brna);
@@ -1921,6 +2039,7 @@ void RNA_def_actuator(BlenderRNA *brna)
rna_def_shape_action_actuator(brna);
rna_def_state_actuator(brna);
rna_def_armature_actuator(brna);
+ rna_def_steering_actuator(brna);
}
#endif
diff --git a/source/blender/makesrna/intern/rna_modifier.c b/source/blender/makesrna/intern/rna_modifier.c
index 22fdfcea29c..ab89ec43740 100644
--- a/source/blender/makesrna/intern/rna_modifier.c
+++ b/source/blender/makesrna/intern/rna_modifier.c
@@ -92,6 +92,7 @@ EnumPropertyItem modifier_type_items[] ={
{eModifierType_Smoke, "SMOKE", ICON_MOD_SMOKE, "Smoke", ""},
{eModifierType_Softbody, "SOFT_BODY", ICON_MOD_SOFT, "Soft Body", ""},
{eModifierType_Surface, "SURFACE", ICON_MOD_PHYSICS, "Surface", ""},
+ {eModifierType_NavMesh, "NAVMESH", ICON_MOD_PHYSICS, "Navigation mesh", ""},
{0, NULL, 0, NULL, NULL}};
#ifdef RNA_RUNTIME
@@ -184,6 +185,8 @@ static StructRNA* rna_Modifier_refine(struct PointerRNA *ptr)
return &RNA_ScrewModifier;
case eModifierType_Warp:
return &RNA_WarpModifier;
+ case eModifierType_NavMesh:
+ return &RNA_NavMeshModifier;
default:
return &RNA_Modifier;
}
@@ -2420,6 +2423,17 @@ static void rna_def_modifier_screw(BlenderRNA *brna)
RNA_def_property_update(prop, 0, "rna_Modifier_update");*/
}
+static void rna_def_modifier_navmesh(BlenderRNA *brna)
+{
+ StructRNA *srna;
+ PropertyRNA *prop;
+
+ srna= RNA_def_struct(brna, "NavMeshModifier", "Modifier");
+ RNA_def_struct_ui_text(srna, "NavMesh Modifier", "NavMesh modifier");
+ RNA_def_struct_sdna(srna, "NavMeshModifierData");
+ RNA_def_struct_ui_icon(srna, ICON_MOD_DECIM);
+}
+
void RNA_def_modifier(BlenderRNA *brna)
{
StructRNA *srna;
@@ -2517,6 +2531,7 @@ void RNA_def_modifier(BlenderRNA *brna)
rna_def_modifier_smoke(brna);
rna_def_modifier_solidify(brna);
rna_def_modifier_screw(brna);
+ rna_def_modifier_navmesh(brna);
}
#endif
diff --git a/source/blender/makesrna/intern/rna_object.c b/source/blender/makesrna/intern/rna_object.c
index 83efd9d1a9f..571f8d36305 100644
--- a/source/blender/makesrna/intern/rna_object.c
+++ b/source/blender/makesrna/intern/rna_object.c
@@ -858,6 +858,8 @@ static int rna_GameObjectSettings_physics_type_get(PointerRNA *ptr)
if (!(ob->gameflag & OB_COLLISION)) {
if (ob->gameflag & OB_OCCLUDER) {
ob->body_type = OB_BODY_TYPE_OCCLUDER;
+ } else if (ob->gameflag & OB_NAVMESH){
+ ob->body_type = OB_BODY_TYPE_NAVMESH;
} else {
ob->body_type = OB_BODY_TYPE_NO_COLLISION;
}
@@ -887,31 +889,35 @@ static void rna_GameObjectSettings_physics_type_set(PointerRNA *ptr, int value)
switch (ob->body_type) {
case OB_BODY_TYPE_SENSOR:
ob->gameflag |= OB_SENSOR|OB_COLLISION|OB_GHOST;
- ob->gameflag &= ~(OB_OCCLUDER|OB_DYNAMIC|OB_RIGID_BODY|OB_SOFT_BODY|OB_ACTOR|OB_ANISOTROPIC_FRICTION|OB_DO_FH|OB_ROT_FH|OB_COLLISION_RESPONSE);
+ ob->gameflag &= ~(OB_OCCLUDER|OB_DYNAMIC|OB_RIGID_BODY|OB_SOFT_BODY|OB_ACTOR|OB_ANISOTROPIC_FRICTION|OB_DO_FH|OB_ROT_FH|OB_COLLISION_RESPONSE|OB_NAVMESH);
break;
case OB_BODY_TYPE_OCCLUDER:
ob->gameflag |= OB_OCCLUDER;
- ob->gameflag &= ~(OB_SENSOR|OB_COLLISION|OB_DYNAMIC);
+ ob->gameflag &= ~(OB_SENSOR|OB_COLLISION|OB_DYNAMIC|OB_NAVMESH);
+ break;
+ case OB_BODY_TYPE_NAVMESH:
+ ob->gameflag |= OB_NAVMESH;
+ ob->gameflag &= ~(OB_SENSOR|OB_COLLISION|OB_DYNAMIC|OB_OCCLUDER);
break;
case OB_BODY_TYPE_NO_COLLISION:
- ob->gameflag &= ~(OB_SENSOR|OB_COLLISION|OB_OCCLUDER|OB_DYNAMIC);
+ ob->gameflag &= ~(OB_SENSOR|OB_COLLISION|OB_OCCLUDER|OB_DYNAMIC|OB_NAVMESH);
break;
case OB_BODY_TYPE_STATIC:
ob->gameflag |= OB_COLLISION;
- ob->gameflag &= ~(OB_DYNAMIC|OB_RIGID_BODY|OB_SOFT_BODY|OB_OCCLUDER|OB_SENSOR);
+ ob->gameflag &= ~(OB_DYNAMIC|OB_RIGID_BODY|OB_SOFT_BODY|OB_OCCLUDER|OB_SENSOR|OB_NAVMESH);
break;
case OB_BODY_TYPE_DYNAMIC:
ob->gameflag |= OB_COLLISION|OB_DYNAMIC|OB_ACTOR;
- ob->gameflag &= ~(OB_RIGID_BODY|OB_SOFT_BODY|OB_OCCLUDER|OB_SENSOR);
+ ob->gameflag &= ~(OB_RIGID_BODY|OB_SOFT_BODY|OB_OCCLUDER|OB_SENSOR|OB_NAVMESH);
break;
case OB_BODY_TYPE_RIGID:
ob->gameflag |= OB_COLLISION|OB_DYNAMIC|OB_RIGID_BODY|OB_ACTOR;
- ob->gameflag &= ~(OB_SOFT_BODY|OB_OCCLUDER|OB_SENSOR);
+ ob->gameflag &= ~(OB_SOFT_BODY|OB_OCCLUDER|OB_SENSOR|OB_NAVMESH);
break;
default:
case OB_BODY_TYPE_SOFT:
ob->gameflag |= OB_COLLISION|OB_DYNAMIC|OB_SOFT_BODY|OB_ACTOR;
- ob->gameflag &= ~(OB_RIGID_BODY|OB_OCCLUDER|OB_SENSOR);
+ ob->gameflag &= ~(OB_RIGID_BODY|OB_OCCLUDER|OB_SENSOR|OB_NAVMESH);
/* assume triangle mesh, if no bounds chosen for soft body */
if ((ob->gameflag & OB_BOUNDS) && (ob->boundtype<OB_BOUND_POLYH))
@@ -1343,6 +1349,7 @@ static void rna_def_object_game_settings(BlenderRNA *brna)
{OB_BODY_TYPE_SOFT, "SOFT_BODY", 0, "Soft Body", "Soft body"},
{OB_BODY_TYPE_OCCLUDER, "OCCLUDE", 0, "Occlude", "Occluder for optimizing scene rendering"},
{OB_BODY_TYPE_SENSOR, "SENSOR", 0, "Sensor", "Collision Sensor, detects static and dynamic objects but not the other collision sensor objects"},
+ {OB_BODY_TYPE_NAVMESH, "NAVMESH", 0, "NavMesh", "Navigation mesh"},
{0, NULL, 0, NULL, NULL}};
srna= RNA_def_struct(brna, "GameObjectSettings", NULL);
@@ -1512,6 +1519,15 @@ static void rna_def_object_game_settings(BlenderRNA *brna)
RNA_def_property_pointer_sdna(prop, NULL, "bsoft");
RNA_def_property_ui_text(prop, "Soft Body Settings", "Settings for Bullet soft body simulation");
+ prop= RNA_def_property(srna, "create_obstacle", PROP_BOOLEAN, PROP_NONE);
+ RNA_def_property_boolean_sdna(prop, NULL, "gameflag", OB_HASOBSTACLE);
+ RNA_def_property_ui_text(prop, "Create obstacle", "Create representation for obstacle simulation");
+
+ prop= RNA_def_property(srna, "obstacle_radius", PROP_FLOAT, PROP_NONE|PROP_UNIT_LENGTH);
+ RNA_def_property_float_sdna(prop, NULL, "obstacleRad");
+ RNA_def_property_range(prop, 0.0, 1000.0);
+ RNA_def_property_ui_text(prop, "Obstacle Radius", "Radius of object representation in obstacle simulation");
+
/* state */
prop= RNA_def_property(srna, "states_visible", PROP_BOOLEAN, PROP_LAYER_MEMBER);
diff --git a/source/blender/makesrna/intern/rna_scene.c b/source/blender/makesrna/intern/rna_scene.c
index 8c2f473c65e..197ddd2ba06 100644
--- a/source/blender/makesrna/intern/rna_scene.c
+++ b/source/blender/makesrna/intern/rna_scene.c
@@ -39,6 +39,7 @@
#include "DNA_particle_types.h"
#include "DNA_scene_types.h"
#include "DNA_userdef_types.h"
+#include "BLI_math.h"
/* Include for Bake Options */
#include "RE_pipeline.h"
@@ -1669,6 +1670,96 @@ void rna_def_render_layer_common(StructRNA *srna, int scene)
else RNA_def_property_clear_flag(prop, PROP_EDITABLE);
}
+static void rna_def_scene_game_recast_data(BlenderRNA *brna)
+{
+ StructRNA *srna;
+ PropertyRNA *prop;
+
+ srna= RNA_def_struct(brna, "SceneGameRecastData", NULL);
+ RNA_def_struct_sdna(srna, "RecastData");
+ RNA_def_struct_nested(brna, srna, "Scene");
+ RNA_def_struct_ui_text(srna, "Recast Data", "Recast data for a Game datablock");
+
+ prop= RNA_def_property(srna, "cell_size", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "cellsize");
+ RNA_def_property_ui_range(prop, 0.1, 1, 1, 2);
+ RNA_def_property_ui_text(prop, "Cell Size", "Rasterized cell size");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+
+ prop= RNA_def_property(srna, "cell_height", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "cellheight");
+ RNA_def_property_ui_range(prop, 0.1, 1, 1, 2);
+ RNA_def_property_ui_text(prop, "Cell Height", "Rasterized cell height");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+
+ prop= RNA_def_property(srna, "agent_height", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "agentheight");
+ RNA_def_property_ui_range(prop, 0.1, 5, 1, 2);
+ RNA_def_property_ui_text(prop, "Agent Height", "Minimum height where the agent can still walk");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+
+ prop= RNA_def_property(srna, "agent_radius", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "agentradius");
+ RNA_def_property_ui_range(prop, 0.1, 5, 1, 2);
+ RNA_def_property_ui_text(prop, "Agent Radius", "Radius of the agent");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+
+ prop= RNA_def_property(srna, "max_climb", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "agentmaxclimb");
+ RNA_def_property_ui_range(prop, 0.1, 5, 1, 2);
+ RNA_def_property_ui_text(prop, "Max Climb", "Maximum height between grid cells the agent can climb");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+
+ prop= RNA_def_property(srna, "max_slope", PROP_FLOAT, PROP_ANGLE);
+ RNA_def_property_float_sdna(prop, NULL, "agentmaxslope");
+ RNA_def_property_range(prop, 0, M_PI/2);
+ RNA_def_property_ui_text(prop, "Max Slope", "Maximum walkable slope angle in degrees");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+
+
+ prop= RNA_def_property(srna, "region_min_size", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "regionminsize");
+ RNA_def_property_ui_range(prop, 0, 150, 1, 2);
+ RNA_def_property_ui_text(prop, "Min Region Size", "Minimum regions size. Smaller regions will be deleted");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+
+ prop= RNA_def_property(srna, "region_merge_size", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "regionmergesize");
+ RNA_def_property_ui_range(prop, 0, 150, 1, 2);
+ RNA_def_property_ui_text(prop, "Merged Region Size", "Minimum regions size. Smaller regions will be merged");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+
+ prop= RNA_def_property(srna, "edge_max_len", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "edgemaxlen");
+ RNA_def_property_ui_range(prop, 0, 50, 1, 2);
+ RNA_def_property_ui_text(prop, "Max Edge Length", "Maximum contour edge length");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+
+ prop= RNA_def_property(srna, "edge_max_error", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "edgemaxerror");
+ RNA_def_property_ui_range(prop, 0.1, 3.0, 1, 2);
+ RNA_def_property_ui_text(prop, "Max Edge Error", "Maximum distance error from contour to cells");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+
+ prop= RNA_def_property(srna, "verts_per_poly", PROP_INT, PROP_NONE);
+ RNA_def_property_int_sdna(prop, NULL, "vertsperpoly");
+ RNA_def_property_ui_range(prop, 3, 12, 1, 0);
+ RNA_def_property_ui_text(prop, "Verts Per Poly", "Max number of vertices per polygon");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+
+ prop= RNA_def_property(srna, "sample_dist", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "detailsampledist");
+ RNA_def_property_ui_range(prop, 0.0, 16.0, 1, 2);
+ RNA_def_property_ui_text(prop, "Sample Distance", "Detail mesh sample spacing");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+
+ prop= RNA_def_property(srna, "sample_max_error", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "detailsamplemaxerror");
+ RNA_def_property_ui_range(prop, 0.0, 16.0, 1, 2);
+ RNA_def_property_ui_text(prop, "Max Sample Error", "Detail mesh simplification max sample error");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+}
+
static void rna_def_scene_game_data(BlenderRNA *brna)
{
StructRNA *srna;
@@ -1718,6 +1809,12 @@ static void rna_def_scene_game_data(BlenderRNA *brna)
{GAME_MAT_GLSL, "GLSL", 0, "GLSL", "OpenGL shading language shaders"},
{0, NULL, 0, NULL, NULL}};
+ static EnumPropertyItem obstacle_simulation_items[] = {
+ {OBSTSIMULATION_NONE, "NONE", 0, "None", ""},
+ {OBSTSIMULATION_TOI_rays, "RVO (rays)", 0, "RVO (rays)", ""},
+ {OBSTSIMULATION_TOI_cells, "RVO (cells)", 0, "RVO (cells)", ""},
+ {0, NULL, 0, NULL, NULL}};
+
srna= RNA_def_struct(brna, "SceneGameData", NULL);
RNA_def_struct_sdna(srna, "GameData");
RNA_def_struct_nested(brna, srna, "Scene");
@@ -1970,6 +2067,33 @@ static void rna_def_scene_game_data(BlenderRNA *brna)
RNA_def_property_boolean_negative_sdna(prop, NULL, "flag", GAME_GLSL_NO_EXTRA_TEX);
RNA_def_property_ui_text(prop, "GLSL Extra Textures", "Use extra textures like normal or specular maps for GLSL rendering");
RNA_def_property_update(prop, NC_SCENE|NA_EDITED, "rna_Scene_glsl_update");
+
+ /* obstacle simulation */
+ prop= RNA_def_property(srna, "obstacle_simulation", PROP_ENUM, PROP_NONE);
+ RNA_def_property_enum_sdna(prop, NULL, "obstacleSimulation");
+ RNA_def_property_enum_items(prop, obstacle_simulation_items);
+ RNA_def_property_ui_text(prop, "Obstacle simulation", "Simulation used for obstacle avoidance in the game engine");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+
+ prop= RNA_def_property(srna, "level_height", PROP_FLOAT, PROP_ACCELERATION);
+ RNA_def_property_float_sdna(prop, NULL, "levelHeight");
+ RNA_def_property_range(prop, 0.0f, 200.0f);
+ RNA_def_property_ui_text(prop, "Level height", "Max difference in heights of obstacles to enable their interaction");
+ RNA_def_property_update(prop, NC_SCENE, NULL);
+
+ prop= RNA_def_property(srna, "show_obstacle_simulation", PROP_BOOLEAN, PROP_NONE);
+ RNA_def_property_boolean_sdna(prop, NULL, "flag", GAME_SHOW_OBSTACLE_SIMULATION);
+ RNA_def_property_ui_text(prop, "Visualization", "Enable debug visualization for obstacle simulation");
+
+ /* Recast Settings */
+ prop= RNA_def_property(srna, "recast_data", PROP_POINTER, PROP_NONE);
+ RNA_def_property_flag(prop, PROP_NEVER_NULL);
+ RNA_def_property_pointer_sdna(prop, NULL, "recastData");
+ RNA_def_property_struct_type(prop, "SceneGameRecastData");
+ RNA_def_property_ui_text(prop, "Recast Data", "");
+
+ /* Nestled Data */
+ rna_def_scene_game_recast_data(brna);
}
static void rna_def_scene_render_layer(BlenderRNA *brna)
diff --git a/source/blender/modifiers/CMakeLists.txt b/source/blender/modifiers/CMakeLists.txt
index 1d942de348b..52755f02056 100644
--- a/source/blender/modifiers/CMakeLists.txt
+++ b/source/blender/modifiers/CMakeLists.txt
@@ -34,12 +34,16 @@ set(INC
../makesdna
../makesrna
../render/extern/include
+ ../editors/include
+ ../gpu
../../../intern/elbeem/extern
../../../intern/guardedalloc
+ ../../../extern/recastnavigation/Recast/Include
)
set(INC_SYS
${ZLIB_INCLUDE_DIRS}
+ ${GLEW_INCLUDE_PATH}
)
set(SRC
@@ -64,6 +68,7 @@ set(SRC
intern/MOD_meshdeform.c
intern/MOD_mirror.c
intern/MOD_multires.c
+ intern/MOD_navmesh.cpp
intern/MOD_none.c
intern/MOD_particleinstance.c
intern/MOD_particlesystem.c
diff --git a/source/blender/modifiers/MOD_modifiertypes.h b/source/blender/modifiers/MOD_modifiertypes.h
index 4e44a226c64..3d8ad7b4128 100644
--- a/source/blender/modifiers/MOD_modifiertypes.h
+++ b/source/blender/modifiers/MOD_modifiertypes.h
@@ -72,6 +72,7 @@ extern ModifierTypeInfo modifierType_ShapeKey;
extern ModifierTypeInfo modifierType_Solidify;
extern ModifierTypeInfo modifierType_Screw;
extern ModifierTypeInfo modifierType_Warp;
+extern ModifierTypeInfo modifierType_NavMesh;
/* MOD_util.c */
void modifier_type_init(ModifierTypeInfo *types[]);
diff --git a/source/blender/modifiers/SConscript b/source/blender/modifiers/SConscript
index 66dd00f0248..f90e637d151 100644
--- a/source/blender/modifiers/SConscript
+++ b/source/blender/modifiers/SConscript
@@ -1,12 +1,14 @@
#!/usr/bin/python
Import ('env')
-sources = env.Glob('intern/*.c')
+sources = env.Glob('intern/*.c') + env.Glob('intern/*.cpp')
incs = '. ./intern'
-incs += ' #/intern/guardedalloc #/intern/decimation/extern #/intern/bsp/extern #/intern/elbeem/extern'
+incs += ' #/intern/guardedalloc #/intern/decimation/extern #/intern/bsp/extern #/intern/elbeem/extern #/extern/glew/include'
incs += ' ../render/extern/include ../blenloader'
incs += ' ../include ../blenlib ../makesdna ../makesrna ../blenkernel ../blenkernel/intern'
+incs += ' ../editors/include ../gpu'
+incs += ' #extern/recastnavigation/Recast/Include'
incs += ' ' + env['BF_ZLIB_INC']
diff --git a/source/blender/modifiers/intern/MOD_navmesh.cpp b/source/blender/modifiers/intern/MOD_navmesh.cpp
new file mode 100644
index 00000000000..888da7ba985
--- /dev/null
+++ b/source/blender/modifiers/intern/MOD_navmesh.cpp
@@ -0,0 +1,269 @@
+/*
+* $Id$
+*
+* ***** BEGIN GPL LICENSE BLOCK *****
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software Foundation,
+* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+* The Original Code is Copyright (C) 2005 by the Blender Foundation.
+* All rights reserved.
+*
+* Contributor(s):
+*
+* ***** END GPL LICENSE BLOCK *****
+*
+*/
+#include <math.h>
+#include "Recast.h"
+
+extern "C"{
+#include "ED_navmesh_conversion.h"
+
+#include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
+#include "BLI_math.h"
+#include "BKE_cdderivedmesh.h"
+#include "BKE_mesh.h"
+#include "BKE_modifier.h"
+#include "BKE_particle.h"
+#include "BKE_customdata.h"
+#include "MEM_guardedalloc.h"
+#include "BIF_gl.h"
+#include "GPU_buffers.h"
+#include "GPU_draw.h"
+#include "UI_resources.h"
+
+static void initData(ModifierData *md)
+{
+ NavMeshModifierData *nmmd = (NavMeshModifierData*) md;
+}
+
+static void copyData(ModifierData *md, ModifierData *target)
+{
+ NavMeshModifierData *nmmd = (NavMeshModifierData*) md;
+ NavMeshModifierData *tnmmd = (NavMeshModifierData*) target;
+
+ //.todo - deep copy
+}
+
+/*
+static void (*drawFacesSolid_original)(DerivedMesh *dm, float (*partial_redraw_planes)[4],
+ int fast, int (*setMaterial)(int, void *attribs)) = NULL;*/
+
+static void drawNavMeshColored(DerivedMesh *dm)
+{
+ int a, glmode;
+ MVert *mvert = (MVert *)CustomData_get_layer(&dm->vertData, CD_MVERT);
+ MFace *mface = (MFace *)CustomData_get_layer(&dm->faceData, CD_MFACE);
+ int* polygonIdx = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST);
+ if (!polygonIdx)
+ return;
+ const float BLACK_COLOR[3] = {0.f, 0.f, 0.f};
+ float col[3];
+ /*
+ //UI_ThemeColor(TH_WIRE);
+ glDisable(GL_LIGHTING);
+ glLineWidth(2.0);
+ dm->drawEdges(dm, 0, 1);
+ glLineWidth(1.0);
+ glEnable(GL_LIGHTING);*/
+
+ glDisable(GL_LIGHTING);
+ if(GPU_buffer_legacy(dm) ) {
+ DEBUG_VBO( "Using legacy code. drawNavMeshColored\n" );
+ //glShadeModel(GL_SMOOTH);
+ glBegin(glmode = GL_QUADS);
+ for(a = 0; a < dm->numFaceData; a++, mface++) {
+ int new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES;
+ int polygonIdx = *(int*)CustomData_get(&dm->faceData, a, CD_RECAST);
+ if (polygonIdx<=0)
+ memcpy(col, BLACK_COLOR, 3*sizeof(float));
+ else
+ intToCol(polygonIdx, col);
+
+ if(new_glmode != glmode) {
+ glEnd();
+ glBegin(glmode = new_glmode);
+ }
+ glColor3fv(col);
+ glVertex3fv(mvert[mface->v1].co);
+ glVertex3fv(mvert[mface->v2].co);
+ glVertex3fv(mvert[mface->v3].co);
+ if(mface->v4) {
+ glVertex3fv(mvert[mface->v4].co);
+ }
+ }
+ glEnd();
+ }
+ glEnable(GL_LIGHTING);
+}
+
+static void navDM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFace *tface, MCol *mcol, int matnr))
+{
+ drawNavMeshColored(dm);
+}
+
+static void navDM_drawFacesSolid(DerivedMesh *dm,
+ float (*partial_redraw_planes)[4],
+ int fast, int (*setMaterial)(int, void *attribs))
+{
+ //drawFacesSolid_original(dm, partial_redraw_planes, fast, setMaterial);
+ drawNavMeshColored(dm);
+}
+
+static DerivedMesh *createNavMeshForVisualization(NavMeshModifierData *mmd,DerivedMesh *dm)
+{
+ DerivedMesh *result;
+ int maxFaces = dm->getNumFaces(dm);
+
+ result = CDDM_copy(dm);
+ if (!CustomData_has_layer(&result->faceData, CD_RECAST))
+ {
+ int *sourceRecastData = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST);
+ CustomData_add_layer_named(&result->faceData, CD_RECAST, CD_DUPLICATE,
+ sourceRecastData, maxFaces, "recastData");
+ }
+ int *recastData = (int*)CustomData_get_layer(&result->faceData, CD_RECAST);
+ result->drawFacesTex = navDM_drawFacesTex;
+ result->drawFacesSolid = navDM_drawFacesSolid;
+
+
+ //process mesh
+ int vertsPerPoly=0, nverts=0, ndtris=0, npolys=0;
+ float* verts=NULL;
+ unsigned short *dtris=NULL, *dmeshes=NULL, *polys=NULL;
+ int *dtrisToPolysMap=NULL, *dtrisToTrisMap=NULL, *trisToFacesMap=NULL;
+
+ bool res = buildNavMeshDataByDerivedMesh(dm, vertsPerPoly, nverts, verts, ndtris, dtris,
+ npolys, dmeshes, polys, dtrisToPolysMap, dtrisToTrisMap,
+ trisToFacesMap);
+ if (res)
+ {
+ //invalidate concave polygon
+ for (size_t polyIdx=0; polyIdx<(size_t)npolys; polyIdx++)
+ {
+ unsigned short* poly = &polys[polyIdx*2*vertsPerPoly];
+ if (!polyIsConvex(poly, vertsPerPoly, verts))
+ {
+ //set negative polygon idx to all faces
+ unsigned short *dmesh = &dmeshes[4*polyIdx];
+ unsigned short tbase = dmesh[2];
+ unsigned short tnum = dmesh[3];
+ for (unsigned short ti=0; ti<tnum; ti++)
+ {
+ unsigned short triidx = dtrisToTrisMap[tbase+ti];
+ unsigned short faceidx = trisToFacesMap[triidx];
+ if (recastData[faceidx]>0)
+ recastData[faceidx] = -recastData[faceidx];
+ }
+ }
+ }
+
+ }
+ else
+ {
+ printf("Error during creation polygon infos\n");
+ }
+
+ //clean up
+ if (verts!=NULL)
+ delete verts;
+ if (dtris!=NULL)
+ delete dtris;
+ if (dmeshes!=NULL)
+ delete dmeshes;
+ if (polys!=NULL)
+ delete polys;
+ if (dtrisToPolysMap!=NULL)
+ delete dtrisToPolysMap;
+ if (dtrisToTrisMap!=NULL)
+ delete dtrisToTrisMap;
+ if (trisToFacesMap!=NULL)
+ delete trisToFacesMap;
+
+ return result;
+}
+
+/*
+static int isDisabled(ModifierData *md, int useRenderParams)
+{
+ NavMeshModifierData *amd = (NavMeshModifierData*) md;
+ return false;
+}*/
+
+
+
+static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *derivedData,
+ int useRenderParams, int isFinalCalc)
+{
+ DerivedMesh *result = NULL;
+ NavMeshModifierData *nmmd = (NavMeshModifierData*) md;
+ bool hasRecastData = CustomData_has_layer(&derivedData->faceData, CD_RECAST)>0;
+ if (ob->body_type!=OB_BODY_TYPE_NAVMESH || !hasRecastData )
+ {
+ //convert to nav mesh object:
+ //1)set physics type
+ ob->gameflag &= ~OB_COLLISION;
+ ob->gameflag |= OB_NAVMESH;
+ ob->body_type = OB_BODY_TYPE_NAVMESH;
+ //2)add and init recast data layer
+ if (!hasRecastData)
+ {
+ Mesh* obmesh = (Mesh *)ob->data;
+ if (obmesh)
+ {
+ int numFaces = obmesh->totface;
+ CustomData_add_layer_named(&obmesh->fdata, CD_RECAST, CD_CALLOC, NULL, numFaces, "recastData");
+ int* recastData = (int*)CustomData_get_layer(&obmesh->fdata, CD_RECAST);
+ for (int i=0; i<numFaces; i++)
+ {
+ recastData[i] = i+1;
+ }
+ CustomData_add_layer_named(&derivedData->faceData, CD_RECAST, CD_REFERENCE, recastData, numFaces, "recastData");
+ }
+ }
+ }
+
+ result = createNavMeshForVisualization(nmmd, derivedData);
+
+ return result;
+}
+
+
+ModifierTypeInfo modifierType_NavMesh = {
+ /* name */ "NavMesh",
+ /* structName */ "NavMeshModifierData",
+ /* structSize */ sizeof(NavMeshModifierData),
+ /* type */ eModifierTypeType_Constructive,
+ /* flags */ (ModifierTypeFlag) (eModifierTypeFlag_AcceptsMesh
+ | eModifierTypeFlag_Single),
+ /* copyData */ copyData,
+ /* deformVerts */ 0,
+ /* deformMatrices */ 0,
+ /* deformVertsEM */ 0,
+ /* deformMatricesEM */ 0,
+ /* applyModifier */ applyModifier,
+ /* applyModifierEM */ 0,
+ /* initData */ initData,
+ /* requiredDataMask */ 0,
+ /* freeData */ 0,
+ /* isDisabled */ 0,
+ /* updateDepgraph */ 0,
+ /* dependsOnTime */ 0,
+ /* foreachObjectLink */ 0,
+ /* foreachIDLink */ 0,
+};
+
+}; \ No newline at end of file
diff --git a/source/blender/modifiers/intern/MOD_util.c b/source/blender/modifiers/intern/MOD_util.c
index e9b835eab81..74fc022c343 100644
--- a/source/blender/modifiers/intern/MOD_util.c
+++ b/source/blender/modifiers/intern/MOD_util.c
@@ -295,5 +295,6 @@ void modifier_type_init(ModifierTypeInfo *types[])
INIT_TYPE(Solidify);
INIT_TYPE(Screw);
INIT_TYPE(Warp);
+ INIT_TYPE(NavMesh);
#undef INIT_TYPE
}
diff --git a/source/creator/CMakeLists.txt b/source/creator/CMakeLists.txt
index bf2808ba4c0..243f51f04db 100644
--- a/source/creator/CMakeLists.txt
+++ b/source/creator/CMakeLists.txt
@@ -797,6 +797,7 @@ endif()
bf_intern_smoke
extern_minilzo
extern_lzma
+ extern_recastnavigation
ge_logic_ketsji
ge_phys_common
ge_logic
diff --git a/source/gameengine/Converter/BL_BlenderDataConversion.cpp b/source/gameengine/Converter/BL_BlenderDataConversion.cpp
index fcfc07e631e..d4b43cd7ac1 100644
--- a/source/gameengine/Converter/BL_BlenderDataConversion.cpp
+++ b/source/gameengine/Converter/BL_BlenderDataConversion.cpp
@@ -180,6 +180,9 @@ extern Material defmaterial; /* material.c */
#include "BL_ArmatureObject.h"
#include "BL_DeformableGameObject.h"
+#include "KX_NavMeshObject.h"
+#include "KX_ObstacleSimulation.h"
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -1741,7 +1744,14 @@ static KX_GameObject *gameobject_from_blenderobject(
// needed for python scripting
kxscene->GetLogicManager()->RegisterMeshName(meshobj->GetName(),meshobj);
-
+
+ if (ob->gameflag & OB_NAVMESH)
+ {
+ gameobj = new KX_NavMeshObject(kxscene,KX_Scene::m_callbacks);
+ gameobj->AddMesh(meshobj);
+ break;
+ }
+
gameobj = new BL_DeformableGameObject(ob,kxscene,KX_Scene::m_callbacks);
// set transformation
@@ -2710,6 +2720,46 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
converter->RegisterWorldInfo(worldinfo);
kxscene->SetWorldInfo(worldinfo);
+ //create object representations for obstacle simulation
+ KX_ObstacleSimulation* obssimulation = kxscene->GetObstacleSimulation();
+ if (obssimulation)
+ {
+ for ( i=0;i<objectlist->GetCount();i++)
+ {
+ KX_GameObject* gameobj = static_cast<KX_GameObject*>(objectlist->GetValue(i));
+ struct Object* blenderobject = gameobj->GetBlenderObject();
+ if (blenderobject->gameflag & OB_HASOBSTACLE)
+ {
+ obssimulation->AddObstacleForObj(gameobj);
+ }
+ }
+ }
+
+ //process navigation mesh objects
+ for ( i=0; i<objectlist->GetCount();i++)
+ {
+ KX_GameObject* gameobj = static_cast<KX_GameObject*>(objectlist->GetValue(i));
+ struct Object* blenderobject = gameobj->GetBlenderObject();
+ if (blenderobject->type==OB_MESH && (blenderobject->gameflag & OB_NAVMESH))
+ {
+ KX_NavMeshObject* navmesh = static_cast<KX_NavMeshObject*>(gameobj);
+ navmesh->SetVisible(0, true);
+ navmesh->BuildNavMesh();
+ if (obssimulation)
+ obssimulation->AddObstaclesForNavMesh(navmesh);
+ }
+ }
+ for ( i=0; i<inactivelist->GetCount();i++)
+ {
+ KX_GameObject* gameobj = static_cast<KX_GameObject*>(inactivelist->GetValue(i));
+ struct Object* blenderobject = gameobj->GetBlenderObject();
+ if (blenderobject->type==OB_MESH && (blenderobject->gameflag & OB_NAVMESH))
+ {
+ KX_NavMeshObject* navmesh = static_cast<KX_NavMeshObject*>(gameobj);
+ navmesh->SetVisible(0, true);
+ }
+ }
+
#define CONVERT_LOGIC
#ifdef CONVERT_LOGIC
// convert logic bricks, sensors, controllers and actuators
diff --git a/source/gameengine/Converter/CMakeLists.txt b/source/gameengine/Converter/CMakeLists.txt
index 3a217ce9d74..039f454e870 100644
--- a/source/gameengine/Converter/CMakeLists.txt
+++ b/source/gameengine/Converter/CMakeLists.txt
@@ -56,6 +56,7 @@ set(INC
../../../intern/guardedalloc
../../../intern/moto/include
../../../intern/string
+ ../../../extern/recastnavigation/Detour/Include
)
set(INC_SYS
diff --git a/source/gameengine/Converter/KX_ConvertActuators.cpp b/source/gameengine/Converter/KX_ConvertActuators.cpp
index 8fc224fba6f..c73ffc2b932 100644
--- a/source/gameengine/Converter/KX_ConvertActuators.cpp
+++ b/source/gameengine/Converter/KX_ConvertActuators.cpp
@@ -72,6 +72,7 @@
#include "KX_SCA_ReplaceMeshActuator.h"
#include "KX_ParentActuator.h"
#include "KX_SCA_DynamicActuator.h"
+#include "KX_SteeringActuator.h"
#include "KX_Scene.h"
#include "KX_KetsjiEngine.h"
@@ -100,6 +101,7 @@
#include "BL_ActionActuator.h"
#include "BL_ShapeActionActuator.h"
#include "BL_ArmatureActuator.h"
+#include "RNA_access.h"
#include "BL_Action.h"
/* end of blender include block */
@@ -1057,6 +1059,45 @@ void BL_ConvertActuators(char* maggiename,
baseact = tmparmact;
break;
}
+ case ACT_STEERING:
+ {
+ bSteeringActuator *stAct = (bSteeringActuator *) bact->data;
+ KX_GameObject *navmeshob = NULL;
+ if (stAct->navmesh)
+ {
+ PointerRNA settings_ptr;
+ RNA_pointer_create((ID *)stAct->navmesh, &RNA_GameObjectSettings, stAct->navmesh, &settings_ptr);
+ if (RNA_enum_get(&settings_ptr, "physics_type") == OB_BODY_TYPE_NAVMESH)
+ navmeshob = converter->FindGameObject(stAct->navmesh);
+ }
+ KX_GameObject *targetob = converter->FindGameObject(stAct->target);
+
+ int mode = KX_SteeringActuator::KX_STEERING_NODEF;
+ switch(stAct->type)
+ {
+ case ACT_STEERING_SEEK:
+ mode = KX_SteeringActuator::KX_STEERING_SEEK;
+ break;
+ case ACT_STEERING_FLEE:
+ mode = KX_SteeringActuator::KX_STEERING_FLEE;
+ break;
+ case ACT_STEERING_PATHFOLLOWING:
+ mode = KX_SteeringActuator::KX_STEERING_PATHFOLLOWING;
+ break;
+ }
+
+ bool selfTerminated = (stAct->flag & ACT_STEERING_SELFTERMINATED) !=0;
+ bool enableVisualization = (stAct->flag & ACT_STEERING_ENABLEVISUALIZATION) !=0;
+ short facingMode = (stAct->flag & ACT_STEERING_AUTOMATICFACING) ? stAct->facingaxis : 0;
+ bool normalup = (stAct->flag & ACT_STEERING_NORMALUP) !=0;
+ KX_SteeringActuator *tmpstact
+ = new KX_SteeringActuator(gameobj, mode, targetob, navmeshob,stAct->dist,
+ stAct->velocity, stAct->acceleration, stAct->turnspeed,
+ selfTerminated, stAct->updateTime,
+ scene->GetObstacleSimulation(), facingMode, normalup, enableVisualization);
+ baseact = tmpstact;
+ break;
+ }
default:
; /* generate some error */
}
diff --git a/source/gameengine/Converter/SConscript b/source/gameengine/Converter/SConscript
index 0ae22d548c5..e155677e522 100644
--- a/source/gameengine/Converter/SConscript
+++ b/source/gameengine/Converter/SConscript
@@ -20,6 +20,7 @@ incs += ' #source/blender/misc #source/blender/blenloader #source/blender/gpu'
incs += ' #source/blender/windowmanager'
incs += ' #source/blender/makesrna'
incs += ' #source/blender/ikplugin'
+incs += ' #extern/recastnavigation/Detour/Include'
incs += ' #extern/Eigen2'
incs += ' ' + env['BF_BULLET_INC']
diff --git a/source/gameengine/GameLogic/SCA_IActuator.h b/source/gameengine/GameLogic/SCA_IActuator.h
index bfcec983e2a..d2a8de32895 100644
--- a/source/gameengine/GameLogic/SCA_IActuator.h
+++ b/source/gameengine/GameLogic/SCA_IActuator.h
@@ -90,6 +90,7 @@ public:
KX_ACT_SHAPEACTION,
KX_ACT_STATE,
KX_ACT_ARMATURE,
+ KX_ACT_STEERING,
};
SCA_IActuator(SCA_IObject* gameobj, KX_ACTUATOR_TYPE type);
diff --git a/source/gameengine/Ketsji/CMakeLists.txt b/source/gameengine/Ketsji/CMakeLists.txt
index 99c9fb25a65..6846da5b019 100644
--- a/source/gameengine/Ketsji/CMakeLists.txt
+++ b/source/gameengine/Ketsji/CMakeLists.txt
@@ -57,6 +57,9 @@ set(INC
set(INC_SYS
${GLEW_INCLUDE_PATH}
+ ../../../extern/recastnavigation/Recast/Include
+ ../../../extern/recastnavigation/Detour/Include
+ ../../../source/blender/editors/include
)
set(SRC
@@ -90,9 +93,11 @@ set(SRC
KX_MeshProxy.cpp
KX_MotionState.cpp
KX_MouseFocusSensor.cpp
+ KX_NavMeshObject.cpp
KX_NearSensor.cpp
KX_ObColorIpoSGController.cpp
KX_ObjectActuator.cpp
+ KX_ObstacleSimulation.cpp
KX_OrientationInterpolator.cpp
KX_ParentActuator.cpp
KX_PhysicsObjectWrapper.cpp
@@ -120,6 +125,7 @@ set(SRC
KX_SceneActuator.cpp
KX_SoundActuator.cpp
KX_StateActuator.cpp
+ KX_SteeringActuator.cpp
KX_TimeCategoryLogger.cpp
KX_TimeLogger.cpp
KX_TouchEventManager.cpp
@@ -167,9 +173,11 @@ set(SRC
KX_MeshProxy.h
KX_MotionState.h
KX_MouseFocusSensor.h
+ KX_NavMeshObject.h
KX_NearSensor.h
KX_ObColorIpoSGController.h
KX_ObjectActuator.h
+ KX_ObstacleSimulation.h
KX_OrientationInterpolator.h
KX_ParentActuator.h
KX_PhysicsEngineEnums.h
@@ -199,6 +207,7 @@ set(SRC
KX_SceneActuator.h
KX_SoundActuator.h
KX_StateActuator.h
+ KX_SteeringActuator.h
KX_TimeCategoryLogger.h
KX_TimeLogger.h
KX_TouchEventManager.h
diff --git a/source/gameengine/Ketsji/KX_GameObject.cpp b/source/gameengine/Ketsji/KX_GameObject.cpp
index 6adaea2d6ad..ae8d7094015 100644
--- a/source/gameengine/Ketsji/KX_GameObject.cpp
+++ b/source/gameengine/Ketsji/KX_GameObject.cpp
@@ -73,6 +73,7 @@ typedef unsigned long uint_ptr;
#include "SCA_ISensor.h"
#include "SCA_IController.h"
#include "NG_NetworkScene.h" //Needed for sendMessage()
+#include "KX_ObstacleSimulation.h"
#include "BL_ActionManager.h"
@@ -110,7 +111,8 @@ KX_GameObject::KX_GameObject(
m_xray(false),
m_pHitObject(NULL),
m_actionManager(NULL),
- m_isDeformable(false)
+ m_isDeformable(false),
+ m_pObstacleSimulation(NULL)
#ifdef WITH_PYTHON
, m_attr_dict(NULL)
#endif
@@ -157,6 +159,12 @@ KX_GameObject::~KX_GameObject()
{
delete m_pGraphicController;
}
+
+ if (m_pObstacleSimulation)
+ {
+ m_pObstacleSimulation->DestroyObstacleForObj(this);
+ }
+
if (m_actionManager)
{
KX_GetActiveScene()->RemoveAnimatedObject(this);
@@ -428,6 +436,14 @@ void KX_GameObject::ProcessReplica()
m_actionManager = new BL_ActionManager(this);
m_state = 0;
+ KX_Scene* scene = KX_GetActiveScene();
+ KX_ObstacleSimulation* obssimulation = scene->GetObstacleSimulation();
+ struct Object* blenderobject = GetBlenderObject();
+ if (obssimulation && (blenderobject->gameflag & OB_HASOBSTACLE))
+ {
+ obssimulation->AddObstacleForObj(this);
+ }
+
#ifdef WITH_PYTHON
if(m_attr_dict)
m_attr_dict= PyDict_Copy(m_attr_dict);
diff --git a/source/gameengine/Ketsji/KX_GameObject.h b/source/gameengine/Ketsji/KX_GameObject.h
index 6e79914172b..655bc9ff080 100644
--- a/source/gameengine/Ketsji/KX_GameObject.h
+++ b/source/gameengine/Ketsji/KX_GameObject.h
@@ -65,6 +65,7 @@ class PHY_IGraphicController;
class PHY_IPhysicsEnvironment;
class BL_ActionManager;
struct Object;
+class KX_ObstacleSimulation;
struct bAction;
#ifdef WITH_PYTHON
@@ -115,6 +116,9 @@ protected:
MT_CmMatrix4x4 m_OpenGL_4x4Matrix;
+ KX_ObstacleSimulation* m_pObstacleSimulation;
+
+
// The action manager is used to play/stop/update actions
BL_ActionManager* m_actionManager;
@@ -864,6 +868,16 @@ public:
}
m_bSuspendDynamics = false;
}
+
+ void RegisterObstacle(KX_ObstacleSimulation* obstacleSimulation)
+ {
+ m_pObstacleSimulation = obstacleSimulation;
+ }
+
+ void UnregisterObstacle()
+ {
+ m_pObstacleSimulation = NULL;
+ }
KX_ClientObjectInfo* getClientInfo() { return m_pClient_info; }
diff --git a/source/gameengine/Ketsji/KX_KetsjiEngine.cpp b/source/gameengine/Ketsji/KX_KetsjiEngine.cpp
index ca67333166c..db919b7bc5a 100644
--- a/source/gameengine/Ketsji/KX_KetsjiEngine.cpp
+++ b/source/gameengine/Ketsji/KX_KetsjiEngine.cpp
@@ -84,6 +84,8 @@
#include "DNA_world_types.h"
#include "DNA_scene_types.h"
+#include "KX_NavMeshObject.h"
+
// If define: little test for Nzc: guarded drawing. If the canvas is
// not valid, skip rendering this frame.
//#define NZC_GUARDED_OUTPUT
@@ -1343,7 +1345,7 @@ void KX_KetsjiEngine::PostRenderScene(KX_Scene* scene)
#ifdef WITH_PYTHON
scene->RunDrawingCallbacks(scene->GetPostDrawCB());
#endif
- m_rasterizer->FlushDebugLines();
+ m_rasterizer->FlushDebugShapes();
}
void KX_KetsjiEngine::StopEngine()
diff --git a/source/gameengine/Ketsji/KX_NavMeshObject.cpp b/source/gameengine/Ketsji/KX_NavMeshObject.cpp
new file mode 100644
index 00000000000..499cbae85e9
--- /dev/null
+++ b/source/gameengine/Ketsji/KX_NavMeshObject.cpp
@@ -0,0 +1,708 @@
+/**
+* $Id$
+* ***** BEGIN GPL LICENSE BLOCK *****
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software Foundation,
+* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+* All rights reserved.
+*
+* The Original Code is: all of this file.
+*
+* Contributor(s): none yet.
+*
+* ***** END GPL LICENSE BLOCK *****
+*/
+
+#include "BLI_math_vector.h"
+#include "KX_NavMeshObject.h"
+#include "RAS_MeshObject.h"
+
+#include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
+
+extern "C" {
+#include "BKE_scene.h"
+#include "BKE_customdata.h"
+#include "BKE_cdderivedmesh.h"
+#include "BKE_DerivedMesh.h"
+
+
+#include "ED_navmesh_conversion.h"
+}
+
+#include "KX_PythonInit.h"
+#include "KX_PyMath.h"
+#include "Value.h"
+#include "Recast.h"
+#include "DetourStatNavMeshBuilder.h"
+#include "KX_ObstacleSimulation.h"
+
+static const int MAX_PATH_LEN = 256;
+static const float polyPickExt[3] = {2, 4, 2};
+
+static void calcMeshBounds(const float* vert, int nverts, float* bmin, float* bmax)
+{
+ bmin[0] = bmax[0] = vert[0];
+ bmin[1] = bmax[1] = vert[1];
+ bmin[2] = bmax[2] = vert[2];
+ for (int i=1; i<nverts; i++)
+ {
+ if (bmin[0]>vert[3*i+0]) bmin[0] = vert[3*i+0];
+ if (bmin[1]>vert[3*i+1]) bmin[1] = vert[3*i+1];
+ if (bmin[2]>vert[3*i+2]) bmin[2] = vert[3*i+2];
+
+ if (bmax[0]<vert[3*i+0]) bmax[0] = vert[3*i+0];
+ if (bmax[1]<vert[3*i+1]) bmax[1] = vert[3*i+1];
+ if (bmax[2]<vert[3*i+2]) bmax[2] = vert[3*i+2];
+ }
+}
+
+inline void flipAxes(float* vec)
+{
+ std::swap(vec[1],vec[2]);
+}
+KX_NavMeshObject::KX_NavMeshObject(void* sgReplicationInfo, SG_Callbacks callbacks)
+: KX_GameObject(sgReplicationInfo, callbacks)
+, m_navMesh(NULL)
+{
+
+}
+
+KX_NavMeshObject::~KX_NavMeshObject()
+{
+ if (m_navMesh)
+ delete m_navMesh;
+}
+
+CValue* KX_NavMeshObject::GetReplica()
+{
+ KX_NavMeshObject* replica = new KX_NavMeshObject(*this);
+ replica->ProcessReplica();
+ return replica;
+}
+
+void KX_NavMeshObject::ProcessReplica()
+{
+ KX_GameObject::ProcessReplica();
+
+ BuildNavMesh();
+ KX_Scene* scene = KX_GetActiveScene();
+ KX_ObstacleSimulation* obssimulation = scene->GetObstacleSimulation();
+ if (obssimulation)
+ obssimulation->AddObstaclesForNavMesh(this);
+
+}
+
+bool KX_NavMeshObject::BuildVertIndArrays(float *&vertices, int& nverts,
+ unsigned short* &polys, int& npolys, unsigned short *&dmeshes,
+ float *&dvertices, int &ndvertsuniq, unsigned short *&dtris,
+ int& ndtris, int &vertsPerPoly)
+{
+ DerivedMesh* dm = mesh_create_derived_no_virtual(KX_GetActiveScene()->GetBlenderScene(), GetBlenderObject(),
+ NULL, CD_MASK_MESH);
+ int* recastData = (int*) dm->getFaceDataArray(dm, CD_RECAST);
+ if (recastData)
+ {
+ int *dtrisToPolysMap=NULL, *dtrisToTrisMap=NULL, *trisToFacesMap=NULL;
+ int nAllVerts = 0;
+ float *allVerts = NULL;
+ buildNavMeshDataByDerivedMesh(dm, vertsPerPoly, nAllVerts, allVerts, ndtris, dtris,
+ npolys, dmeshes, polys, dtrisToPolysMap, dtrisToTrisMap, trisToFacesMap);
+
+ unsigned short *verticesMap = new unsigned short[nAllVerts];
+ memset(verticesMap, 0xffff, sizeof(unsigned short)*nAllVerts);
+ int curIdx = 0;
+ //vertices - mesh verts
+ //iterate over all polys and create map for their vertices first...
+ for (int polyidx=0; polyidx<npolys; polyidx++)
+ {
+ unsigned short* poly = &polys[polyidx*vertsPerPoly*2];
+ for (int i=0; i<vertsPerPoly; i++)
+ {
+ unsigned short idx = poly[i];
+ if (idx==0xffff)
+ break;
+ if (verticesMap[idx]==0xffff)
+ {
+ verticesMap[idx] = curIdx++;
+ }
+ poly[i] = verticesMap[idx];
+ }
+ }
+ nverts = curIdx;
+ //...then iterate over detailed meshes
+ //transform indices to local ones (for each navigation polygon)
+ for (int polyidx=0; polyidx<npolys; polyidx++)
+ {
+ unsigned short *poly = &polys[polyidx*vertsPerPoly*2];
+ int nv = polyNumVerts(poly, vertsPerPoly);
+ unsigned short *dmesh = &dmeshes[4*polyidx];
+ unsigned short tribase = dmesh[2];
+ unsigned short trinum = dmesh[3];
+ unsigned short vbase = curIdx;
+ for (int j=0; j<trinum; j++)
+ {
+ unsigned short* dtri = &dtris[(tribase+j)*3*2];
+ for (int k=0; k<3; k++)
+ {
+ int newVertexIdx = verticesMap[dtri[k]];
+ if (newVertexIdx==0xffff)
+ {
+ newVertexIdx = curIdx++;
+ verticesMap[dtri[k]] = newVertexIdx;
+ }
+
+ if (newVertexIdx<nverts)
+ {
+ //it's polygon vertex ("shared")
+ int idxInPoly = polyFindVertex(poly, vertsPerPoly, newVertexIdx);
+ if (idxInPoly==-1)
+ {
+ printf("Building NavMeshObject: Error! Can't find vertex in polygon\n");
+ return false;
+ }
+ dtri[k] = idxInPoly;
+ }
+ else
+ {
+ dtri[k] = newVertexIdx - vbase + nv;
+ }
+ }
+ }
+ dmesh[0] = vbase-nverts; //verts base
+ dmesh[1] = curIdx-vbase; //verts num
+ }
+
+ vertices = new float[nverts*3];
+ ndvertsuniq = curIdx - nverts;
+ if (ndvertsuniq>0)
+ {
+ dvertices = new float[ndvertsuniq*3];
+ }
+ for (int vi=0; vi<nAllVerts; vi++)
+ {
+ int newIdx = verticesMap[vi];
+ if (newIdx!=0xffff)
+ {
+ if (newIdx<nverts)
+ {
+ //navigation mesh vertex
+ memcpy(vertices+3*newIdx, allVerts+3*vi, 3*sizeof(float));
+ }
+ else
+ {
+ //detailed mesh vertex
+ memcpy(dvertices+3*(newIdx-nverts), allVerts+3*vi, 3*sizeof(float));
+ }
+ }
+ }
+ }
+ else
+ {
+ //create from RAS_MeshObject (detailed mesh is fake)
+ RAS_MeshObject* meshobj = GetMesh(0);
+ vertsPerPoly = 3;
+ nverts = meshobj->m_sharedvertex_map.size();
+ if (nverts >= 0xffff)
+ return false;
+ //calculate count of tris
+ int nmeshpolys = meshobj->NumPolygons();
+ npolys = nmeshpolys;
+ for (int p=0; p<nmeshpolys; p++)
+ {
+ int vertcount = meshobj->GetPolygon(p)->VertexCount();
+ npolys+=vertcount-3;
+ }
+
+ //create verts
+ vertices = new float[nverts*3];
+ float* vert = vertices;
+ for (int vi=0; vi<nverts; vi++)
+ {
+ const float* pos = !meshobj->m_sharedvertex_map[vi].empty() ? meshobj->GetVertexLocation(vi) : NULL;
+ if (pos)
+ copy_v3_v3(vert, pos);
+ else
+ {
+ memset(vert, NULL, 3*sizeof(float)); //vertex isn't in any poly, set dummy zero coordinates
+ }
+ vert+=3;
+ }
+
+ //create tris
+ polys = new unsigned short[npolys*3*2];
+ memset(polys, 0xff, sizeof(unsigned short)*3*2*npolys);
+ unsigned short *poly = polys;
+ RAS_Polygon* raspoly;
+ for (int p=0; p<nmeshpolys; p++)
+ {
+ raspoly = meshobj->GetPolygon(p);
+ for (int v=0; v<raspoly->VertexCount()-2; v++)
+ {
+ poly[0]= raspoly->GetVertex(0)->getOrigIndex();
+ for (size_t i=1; i<3; i++)
+ {
+ poly[i]= raspoly->GetVertex(v+i)->getOrigIndex();
+ }
+ poly += 6;
+ }
+ }
+ dmeshes = NULL;
+ dvertices = NULL;
+ ndvertsuniq = 0;
+ dtris = NULL;
+ ndtris = npolys;
+ }
+ dm->release(dm);
+
+ return true;
+}
+
+
+bool KX_NavMeshObject::BuildNavMesh()
+{
+ if (m_navMesh)
+ {
+ delete m_navMesh;
+ m_navMesh = NULL;
+ }
+
+ if (GetMeshCount()==0)
+ {
+ printf("Can't find mesh for navmesh object: %s \n", m_name);
+ return false;
+ }
+
+ float *vertices = NULL, *dvertices = NULL;
+ unsigned short *polys = NULL, *dtris = NULL, *dmeshes = NULL;
+ int nverts = 0, npolys = 0, ndvertsuniq = 0, ndtris = 0;
+ int vertsPerPoly = 0;
+ if (!BuildVertIndArrays(vertices, nverts, polys, npolys,
+ dmeshes, dvertices, ndvertsuniq, dtris, ndtris, vertsPerPoly )
+ || vertsPerPoly<3)
+ {
+ printf("Can't build navigation mesh data for object:%s \n", m_name);
+ return false;
+ }
+
+ MT_Point3 pos;
+ if (dmeshes==NULL)
+ {
+ for (int i=0; i<nverts; i++)
+ {
+ flipAxes(&vertices[i*3]);
+ }
+ for (int i=0; i<ndvertsuniq; i++)
+ {
+ flipAxes(&dvertices[i*3]);
+ }
+ }
+
+ buildMeshAdjacency(polys, npolys, nverts, vertsPerPoly);
+
+ float cs = 0.2f;
+
+ if (!nverts || !npolys)
+ return false;
+
+ float bmin[3], bmax[3];
+ calcMeshBounds(vertices, nverts, bmin, bmax);
+ //quantize vertex pos
+ unsigned short* vertsi = new unsigned short[3*nverts];
+ float ics = 1.f/cs;
+ for (int i=0; i<nverts; i++)
+ {
+ vertsi[3*i+0] = static_cast<unsigned short>((vertices[3*i+0]-bmin[0])*ics);
+ vertsi[3*i+1] = static_cast<unsigned short>((vertices[3*i+1]-bmin[1])*ics);
+ vertsi[3*i+2] = static_cast<unsigned short>((vertices[3*i+2]-bmin[2])*ics);
+ }
+
+ // Calculate data size
+ const int headerSize = sizeof(dtStatNavMeshHeader);
+ const int vertsSize = sizeof(float)*3*nverts;
+ const int polysSize = sizeof(dtStatPoly)*npolys;
+ const int nodesSize = sizeof(dtStatBVNode)*npolys*2;
+ const int detailMeshesSize = sizeof(dtStatPolyDetail)*npolys;
+ const int detailVertsSize = sizeof(float)*3*ndvertsuniq;
+ const int detailTrisSize = sizeof(unsigned char)*4*ndtris;
+
+ const int dataSize = headerSize + vertsSize + polysSize + nodesSize +
+ detailMeshesSize + detailVertsSize + detailTrisSize;
+ unsigned char* data = new unsigned char[dataSize];
+ if (!data)
+ return false;
+ memset(data, 0, dataSize);
+
+ unsigned char* d = data;
+ dtStatNavMeshHeader* header = (dtStatNavMeshHeader*)d; d += headerSize;
+ float* navVerts = (float*)d; d += vertsSize;
+ dtStatPoly* navPolys = (dtStatPoly*)d; d += polysSize;
+ dtStatBVNode* navNodes = (dtStatBVNode*)d; d += nodesSize;
+ dtStatPolyDetail* navDMeshes = (dtStatPolyDetail*)d; d += detailMeshesSize;
+ float* navDVerts = (float*)d; d += detailVertsSize;
+ unsigned char* navDTris = (unsigned char*)d; d += detailTrisSize;
+
+ // Store header
+ header->magic = DT_STAT_NAVMESH_MAGIC;
+ header->version = DT_STAT_NAVMESH_VERSION;
+ header->npolys = npolys;
+ header->nverts = nverts;
+ header->cs = cs;
+ header->bmin[0] = bmin[0];
+ header->bmin[1] = bmin[1];
+ header->bmin[2] = bmin[2];
+ header->bmax[0] = bmax[0];
+ header->bmax[1] = bmax[1];
+ header->bmax[2] = bmax[2];
+ header->ndmeshes = npolys;
+ header->ndverts = ndvertsuniq;
+ header->ndtris = ndtris;
+
+ // Store vertices
+ for (int i = 0; i < nverts; ++i)
+ {
+ const unsigned short* iv = &vertsi[i*3];
+ float* v = &navVerts[i*3];
+ v[0] = bmin[0] + iv[0] * cs;
+ v[1] = bmin[1] + iv[1] * cs;
+ v[2] = bmin[2] + iv[2] * cs;
+ }
+ //memcpy(navVerts, vertices, nverts*3*sizeof(float));
+
+ // Store polygons
+ const unsigned short* src = polys;
+ for (int i = 0; i < npolys; ++i)
+ {
+ dtStatPoly* p = &navPolys[i];
+ p->nv = 0;
+ for (int j = 0; j < vertsPerPoly; ++j)
+ {
+ if (src[j] == 0xffff) break;
+ p->v[j] = src[j];
+ p->n[j] = src[vertsPerPoly+j]+1;
+ p->nv++;
+ }
+ src += vertsPerPoly*2;
+ }
+
+ header->nnodes = createBVTree(vertsi, nverts, polys, npolys, vertsPerPoly,
+ cs, cs, npolys*2, navNodes);
+
+
+ if (dmeshes==NULL)
+ {
+ //create fake detail meshes
+ for (int i = 0; i < npolys; ++i)
+ {
+ dtStatPolyDetail& dtl = navDMeshes[i];
+ dtl.vbase = 0;
+ dtl.nverts = 0;
+ dtl.tbase = i;
+ dtl.ntris = 1;
+ }
+ // setup triangles.
+ unsigned char* tri = navDTris;
+ for(size_t i=0; i<ndtris; i++)
+ {
+ for (size_t j=0; j<3; j++)
+ tri[4*i+j] = j;
+ }
+ }
+ else
+ {
+ //verts
+ memcpy(navDVerts, dvertices, ndvertsuniq*3*sizeof(float));
+ //tris
+ unsigned char* tri = navDTris;
+ for(size_t i=0; i<ndtris; i++)
+ {
+ for (size_t j=0; j<3; j++)
+ tri[4*i+j] = dtris[6*i+j];
+ }
+ //detailed meshes
+ for (int i = 0; i < npolys; ++i)
+ {
+ dtStatPolyDetail& dtl = navDMeshes[i];
+ dtl.vbase = dmeshes[i*4+0];
+ dtl.nverts = dmeshes[i*4+1];
+ dtl.tbase = dmeshes[i*4+2];
+ dtl.ntris = dmeshes[i*4+3];
+ }
+ }
+
+ m_navMesh = new dtStatNavMesh;
+ m_navMesh->init(data, dataSize, true);
+
+ delete [] vertices;
+ delete [] polys;
+ if (dvertices)
+ {
+ delete [] dvertices;
+ }
+
+ return true;
+}
+
+dtStatNavMesh* KX_NavMeshObject::GetNavMesh()
+{
+ return m_navMesh;
+}
+
+void KX_NavMeshObject::DrawNavMesh(NavMeshRenderMode renderMode)
+{
+ if (!m_navMesh)
+ return;
+ MT_Vector3 color(0.f, 0.f, 0.f);
+
+ switch (renderMode)
+ {
+ case RM_POLYS :
+ case RM_WALLS :
+ for (int pi=0; pi<m_navMesh->getPolyCount(); pi++)
+ {
+ const dtStatPoly* poly = m_navMesh->getPoly(pi);
+
+ for (int i = 0, j = (int)poly->nv-1; i < (int)poly->nv; j = i++)
+ {
+ if (poly->n[j] && renderMode==RM_WALLS)
+ continue;
+ const float* vif = m_navMesh->getVertex(poly->v[i]);
+ const float* vjf = m_navMesh->getVertex(poly->v[j]);
+ MT_Point3 vi(vif[0], vif[2], vif[1]);
+ MT_Point3 vj(vjf[0], vjf[2], vjf[1]);
+ vi = TransformToWorldCoords(vi);
+ vj = TransformToWorldCoords(vj);
+ KX_RasterizerDrawDebugLine(vi, vj, color);
+ }
+ }
+ break;
+ case RM_TRIS :
+ for (int i = 0; i < m_navMesh->getPolyDetailCount(); ++i)
+ {
+ const dtStatPoly* p = m_navMesh->getPoly(i);
+ const dtStatPolyDetail* pd = m_navMesh->getPolyDetail(i);
+
+ for (int j = 0; j < pd->ntris; ++j)
+ {
+ const unsigned char* t = m_navMesh->getDetailTri(pd->tbase+j);
+ MT_Point3 tri[3];
+ for (int k = 0; k < 3; ++k)
+ {
+ const float* v;
+ if (t[k] < p->nv)
+ v = m_navMesh->getVertex(p->v[t[k]]);
+ else
+ v = m_navMesh->getDetailVertex(pd->vbase+(t[k]-p->nv));
+ float pos[3];
+ vcopy(pos, v);
+ flipAxes(pos);
+ tri[k].setValue(pos);
+ }
+
+ for (int k=0; k<3; k++)
+ tri[k] = TransformToWorldCoords(tri[k]);
+
+ for (int k=0; k<3; k++)
+ KX_RasterizerDrawDebugLine(tri[k], tri[(k+1)%3], color);
+ }
+ }
+ break;
+ }
+}
+
+MT_Point3 KX_NavMeshObject::TransformToLocalCoords(const MT_Point3& wpos)
+{
+ MT_Matrix3x3 orientation = NodeGetWorldOrientation();
+ const MT_Vector3& scaling = NodeGetWorldScaling();
+ orientation.scale(scaling[0], scaling[1], scaling[2]);
+ MT_Transform worldtr(NodeGetWorldPosition(), orientation);
+ MT_Transform invworldtr;
+ invworldtr.invert(worldtr);
+ MT_Point3 lpos = invworldtr(wpos);
+ return lpos;
+}
+
+MT_Point3 KX_NavMeshObject::TransformToWorldCoords(const MT_Point3& lpos)
+{
+ MT_Matrix3x3 orientation = NodeGetWorldOrientation();
+ const MT_Vector3& scaling = NodeGetWorldScaling();
+ orientation.scale(scaling[0], scaling[1], scaling[2]);
+ MT_Transform worldtr(NodeGetWorldPosition(), orientation);
+ MT_Point3 wpos = worldtr(lpos);
+ return wpos;
+}
+
+int KX_NavMeshObject::FindPath(const MT_Point3& from, const MT_Point3& to, float* path, int maxPathLen)
+{
+ if (!m_navMesh)
+ return 0;
+ MT_Point3 localfrom = TransformToLocalCoords(from);
+ MT_Point3 localto = TransformToLocalCoords(to);
+ float spos[3], epos[3];
+ localfrom.getValue(spos); flipAxes(spos);
+ localto.getValue(epos); flipAxes(epos);
+ dtStatPolyRef sPolyRef = m_navMesh->findNearestPoly(spos, polyPickExt);
+ dtStatPolyRef ePolyRef = m_navMesh->findNearestPoly(epos, polyPickExt);
+
+ int pathLen = 0;
+ if (sPolyRef && ePolyRef)
+ {
+ dtStatPolyRef* polys = new dtStatPolyRef[maxPathLen];
+ int npolys;
+ npolys = m_navMesh->findPath(sPolyRef, ePolyRef, spos, epos, polys, maxPathLen);
+ if (npolys)
+ {
+ pathLen = m_navMesh->findStraightPath(spos, epos, polys, npolys, path, maxPathLen);
+ for (int i=0; i<pathLen; i++)
+ {
+ flipAxes(&path[i*3]);
+ MT_Point3 waypoint(&path[i*3]);
+ waypoint = TransformToWorldCoords(waypoint);
+ waypoint.getValue(&path[i*3]);
+ }
+ }
+ }
+
+ return pathLen;
+}
+
+float KX_NavMeshObject::Raycast(const MT_Point3& from, const MT_Point3& to)
+{
+ if (!m_navMesh)
+ return 0.f;
+ MT_Point3 localfrom = TransformToLocalCoords(from);
+ MT_Point3 localto = TransformToLocalCoords(to);
+ float spos[3], epos[3];
+ localfrom.getValue(spos); flipAxes(spos);
+ localto.getValue(epos); flipAxes(epos);
+ dtStatPolyRef sPolyRef = m_navMesh->findNearestPoly(spos, polyPickExt);
+ float t=0;
+ static dtStatPolyRef polys[MAX_PATH_LEN];
+ m_navMesh->raycast(sPolyRef, spos, epos, t, polys, MAX_PATH_LEN);
+ return t;
+}
+
+void KX_NavMeshObject::DrawPath(const float *path, int pathLen, const MT_Vector3& color)
+{
+ MT_Vector3 a,b;
+ for (int i=0; i<pathLen-1; i++)
+ {
+ a.setValue(&path[3*i]);
+ b.setValue(&path[3*(i+1)]);
+ KX_RasterizerDrawDebugLine(a, b, color);
+ }
+}
+
+
+#ifndef DISABLE_PYTHON
+//----------------------------------------------------------------------------
+//Python
+
+PyTypeObject KX_NavMeshObject::Type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "KX_NavMeshObject",
+ sizeof(PyObjectPlus_Proxy),
+ 0,
+ py_base_dealloc,
+ 0,
+ 0,
+ 0,
+ 0,
+ py_base_repr,
+ 0,
+ 0,
+ 0,
+ 0,0,0,0,0,0,
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ 0,0,0,0,0,0,0,
+ Methods,
+ 0,
+ 0,
+ &KX_GameObject::Type,
+ 0,0,0,0,0,0,
+ py_base_new
+};
+
+PyAttributeDef KX_NavMeshObject::Attributes[] = {
+ { NULL } //Sentinel
+};
+
+//KX_PYMETHODTABLE_NOARGS(KX_GameObject, getD),
+PyMethodDef KX_NavMeshObject::Methods[] = {
+ KX_PYMETHODTABLE(KX_NavMeshObject, findPath),
+ KX_PYMETHODTABLE(KX_NavMeshObject, raycast),
+ KX_PYMETHODTABLE(KX_NavMeshObject, draw),
+ KX_PYMETHODTABLE(KX_NavMeshObject, rebuild),
+ {NULL,NULL} //Sentinel
+};
+
+KX_PYMETHODDEF_DOC(KX_NavMeshObject, findPath,
+ "findPath(start, goal): find path from start to goal points\n"
+ "Returns a path as list of points)\n")
+{
+ PyObject *ob_from, *ob_to;
+ if (!PyArg_ParseTuple(args,"OO:getPath",&ob_from,&ob_to))
+ return NULL;
+ MT_Point3 from, to;
+ if (!PyVecTo(ob_from, from) || !PyVecTo(ob_to, to))
+ return NULL;
+
+ float path[MAX_PATH_LEN*3];
+ int pathLen = FindPath(from, to, path, MAX_PATH_LEN);
+ PyObject *pathList = PyList_New( pathLen );
+ for (int i=0; i<pathLen; i++)
+ {
+ MT_Point3 point(&path[3*i]);
+ PyList_SET_ITEM(pathList, i, PyObjectFrom(point));
+ }
+
+ return pathList;
+}
+
+KX_PYMETHODDEF_DOC(KX_NavMeshObject, raycast,
+ "raycast(start, goal): raycast from start to goal points\n"
+ "Returns hit factor)\n")
+{
+ PyObject *ob_from, *ob_to;
+ if (!PyArg_ParseTuple(args,"OO:getPath",&ob_from,&ob_to))
+ return NULL;
+ MT_Point3 from, to;
+ if (!PyVecTo(ob_from, from) || !PyVecTo(ob_to, to))
+ return NULL;
+ float hit = Raycast(from, to);
+ return PyFloat_FromDouble(hit);
+}
+
+KX_PYMETHODDEF_DOC(KX_NavMeshObject, draw,
+ "draw(mode): navigation mesh debug drawing\n"
+ "mode: WALLS, POLYS, TRIS\n")
+{
+ int arg;
+ NavMeshRenderMode renderMode = RM_TRIS;
+ if (PyArg_ParseTuple(args,"i:rebuild",&arg) && arg>=0 && arg<RM_MAX)
+ renderMode = (NavMeshRenderMode)arg;
+ DrawNavMesh(renderMode);
+ Py_RETURN_NONE;
+}
+
+KX_PYMETHODDEF_DOC_NOARGS(KX_NavMeshObject, rebuild,
+ "rebuild(): rebuild navigation mesh\n")
+{
+ BuildNavMesh();
+ Py_RETURN_NONE;
+}
+
+#endif // DISABLE_PYTHON \ No newline at end of file
diff --git a/source/gameengine/Ketsji/KX_NavMeshObject.h b/source/gameengine/Ketsji/KX_NavMeshObject.h
new file mode 100644
index 00000000000..78e9488ad1c
--- /dev/null
+++ b/source/gameengine/Ketsji/KX_NavMeshObject.h
@@ -0,0 +1,83 @@
+/**
+* $Id$
+*
+* ***** BEGIN GPL LICENSE BLOCK *****
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software Foundation,
+* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+* All rights reserved.
+*
+* The Original Code is: all of this file.
+*
+* Contributor(s): none yet.
+*
+* ***** END GPL LICENSE BLOCK *****
+*/
+#ifndef __KX_NAVMESHOBJECT
+#define __KX_NAVMESHOBJECT
+#include "DetourStatNavMesh.h"
+#include "KX_GameObject.h"
+#include "PyObjectPlus.h"
+#include <vector>
+
+class RAS_MeshObject;
+class MT_Transform;
+
+class KX_NavMeshObject: public KX_GameObject
+{
+ Py_Header;
+
+protected:
+ dtStatNavMesh* m_navMesh;
+
+ bool BuildVertIndArrays(float *&vertices, int& nverts,
+ unsigned short* &polys, int& npolys, unsigned short *&dmeshes,
+ float *&dvertices, int &ndvertsuniq, unsigned short* &dtris,
+ int& ndtris, int &vertsPerPoly);
+
+public:
+ KX_NavMeshObject(void* sgReplicationInfo, SG_Callbacks callbacks);
+ ~KX_NavMeshObject();
+
+ virtual CValue* GetReplica();
+ virtual void ProcessReplica();
+
+
+ bool BuildNavMesh();
+ dtStatNavMesh* GetNavMesh();
+ int FindPath(const MT_Point3& from, const MT_Point3& to, float* path, int maxPathLen);
+ float Raycast(const MT_Point3& from, const MT_Point3& to);
+
+ enum NavMeshRenderMode {RM_WALLS, RM_POLYS, RM_TRIS, RM_MAX};
+ void DrawNavMesh(NavMeshRenderMode mode);
+ void DrawPath(const float *path, int pathLen, const MT_Vector3& color);
+
+ MT_Point3 TransformToLocalCoords(const MT_Point3& wpos);
+ MT_Point3 TransformToWorldCoords(const MT_Point3& lpos);
+#ifndef DISABLE_PYTHON
+ /* --------------------------------------------------------------------- */
+ /* Python interface ---------------------------------------------------- */
+ /* --------------------------------------------------------------------- */
+
+ KX_PYMETHOD_DOC(KX_NavMeshObject, findPath);
+ KX_PYMETHOD_DOC(KX_NavMeshObject, raycast);
+ KX_PYMETHOD_DOC(KX_NavMeshObject, draw);
+ KX_PYMETHOD_DOC_NOARGS(KX_NavMeshObject, rebuild);
+#endif
+};
+
+#endif //__KX_NAVMESHOBJECT
+
diff --git a/source/gameengine/Ketsji/KX_ObstacleSimulation.cpp b/source/gameengine/Ketsji/KX_ObstacleSimulation.cpp
new file mode 100644
index 00000000000..9bca98d7fde
--- /dev/null
+++ b/source/gameengine/Ketsji/KX_ObstacleSimulation.cpp
@@ -0,0 +1,869 @@
+/**
+* Simulation for obstacle avoidance behavior
+*
+* $Id$
+*
+* ***** BEGIN GPL LICENSE BLOCK *****
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version. The Blender
+* Foundation also sells licenses for use in proprietary software under
+* the Blender License. See http://www.blender.org/BL/ for information
+* about this.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software Foundation,
+* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+* All rights reserved.
+*
+* The Original Code is: all of this file.
+*
+* Contributor(s): none yet.
+*
+* ***** END GPL LICENSE BLOCK *****
+*/
+
+#include "KX_ObstacleSimulation.h"
+#include "KX_NavMeshObject.h"
+#include "KX_PythonInit.h"
+#include "DNA_object_types.h"
+#include "BLI_math.h"
+
+namespace
+{
+ inline float perp(const MT_Vector2& a, const MT_Vector2& b) { return a.x()*b.y() - a.y()*b.x(); }
+
+ inline float sqr(float x) { return x*x; }
+ inline float lerp(float a, float b, float t) { return a + (b-a)*t; }
+ inline float clamp(float a, float mn, float mx) { return a < mn ? mn : (a > mx ? mx : a); }
+
+ inline float vdistsqr(const float* a, const float* b) { return sqr(b[0]-a[0]) + sqr(b[1]-a[1]); }
+ inline float vdist(const float* a, const float* b) { return sqrtf(vdistsqr(a,b)); }
+ inline void vcpy(float* a, const float* b) { a[0]=b[0]; a[1]=b[1]; }
+ inline float vdot(const float* a, const float* b) { return a[0]*b[0] + a[1]*b[1]; }
+ inline float vperp(const float* a, const float* b) { return a[0]*b[1] - a[1]*b[0]; }
+ inline void vsub(float* v, const float* a, const float* b) { v[0] = a[0]-b[0]; v[1] = a[1]-b[1]; }
+ inline void vadd(float* v, const float* a, const float* b) { v[0] = a[0]+b[0]; v[1] = a[1]+b[1]; }
+ inline void vscale(float* v, const float* a, const float s) { v[0] = a[0]*s; v[1] = a[1]*s; }
+ inline void vset(float* v, float x, float y) { v[0]=x; v[1]=y; }
+ inline float vlensqr(const float* v) { return vdot(v,v); }
+ inline float vlen(const float* v) { return sqrtf(vlensqr(v)); }
+ inline void vlerp(float* v, const float* a, const float* b, float t) { v[0] = lerp(a[0], b[0], t); v[1] = lerp(a[1], b[1], t); }
+ inline void vmad(float* v, const float* a, const float* b, float s) { v[0] = a[0] + b[0]*s; v[1] = a[1] + b[1]*s; }
+ inline void vnorm(float* v)
+ {
+ float d = vlen(v);
+ if (d > 0.0001f)
+ {
+ d = 1.0f/d;
+ v[0] *= d;
+ v[1] *= d;
+ }
+ }
+}
+inline float triarea(const float* a, const float* b, const float* c)
+{
+ return (b[0]*a[1] - a[0]*b[1]) + (c[0]*b[1] - b[0]*c[1]) + (a[0]*c[1] - c[0]*a[1]);
+}
+
+static void closestPtPtSeg(const float* pt,
+ const float* sp, const float* sq,
+ float& t)
+{
+ float dir[2],diff[3];
+ vsub(dir,sq,sp);
+ vsub(diff,pt,sp);
+ t = vdot(diff,dir);
+ if (t <= 0.0f) { t = 0; return; }
+ float d = vdot(dir,dir);
+ if (t >= d) { t = 1; return; }
+ t /= d;
+}
+
+static float distPtSegSqr(const float* pt, const float* sp, const float* sq)
+{
+ float t;
+ closestPtPtSeg(pt, sp,sq, t);
+ float np[2];
+ vlerp(np, sp,sq, t);
+ return vdistsqr(pt,np);
+}
+
+static int sweepCircleCircle(const MT_Vector3& pos0, const MT_Scalar r0, const MT_Vector2& v,
+ const MT_Vector3& pos1, const MT_Scalar r1,
+ float& tmin, float& tmax)
+{
+ static const float EPS = 0.0001f;
+ MT_Vector2 c0(pos0.x(), pos0.y());
+ MT_Vector2 c1(pos1.x(), pos1.y());
+ MT_Vector2 s = c1 - c0;
+ MT_Scalar r = r0+r1;
+ float c = s.length2() - r*r;
+ float a = v.length2();
+ if (a < EPS) return 0; // not moving
+
+ // Overlap, calc time to exit.
+ float b = MT_dot(v,s);
+ float d = b*b - a*c;
+ if (d < 0.0f) return 0; // no intersection.
+ tmin = (b - sqrtf(d)) / a;
+ tmax = (b + sqrtf(d)) / a;
+ return 1;
+}
+
+static int sweepCircleSegment(const MT_Vector3& pos0, const MT_Scalar r0, const MT_Vector2& v,
+ const MT_Vector3& pa, const MT_Vector3& pb, const MT_Scalar sr,
+ float& tmin, float &tmax)
+{
+ // equation parameters
+ MT_Vector2 c0(pos0.x(), pos0.y());
+ MT_Vector2 sa(pa.x(), pa.y());
+ MT_Vector2 sb(pb.x(), pb.y());
+ MT_Vector2 L = sb-sa;
+ MT_Vector2 H = c0-sa;
+ MT_Scalar radius = r0+sr;
+ float l2 = L.length2();
+ float r2 = radius * radius;
+ float dl = perp(v, L);
+ float hl = perp(H, L);
+ float a = dl * dl;
+ float b = 2.0f * hl * dl;
+ float c = hl * hl - (r2 * l2);
+ float d = (b*b) - (4.0f * a * c);
+
+ // infinite line missed by infinite ray.
+ if (d < 0.0f)
+ return 0;
+
+ d = sqrtf(d);
+ tmin = (-b - d) / (2.0f * a);
+ tmax = (-b + d) / (2.0f * a);
+
+ // line missed by ray range.
+ /* if (tmax < 0.0f || tmin > 1.0f)
+ return 0;*/
+
+ // find what part of the ray was collided.
+ MT_Vector2 Pedge;
+ Pedge = c0+v*tmin;
+ H = Pedge - sa;
+ float e0 = MT_dot(H, L) / l2;
+ Pedge = c0 + v*tmax;
+ H = Pedge - sa;
+ float e1 = MT_dot(H, L) / l2;
+
+ if (e0 < 0.0f || e1 < 0.0f)
+ {
+ float ctmin, ctmax;
+ if (sweepCircleCircle(pos0, r0, v, pa, sr, ctmin, ctmax))
+ {
+ if (e0 < 0.0f && ctmin > tmin)
+ tmin = ctmin;
+ if (e1 < 0.0f && ctmax < tmax)
+ tmax = ctmax;
+ }
+ else
+ {
+ return 0;
+ }
+ }
+
+ if (e0 > 1.0f || e1 > 1.0f)
+ {
+ float ctmin, ctmax;
+ if (sweepCircleCircle(pos0, r0, v, pb, sr, ctmin, ctmax))
+ {
+ if (e0 > 1.0f && ctmin > tmin)
+ tmin = ctmin;
+ if (e1 > 1.0f && ctmax < tmax)
+ tmax = ctmax;
+ }
+ else
+ {
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
+static bool inBetweenAngle(float a, float amin, float amax, float& t)
+{
+ if (amax < amin) amax += (float)M_PI*2;
+ if (a < amin-(float)M_PI) a += (float)M_PI*2;
+ if (a > amin+(float)M_PI) a -= (float)M_PI*2;
+ if (a >= amin && a < amax)
+ {
+ t = (a-amin) / (amax-amin);
+ return true;
+ }
+ return false;
+}
+
+static float interpolateToi(float a, const float* dir, const float* toi, const int ntoi)
+{
+ for (int i = 0; i < ntoi; ++i)
+ {
+ int next = (i+1) % ntoi;
+ float t;
+ if (inBetweenAngle(a, dir[i], dir[next], t))
+ {
+ return lerp(toi[i], toi[next], t);
+ }
+ }
+ return 0;
+}
+
+KX_ObstacleSimulation::KX_ObstacleSimulation(MT_Scalar levelHeight, bool enableVisualization)
+: m_levelHeight(levelHeight)
+, m_enableVisualization(enableVisualization)
+{
+
+}
+
+KX_ObstacleSimulation::~KX_ObstacleSimulation()
+{
+ for (size_t i=0; i<m_obstacles.size(); i++)
+ {
+ KX_Obstacle* obs = m_obstacles[i];
+ delete obs;
+ }
+ m_obstacles.clear();
+}
+KX_Obstacle* KX_ObstacleSimulation::CreateObstacle(KX_GameObject* gameobj)
+{
+ KX_Obstacle* obstacle = new KX_Obstacle();
+ obstacle->m_gameObj = gameobj;
+
+ vset(obstacle->vel, 0,0);
+ vset(obstacle->pvel, 0,0);
+ vset(obstacle->dvel, 0,0);
+ vset(obstacle->nvel, 0,0);
+ for (int i = 0; i < VEL_HIST_SIZE; ++i)
+ vset(&obstacle->hvel[i*2], 0,0);
+ obstacle->hhead = 0;
+
+ gameobj->RegisterObstacle(this);
+ m_obstacles.push_back(obstacle);
+ return obstacle;
+}
+
+void KX_ObstacleSimulation::AddObstacleForObj(KX_GameObject* gameobj)
+{
+ KX_Obstacle* obstacle = CreateObstacle(gameobj);
+ struct Object* blenderobject = gameobj->GetBlenderObject();
+ obstacle->m_type = KX_OBSTACLE_OBJ;
+ obstacle->m_shape = KX_OBSTACLE_CIRCLE;
+ obstacle->m_rad = blenderobject->obstacleRad;
+}
+
+void KX_ObstacleSimulation::AddObstaclesForNavMesh(KX_NavMeshObject* navmeshobj)
+{
+ dtStatNavMesh* navmesh = navmeshobj->GetNavMesh();
+ if (navmesh)
+ {
+ int npoly = navmesh->getPolyCount();
+ for (int pi=0; pi<npoly; pi++)
+ {
+ const dtStatPoly* poly = navmesh->getPoly(pi);
+
+ for (int i = 0, j = (int)poly->nv-1; i < (int)poly->nv; j = i++)
+ {
+ if (poly->n[j]) continue;
+ const float* vj = navmesh->getVertex(poly->v[j]);
+ const float* vi = navmesh->getVertex(poly->v[i]);
+
+ KX_Obstacle* obstacle = CreateObstacle(navmeshobj);
+ obstacle->m_type = KX_OBSTACLE_NAV_MESH;
+ obstacle->m_shape = KX_OBSTACLE_SEGMENT;
+ obstacle->m_pos = MT_Point3(vj[0], vj[2], vj[1]);
+ obstacle->m_pos2 = MT_Point3(vi[0], vi[2], vi[1]);
+ obstacle->m_rad = 0;
+ }
+ }
+ }
+}
+
+void KX_ObstacleSimulation::DestroyObstacleForObj(KX_GameObject* gameobj)
+{
+ for (size_t i=0; i<m_obstacles.size(); )
+ {
+ if (m_obstacles[i]->m_gameObj == gameobj)
+ {
+ KX_Obstacle* obstacle = m_obstacles[i];
+ obstacle->m_gameObj->UnregisterObstacle();
+ m_obstacles[i] = m_obstacles.back();
+ m_obstacles.pop_back();
+ delete obstacle;
+ }
+ else
+ i++;
+ }
+}
+
+void KX_ObstacleSimulation::UpdateObstacles()
+{
+ for (size_t i=0; i<m_obstacles.size(); i++)
+ {
+ if (m_obstacles[i]->m_type==KX_OBSTACLE_NAV_MESH || m_obstacles[i]->m_shape==KX_OBSTACLE_SEGMENT)
+ continue;
+
+ KX_Obstacle* obs = m_obstacles[i];
+ obs->m_pos = obs->m_gameObj->NodeGetWorldPosition();
+ obs->vel[0] = obs->m_gameObj->GetLinearVelocity().x();
+ obs->vel[1] = obs->m_gameObj->GetLinearVelocity().y();
+
+ // Update velocity history and calculate perceived (average) velocity.
+ vcpy(&obs->hvel[obs->hhead*2], obs->vel);
+ obs->hhead = (obs->hhead+1) % VEL_HIST_SIZE;
+ vset(obs->pvel,0,0);
+ for (int j = 0; j < VEL_HIST_SIZE; ++j)
+ vadd(obs->pvel, obs->pvel, &obs->hvel[j*2]);
+ vscale(obs->pvel, obs->pvel, 1.0f/VEL_HIST_SIZE);
+ }
+}
+
+KX_Obstacle* KX_ObstacleSimulation::GetObstacle(KX_GameObject* gameobj)
+{
+ for (size_t i=0; i<m_obstacles.size(); i++)
+ {
+ if (m_obstacles[i]->m_gameObj == gameobj)
+ return m_obstacles[i];
+ }
+
+ return NULL;
+}
+
+void KX_ObstacleSimulation::AdjustObstacleVelocity(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj,
+ MT_Vector3& velocity, MT_Scalar maxDeltaSpeed,MT_Scalar maxDeltaAngle)
+{
+}
+
+void KX_ObstacleSimulation::DrawObstacles()
+{
+ if (!m_enableVisualization)
+ return;
+ static const MT_Vector3 bluecolor(0,0,1);
+ static const MT_Vector3 normal(0.,0.,1.);
+ static const int SECTORS_NUM = 32;
+ for (size_t i=0; i<m_obstacles.size(); i++)
+ {
+ if (m_obstacles[i]->m_shape==KX_OBSTACLE_SEGMENT)
+ {
+ MT_Point3 p1 = m_obstacles[i]->m_pos;
+ MT_Point3 p2 = m_obstacles[i]->m_pos2;
+ //apply world transform
+ if (m_obstacles[i]->m_type == KX_OBSTACLE_NAV_MESH)
+ {
+ KX_NavMeshObject* navmeshobj = static_cast<KX_NavMeshObject*>(m_obstacles[i]->m_gameObj);
+ p1 = navmeshobj->TransformToWorldCoords(p1);
+ p2 = navmeshobj->TransformToWorldCoords(p2);
+ }
+
+ KX_RasterizerDrawDebugLine(p1, p2, bluecolor);
+ }
+ else if (m_obstacles[i]->m_shape==KX_OBSTACLE_CIRCLE)
+ {
+ KX_RasterizerDrawDebugCircle(m_obstacles[i]->m_pos, m_obstacles[i]->m_rad, bluecolor,
+ normal, SECTORS_NUM);
+ }
+ }
+}
+
+static MT_Point3 nearestPointToObstacle(MT_Point3& pos ,KX_Obstacle* obstacle)
+{
+ switch (obstacle->m_shape)
+ {
+ case KX_OBSTACLE_SEGMENT :
+ {
+ MT_Vector3 ab = obstacle->m_pos2 - obstacle->m_pos;
+ if (!ab.fuzzyZero())
+ {
+ MT_Vector3 abdir = ab.normalized();
+ MT_Vector3 v = pos - obstacle->m_pos;
+ MT_Scalar proj = abdir.dot(v);
+ CLAMP(proj, 0, ab.length());
+ MT_Point3 res = obstacle->m_pos + abdir*proj;
+ return res;
+ }
+ }
+ case KX_OBSTACLE_CIRCLE :
+ default:
+ return obstacle->m_pos;
+ }
+}
+
+static bool filterObstacle(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, KX_Obstacle* otherObst,
+ float levelHeight)
+{
+ //filter obstacles by type
+ if ( (otherObst == activeObst) ||
+ (otherObst->m_type==KX_OBSTACLE_NAV_MESH && otherObst->m_gameObj!=activeNavMeshObj) )
+ return false;
+
+ //filter obstacles by position
+ MT_Point3 p = nearestPointToObstacle(activeObst->m_pos, otherObst);
+ if ( fabs(activeObst->m_pos.z() - p.z()) > levelHeight)
+ return false;
+
+ return true;
+}
+
+///////////*********TOI_rays**********/////////////////
+KX_ObstacleSimulationTOI::KX_ObstacleSimulationTOI(MT_Scalar levelHeight, bool enableVisualization)
+: KX_ObstacleSimulation(levelHeight, enableVisualization),
+ m_maxSamples(32),
+ m_minToi(0.0f),
+ m_maxToi(0.0f),
+ m_velWeight(1.0f),
+ m_curVelWeight(1.0f),
+ m_toiWeight(1.0f),
+ m_collisionWeight(1.0f)
+{
+}
+
+
+void KX_ObstacleSimulationTOI::AdjustObstacleVelocity(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj,
+ MT_Vector3& velocity, MT_Scalar maxDeltaSpeed, MT_Scalar maxDeltaAngle)
+{
+ int nobs = m_obstacles.size();
+ int obstidx = std::find(m_obstacles.begin(), m_obstacles.end(), activeObst) - m_obstacles.begin();
+ if (obstidx == nobs)
+ return;
+
+ vset(activeObst->dvel, velocity.x(), velocity.y());
+
+ //apply RVO
+ sampleRVO(activeObst, activeNavMeshObj, maxDeltaAngle);
+
+ // Fake dynamic constraint.
+ float dv[2];
+ float vel[2];
+ vsub(dv, activeObst->nvel, activeObst->vel);
+ float ds = vlen(dv);
+ if (ds > maxDeltaSpeed || ds<-maxDeltaSpeed)
+ vscale(dv, dv, fabs(maxDeltaSpeed/ds));
+ vadd(vel, activeObst->vel, dv);
+
+ velocity.x() = vel[0];
+ velocity.y() = vel[1];
+}
+
+///////////*********TOI_rays**********/////////////////
+static const int AVOID_MAX_STEPS = 128;
+struct TOICircle
+{
+ TOICircle() : n(0), minToi(0), maxToi(1) {}
+ float toi[AVOID_MAX_STEPS]; // Time of impact (seconds)
+ float toie[AVOID_MAX_STEPS]; // Time of exit (seconds)
+ float dir[AVOID_MAX_STEPS]; // Direction (radians)
+ int n; // Number of samples
+ float minToi, maxToi; // Min/max TOI (seconds)
+};
+
+KX_ObstacleSimulationTOI_rays::KX_ObstacleSimulationTOI_rays(MT_Scalar levelHeight, bool enableVisualization):
+ KX_ObstacleSimulationTOI(levelHeight, enableVisualization)
+{
+ m_maxSamples = 32;
+ m_minToi = 0.5f;
+ m_maxToi = 1.2f;
+ m_velWeight = 4.0f;
+ m_toiWeight = 1.0f;
+ m_collisionWeight = 100.0f;
+}
+
+
+void KX_ObstacleSimulationTOI_rays::sampleRVO(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj,
+ const float maxDeltaAngle)
+{
+ MT_Vector2 vel(activeObst->dvel[0], activeObst->dvel[1]);
+ float vmax = (float) vel.length();
+ float odir = (float) atan2(vel.y(), vel.x());
+
+ MT_Vector2 ddir = vel;
+ ddir.normalize();
+
+ float bestScore = FLT_MAX;
+ float bestDir = odir;
+ float bestToi = 0;
+
+ TOICircle tc;
+ tc.n = m_maxSamples;
+ tc.minToi = m_minToi;
+ tc.maxToi = m_maxToi;
+
+ const int iforw = m_maxSamples/2;
+ const float aoff = (float)iforw / (float)m_maxSamples;
+
+ size_t nobs = m_obstacles.size();
+ for (int iter = 0; iter < m_maxSamples; ++iter)
+ {
+ // Calculate sample velocity
+ const float ndir = ((float)iter/(float)m_maxSamples) - aoff;
+ const float dir = odir+ndir*M_PI*2;
+ MT_Vector2 svel;
+ svel.x() = cosf(dir) * vmax;
+ svel.y() = sinf(dir) * vmax;
+
+ // Find min time of impact and exit amongst all obstacles.
+ float tmin = m_maxToi;
+ float tmine = 0;
+ for (int i = 0; i < nobs; ++i)
+ {
+ KX_Obstacle* ob = m_obstacles[i];
+ bool res = filterObstacle(activeObst, activeNavMeshObj, ob, m_levelHeight);
+ if (!res)
+ continue;
+
+ float htmin,htmax;
+
+ if (ob->m_shape == KX_OBSTACLE_CIRCLE)
+ {
+ MT_Vector2 vab;
+ if (vlen(ob->vel) < 0.01f*0.01f)
+ {
+ // Stationary, use VO
+ vab = svel;
+ }
+ else
+ {
+ // Moving, use RVO
+ vab = 2*svel - vel - ob->vel;
+ }
+
+ if (!sweepCircleCircle(activeObst->m_pos, activeObst->m_rad,
+ vab, ob->m_pos, ob->m_rad, htmin, htmax))
+ continue;
+ }
+ else if (ob->m_shape == KX_OBSTACLE_SEGMENT)
+ {
+ MT_Point3 p1 = ob->m_pos;
+ MT_Point3 p2 = ob->m_pos2;
+ //apply world transform
+ if (ob->m_type == KX_OBSTACLE_NAV_MESH)
+ {
+ KX_NavMeshObject* navmeshobj = static_cast<KX_NavMeshObject*>(ob->m_gameObj);
+ p1 = navmeshobj->TransformToWorldCoords(p1);
+ p2 = navmeshobj->TransformToWorldCoords(p2);
+ }
+ if (!sweepCircleSegment(activeObst->m_pos, activeObst->m_rad, svel,
+ p1, p2, ob->m_rad, htmin, htmax))
+ continue;
+ }
+
+ if (htmin > 0.0f)
+ {
+ // The closest obstacle is somewhere ahead of us, keep track of nearest obstacle.
+ if (htmin < tmin)
+ tmin = htmin;
+ }
+ else if (htmax > 0.0f)
+ {
+ // The agent overlaps the obstacle, keep track of first safe exit.
+ if (htmax > tmine)
+ tmine = htmax;
+ }
+ }
+
+ // Calculate sample penalties and final score.
+ const float apen = m_velWeight * fabsf(ndir);
+ const float tpen = m_toiWeight * (1.0f/(0.0001f+tmin/m_maxToi));
+ const float cpen = m_collisionWeight * (tmine/m_minToi)*(tmine/m_minToi);
+ const float score = apen + tpen + cpen;
+
+ // Update best score.
+ if (score < bestScore)
+ {
+ bestDir = dir;
+ bestToi = tmin;
+ bestScore = score;
+ }
+
+ tc.dir[iter] = dir;
+ tc.toi[iter] = tmin;
+ tc.toie[iter] = tmine;
+ }
+
+ if (vlen(activeObst->vel) > 0.1)
+ {
+ // Constrain max turn rate.
+ float cura = atan2(activeObst->vel[1],activeObst->vel[0]);
+ float da = bestDir - cura;
+ if (da < -M_PI) da += (float)M_PI*2;
+ if (da > M_PI) da -= (float)M_PI*2;
+ if (da < -maxDeltaAngle)
+ {
+ bestDir = cura - maxDeltaAngle;
+ bestToi = min(bestToi, interpolateToi(bestDir, tc.dir, tc.toi, tc.n));
+ }
+ else if (da > maxDeltaAngle)
+ {
+ bestDir = cura + maxDeltaAngle;
+ bestToi = min(bestToi, interpolateToi(bestDir, tc.dir, tc.toi, tc.n));
+ }
+ }
+
+ // Adjust speed when time of impact is less than min TOI.
+ if (bestToi < m_minToi)
+ vmax *= bestToi/m_minToi;
+
+ // New steering velocity.
+ activeObst->nvel[0] = cosf(bestDir) * vmax;
+ activeObst->nvel[1] = sinf(bestDir) * vmax;
+}
+
+///////////********* TOI_cells**********/////////////////
+
+static void processSamples(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj,
+ KX_Obstacles& obstacles, float levelHeight, const float vmax,
+ const float* spos, const float cs, const int nspos, float* res,
+ float maxToi, float velWeight, float curVelWeight, float sideWeight,
+ float toiWeight)
+{
+ vset(res, 0,0);
+
+ const float ivmax = 1.0f / vmax;
+
+ float adir[2], adist;
+ vcpy(adir, activeObst->pvel);
+ if (vlen(adir) > 0.01f)
+ vnorm(adir);
+ else
+ vset(adir,0,0);
+ float activeObstPos[2];
+ vset(activeObstPos, activeObst->m_pos.x(), activeObst->m_pos.y());
+ adist = vdot(adir, activeObstPos);
+
+ float minPenalty = FLT_MAX;
+
+ for (int n = 0; n < nspos; ++n)
+ {
+ float vcand[2];
+ vcpy(vcand, &spos[n*2]);
+
+ // Find min time of impact and exit amongst all obstacles.
+ float tmin = maxToi;
+ float side = 0;
+ int nside = 0;
+
+ for (int i = 0; i < obstacles.size(); ++i)
+ {
+ KX_Obstacle* ob = obstacles[i];
+ bool res = filterObstacle(activeObst, activeNavMeshObj, ob, levelHeight);
+ if (!res)
+ continue;
+ float htmin, htmax;
+
+ if (ob->m_shape==KX_OBSTACLE_CIRCLE)
+ {
+ float vab[2];
+
+ // Moving, use RVO
+ vscale(vab, vcand, 2);
+ vsub(vab, vab, activeObst->vel);
+ vsub(vab, vab, ob->vel);
+
+ // Side
+ // NOTE: dp, and dv are constant over the whole calculation,
+ // they can be precomputed per object.
+ const float* pa = activeObstPos;
+ float pb[2];
+ vset(pb, ob->m_pos.x(), ob->m_pos.y());
+
+ const float orig[2] = {0,0};
+ float dp[2],dv[2],np[2];
+ vsub(dp,pb,pa);
+ vnorm(dp);
+ vsub(dv,ob->dvel, activeObst->dvel);
+
+ const float a = triarea(orig, dp,dv);
+ if (a < 0.01f)
+ {
+ np[0] = -dp[1];
+ np[1] = dp[0];
+ }
+ else
+ {
+ np[0] = dp[1];
+ np[1] = -dp[0];
+ }
+
+ side += clamp(min(vdot(dp,vab)*2,vdot(np,vab)*2), 0.0f, 1.0f);
+ nside++;
+
+ if (!sweepCircleCircle(activeObst->m_pos, activeObst->m_rad, vab, ob->m_pos, ob->m_rad,
+ htmin, htmax))
+ continue;
+
+ // Handle overlapping obstacles.
+ if (htmin < 0.0f && htmax > 0.0f)
+ {
+ // Avoid more when overlapped.
+ htmin = -htmin * 0.5f;
+ }
+ }
+ else if (ob->m_shape == KX_OBSTACLE_SEGMENT)
+ {
+ MT_Point3 p1 = ob->m_pos;
+ MT_Point3 p2 = ob->m_pos2;
+ //apply world transform
+ if (ob->m_type == KX_OBSTACLE_NAV_MESH)
+ {
+ KX_NavMeshObject* navmeshobj = static_cast<KX_NavMeshObject*>(ob->m_gameObj);
+ p1 = navmeshobj->TransformToWorldCoords(p1);
+ p2 = navmeshobj->TransformToWorldCoords(p2);
+ }
+ float p[2], q[2];
+ vset(p, p1.x(), p1.y());
+ vset(q, p2.x(), p2.y());
+
+ // NOTE: the segments are assumed to come from a navmesh which is shrunken by
+ // the agent radius, hence the use of really small radius.
+ // This can be handle more efficiently by using seg-seg test instead.
+ // If the whole segment is to be treated as obstacle, use agent->rad instead of 0.01f!
+ const float r = 0.01f; // agent->rad
+ if (distPtSegSqr(activeObstPos, p, q) < sqr(r+ob->m_rad))
+ {
+ float sdir[2], snorm[2];
+ vsub(sdir, q, p);
+ snorm[0] = sdir[1];
+ snorm[1] = -sdir[0];
+ // If the velocity is pointing towards the segment, no collision.
+ if (vdot(snorm, vcand) < 0.0f)
+ continue;
+ // Else immediate collision.
+ htmin = 0.0f;
+ htmax = 10.0f;
+ }
+ else
+ {
+ if (!sweepCircleSegment(activeObstPos, r, vcand, p, q, ob->m_rad, htmin, htmax))
+ continue;
+ }
+
+ // Avoid less when facing walls.
+ htmin *= 2.0f;
+ }
+
+ if (htmin >= 0.0f)
+ {
+ // The closest obstacle is somewhere ahead of us, keep track of nearest obstacle.
+ if (htmin < tmin)
+ tmin = htmin;
+ }
+ }
+
+ // Normalize side bias, to prevent it dominating too much.
+ if (nside)
+ side /= nside;
+
+ const float vpen = velWeight * (vdist(vcand, activeObst->dvel) * ivmax);
+ const float vcpen = curVelWeight * (vdist(vcand, activeObst->vel) * ivmax);
+ const float spen = sideWeight * side;
+ const float tpen = toiWeight * (1.0f/(0.1f+tmin/maxToi));
+
+ const float penalty = vpen + vcpen + spen + tpen;
+
+ if (penalty < minPenalty)
+ {
+ minPenalty = penalty;
+ vcpy(res, vcand);
+ }
+ }
+}
+
+void KX_ObstacleSimulationTOI_cells::sampleRVO(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj,
+ const float maxDeltaAngle)
+{
+ vset(activeObst->nvel, 0.f, 0.f);
+ float vmax = vlen(activeObst->dvel);
+
+ float* spos = new float[2*m_maxSamples];
+ int nspos = 0;
+
+ if (!m_adaptive)
+ {
+ const float cvx = activeObst->dvel[0]*m_bias;
+ const float cvy = activeObst->dvel[1]*m_bias;
+ float vmax = vlen(activeObst->dvel);
+ const float vrange = vmax*(1-m_bias);
+ const float cs = 1.0f / (float)m_sampleRadius*vrange;
+
+ for (int y = -m_sampleRadius; y <= m_sampleRadius; ++y)
+ {
+ for (int x = -m_sampleRadius; x <= m_sampleRadius; ++x)
+ {
+ if (nspos < m_maxSamples)
+ {
+ const float vx = cvx + (float)(x+0.5f)*cs;
+ const float vy = cvy + (float)(y+0.5f)*cs;
+ if (vx*vx+vy*vy > sqr(vmax+cs/2)) continue;
+ spos[nspos*2+0] = vx;
+ spos[nspos*2+1] = vy;
+ nspos++;
+ }
+ }
+ }
+ processSamples(activeObst, activeNavMeshObj, m_obstacles, m_levelHeight, vmax, spos, cs/2,
+ nspos, activeObst->nvel, m_maxToi, m_velWeight, m_curVelWeight, m_collisionWeight, m_toiWeight);
+ }
+ else
+ {
+ int rad;
+ float res[2];
+ float cs;
+ // First sample location.
+ rad = 4;
+ res[0] = activeObst->dvel[0]*m_bias;
+ res[1] = activeObst->dvel[1]*m_bias;
+ cs = vmax*(2-m_bias*2) / (float)(rad-1);
+
+ for (int k = 0; k < 5; ++k)
+ {
+ const float half = (rad-1)*cs*0.5f;
+
+ nspos = 0;
+ for (int y = 0; y < rad; ++y)
+ {
+ for (int x = 0; x < rad; ++x)
+ {
+ const float vx = res[0] + x*cs - half;
+ const float vy = res[1] + y*cs - half;
+ if (vx*vx+vy*vy > sqr(vmax+cs/2)) continue;
+ spos[nspos*2+0] = vx;
+ spos[nspos*2+1] = vy;
+ nspos++;
+ }
+ }
+
+ processSamples(activeObst, activeNavMeshObj, m_obstacles, m_levelHeight, vmax, spos, cs/2,
+ nspos, res, m_maxToi, m_velWeight, m_curVelWeight, m_collisionWeight, m_toiWeight);
+
+ cs *= 0.5f;
+ }
+ vcpy(activeObst->nvel, res);
+ }
+}
+
+KX_ObstacleSimulationTOI_cells::KX_ObstacleSimulationTOI_cells(MT_Scalar levelHeight, bool enableVisualization)
+: KX_ObstacleSimulationTOI(levelHeight, enableVisualization)
+, m_bias(0.4f)
+, m_adaptive(true)
+, m_sampleRadius(15)
+{
+ m_maxSamples = (m_sampleRadius*2+1)*(m_sampleRadius*2+1) + 100;
+ m_maxToi = 1.5f;
+ m_velWeight = 2.0f;
+ m_curVelWeight = 0.75f;
+ m_toiWeight = 2.5f;
+ m_collisionWeight = 0.75f; //side_weight
+} \ No newline at end of file
diff --git a/source/gameengine/Ketsji/KX_ObstacleSimulation.h b/source/gameengine/Ketsji/KX_ObstacleSimulation.h
new file mode 100644
index 00000000000..d926e8deb71
--- /dev/null
+++ b/source/gameengine/Ketsji/KX_ObstacleSimulation.h
@@ -0,0 +1,145 @@
+/**
+* Simulation for obstacle avoidance behavior
+* (based on Cane Project - http://code.google.com/p/cane by Mikko Mononen (c) 2009)
+*
+*
+* $Id$
+*
+* ***** BEGIN GPL LICENSE BLOCK *****
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version. The Blender
+* Foundation also sells licenses for use in proprietary software under
+* the Blender License. See http://www.blender.org/BL/ for information
+* about this.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software Foundation,
+* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+* All rights reserved.
+*
+* The Original Code is: all of this file.
+*
+* Contributor(s): none yet.
+*
+* ***** END GPL LICENSE BLOCK *****
+*/
+
+#ifndef __KX_OBSTACLESIMULATION
+#define __KX_OBSTACLESIMULATION
+
+#include <vector>
+#include "MT_Point2.h"
+#include "MT_Point3.h"
+
+class KX_GameObject;
+class KX_NavMeshObject;
+
+enum KX_OBSTACLE_TYPE
+{
+ KX_OBSTACLE_OBJ,
+ KX_OBSTACLE_NAV_MESH,
+};
+
+enum KX_OBSTACLE_SHAPE
+{
+ KX_OBSTACLE_CIRCLE,
+ KX_OBSTACLE_SEGMENT,
+};
+
+#define VEL_HIST_SIZE 6
+struct KX_Obstacle
+{
+ KX_OBSTACLE_TYPE m_type;
+ KX_OBSTACLE_SHAPE m_shape;
+ MT_Point3 m_pos;
+ MT_Point3 m_pos2;
+ MT_Scalar m_rad;
+
+ float vel[2];
+ float pvel[2];
+ float dvel[2];
+ float nvel[2];
+ float hvel[VEL_HIST_SIZE*2];
+ int hhead;
+
+
+ KX_GameObject* m_gameObj;
+};
+typedef std::vector<KX_Obstacle*> KX_Obstacles;
+
+class KX_ObstacleSimulation
+{
+protected:
+ KX_Obstacles m_obstacles;
+
+ MT_Scalar m_levelHeight;
+ bool m_enableVisualization;
+
+ KX_Obstacle* CreateObstacle(KX_GameObject* gameobj);
+public:
+ KX_ObstacleSimulation(MT_Scalar levelHeight, bool enableVisualization);
+ virtual ~KX_ObstacleSimulation();
+
+ void DrawObstacles();
+ //void DebugDraw();
+
+ void AddObstacleForObj(KX_GameObject* gameobj);
+ void DestroyObstacleForObj(KX_GameObject* gameobj);
+ void AddObstaclesForNavMesh(KX_NavMeshObject* navmesh);
+ KX_Obstacle* GetObstacle(KX_GameObject* gameobj);
+ void UpdateObstacles();
+ virtual void AdjustObstacleVelocity(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj,
+ MT_Vector3& velocity, MT_Scalar maxDeltaSpeed,MT_Scalar maxDeltaAngle);
+
+};
+class KX_ObstacleSimulationTOI: public KX_ObstacleSimulation
+{
+protected:
+ int m_maxSamples; // Number of sample steps
+ float m_minToi; // Min TOI
+ float m_maxToi; // Max TOI
+ float m_velWeight; // Sample selection angle weight
+ float m_curVelWeight; // Sample selection current velocity weight
+ float m_toiWeight; // Sample selection TOI weight
+ float m_collisionWeight; // Sample selection collision weight
+
+ virtual void sampleRVO(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj,
+ const float maxDeltaAngle) = 0;
+public:
+ KX_ObstacleSimulationTOI(MT_Scalar levelHeight, bool enableVisualization);
+ virtual void AdjustObstacleVelocity(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj,
+ MT_Vector3& velocity, MT_Scalar maxDeltaSpeed,MT_Scalar maxDeltaAngle);
+};
+
+class KX_ObstacleSimulationTOI_rays: public KX_ObstacleSimulationTOI
+{
+protected:
+ virtual void sampleRVO(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj,
+ const float maxDeltaAngle);
+public:
+ KX_ObstacleSimulationTOI_rays(MT_Scalar levelHeight, bool enableVisualization);
+};
+
+class KX_ObstacleSimulationTOI_cells: public KX_ObstacleSimulationTOI
+{
+protected:
+ float m_bias;
+ bool m_adaptive;
+ int m_sampleRadius;
+ virtual void sampleRVO(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj,
+ const float maxDeltaAngle);
+public:
+ KX_ObstacleSimulationTOI_cells(MT_Scalar levelHeight, bool enableVisualization);
+};
+
+#endif
diff --git a/source/gameengine/Ketsji/KX_PythonInit.cpp b/source/gameengine/Ketsji/KX_PythonInit.cpp
index 395e2048cb7..fb39e944559 100644
--- a/source/gameengine/Ketsji/KX_PythonInit.cpp
+++ b/source/gameengine/Ketsji/KX_PythonInit.cpp
@@ -87,6 +87,8 @@ extern "C" {
#include "KX_GameActuator.h"
#include "KX_ParentActuator.h"
#include "KX_SCA_DynamicActuator.h"
+#include "KX_SteeringActuator.h"
+#include "KX_NavMeshObject.h"
#include "SCA_IInputDevice.h"
#include "SCA_PropertySensor.h"
@@ -181,6 +183,13 @@ void KX_RasterizerDrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,cons
gp_Rasterizer->DrawDebugLine(from,to,color);
}
+void KX_RasterizerDrawDebugCircle(const MT_Vector3& center, const MT_Scalar radius, const MT_Vector3& color,
+ const MT_Vector3& normal, int nsector)
+{
+ if (gp_Rasterizer)
+ gp_Rasterizer->DrawDebugCircle(center, radius, color, normal, nsector);
+}
+
#ifdef WITH_PYTHON
static PyObject *gp_OrigPythonSysPath= NULL;
@@ -1655,6 +1664,16 @@ PyObject* initGameLogic(KX_KetsjiEngine *engine, KX_Scene* scene) // quick hack
KX_MACRO_addTypesToDict(d, ROT_MODE_ZXY, ROT_MODE_ZXY);
KX_MACRO_addTypesToDict(d, ROT_MODE_ZYX, ROT_MODE_ZYX);
+ /* Steering actuator */
+ KX_MACRO_addTypesToDict(d, KX_STEERING_SEEK, KX_SteeringActuator::KX_STEERING_SEEK);
+ KX_MACRO_addTypesToDict(d, KX_STEERING_FLEE, KX_SteeringActuator::KX_STEERING_FLEE);
+ KX_MACRO_addTypesToDict(d, KX_STEERING_PATHFOLLOWING, KX_SteeringActuator::KX_STEERING_PATHFOLLOWING);
+
+ /* KX_NavMeshObject render mode */
+ KX_MACRO_addTypesToDict(d, RM_WALLS, KX_NavMeshObject::RM_WALLS);
+ KX_MACRO_addTypesToDict(d, RM_POLYS, KX_NavMeshObject::RM_POLYS);
+ KX_MACRO_addTypesToDict(d, RM_TRIS, KX_NavMeshObject::RM_TRIS);
+
/* BL_Action play modes */
KX_MACRO_addTypesToDict(d, KX_ACTION_MODE_PLAY, BL_Action::ACT_MODE_PLAY);
KX_MACRO_addTypesToDict(d, KX_ACTION_MODE_LOOP, BL_Action::ACT_MODE_LOOP);
diff --git a/source/gameengine/Ketsji/KX_PythonInit.h b/source/gameengine/Ketsji/KX_PythonInit.h
index 1b172c35eff..d76e8f913df 100644
--- a/source/gameengine/Ketsji/KX_PythonInit.h
+++ b/source/gameengine/Ketsji/KX_PythonInit.h
@@ -72,6 +72,9 @@ class KX_KetsjiEngine* KX_GetActiveEngine();
#include "MT_Vector3.h"
void KX_RasterizerDrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,const MT_Vector3& color);
+void KX_RasterizerDrawDebugCircle(const MT_Vector3& center, const MT_Scalar radius, const MT_Vector3& color,
+ const MT_Vector3& normal, int nsector);
+
#endif //__KX_PYTHON_INIT
diff --git a/source/gameengine/Ketsji/KX_PythonInitTypes.cpp b/source/gameengine/Ketsji/KX_PythonInitTypes.cpp
index 1c4a17e31fc..49a08135c38 100644
--- a/source/gameengine/Ketsji/KX_PythonInitTypes.cpp
+++ b/source/gameengine/Ketsji/KX_PythonInitTypes.cpp
@@ -68,6 +68,7 @@
#include "KX_SCA_ReplaceMeshActuator.h"
#include "KX_SceneActuator.h"
#include "KX_StateActuator.h"
+#include "KX_SteeringActuator.h"
#include "KX_TrackToActuator.h"
#include "KX_VehicleWrapper.h"
#include "KX_VertexProxy.h"
@@ -99,6 +100,7 @@
#include "SCA_PythonController.h"
#include "SCA_RandomActuator.h"
#include "SCA_IController.h"
+#include "KX_NavMeshObject.h"
static void PyType_Attr_Set(PyGetSetDef *attr_getset, PyAttributeDef *attr)
{
@@ -217,9 +219,11 @@ void initPyTypes(void)
PyType_Ready_Attr(dict, KX_SCA_EndObjectActuator, init_getset);
PyType_Ready_Attr(dict, KX_SCA_ReplaceMeshActuator, init_getset);
PyType_Ready_Attr(dict, KX_Scene, init_getset);
+ PyType_Ready_Attr(dict, KX_NavMeshObject, init_getset);
PyType_Ready_Attr(dict, KX_SceneActuator, init_getset);
PyType_Ready_Attr(dict, KX_SoundActuator, init_getset);
PyType_Ready_Attr(dict, KX_StateActuator, init_getset);
+ PyType_Ready_Attr(dict, KX_SteeringActuator, init_getset);
PyType_Ready_Attr(dict, KX_TouchSensor, init_getset);
PyType_Ready_Attr(dict, KX_TrackToActuator, init_getset);
PyType_Ready_Attr(dict, KX_VehicleWrapper, init_getset);
diff --git a/source/gameengine/Ketsji/KX_Scene.cpp b/source/gameengine/Ketsji/KX_Scene.cpp
index 06e343cedb2..00638478349 100644
--- a/source/gameengine/Ketsji/KX_Scene.cpp
+++ b/source/gameengine/Ketsji/KX_Scene.cpp
@@ -87,6 +87,7 @@
#include "BL_ModifierDeformer.h"
#include "BL_ShapeDeformer.h"
#include "BL_DeformableGameObject.h"
+#include "KX_ObstacleSimulation.h"
#ifdef USE_BULLET
#include "KX_SoftBodyDeformer.h"
@@ -214,6 +215,19 @@ KX_Scene::KX_Scene(class SCA_IInputDevice* keyboarddevice,
m_bucketmanager=new RAS_BucketManager();
+ bool showObstacleSimulation = scene->gm.flag & GAME_SHOW_OBSTACLE_SIMULATION;
+ switch (scene->gm.obstacleSimulation)
+ {
+ case OBSTSIMULATION_TOI_rays:
+ m_obstacleSimulation = new KX_ObstacleSimulationTOI_rays((MT_Scalar)scene->gm.levelHeight, showObstacleSimulation);
+ break;
+ case OBSTSIMULATION_TOI_cells:
+ m_obstacleSimulation = new KX_ObstacleSimulationTOI_cells((MT_Scalar)scene->gm.levelHeight, showObstacleSimulation);
+ break;
+ default:
+ m_obstacleSimulation = NULL;
+ }
+
#ifdef WITH_PYTHON
m_attr_dict = PyDict_New(); /* new ref */
m_draw_call_pre = NULL;
@@ -236,6 +250,9 @@ KX_Scene::~KX_Scene()
this->RemoveObject(parentobj);
}
+ if (m_obstacleSimulation)
+ delete m_obstacleSimulation;
+
if(m_objectlist)
m_objectlist->Release();
@@ -1545,6 +1562,10 @@ void KX_Scene::LogicEndFrame()
obj->Release();
RemoveObject(obj);
}
+
+ //prepare obstacle simulation for new frame
+ if (m_obstacleSimulation)
+ m_obstacleSimulation->UpdateObstacles();
}
@@ -1977,6 +1998,8 @@ PyMethodDef KX_Scene::Methods[] = {
KX_PYMETHODTABLE(KX_Scene, replace),
KX_PYMETHODTABLE(KX_Scene, suspend),
KX_PYMETHODTABLE(KX_Scene, resume),
+ KX_PYMETHODTABLE(KX_Scene, drawObstacleSimulation),
+
/* dict style access */
KX_PYMETHODTABLE(KX_Scene, get),
@@ -2301,6 +2324,16 @@ KX_PYMETHODDEF_DOC(KX_Scene, resume,
Py_RETURN_NONE;
}
+KX_PYMETHODDEF_DOC(KX_Scene, drawObstacleSimulation,
+ "drawObstacleSimulation()\n"
+ "Draw debug visualization of obstacle simulation.\n")
+{
+ if (GetObstacleSimulation())
+ GetObstacleSimulation()->DrawObstacles();
+
+ Py_RETURN_NONE;
+}
+
/* Matches python dict.get(key, [default]) */
KX_PYMETHODDEF_DOC(KX_Scene, get, "")
{
diff --git a/source/gameengine/Ketsji/KX_Scene.h b/source/gameengine/Ketsji/KX_Scene.h
index 499861bce50..5954d5465ba 100644
--- a/source/gameengine/Ketsji/KX_Scene.h
+++ b/source/gameengine/Ketsji/KX_Scene.h
@@ -88,6 +88,7 @@ class SCA_JoystickManager;
class btCollisionShape;
class KX_BlenderSceneConverter;
struct KX_ClientObjectInfo;
+class KX_ObstacleSimulation;
#ifdef WITH_CXX_GUARDEDALLOC
#include "MEM_guardedalloc.h"
@@ -293,6 +294,9 @@ protected:
struct Scene* m_blenderScene;
RAS_2DFilterManager m_filtermanager;
+
+ KX_ObstacleSimulation* m_obstacleSimulation;
+
public:
KX_Scene(class SCA_IInputDevice* keyboarddevice,
class SCA_IInputDevice* mousedevice,
@@ -585,6 +589,8 @@ public:
void Update2DFilter(vector<STR_String>& propNames, void* gameObj, RAS_2DFilterManager::RAS_2DFILTER_MODE filtermode, int pass, STR_String& text);
void Render2DFilters(RAS_ICanvas* canvas);
+ KX_ObstacleSimulation* GetObstacleSimulation() {return m_obstacleSimulation;};
+
#ifdef WITH_PYTHON
/* --------------------------------------------------------------------- */
/* Python interface ---------------------------------------------------- */
@@ -597,6 +603,8 @@ public:
KX_PYMETHOD_DOC(KX_Scene, suspend);
KX_PYMETHOD_DOC(KX_Scene, resume);
KX_PYMETHOD_DOC(KX_Scene, get);
+ KX_PYMETHOD_DOC(KX_Scene, drawObstacleSimulation);
+
/* attributes */
static PyObject* pyattr_get_name(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
diff --git a/source/gameengine/Ketsji/KX_SteeringActuator.cpp b/source/gameengine/Ketsji/KX_SteeringActuator.cpp
new file mode 100644
index 00000000000..a0a2e148c1e
--- /dev/null
+++ b/source/gameengine/Ketsji/KX_SteeringActuator.cpp
@@ -0,0 +1,630 @@
+/**
+* Add steering behaviors
+*
+* $Id$
+*
+* ***** BEGIN GPL LICENSE BLOCK *****
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version. The Blender
+* Foundation also sells licenses for use in proprietary software under
+* the Blender License. See http://www.blender.org/BL/ for information
+* about this.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software Foundation,
+* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+* All rights reserved.
+*
+* The Original Code is: all of this file.
+*
+* Contributor(s): none yet.
+*
+* ***** END GPL LICENSE BLOCK *****
+*/
+
+#include "BLI_math.h"
+#include "KX_SteeringActuator.h"
+#include "KX_GameObject.h"
+#include "KX_NavMeshObject.h"
+#include "KX_ObstacleSimulation.h"
+#include "KX_PythonInit.h"
+#include "KX_PyMath.h"
+#include "Recast.h"
+
+/* ------------------------------------------------------------------------- */
+/* Native functions */
+/* ------------------------------------------------------------------------- */
+
+KX_SteeringActuator::KX_SteeringActuator(SCA_IObject *gameobj,
+ int mode,
+ KX_GameObject *target,
+ KX_GameObject *navmesh,
+ float distance,
+ float velocity,
+ float acceleration,
+ float turnspeed,
+ bool isSelfTerminated,
+ int pathUpdatePeriod,
+ KX_ObstacleSimulation* simulation,
+ short facingmode,
+ bool normalup,
+ bool enableVisualization) :
+ SCA_IActuator(gameobj, KX_ACT_STEERING),
+ m_mode(mode),
+ m_target(target),
+ m_distance(distance),
+ m_velocity(velocity),
+ m_acceleration(acceleration),
+ m_turnspeed(turnspeed),
+ m_isSelfTerminated(isSelfTerminated),
+ m_pathUpdatePeriod(pathUpdatePeriod),
+ m_updateTime(0),
+ m_isActive(false),
+ m_simulation(simulation),
+ m_enableVisualization(enableVisualization),
+ m_facingMode(facingmode),
+ m_normalUp(normalup),
+ m_obstacle(NULL),
+ m_pathLen(0),
+ m_wayPointIdx(-1),
+ m_steerVec(MT_Vector3(0, 0, 0))
+{
+ m_navmesh = static_cast<KX_NavMeshObject*>(navmesh);
+ if (m_navmesh)
+ m_navmesh->RegisterActuator(this);
+ if (m_target)
+ m_target->RegisterActuator(this);
+
+ if (m_simulation)
+ m_obstacle = m_simulation->GetObstacle((KX_GameObject*)gameobj);
+ KX_GameObject* parent = ((KX_GameObject*)gameobj)->GetParent();
+ if (m_facingMode>0 && parent)
+ {
+ m_parentlocalmat = parent->GetSGNode()->GetLocalOrientation();
+ }
+ else
+ m_parentlocalmat.setIdentity();
+}
+
+KX_SteeringActuator::~KX_SteeringActuator()
+{
+ if (m_navmesh)
+ m_navmesh->UnregisterActuator(this);
+ if (m_target)
+ m_target->UnregisterActuator(this);
+}
+
+CValue* KX_SteeringActuator::GetReplica()
+{
+ KX_SteeringActuator* replica = new KX_SteeringActuator(*this);
+ // replication just copy the m_base pointer => common random generator
+ replica->ProcessReplica();
+ return replica;
+}
+
+void KX_SteeringActuator::ProcessReplica()
+{
+ if (m_target)
+ m_target->RegisterActuator(this);
+ if (m_navmesh)
+ m_navmesh->RegisterActuator(this);
+ SCA_IActuator::ProcessReplica();
+}
+
+
+bool KX_SteeringActuator::UnlinkObject(SCA_IObject* clientobj)
+{
+ if (clientobj == m_target)
+ {
+ m_target = NULL;
+ return true;
+ }
+ else if (clientobj == m_navmesh)
+ {
+ m_navmesh = NULL;
+ return true;
+ }
+ return false;
+}
+
+void KX_SteeringActuator::Relink(CTR_Map<CTR_HashedPtr, void*> *obj_map)
+{
+ void **h_obj = (*obj_map)[m_target];
+ if (h_obj) {
+ if (m_target)
+ m_target->UnregisterActuator(this);
+ m_target = (KX_GameObject*)(*h_obj);
+ m_target->RegisterActuator(this);
+ }
+
+ h_obj = (*obj_map)[m_navmesh];
+ if (h_obj) {
+ if (m_navmesh)
+ m_navmesh->UnregisterActuator(this);
+ m_navmesh = (KX_NavMeshObject*)(*h_obj);
+ m_navmesh->RegisterActuator(this);
+ }
+}
+
+bool KX_SteeringActuator::Update(double curtime, bool frame)
+{
+ if (frame)
+ {
+ double delta = curtime - m_updateTime;
+ m_updateTime = curtime;
+
+ if (m_posevent && !m_isActive)
+ {
+ delta = 0;
+ m_pathUpdateTime = -1;
+ m_updateTime = curtime;
+ m_isActive = true;
+ }
+ bool bNegativeEvent = IsNegativeEvent();
+ if (bNegativeEvent)
+ m_isActive = false;
+
+ RemoveAllEvents();
+
+ if (!delta)
+ return true;
+
+ if (bNegativeEvent || !m_target)
+ return false; // do nothing on negative events
+
+ KX_GameObject *obj = (KX_GameObject*) GetParent();
+ const MT_Point3& mypos = obj->NodeGetWorldPosition();
+ const MT_Point3& targpos = m_target->NodeGetWorldPosition();
+ MT_Vector3 vectotarg = targpos - mypos;
+ MT_Vector3 vectotarg2d = vectotarg;
+ vectotarg2d.z() = 0;
+ m_steerVec = MT_Vector3(0, 0, 0);
+ bool apply_steerforce = false;
+ bool terminate = true;
+
+ switch (m_mode) {
+ case KX_STEERING_SEEK:
+ if (vectotarg2d.length2()>m_distance*m_distance)
+ {
+ terminate = false;
+ m_steerVec = vectotarg;
+ m_steerVec.normalize();
+ apply_steerforce = true;
+ }
+ break;
+ case KX_STEERING_FLEE:
+ if (vectotarg2d.length2()<m_distance*m_distance)
+ {
+ terminate = false;
+ m_steerVec = -vectotarg;
+ m_steerVec.normalize();
+ apply_steerforce = true;
+ }
+ break;
+ case KX_STEERING_PATHFOLLOWING:
+ if (m_navmesh && vectotarg.length2()>m_distance*m_distance)
+ {
+ terminate = false;
+
+ static const MT_Scalar WAYPOINT_RADIUS(0.25);
+
+ if (m_pathUpdateTime<0 || (m_pathUpdatePeriod>=0 &&
+ curtime - m_pathUpdateTime>((double)m_pathUpdatePeriod/1000)))
+ {
+ m_pathUpdateTime = curtime;
+ m_pathLen = m_navmesh->FindPath(mypos, targpos, m_path, MAX_PATH_LENGTH);
+ m_wayPointIdx = m_pathLen > 1 ? 1 : -1;
+ }
+
+ if (m_wayPointIdx>0)
+ {
+ MT_Vector3 waypoint(&m_path[3*m_wayPointIdx]);
+ if ((waypoint-mypos).length2()<WAYPOINT_RADIUS*WAYPOINT_RADIUS)
+ {
+ m_wayPointIdx++;
+ if (m_wayPointIdx>=m_pathLen)
+ {
+ m_wayPointIdx = -1;
+ terminate = true;
+ }
+ else
+ waypoint.setValue(&m_path[3*m_wayPointIdx]);
+ }
+
+ m_steerVec = waypoint - mypos;
+ apply_steerforce = true;
+
+
+ if (m_enableVisualization)
+ {
+ //debug draw
+ static const MT_Vector3 PATH_COLOR(1,0,0);
+ m_navmesh->DrawPath(m_path, m_pathLen, PATH_COLOR);
+ }
+ }
+
+ }
+ break;
+ }
+
+ if (apply_steerforce)
+ {
+ bool isdyna = obj->IsDynamic();
+ if (isdyna)
+ m_steerVec.z() = 0;
+ if (!m_steerVec.fuzzyZero())
+ m_steerVec.normalize();
+ MT_Vector3 newvel = m_velocity*m_steerVec;
+
+ //adjust velocity to avoid obstacles
+ if (m_simulation && m_obstacle /*&& !newvel.fuzzyZero()*/)
+ {
+ if (m_enableVisualization)
+ KX_RasterizerDrawDebugLine(mypos, mypos + newvel, MT_Vector3(1.,0.,0.));
+ m_simulation->AdjustObstacleVelocity(m_obstacle, m_mode!=KX_STEERING_PATHFOLLOWING ? m_navmesh : NULL,
+ newvel, m_acceleration*delta, m_turnspeed/180.0f*M_PI*delta);
+ if (m_enableVisualization)
+ KX_RasterizerDrawDebugLine(mypos, mypos + newvel, MT_Vector3(0.,1.,0.));
+ }
+
+ HandleActorFace(newvel);
+ if (isdyna)
+ {
+ //temporary solution: set 2D steering velocity directly to obj
+ //correct way is to apply physical force
+ MT_Vector3 curvel = obj->GetLinearVelocity();
+ newvel.z() = curvel.z();
+ obj->setLinearVelocity(newvel, false);
+ }
+ else
+ {
+ MT_Vector3 movement = delta*newvel;
+ obj->ApplyMovement(movement, false);
+ }
+ }
+ else
+ {
+ if (m_simulation && m_obstacle)
+ {
+ m_obstacle->dvel[0] = 0.f;
+ m_obstacle->dvel[1] = 0.f;
+ }
+
+ }
+
+ if (terminate && m_isSelfTerminated)
+ return false;
+ }
+
+ return true;
+}
+
+const MT_Vector3& KX_SteeringActuator::GetSteeringVec()
+{
+ static MT_Vector3 ZERO_VECTOR(0, 0, 0);
+ if (m_isActive)
+ return m_steerVec;
+ else
+ return ZERO_VECTOR;
+}
+
+inline float vdot2(const float* a, const float* b)
+{
+ return a[0]*b[0] + a[2]*b[2];
+}
+static bool barDistSqPointToTri(const float* p, const float* a, const float* b, const float* c)
+{
+ float v0[3], v1[3], v2[3];
+ vsub(v0, c,a);
+ vsub(v1, b,a);
+ vsub(v2, p,a);
+
+ const float dot00 = vdot2(v0, v0);
+ const float dot01 = vdot2(v0, v1);
+ const float dot02 = vdot2(v0, v2);
+ const float dot11 = vdot2(v1, v1);
+ const float dot12 = vdot2(v1, v2);
+
+ // Compute barycentric coordinates
+ float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01);
+ float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
+ float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
+
+ float ud = u<0.f ? -u : (u>1.f ? u-1.f : 0.f);
+ float vd = v<0.f ? -v : (v>1.f ? v-1.f : 0.f);
+ return ud*ud+vd*vd ;
+}
+
+inline void flipAxes(float* vec)
+{
+ std::swap(vec[1],vec[2]);
+}
+
+static bool getNavmeshNormal(dtStatNavMesh* navmesh, const MT_Vector3& pos, MT_Vector3& normal)
+{
+ static const float polyPickExt[3] = {2, 4, 2};
+ float spos[3];
+ pos.getValue(spos);
+ flipAxes(spos);
+ dtStatPolyRef sPolyRef = navmesh->findNearestPoly(spos, polyPickExt);
+ if (sPolyRef == 0)
+ return false;
+ const dtStatPoly* p = navmesh->getPoly(sPolyRef-1);
+ const dtStatPolyDetail* pd = navmesh->getPolyDetail(sPolyRef-1);
+
+ float distMin = FLT_MAX;
+ int idxMin = -1;
+ for (int i = 0; i < pd->ntris; ++i)
+ {
+ const unsigned char* t = navmesh->getDetailTri(pd->tbase+i);
+ const float* v[3];
+ for (int j = 0; j < 3; ++j)
+ {
+ if (t[j] < p->nv)
+ v[j] = navmesh->getVertex(p->v[t[j]]);
+ else
+ v[j] = navmesh->getDetailVertex(pd->vbase+(t[j]-p->nv));
+ }
+ float dist = barDistSqPointToTri(spos, v[0], v[1], v[2]);
+ if (dist<distMin)
+ {
+ distMin = dist;
+ idxMin = i;
+ }
+ }
+
+ if (idxMin>=0)
+ {
+ const unsigned char* t = navmesh->getDetailTri(pd->tbase+idxMin);
+ const float* v[3];
+ for (int j = 0; j < 3; ++j)
+ {
+ if (t[j] < p->nv)
+ v[j] = navmesh->getVertex(p->v[t[j]]);
+ else
+ v[j] = navmesh->getDetailVertex(pd->vbase+(t[j]-p->nv));
+ }
+ MT_Vector3 tri[3];
+ for (size_t j=0; j<3; j++)
+ tri[j].setValue(v[j][0],v[j][2],v[j][1]);
+ MT_Vector3 a,b;
+ a = tri[1]-tri[0];
+ b = tri[2]-tri[0];
+ normal = b.cross(a).safe_normalized();
+ return true;
+ }
+
+ return false;
+}
+
+void KX_SteeringActuator::HandleActorFace(MT_Vector3& velocity)
+{
+ if (m_facingMode==0 && (!m_navmesh || !m_normalUp))
+ return;
+ KX_GameObject* curobj = (KX_GameObject*) GetParent();
+ MT_Vector3 dir = m_facingMode==0 ? curobj->NodeGetLocalOrientation().getColumn(1) : velocity;
+ if (dir.fuzzyZero())
+ return;
+ dir.normalize();
+ MT_Vector3 up(0,0,1);
+ MT_Vector3 left;
+ MT_Matrix3x3 mat;
+
+ if (m_navmesh && m_normalUp)
+ {
+ dtStatNavMesh* navmesh = m_navmesh->GetNavMesh();
+ MT_Vector3 normal;
+ MT_Vector3 trpos = m_navmesh->TransformToLocalCoords(curobj->NodeGetWorldPosition());
+ if (getNavmeshNormal(navmesh, trpos, normal))
+ {
+
+ left = (dir.cross(up)).safe_normalized();
+ dir = (-left.cross(normal)).safe_normalized();
+ up = normal;
+ }
+ }
+
+ switch (m_facingMode)
+ {
+ case 1: // TRACK X
+ {
+ left = dir.safe_normalized();
+ dir = -(left.cross(up)).safe_normalized();
+ break;
+ };
+ case 2: // TRACK Y
+ {
+ left = (dir.cross(up)).safe_normalized();
+ break;
+ }
+
+ case 3: // track Z
+ {
+ left = up.safe_normalized();
+ up = dir.safe_normalized();
+ dir = left;
+ left = (dir.cross(up)).safe_normalized();
+ break;
+ }
+
+ case 4: // TRACK -X
+ {
+ left = -dir.safe_normalized();
+ dir = -(left.cross(up)).safe_normalized();
+ break;
+ };
+ case 5: // TRACK -Y
+ {
+ left = (-dir.cross(up)).safe_normalized();
+ dir = -dir;
+ break;
+ }
+ case 6: // track -Z
+ {
+ left = up.safe_normalized();
+ up = -dir.safe_normalized();
+ dir = left;
+ left = (dir.cross(up)).safe_normalized();
+ break;
+ }
+ }
+
+ mat.setValue (
+ left[0], dir[0],up[0],
+ left[1], dir[1],up[1],
+ left[2], dir[2],up[2]
+ );
+
+
+
+ KX_GameObject* parentObject = curobj->GetParent();
+ if(parentObject)
+ {
+ MT_Point3 localpos;
+ localpos = curobj->GetSGNode()->GetLocalPosition();
+ MT_Matrix3x3 parentmatinv;
+ parentmatinv = parentObject->NodeGetWorldOrientation ().inverse ();
+ mat = parentmatinv * mat;
+ mat = m_parentlocalmat * mat;
+ curobj->NodeSetLocalOrientation(mat);
+ curobj->NodeSetLocalPosition(localpos);
+ }
+ else
+ {
+ curobj->NodeSetLocalOrientation(mat);
+ }
+
+}
+
+#ifndef DISABLE_PYTHON
+
+/* ------------------------------------------------------------------------- */
+/* Python functions */
+/* ------------------------------------------------------------------------- */
+
+/* Integration hooks ------------------------------------------------------- */
+PyTypeObject KX_SteeringActuator::Type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "KX_SteeringActuator",
+ sizeof(PyObjectPlus_Proxy),
+ 0,
+ py_base_dealloc,
+ 0,
+ 0,
+ 0,
+ 0,
+ py_base_repr,
+ 0,0,0,0,0,0,0,0,0,
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+ 0,0,0,0,0,0,0,
+ Methods,
+ 0,
+ 0,
+ &SCA_IActuator::Type,
+ 0,0,0,0,0,0,
+ py_base_new
+};
+
+PyMethodDef KX_SteeringActuator::Methods[] = {
+ {NULL,NULL} //Sentinel
+};
+
+PyAttributeDef KX_SteeringActuator::Attributes[] = {
+ KX_PYATTRIBUTE_INT_RW("behaviour", KX_STEERING_NODEF+1, KX_STEERING_MAX-1, true, KX_SteeringActuator, m_mode),
+ KX_PYATTRIBUTE_RW_FUNCTION("target", KX_SteeringActuator, pyattr_get_target, pyattr_set_target),
+ KX_PYATTRIBUTE_RW_FUNCTION("navmesh", KX_SteeringActuator, pyattr_get_navmesh, pyattr_set_navmesh),
+ KX_PYATTRIBUTE_FLOAT_RW("distance", 0.0f, 1000.0f, KX_SteeringActuator, m_distance),
+ KX_PYATTRIBUTE_FLOAT_RW("velocity", 0.0f, 1000.0f, KX_SteeringActuator, m_velocity),
+ KX_PYATTRIBUTE_FLOAT_RW("acceleration", 0.0f, 1000.0f, KX_SteeringActuator, m_acceleration),
+ KX_PYATTRIBUTE_FLOAT_RW("turnspeed", 0.0f, 720.0f, KX_SteeringActuator, m_turnspeed),
+ KX_PYATTRIBUTE_BOOL_RW("selfterminated", KX_SteeringActuator, m_isSelfTerminated),
+ KX_PYATTRIBUTE_BOOL_RW("enableVisualization", KX_SteeringActuator, m_enableVisualization),
+ KX_PYATTRIBUTE_RO_FUNCTION("steeringVec", KX_SteeringActuator, pyattr_get_steeringVec),
+ KX_PYATTRIBUTE_SHORT_RW("facingMode", 0, 6, true, KX_SteeringActuator, m_facingMode),
+ KX_PYATTRIBUTE_INT_RW("pathUpdatePeriod", -1, 100000, true, KX_SteeringActuator, m_pathUpdatePeriod),
+ { NULL } //Sentinel
+};
+
+PyObject* KX_SteeringActuator::pyattr_get_target(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
+{
+ KX_SteeringActuator* actuator = static_cast<KX_SteeringActuator*>(self);
+ if (!actuator->m_target)
+ Py_RETURN_NONE;
+ else
+ return actuator->m_target->GetProxy();
+}
+
+int KX_SteeringActuator::pyattr_set_target(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+ KX_SteeringActuator* actuator = static_cast<KX_SteeringActuator*>(self);
+ KX_GameObject *gameobj;
+
+ if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.object = value: KX_SteeringActuator"))
+ return PY_SET_ATTR_FAIL; // ConvertPythonToGameObject sets the error
+
+ if (actuator->m_target != NULL)
+ actuator->m_target->UnregisterActuator(actuator);
+
+ actuator->m_target = (KX_GameObject*) gameobj;
+
+ if (actuator->m_target)
+ actuator->m_target->RegisterActuator(actuator);
+
+ return PY_SET_ATTR_SUCCESS;
+}
+
+PyObject* KX_SteeringActuator::pyattr_get_navmesh(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
+{
+ KX_SteeringActuator* actuator = static_cast<KX_SteeringActuator*>(self);
+ if (!actuator->m_navmesh)
+ Py_RETURN_NONE;
+ else
+ return actuator->m_navmesh->GetProxy();
+}
+
+int KX_SteeringActuator::pyattr_set_navmesh(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+ KX_SteeringActuator* actuator = static_cast<KX_SteeringActuator*>(self);
+ KX_GameObject *gameobj;
+
+ if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.object = value: KX_SteeringActuator"))
+ return PY_SET_ATTR_FAIL; // ConvertPythonToGameObject sets the error
+
+ if (!PyObject_TypeCheck(value, &KX_NavMeshObject::Type))
+ {
+ PyErr_Format(PyExc_TypeError, "KX_NavMeshObject is expected");
+ return PY_SET_ATTR_FAIL;
+ }
+
+ if (actuator->m_navmesh != NULL)
+ actuator->m_navmesh->UnregisterActuator(actuator);
+
+ actuator->m_navmesh = static_cast<KX_NavMeshObject*>(gameobj);
+
+ if (actuator->m_navmesh)
+ actuator->m_navmesh->RegisterActuator(actuator);
+
+ return PY_SET_ATTR_SUCCESS;
+}
+
+PyObject* KX_SteeringActuator::pyattr_get_steeringVec(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
+{
+ KX_SteeringActuator* actuator = static_cast<KX_SteeringActuator*>(self);
+ const MT_Vector3& steeringVec = actuator->GetSteeringVec();
+ return PyObjectFrom(steeringVec);
+}
+
+#endif // DISABLE_PYTHON
+
+/* eof */
+
diff --git a/source/gameengine/Ketsji/KX_SteeringActuator.h b/source/gameengine/Ketsji/KX_SteeringActuator.h
new file mode 100644
index 00000000000..4f8303107f7
--- /dev/null
+++ b/source/gameengine/Ketsji/KX_SteeringActuator.h
@@ -0,0 +1,130 @@
+/**
+* Add steering behaviors
+*
+*
+* $Id$
+*
+* ***** BEGIN GPL LICENSE BLOCK *****
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version. The Blender
+* Foundation also sells licenses for use in proprietary software under
+* the Blender License. See http://www.blender.org/BL/ for information
+* about this.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software Foundation,
+* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+* All rights reserved.
+*
+* The Original Code is: all of this file.
+*
+* Contributor(s): none yet.
+*
+* ***** END GPL LICENSE BLOCK *****
+*/
+
+#ifndef __KX_STEERINGACTUATOR
+#define __KX_STEERINGACTUATOR
+
+#include "SCA_IActuator.h"
+#include "SCA_LogicManager.h"
+#include "MT_Matrix3x3.h"
+
+class KX_GameObject;
+class KX_NavMeshObject;
+struct KX_Obstacle;
+class KX_ObstacleSimulation;
+const int MAX_PATH_LENGTH = 128;
+
+class KX_SteeringActuator : public SCA_IActuator
+{
+ Py_Header;
+
+ /** Target object */
+ KX_GameObject *m_target;
+ KX_NavMeshObject *m_navmesh;
+ int m_mode;
+ float m_distance;
+ float m_velocity;
+ float m_acceleration;
+ float m_turnspeed;
+ KX_ObstacleSimulation* m_simulation;
+
+ KX_Obstacle* m_obstacle;
+ double m_updateTime;
+ bool m_isActive;
+ bool m_isSelfTerminated;
+ bool m_enableVisualization;
+ short m_facingMode;
+ bool m_normalUp;
+ float m_path[MAX_PATH_LENGTH*3];
+ int m_pathLen;
+ int m_pathUpdatePeriod;
+ double m_pathUpdateTime;
+ int m_wayPointIdx;
+ MT_Matrix3x3 m_parentlocalmat;
+ MT_Vector3 m_steerVec;
+ void HandleActorFace(MT_Vector3& velocity);
+public:
+ enum KX_STEERINGACT_MODE
+ {
+ KX_STEERING_NODEF = 0,
+ KX_STEERING_SEEK,
+ KX_STEERING_FLEE,
+ KX_STEERING_PATHFOLLOWING,
+ KX_STEERING_MAX
+ };
+
+ KX_SteeringActuator(class SCA_IObject* gameobj,
+ int mode,
+ KX_GameObject *target,
+ KX_GameObject *navmesh,
+ float distance,
+ float velocity,
+ float acceleration,
+ float turnspeed,
+ bool isSelfTerminated,
+ int pathUpdatePeriod,
+ KX_ObstacleSimulation* simulation,
+ short facingmode,
+ bool normalup,
+ bool enableVisualization);
+ virtual ~KX_SteeringActuator();
+ virtual bool Update(double curtime, bool frame);
+
+ virtual CValue* GetReplica();
+ virtual void ProcessReplica();
+ virtual void Relink(CTR_Map<CTR_HashedPtr, void*> *obj_map);
+ virtual bool UnlinkObject(SCA_IObject* clientobj);
+ const MT_Vector3& GetSteeringVec();
+
+#ifndef DISABLE_PYTHON
+
+ /* --------------------------------------------------------------------- */
+ /* Python interface ---------------------------------------------------- */
+ /* --------------------------------------------------------------------- */
+
+ /* These are used to get and set m_target */
+ static PyObject* pyattr_get_target(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef);
+ static int pyattr_set_target(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
+ static PyObject* pyattr_get_navmesh(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef);
+ static int pyattr_set_navmesh(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
+ static PyObject* pyattr_get_steeringVec(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef);
+
+
+#endif // DISABLE_PYTHON
+
+}; /* end of class KX_SteeringActuator : public SCA_PropertyActuator */
+
+#endif
+
diff --git a/source/gameengine/Ketsji/SConscript b/source/gameengine/Ketsji/SConscript
index c5509dd7de8..7c9785c24e7 100644
--- a/source/gameengine/Ketsji/SConscript
+++ b/source/gameengine/Ketsji/SConscript
@@ -20,6 +20,8 @@ incs += ' #source/gameengine/GameLogic #source/gameengine/Expressions #source/ga
incs += ' #source/gameengine/SceneGraph #source/gameengine/Physics/common'
incs += ' #source/gameengine/Physics/Dummy'
incs += ' #source/blender/misc #source/blender/blenloader #extern/glew/include #source/blender/gpu'
+incs += ' #extern/recastnavigation/Recast/Include #extern/recastnavigation/Detour/Include'
+incs += ' #source/blender/editors/include'
incs += ' ' + env['BF_BULLET_INC']
incs += ' ' + env['BF_OPENGL_INC']
diff --git a/source/gameengine/Rasterizer/RAS_IRasterizer.h b/source/gameengine/Rasterizer/RAS_IRasterizer.h
index c46ebf742a0..877a7219a1c 100644
--- a/source/gameengine/Rasterizer/RAS_IRasterizer.h
+++ b/source/gameengine/Rasterizer/RAS_IRasterizer.h
@@ -393,7 +393,9 @@ public:
virtual void SetPolygonOffset(float mult, float add) = 0;
virtual void DrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,const MT_Vector3& color)=0;
- virtual void FlushDebugLines()=0;
+ virtual void DrawDebugCircle(const MT_Vector3& center, const MT_Scalar radius, const MT_Vector3& color,
+ const MT_Vector3& normal, int nsector)=0;
+ virtual void FlushDebugShapes()=0;
diff --git a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp
index 50d034a5a5a..6ad9e591474 100644
--- a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp
+++ b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp
@@ -55,6 +55,10 @@
#include "BKE_DerivedMesh.h"
+#ifndef M_PI
+#define M_PI 3.14159265358979323846
+#endif
+
/**
* 32x32 bit masks for vinterlace stereo mode
*/
@@ -354,9 +358,9 @@ void RAS_OpenGLRasterizer::ClearCachingInfo(void)
m_materialCachingInfo = 0;
}
-void RAS_OpenGLRasterizer::FlushDebugLines()
+void RAS_OpenGLRasterizer::FlushDebugShapes()
{
- if(!m_debugLines.size())
+ if(!m_debugShapes.size())
return;
// DrawDebugLines
@@ -368,29 +372,67 @@ void RAS_OpenGLRasterizer::FlushDebugLines()
if(light) glDisable(GL_LIGHTING);
if(tex) glDisable(GL_TEXTURE_2D);
+ //draw lines
glBegin(GL_LINES);
- for (unsigned int i=0;i<m_debugLines.size();i++)
+ for (unsigned int i=0;i<m_debugShapes.size();i++)
{
- glColor4f(m_debugLines[i].m_color[0],m_debugLines[i].m_color[1],m_debugLines[i].m_color[2],1.f);
- const MT_Scalar* fromPtr = &m_debugLines[i].m_from.x();
- const MT_Scalar* toPtr= &m_debugLines[i].m_to.x();
-
+ if (m_debugShapes[i].m_type != OglDebugShape::LINE)
+ continue;
+ glColor4f(m_debugShapes[i].m_color[0],m_debugShapes[i].m_color[1],m_debugShapes[i].m_color[2],1.f);
+ const MT_Scalar* fromPtr = &m_debugShapes[i].m_pos.x();
+ const MT_Scalar* toPtr= &m_debugShapes[i].m_param.x();
glVertex3dv(fromPtr);
glVertex3dv(toPtr);
}
glEnd();
+ //draw circles
+ for (unsigned int i=0;i<m_debugShapes.size();i++)
+ {
+ if (m_debugShapes[i].m_type != OglDebugShape::CIRCLE)
+ continue;
+ glBegin(GL_LINE_LOOP);
+ glColor4f(m_debugShapes[i].m_color[0],m_debugShapes[i].m_color[1],m_debugShapes[i].m_color[2],1.f);
+
+ static const MT_Vector3 worldUp(0.,0.,1.);
+ MT_Vector3 norm = m_debugShapes[i].m_param;
+ MT_Matrix3x3 tr;
+ if (norm.fuzzyZero() || norm == worldUp)
+ {
+ tr.setIdentity();
+ }
+ else
+ {
+ MT_Vector3 xaxis, yaxis;
+ xaxis = MT_cross(norm, worldUp);
+ yaxis = MT_cross(xaxis, norm);
+ tr.setValue(xaxis.x(), xaxis.y(), xaxis.z(),
+ yaxis.x(), yaxis.y(), yaxis.z(),
+ norm.x(), norm.y(), norm.z());
+ }
+ MT_Scalar rad = m_debugShapes[i].m_param2.x();
+ int n = (int) m_debugShapes[i].m_param2.y();
+ for (int j = 0; j<n; j++)
+ {
+ MT_Scalar theta = j*M_PI*2/n;
+ MT_Vector3 pos(cos(theta)*rad, sin(theta)*rad, 0.);
+ pos = pos*tr;
+ pos += m_debugShapes[i].m_pos;
+ const MT_Scalar* posPtr = &pos.x();
+ glVertex3dv(posPtr);
+ }
+ glEnd();
+ }
+
if(light) glEnable(GL_LIGHTING);
if(tex) glEnable(GL_TEXTURE_2D);
- m_debugLines.clear();
+ m_debugShapes.clear();
}
void RAS_OpenGLRasterizer::EndFrame()
{
-
-
- FlushDebugLines();
+ FlushDebugShapes();
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
diff --git a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.h b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.h
index 61568df91eb..c0e02f6df77 100644
--- a/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.h
+++ b/source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.h
@@ -49,10 +49,15 @@ using namespace std;
#define RAS_MAX_TEXCO 8 // match in BL_Material
#define RAS_MAX_ATTRIB 16 // match in BL_BlenderShader
-struct OglDebugLine
+struct OglDebugShape
{
- MT_Vector3 m_from;
- MT_Vector3 m_to;
+ enum SHAPE_TYPE{
+ LINE, CIRCLE
+ };
+ SHAPE_TYPE m_type;
+ MT_Vector3 m_pos;
+ MT_Vector3 m_param;
+ MT_Vector3 m_param2;
MT_Vector3 m_color;
};
@@ -256,18 +261,32 @@ public:
virtual void SetPolygonOffset(float mult, float add);
- virtual void FlushDebugLines();
+ virtual void FlushDebugShapes();
- virtual void DrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,const MT_Vector3& color)
+ virtual void DrawDebugLine(const MT_Vector3& from,const MT_Vector3& to,const MT_Vector3& color)
{
- OglDebugLine line;
- line.m_from = from;
- line.m_to = to;
+ OglDebugShape line;
+ line.m_type = OglDebugShape::LINE;
+ line.m_pos= from;
+ line.m_param = to;
line.m_color = color;
- m_debugLines.push_back(line);
+ m_debugShapes.push_back(line);
+ }
+
+ virtual void DrawDebugCircle(const MT_Vector3& center, const MT_Scalar radius, const MT_Vector3& color,
+ const MT_Vector3& normal, int nsector)
+ {
+ OglDebugShape line;
+ line.m_type = OglDebugShape::CIRCLE;
+ line.m_pos= center;
+ line.m_param = normal;
+ line.m_color = color;
+ line.m_param2.x() = radius;
+ line.m_param2.y() = (float) nsector;
+ m_debugShapes.push_back(line);
}
- std::vector <OglDebugLine> m_debugLines;
+ std::vector <OglDebugShape> m_debugShapes;
virtual void SetTexCoordNum(int num);
virtual void SetAttribNum(int num);