Refactored the GUI.

Separeted demo code into builders. The examples should be easier to follow now.
Added scroll wheel support to gui scrolling windows.
This commit is contained in:
Mikko Mononen 2009-06-30 15:17:25 +00:00
parent b31b9334c3
commit dd0fcedaa4
17 changed files with 8989 additions and 2884 deletions

View File

@ -37,8 +37,8 @@ inline void intToCol(int i, float* col)
void rcDebugDrawHeightfieldSolid(const struct rcHeightfield& hf);
void rcDebugDrawHeightfieldWalkable(const struct rcHeightfield& hf);
void rcDebugDrawMesh(const class rcMeshLoaderObj& mesh, const unsigned char* flags);
void rcDebugDrawMeshSlope(const class rcMeshLoaderObj& mesh, const float walkableSlopeAngle);
void rcDebugDrawMesh(const float* verts, int nverts, const int* tris, const float* normals, int ntris, const unsigned char* flags);
void rcDebugDrawMeshSlope(const float* verts, int nverts, const int* tris, const float* normals, int ntris, const float walkableSlopeAngle);
void rcDebugDrawCompactHeightfieldSolid(const struct rcCompactHeightfield& chf);
void rcDebugDrawCompactHeightfieldRegions(const struct rcCompactHeightfield& chf);

View File

@ -24,15 +24,12 @@
#include "MeshLoaderObj.h"
#include "Recast.h"
void rcDebugDrawMesh(const rcMeshLoaderObj& mesh, const unsigned char* flags)
{
int nt = mesh.getTriCount();
const float* verts = mesh.getVerts();
const float* normals = mesh.getNormals();
const int* tris = mesh.getTris();
void rcDebugDrawMesh(const float* verts, int nverts,
const int* tris, const float* normals, int ntris,
const unsigned char* flags)
{
glBegin(GL_TRIANGLES);
for (int i = 0; i < nt*3; i += 3)
for (int i = 0; i < ntris*3; i += 3)
{
float a = (2+normals[i+0]+normals[i+1])/4;
if (flags && !flags[i/3])
@ -46,17 +43,14 @@ void rcDebugDrawMesh(const rcMeshLoaderObj& mesh, const unsigned char* flags)
glEnd();
}
void rcDebugDrawMeshSlope(const rcMeshLoaderObj& mesh, const float walkableSlopeAngle)
void rcDebugDrawMeshSlope(const float* verts, int nverts,
const int* tris, const float* normals, int ntris,
const float walkableSlopeAngle)
{
const float walkableThr = cosf(walkableSlopeAngle/180.0f*(float)M_PI);
const int nt = mesh.getTriCount();
const float* verts = mesh.getVerts();
const float* normals = mesh.getNormals();
const int* tris = mesh.getTris();
glBegin(GL_TRIANGLES);
for (int i = 0; i < nt*3; i += 3)
for (int i = 0; i < ntris*3; i += 3)
{
const float* norm = &normals[i];
float a = (2+norm[0]+norm[1])/4;

File diff suppressed because it is too large Load Diff

View File

@ -268,8 +268,6 @@
<array>
<string>29B97314FDCFA39411CA2CEA</string>
<string>080E96DDFE201D6D7F000001</string>
<string>6BDD9E030F91110C00904EEF</string>
<string>6B137C7D0F7FCBE800459200</string>
<string>29B97315FDCFA39411CA2CEA</string>
<string>29B97317FDCFA39411CA2CEA</string>
<string>1C37FBAC04509CD000000102</string>
@ -278,8 +276,7 @@
<key>PBXSmartGroupTreeModuleOutlineStateSelectionKey</key>
<array>
<array>
<integer>11</integer>
<integer>9</integer>
<integer>19</integer>
<integer>1</integer>
<integer>0</integer>
</array>
@ -320,7 +317,7 @@
<key>PBXProjectModuleGUID</key>
<string>6B8632A30F78115100E2684A</string>
<key>PBXProjectModuleLabel</key>
<string>RecastContour.cpp</string>
<string>ChunkyTriMesh.cpp</string>
<key>PBXSplitModuleInNavigatorKey</key>
<dict>
<key>Split0</key>
@ -328,46 +325,48 @@
<key>PBXProjectModuleGUID</key>
<string>6B8632A40F78115100E2684A</string>
<key>PBXProjectModuleLabel</key>
<string>RecastContour.cpp</string>
<string>ChunkyTriMesh.cpp</string>
<key>_historyCapacity</key>
<integer>0</integer>
<key>bookmark</key>
<string>6B6BC6F60FE7A3A4009E3CB9</string>
<string>6B25B6260FFA63C8004F1BC4</string>
<key>history</key>
<array>
<string>6B8633370F7813A600E2684A</string>
<string>6BB87E0B0F9DE8A300E33F12</string>
<string>6B7707AF0FBD66CF00D21BAE</string>
<string>6B7707EF0FBD90F100D21BAE</string>
<string>6B7707F00FBD90F100D21BAE</string>
<string>6BB7876B0FC03EAD003C24DB</string>
<string>6BB787710FC03EAD003C24DB</string>
<string>6BB7881E0FC0593E003C24DB</string>
<string>6BB7881F0FC0593E003C24DB</string>
<string>6B8171D70FC327630022159F</string>
<string>6BB85D3A0FCEAA6300758966</string>
<string>6B09CDFF0FD5563E005637D5</string>
<string>6B09CE5C0FD5BDE6005637D5</string>
<string>6B09CE5F0FD5BDE6005637D5</string>
<string>6B09CE730FD5BEC3005637D5</string>
<string>6B8FD19C0FD7ED310059D643</string>
<string>6B8FD1CF0FD7F56C0059D643</string>
<string>6BAE920D0FD8014C002875DB</string>
<string>6B8B580A0FD80DA20088BD0D</string>
<string>6B8A71F40FDAB52200A0A6FB</string>
<string>6B995BDF0FE0D9B300D5C493</string>
<string>6B6BC6C70FE7A3A4009E3CB9</string>
<string>6B6BC6C80FE7A3A4009E3CB9</string>
<string>6B6BC6C90FE7A3A4009E3CB9</string>
<string>6B6BC6CA0FE7A3A4009E3CB9</string>
<string>6B6BC6CB0FE7A3A4009E3CB9</string>
<string>6B6BC6CC0FE7A3A4009E3CB9</string>
<string>6B6BC6CD0FE7A3A4009E3CB9</string>
<string>6B6BC6CE0FE7A3A4009E3CB9</string>
<string>6BC745A70FF527E50083A694</string>
<string>6BC745A80FF527E50083A694</string>
<string>6B9B7D990FF91AC600A9090F</string>
<string>6B9B7D9D0FF91AC600A9090F</string>
<string>6B25B3A60FFA124D004F1BC4</string>
<string>6B25B3A80FFA124D004F1BC4</string>
<string>6B25B3AB0FFA124D004F1BC4</string>
<string>6B25B43E0FFA1786004F1BC4</string>
<string>6B25B44B0FFA1968004F1BC4</string>
<string>6B25B4710FFA1FE0004F1BC4</string>
<string>6B25B4740FFA1FE0004F1BC4</string>
<string>6B25B54F0FFA5899004F1BC4</string>
<string>6B25B5AF0FFA5A5A004F1BC4</string>
<string>6B25B5CB0FFA5CCE004F1BC4</string>
<string>6B25B6230FFA63C8004F1BC4</string>
<string>6B25B6240FFA63C8004F1BC4</string>
</array>
<key>prevStack</key>
<array>
<string>6B86333B0F7813A600E2684A</string>
<string>6B1E02680F924A8500CC0038</string>
<string>6B1E02750F924A8500CC0038</string>
<string>6B1E028F0F924D5A00CC0038</string>
@ -389,51 +388,53 @@
<string>6BB7882A0FC0593E003C24DB</string>
<string>6BB7882B0FC0593E003C24DB</string>
<string>6BB85D3E0FCEAA6300758966</string>
<string>6B09CE1E0FD55805005637D5</string>
<string>6B09CE2D0FD55B4D005637D5</string>
<string>6BC620920FD7C2380022CACF</string>
<string>6B8FD1A20FD7ED310059D643</string>
<string>6B8FD1A30FD7ED310059D643</string>
<string>6B8FD1A40FD7ED310059D643</string>
<string>6B6BC6CF0FE7A3A4009E3CB9</string>
<string>6B6BC6D00FE7A3A4009E3CB9</string>
<string>6B6BC6D10FE7A3A4009E3CB9</string>
<string>6B6BC6D20FE7A3A4009E3CB9</string>
<string>6B6BC6D30FE7A3A4009E3CB9</string>
<string>6B6BC6D40FE7A3A4009E3CB9</string>
<string>6B6BC6D50FE7A3A4009E3CB9</string>
<string>6B6BC6D60FE7A3A4009E3CB9</string>
<string>6B6BC6D70FE7A3A4009E3CB9</string>
<string>6B6BC6D80FE7A3A4009E3CB9</string>
<string>6B6BC6D90FE7A3A4009E3CB9</string>
<string>6B6BC6DA0FE7A3A4009E3CB9</string>
<string>6B6BC6DB0FE7A3A4009E3CB9</string>
<string>6B6BC6DC0FE7A3A4009E3CB9</string>
<string>6B6BC6DD0FE7A3A4009E3CB9</string>
<string>6B6BC6DE0FE7A3A4009E3CB9</string>
<string>6B6BC6DF0FE7A3A4009E3CB9</string>
<string>6B6BC6E00FE7A3A4009E3CB9</string>
<string>6B6BC6E10FE7A3A4009E3CB9</string>
<string>6B6BC6E20FE7A3A4009E3CB9</string>
<string>6B6BC6E30FE7A3A4009E3CB9</string>
<string>6B6BC6E40FE7A3A4009E3CB9</string>
<string>6B6BC6E50FE7A3A4009E3CB9</string>
<string>6B6BC6E60FE7A3A4009E3CB9</string>
<string>6B6BC6E70FE7A3A4009E3CB9</string>
<string>6B6BC6E80FE7A3A4009E3CB9</string>
<string>6B6BC6E90FE7A3A4009E3CB9</string>
<string>6B6BC6EA0FE7A3A4009E3CB9</string>
<string>6B6BC6EB0FE7A3A4009E3CB9</string>
<string>6B6BC6EC0FE7A3A4009E3CB9</string>
<string>6B6BC6ED0FE7A3A4009E3CB9</string>
<string>6B6BC6EE0FE7A3A4009E3CB9</string>
<string>6B6BC6EF0FE7A3A4009E3CB9</string>
<string>6B6BC6F00FE7A3A4009E3CB9</string>
<string>6B6BC6F10FE7A3A4009E3CB9</string>
<string>6B6BC6F20FE7A3A4009E3CB9</string>
<string>6B6BC6F30FE7A3A4009E3CB9</string>
<string>6B6BC6F40FE7A3A4009E3CB9</string>
<string>6B6BC6F50FE7A3A4009E3CB9</string>
<string>6BC745AD0FF527E50083A694</string>
<string>6BC745AE0FF527E50083A694</string>
<string>6BC745AF0FF527E50083A694</string>
<string>6B9B7DA30FF91AC600A9090F</string>
<string>6B9B7DA40FF91AC600A9090F</string>
<string>6B25B3B70FFA124D004F1BC4</string>
<string>6B25B3B80FFA124D004F1BC4</string>
<string>6B25B3B90FFA124D004F1BC4</string>
<string>6B25B3BA0FFA124D004F1BC4</string>
<string>6B25B3BB0FFA124D004F1BC4</string>
<string>6B25B3BC0FFA124D004F1BC4</string>
<string>6B25B3BD0FFA124D004F1BC4</string>
<string>6B25B3BE0FFA124D004F1BC4</string>
<string>6B25B3BF0FFA124D004F1BC4</string>
<string>6B25B3C00FFA124D004F1BC4</string>
<string>6B25B3C20FFA124D004F1BC4</string>
<string>6B25B3C60FFA124D004F1BC4</string>
<string>6B25B3C70FFA124D004F1BC4</string>
<string>6B25B3F40FFA124D004F1BC4</string>
<string>6B25B4120FFA1545004F1BC4</string>
<string>6B25B4130FFA1545004F1BC4</string>
<string>6B86333B0F7813A600E2684A</string>
<string>6B25B4080FFA13E9004F1BC4</string>
<string>6B25B4280FFA16C0004F1BC4</string>
<string>6B25B4360FFA174A004F1BC4</string>
<string>6B25B4370FFA174A004F1BC4</string>
<string>6B25B4400FFA1786004F1BC4</string>
<string>6B25B4420FFA1786004F1BC4</string>
<string>6B25B4500FFA1968004F1BC4</string>
<string>6B25B4510FFA1968004F1BC4</string>
<string>6B25B4530FFA1968004F1BC4</string>
<string>6B25B4550FFA1968004F1BC4</string>
<string>6B25B4560FFA1968004F1BC4</string>
<string>6B25B4580FFA1968004F1BC4</string>
<string>6B25B4590FFA1968004F1BC4</string>
<string>6B25B45A0FFA1968004F1BC4</string>
<string>6B25B4780FFA1FE0004F1BC4</string>
<string>6B25B4850FFA1FE0004F1BC4</string>
<string>6B25B55A0FFA5899004F1BC4</string>
<string>6B25B56D0FFA5899004F1BC4</string>
<string>6B25B5770FFA5899004F1BC4</string>
<string>6B25B5890FFA58F3004F1BC4</string>
<string>6B25B5A00FFA5A03004F1BC4</string>
<string>6B25B5CF0FFA5CCE004F1BC4</string>
<string>6B25B5D80FFA5CCE004F1BC4</string>
<string>6B25B6250FFA63C8004F1BC4</string>
</array>
</dict>
<key>SplitCount</key>
@ -447,18 +448,18 @@
<key>GeometryConfiguration</key>
<dict>
<key>Frame</key>
<string>{{0, 0}, {1030, 673}}</string>
<string>{{0, 0}, {1030, 512}}</string>
<key>RubberWindowFrame</key>
<string>0 59 1280 719 0 0 1280 778 </string>
</dict>
<key>Module</key>
<string>PBXNavigatorGroup</string>
<key>Proportion</key>
<string>673pt</string>
<string>512pt</string>
</dict>
<dict>
<key>Proportion</key>
<string>0pt</string>
<string>161pt</string>
<key>Tabs</key>
<array>
<dict>
@ -472,7 +473,7 @@
<key>GeometryConfiguration</key>
<dict>
<key>Frame</key>
<string>{{10, 27}, {1030, 61}}</string>
<string>{{10, 27}, {1030, 134}}</string>
</dict>
<key>Module</key>
<string>XCDetailModule</string>
@ -489,8 +490,6 @@
<dict>
<key>Frame</key>
<string>{{10, 27}, {1030, -27}}</string>
<key>RubberWindowFrame</key>
<string>0 59 1280 719 0 0 1280 778 </string>
</dict>
<key>Module</key>
<string>PBXProjectFindModule</string>
@ -528,7 +527,9 @@
<key>GeometryConfiguration</key>
<dict>
<key>Frame</key>
<string>{{10, 27}, {1030, 52}}</string>
<string>{{10, 27}, {1030, 134}}</string>
<key>RubberWindowFrame</key>
<string>0 59 1280 719 0 0 1280 778 </string>
</dict>
<key>Module</key>
<string>PBXBuildResultsModule</string>
@ -556,11 +557,11 @@
</array>
<key>TableOfContents</key>
<array>
<string>6B6BC6F70FE7A3A4009E3CB9</string>
<string>6B25B3F90FFA1250004F1BC4</string>
<string>1CA23ED40692098700951B8B</string>
<string>6B6BC6F80FE7A3A4009E3CB9</string>
<string>6B25B3FA0FFA1250004F1BC4</string>
<string>6B8632A30F78115100E2684A</string>
<string>6B6BC6F90FE7A3A4009E3CB9</string>
<string>6B25B3FB0FFA1250004F1BC4</string>
<string>1CA23EDF0692099D00951B8B</string>
<string>1CA23EE00692099D00951B8B</string>
<string>1CA23EE10692099D00951B8B</string>
@ -609,12 +610,12 @@
<key>GeometryConfiguration</key>
<dict>
<key>Frame</key>
<string>{{0, 0}, {1280, 190}}</string>
<string>{{0, 0}, {1280, 199}}</string>
</dict>
<key>Module</key>
<string>PBXDebugCLIModule</string>
<key>Proportion</key>
<string>190pt</string>
<string>199pt</string>
</dict>
<dict>
<key>ContentConfiguration</key>
@ -633,8 +634,8 @@
<string>yes</string>
<key>sizes</key>
<array>
<string>{{0, 0}, {637, 124}}</string>
<string>{{637, 0}, {643, 124}}</string>
<string>{{0, 0}, {637, 130}}</string>
<string>{{637, 0}, {643, 130}}</string>
</array>
</dict>
<key>VerticalSplitView</key>
@ -649,8 +650,8 @@
<string>yes</string>
<key>sizes</key>
<array>
<string>{{0, 0}, {1280, 124}}</string>
<string>{{0, 124}, {1280, 327}}</string>
<string>{{0, 0}, {1280, 130}}</string>
<string>{{0, 130}, {1280, 344}}</string>
</array>
</dict>
</dict>
@ -670,7 +671,7 @@
<key>DebugSTDIOWindowFrame</key>
<string>{{200, 200}, {500, 300}}</string>
<key>Frame</key>
<string>{{0, 195}, {1280, 451}}</string>
<string>{{0, 204}, {1280, 474}}</string>
<key>PBXDebugSessionStackFrameViewKey</key>
<dict>
<key>DebugVariablesTableConfiguration</key>
@ -683,13 +684,13 @@
<real>413</real>
</array>
<key>Frame</key>
<string>{{637, 0}, {643, 124}}</string>
<string>{{637, 0}, {643, 130}}</string>
</dict>
</dict>
<key>Module</key>
<string>PBXDebugSessionModule</string>
<key>Proportion</key>
<string>451pt</string>
<string>474pt</string>
</dict>
</array>
<key>Name</key>
@ -707,14 +708,14 @@
</array>
<key>TableOfContents</key>
<array>
<string>6B6BC6FA0FE7A3A4009E3CB9</string>
<string>6B25B3FC0FFA1250004F1BC4</string>
<string>1CCC7628064C1048000F2A68</string>
<string>1CCC7629064C1048000F2A68</string>
<string>6B6BC6FB0FE7A3A4009E3CB9</string>
<string>6B6BC6FC0FE7A3A4009E3CB9</string>
<string>6B6BC6FD0FE7A3A4009E3CB9</string>
<string>6B6BC6FE0FE7A3A4009E3CB9</string>
<string>6B6BC6FF0FE7A3A4009E3CB9</string>
<string>6B25B3FD0FFA1250004F1BC4</string>
<string>6B25B3FE0FFA1250004F1BC4</string>
<string>6B25B3FF0FFA1250004F1BC4</string>
<string>6B25B4000FFA1250004F1BC4</string>
<string>6B8632A30F78115100E2684A</string>
</array>
<key>ToolbarConfiguration</key>
<string>xcode.toolbar.config.debugV3</string>
@ -744,6 +745,8 @@
<integer>5</integer>
<key>WindowOrderList</key>
<array>
<string>6B25B4CD0FFA2673004F1BC4</string>
<string>6B25B4CE0FFA2673004F1BC4</string>
<string>/Users/memon/Code/recastnavigation/RecastDemo/Build/Xcode/Recast.xcodeproj</string>
</array>
<key>WindowString</key>

View File

@ -8,7 +8,6 @@
/* Begin PBXBuildFile section */
1DDD58160DA1D0A300B32029 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1DDD58140DA1D0A300B32029 /* MainMenu.xib */; };
6B137C6F0F7FCBBB00459200 /* demo.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B137C6A0F7FCBBB00459200 /* demo.cpp */; };
6B137C700F7FCBBB00459200 /* glfont.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B137C6B0F7FCBBB00459200 /* glfont.cpp */; };
6B137C710F7FCBBB00459200 /* imgui.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B137C6C0F7FCBBB00459200 /* imgui.cpp */; };
6B137C720F7FCBBB00459200 /* MeshLoaderObj.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B137C6D0F7FCBBB00459200 /* MeshLoaderObj.cpp */; };
@ -22,6 +21,11 @@
6B137C910F7FCC1100459200 /* RecastRasterization.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B137C880F7FCC1100459200 /* RecastRasterization.cpp */; };
6B137C920F7FCC1100459200 /* RecastRegion.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B137C890F7FCC1100459200 /* RecastRegion.cpp */; };
6B137C930F7FCC1100459200 /* RecastTimer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B137C8A0F7FCC1100459200 /* RecastTimer.cpp */; };
6B25B6190FFA62BE004F1BC4 /* Builder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B25B6140FFA62BE004F1BC4 /* Builder.cpp */; };
6B25B61A0FFA62BE004F1BC4 /* BuilderStatMesh.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B25B6150FFA62BE004F1BC4 /* BuilderStatMesh.cpp */; };
6B25B61B0FFA62BE004F1BC4 /* BuilderStatMeshSimple.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B25B6160FFA62BE004F1BC4 /* BuilderStatMeshSimple.cpp */; };
6B25B61C0FFA62BE004F1BC4 /* BuilderStatMeshTiling.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B25B6170FFA62BE004F1BC4 /* BuilderStatMeshTiling.cpp */; };
6B25B61D0FFA62BE004F1BC4 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B25B6180FFA62BE004F1BC4 /* main.cpp */; };
6B8632DA0F78122C00E2684A /* SDL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6B8632D90F78122C00E2684A /* SDL.framework */; };
6B8632DC0F78123E00E2684A /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6B8632DB0F78123E00E2684A /* OpenGL.framework */; };
6BB788170FC0472B003C24DB /* ChunkyTriMesh.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6BB788160FC0472B003C24DB /* ChunkyTriMesh.cpp */; };
@ -40,7 +44,6 @@
29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = "<absolute>"; };
29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
32CA4F630368D1EE00C91783 /* Recast_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Recast_Prefix.pch; sourceTree = "<group>"; };
6B137C6A0F7FCBBB00459200 /* demo.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = demo.cpp; path = ../../Source/demo.cpp; sourceTree = SOURCE_ROOT; };
6B137C6B0F7FCBBB00459200 /* glfont.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = glfont.cpp; path = ../../Source/glfont.cpp; sourceTree = SOURCE_ROOT; };
6B137C6C0F7FCBBB00459200 /* imgui.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = imgui.cpp; path = ../../Source/imgui.cpp; sourceTree = SOURCE_ROOT; };
6B137C6D0F7FCBBB00459200 /* MeshLoaderObj.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = MeshLoaderObj.cpp; path = ../../Source/MeshLoaderObj.cpp; sourceTree = SOURCE_ROOT; };
@ -62,6 +65,15 @@
6B137C880F7FCC1100459200 /* RecastRasterization.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = RecastRasterization.cpp; path = ../../../Recast/Source/RecastRasterization.cpp; sourceTree = SOURCE_ROOT; };
6B137C890F7FCC1100459200 /* RecastRegion.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = RecastRegion.cpp; path = ../../../Recast/Source/RecastRegion.cpp; sourceTree = SOURCE_ROOT; };
6B137C8A0F7FCC1100459200 /* RecastTimer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = RecastTimer.cpp; path = ../../../Recast/Source/RecastTimer.cpp; sourceTree = SOURCE_ROOT; };
6B25B6100FFA62AD004F1BC4 /* Builder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Builder.h; path = ../../Include/Builder.h; sourceTree = SOURCE_ROOT; };
6B25B6110FFA62AD004F1BC4 /* BuilderStatMesh.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BuilderStatMesh.h; path = ../../Include/BuilderStatMesh.h; sourceTree = SOURCE_ROOT; };
6B25B6120FFA62AD004F1BC4 /* BuilderStatMeshSimple.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BuilderStatMeshSimple.h; path = ../../Include/BuilderStatMeshSimple.h; sourceTree = SOURCE_ROOT; };
6B25B6130FFA62AD004F1BC4 /* BuilderStatMeshTiling.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BuilderStatMeshTiling.h; path = ../../Include/BuilderStatMeshTiling.h; sourceTree = SOURCE_ROOT; };
6B25B6140FFA62BE004F1BC4 /* Builder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Builder.cpp; path = ../../Source/Builder.cpp; sourceTree = SOURCE_ROOT; };
6B25B6150FFA62BE004F1BC4 /* BuilderStatMesh.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = BuilderStatMesh.cpp; path = ../../Source/BuilderStatMesh.cpp; sourceTree = SOURCE_ROOT; };
6B25B6160FFA62BE004F1BC4 /* BuilderStatMeshSimple.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = BuilderStatMeshSimple.cpp; path = ../../Source/BuilderStatMeshSimple.cpp; sourceTree = SOURCE_ROOT; };
6B25B6170FFA62BE004F1BC4 /* BuilderStatMeshTiling.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = BuilderStatMeshTiling.cpp; path = ../../Source/BuilderStatMeshTiling.cpp; sourceTree = SOURCE_ROOT; };
6B25B6180FFA62BE004F1BC4 /* main.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = main.cpp; path = ../../Source/main.cpp; sourceTree = SOURCE_ROOT; };
6B8632D90F78122C00E2684A /* SDL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL.framework; path = Library/Frameworks/SDL.framework; sourceTree = SDKROOT; };
6B8632DB0F78123E00E2684A /* OpenGL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGL.framework; path = System/Library/Frameworks/OpenGL.framework; sourceTree = SDKROOT; };
6BB788160FC0472B003C24DB /* ChunkyTriMesh.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ChunkyTriMesh.cpp; path = ../../Source/ChunkyTriMesh.cpp; sourceTree = SOURCE_ROOT; };
@ -93,6 +105,15 @@
080E96DDFE201D6D7F000001 /* Classes */ = {
isa = PBXGroup;
children = (
6B25B6140FFA62BE004F1BC4 /* Builder.cpp */,
6B25B6150FFA62BE004F1BC4 /* BuilderStatMesh.cpp */,
6B25B6160FFA62BE004F1BC4 /* BuilderStatMeshSimple.cpp */,
6B25B6170FFA62BE004F1BC4 /* BuilderStatMeshTiling.cpp */,
6B25B6180FFA62BE004F1BC4 /* main.cpp */,
6B25B6100FFA62AD004F1BC4 /* Builder.h */,
6B25B6110FFA62AD004F1BC4 /* BuilderStatMesh.h */,
6B25B6120FFA62AD004F1BC4 /* BuilderStatMeshSimple.h */,
6B25B6130FFA62AD004F1BC4 /* BuilderStatMeshTiling.h */,
6BDD9E030F91110C00904EEF /* Detour */,
6B137C7D0F7FCBE800459200 /* Recast */,
6B137C790F7FCBE400459200 /* glfont.h */,
@ -103,7 +124,6 @@
6B137C6D0F7FCBBB00459200 /* MeshLoaderObj.cpp */,
6BB788160FC0472B003C24DB /* ChunkyTriMesh.cpp */,
6BB788180FC04753003C24DB /* ChunkyTriMesh.h */,
6B137C6A0F7FCBBB00459200 /* demo.cpp */,
6B137C7C0F7FCBE400459200 /* SDLMain.h */,
6B137C6E0F7FCBBB00459200 /* SDLMain.m */,
);
@ -265,7 +285,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6B137C6F0F7FCBBB00459200 /* demo.cpp in Sources */,
6B137C700F7FCBBB00459200 /* glfont.cpp in Sources */,
6B137C710F7FCBBB00459200 /* imgui.cpp in Sources */,
6B137C720F7FCBBB00459200 /* MeshLoaderObj.cpp in Sources */,
@ -283,6 +302,11 @@
6BDD9E0B0F91113800904EEF /* DetourStatNavMesh.cpp in Sources */,
6BDD9E0C0F91113800904EEF /* DetourStatNavMeshBuilder.cpp in Sources */,
6BB788170FC0472B003C24DB /* ChunkyTriMesh.cpp in Sources */,
6B25B6190FFA62BE004F1BC4 /* Builder.cpp in Sources */,
6B25B61A0FFA62BE004F1BC4 /* BuilderStatMesh.cpp in Sources */,
6B25B61B0FFA62BE004F1BC4 /* BuilderStatMeshSimple.cpp in Sources */,
6B25B61C0FFA62BE004F1BC4 /* BuilderStatMeshTiling.cpp in Sources */,
6B25B61D0FFA62BE004F1BC4 /* main.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -0,0 +1,49 @@
#ifndef RECASTBUILDER_H
#define RECASTBUILDER_H
class Builder
{
protected:
const float* m_verts;
int m_nverts;
const int* m_tris;
const float* m_trinorms;
int m_ntris;
float m_bmin[3], m_bmax[3];
float m_cellSize;
float m_cellHeight;
float m_agentHeight;
float m_agentRadius;
float m_agentMaxClimb;
float m_agentMaxSlope;
float m_regionMinSize;
float m_regionMergeSize;
float m_edgeMaxLen;
float m_edgeMaxError;
float m_vertsPerPoly;
public:
Builder();
virtual ~Builder();
virtual void handleSettings();
virtual void handleTools();
virtual void handleDebugMode();
virtual void setToolStartPos(const float* p);
virtual void setToolEndPos(const float* p);
virtual void handleRender();
virtual void handleMeshChanged(const float* verts, int nverts,
const int* tris, const float* trinorms, int ntris,
const float* bmin, const float* bmax);
virtual bool handleBuild();
void resetCommonSettings();
void handleCommonSettings();
};
#endif // RECASTBUILDER_H

View File

@ -0,0 +1,67 @@
#ifndef RECASTBUILDERSTATMESH_H
#define RECASTBUILDERSTATMESH_H
#include "Builder.h"
#include "DetourStatNavMesh.h"
#include "Recast.h"
#include "RecastLog.h"
class BuilderStatMesh : public Builder
{
protected:
dtStatNavMesh* m_navMesh;
enum ToolMode
{
TOOLMODE_PATHFIND,
TOOLMODE_RAYCAST,
TOOLMODE_DISTANCE_TO_WALL,
TOOLMODE_FIND_POLYS_AROUND,
};
ToolMode m_toolMode;
static const int MAX_POLYS = 256;
dtPolyRef m_startRef;
dtPolyRef m_endRef;
dtPolyRef m_polys[MAX_POLYS];
int m_npolys;
float m_straightPath[MAX_POLYS*3];
int m_nstraightPath;
float m_polyPickExt[3];
float m_spos[3];
float m_epos[3];
float m_hitPos[3];
float m_hitNormal[3];
float m_distanceToWall;
bool m_sposSet;
bool m_eposSet;
enum ToolRenderFlags
{
NAVMESH_POLYS = 0x01,
NAVMESH_BVTREE = 0x02,
NAVMESH_TOOLS = 0x04,
};
void toolCleanup();
void toolReset();
void toolRecalc();
void toolRender(int flags);
void drawAgent(const float* pos, float r, float h, float c, const float* col);
public:
BuilderStatMesh();
virtual ~BuilderStatMesh();
virtual void handleTools();
virtual void setToolStartPos(const float* p);
virtual void setToolEndPos(const float* p);
};
#endif // RECASTBUILDERSTATMESH_H

View File

@ -0,0 +1,62 @@
#ifndef RECASTBUILDERSTATMESHSIMPLE_H
#define RECASTBUILDERSTATMESHSIMPLE_H
#include "BuilderStatMesh.h"
#include "DetourStatNavMesh.h"
#include "Recast.h"
#include "RecastLog.h"
class BuilderStatMeshSimple : public BuilderStatMesh
{
protected:
bool m_keepInterResults;
rcBuildTimes m_buildTimes;
unsigned char* m_triflags;
rcHeightfield* m_solid;
rcCompactHeightfield* m_chf;
rcContourSet* m_cset;
rcPolyMesh* m_polyMesh;
rcConfig m_cfg;
enum DrawMode
{
DRAWMODE_NAVMESH,
DRAWMODE_NAVMESH_TRANS,
DRAWMODE_NAVMESH_BVTREE,
DRAWMODE_NAVMESH_INVIS,
DRAWMODE_MESH,
DRAWMODE_VOXELS,
DRAWMODE_VOXELS_WALKABLE,
DRAWMODE_COMPACT,
DRAWMODE_COMPACT_DISTANCE,
DRAWMODE_COMPACT_REGIONS,
DRAWMODE_REGION_CONNECTIONS,
DRAWMODE_RAW_CONTOURS,
DRAWMODE_BOTH_CONTOURS,
DRAWMODE_CONTOURS,
DRAWMODE_POLYMESH,
MAX_DRAWMODE
};
DrawMode m_drawMode;
void cleanup();
public:
BuilderStatMeshSimple();
virtual ~BuilderStatMeshSimple();
virtual void handleSettings();
virtual void handleDebugMode();
virtual void handleRender();
virtual void handleMeshChanged(const float* verts, int nverts,
const int* tris, const float* trinorms, int ntris,
const float* bmin, const float* bmax);
virtual bool handleBuild();
};
#endif // RECASTBUILDERSTATMESHSIMPLE_H

View File

@ -0,0 +1,83 @@
#ifndef RECASTBUILDERSTATMESHTILING_H
#define RECASTBUILDERSTATMESHTILING_H
#include "BuilderStatMesh.h"
#include "DetourStatNavMesh.h"
#include "Recast.h"
#include "RecastLog.h"
#include "ChunkyTriMesh.h"
class BuilderStatMeshTiling : public BuilderStatMesh
{
protected:
struct Tile
{
inline Tile() : chf(0), cset(0), solid(0), buildTime(0) {}
inline ~Tile() { delete chf; delete cset; delete solid; }
rcCompactHeightfield* chf;
rcHeightfield* solid;
rcContourSet* cset;
int buildTime;
};
struct TileSet
{
inline TileSet() : width(0), height(0), tiles(0) {}
inline ~TileSet() { delete [] tiles; }
int width, height;
float bmin[3], bmax[3];
float cs, ch;
Tile* tiles;
};
bool m_keepInterResults;
float m_tileSize;
rcBuildTimes m_buildTimes;
rcChunkyTriMesh* m_chunkyMesh;
rcPolyMesh* m_polyMesh;
rcConfig m_cfg;
TileSet* m_tileSet;
enum DrawMode
{
DRAWMODE_NAVMESH,
DRAWMODE_NAVMESH_TRANS,
DRAWMODE_NAVMESH_BVTREE,
DRAWMODE_NAVMESH_INVIS,
DRAWMODE_MESH,
DRAWMODE_VOXELS,
DRAWMODE_VOXELS_WALKABLE,
DRAWMODE_COMPACT,
DRAWMODE_COMPACT_DISTANCE,
DRAWMODE_COMPACT_REGIONS,
DRAWMODE_REGION_CONNECTIONS,
DRAWMODE_RAW_CONTOURS,
DRAWMODE_BOTH_CONTOURS,
DRAWMODE_CONTOURS,
DRAWMODE_POLYMESH,
MAX_DRAWMODE
};
DrawMode m_drawMode;
void cleanup();
public:
BuilderStatMeshTiling();
virtual ~BuilderStatMeshTiling();
virtual void handleSettings();
virtual void handleDebugMode();
virtual void handleRender();
virtual void handleMeshChanged(const float* verts, int nverts,
const int* tris, const float* trinorms, int ntris,
const float* bmin, const float* bmax);
virtual bool handleBuild();
};
#endif // RECASTBUILDERSTATMESHTILING_H

View File

@ -19,10 +19,18 @@
#ifndef IMGUI_H
#define IMGUI_H
#define GENID ((__LINE__) << 16)
#define GENID1(x) ((__LINE__) << 16 | (x))
#define GENID ((__LINE__ ^ (int)__FILE__) << 16)
#define GENID1(x) ((__LINE__ ^ (int)__FILE__) << 16 | (x))
void imguiBeginFrame();
enum imguiMouseButton
{
IMGUI_MBUT_LEFT = 0x01,
IMGUI_MBUT_RIGHT = 0x02,
IMGUI_MBUT_UP = 0x04,
IMGUI_MBUT_DOWN = 0x08,
};
void imguiBeginFrame(int mx, int my, unsigned char mbut);
void imguiEndFrame();
void imguiRender(void (*drawText)(int x, int y, int dir, const char* text, unsigned int col));

View File

@ -0,0 +1,119 @@
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>
#include "Builder.h"
#include "Recast.h"
#include "RecastDebugDraw.h"
#include "imgui.h"
#ifdef WIN32
# define snprintf _snprintf
#endif
Builder::Builder() :
m_verts(0), m_nverts(0), m_tris(0), m_trinorms(0), m_ntris(0)
{
resetCommonSettings();
}
Builder::~Builder()
{
}
void Builder::handleSettings()
{
}
void Builder::handleTools()
{
}
void Builder::handleDebugMode()
{
}
void Builder::handleRender()
{
if (!m_verts || !m_tris || !m_trinorms)
return;
// Draw mesh
rcDebugDrawMesh(m_verts, m_nverts, m_tris, m_trinorms, m_ntris, 0);
// Draw bounds
float col[4] = {1,1,1,0.5f};
rcDebugDrawBoxWire(m_bmin[0],m_bmin[1],m_bmin[2], m_bmax[0],m_bmax[1],m_bmax[2], col);
}
void Builder::handleMeshChanged(const float* verts, int nverts,
const int* tris, const float* trinorms, int ntris,
const float* bmin, const float* bmax)
{
m_verts = verts;
m_nverts = nverts;
m_tris = tris;
m_trinorms = trinorms;
m_ntris = ntris;
vcopy(m_bmin, bmin);
vcopy(m_bmax, bmax);
}
void Builder::resetCommonSettings()
{
m_cellSize = 0.3f;
m_cellHeight = 0.2f;
m_agentHeight = 2.0f;
m_agentRadius = 0.6f;
m_agentMaxClimb = 0.9f;
m_agentMaxSlope = 45.0f;
m_regionMinSize = 50;
m_regionMergeSize = 20;
m_edgeMaxLen = 12.0f;
m_edgeMaxError = 1.3f;
m_vertsPerPoly = 6.0f;
}
void Builder::handleCommonSettings()
{
imguiLabel(GENID, "Rasterization");
imguiSlider(GENID, "Cell Size", &m_cellSize, 0.1f, 1.0f, 0.01f);
imguiSlider(GENID, "Cell Height", &m_cellHeight, 0.1f, 1.0f, 0.01f);
int gw = 0, gh = 0;
rcCalcGridSize(m_bmin, m_bmax, m_cellSize, &gw, &gh);
char text[64];
snprintf(text, 64, "Voxels %d x %d", gw, gh);
imguiValue(GENID, text);
imguiSeparator();
imguiLabel(GENID, "Agent");
imguiSlider(GENID, "Height", &m_agentHeight, 0.1f, 5.0f, 0.1f);
imguiSlider(GENID, "Radius", &m_agentRadius, 0.0f, 5.0f, 0.1f);
imguiSlider(GENID, "Max Climb", &m_agentMaxClimb, 0.1f, 5.0f, 0.1f);
imguiSlider(GENID, "Max Slope", &m_agentMaxSlope, 0.0f, 90.0f, 1.0f);
imguiSeparator();
imguiLabel(GENID, "Region");
imguiSlider(GENID, "Min Region Size", &m_regionMinSize, 0.0f, 150.0f, 1.0f);
imguiSlider(GENID, "Merged Region Size", &m_regionMergeSize, 0.0f, 150.0f, 1.0f);
imguiSeparator();
imguiLabel(GENID, "Polygonization");
imguiSlider(GENID, "Max Edge Length", &m_edgeMaxLen, 0.0f, 50.0f, 1.0f);
imguiSlider(GENID, "Max Edge Error", &m_edgeMaxError, 0.1f, 3.0f, 0.1f);
imguiSlider(GENID, "Verts Per Poly", &m_vertsPerPoly, 3.0f, 12.0f, 1.0f);
imguiSeparator();
}
void Builder::setToolStartPos(const float* p)
{
}
void Builder::setToolEndPos(const float* p)
{
}
bool Builder::handleBuild()
{
return true;
}

View File

@ -0,0 +1,286 @@
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "SDL.h"
#include "SDL_Opengl.h"
#include "imgui.h"
#include "Builder.h"
#include "BuilderStatMesh.h"
#include "Recast.h"
#include "RecastTimer.h"
#include "RecastDebugDraw.h"
#include "DetourStatNavMesh.h"
#include "DetourStatNavMeshBuilder.h"
#include "DetourDebugDraw.h"
#ifdef WIN32
# define snprintf _snprintf
#endif
BuilderStatMesh::BuilderStatMesh() :
m_navMesh(0),
m_toolMode(TOOLMODE_PATHFIND),
m_sposSet(false),
m_eposSet(false)
{
toolReset();
m_polyPickExt[0] = 2;
m_polyPickExt[1] = 4;
m_polyPickExt[2] = 2;
}
BuilderStatMesh::~BuilderStatMesh()
{
toolCleanup();
}
void BuilderStatMesh::handleTools()
{
if (imguiCheck(GENID, "Pathfind", m_toolMode == TOOLMODE_PATHFIND))
{
m_toolMode = TOOLMODE_PATHFIND;
toolRecalc();
}
if (imguiCheck(GENID, "Distance to Wall", m_toolMode == TOOLMODE_DISTANCE_TO_WALL))
{
m_toolMode = TOOLMODE_DISTANCE_TO_WALL;
toolRecalc();
}
if (imguiCheck(GENID, "Raycast", m_toolMode == TOOLMODE_RAYCAST))
{
m_toolMode = TOOLMODE_RAYCAST;
toolRecalc();
}
if (imguiCheck(GENID, "Find Polys Around", m_toolMode == TOOLMODE_FIND_POLYS_AROUND))
{
m_toolMode = TOOLMODE_FIND_POLYS_AROUND;
toolRecalc();
}
}
void BuilderStatMesh::setToolStartPos(const float* p)
{
m_sposSet = true;
vcopy(m_spos, p);
toolRecalc();
}
void BuilderStatMesh::setToolEndPos(const float* p)
{
m_eposSet = true;
vcopy(m_epos, p);
toolRecalc();
}
void BuilderStatMesh::toolCleanup()
{
delete m_navMesh;
m_navMesh = 0;
}
void BuilderStatMesh::toolReset()
{
m_startRef = 0;
m_endRef = 0;
m_npolys = 0;
m_nstraightPath = 0;
memset(m_hitPos, 0, sizeof(m_hitPos));
memset(m_hitNormal, 0, sizeof(m_hitNormal));
m_distanceToWall = 0;
}
void BuilderStatMesh::toolRecalc()
{
if (!m_navMesh)
return;
if (m_sposSet)
m_startRef = m_navMesh->findNearestPoly(m_spos, m_polyPickExt);
else
m_startRef = 0;
if (m_eposSet)
m_endRef = m_navMesh->findNearestPoly(m_epos, m_polyPickExt);
else
m_endRef = 0;
if (m_toolMode == TOOLMODE_PATHFIND)
{
if (m_sposSet && m_eposSet && m_startRef && m_endRef)
{
m_npolys = m_navMesh->findPath(m_startRef, m_endRef, m_polys, MAX_POLYS);
if (m_npolys)
m_nstraightPath = m_navMesh->findStraightPath(m_spos, m_epos, m_polys, m_npolys, m_straightPath, MAX_POLYS);
}
else
{
m_npolys = 0;
m_nstraightPath = 0;
}
}
else if (m_toolMode == TOOLMODE_RAYCAST)
{
m_nstraightPath = 0;
if (m_sposSet && m_eposSet && m_startRef)
{
float t = 0;
m_npolys = 0;
m_nstraightPath = 2;
m_straightPath[0] = m_spos[0];
m_straightPath[1] = m_spos[1];
m_straightPath[2] = m_spos[2];
if (m_navMesh->raycast(m_startRef, m_spos, m_epos, t, m_polys[0]))
{
m_npolys = 1;
m_straightPath[3] = m_spos[0] + (m_epos[0] - m_spos[0]) * t;
m_straightPath[4] = m_spos[1] + (m_epos[1] - m_spos[1]) * t;
m_straightPath[5] = m_spos[2] + (m_epos[2] - m_spos[2]) * t;
}
else
{
m_straightPath[3] = m_epos[0];
m_straightPath[4] = m_epos[1];
m_straightPath[5] = m_epos[2];
}
}
}
else if (m_toolMode == TOOLMODE_DISTANCE_TO_WALL)
{
m_distanceToWall = 0;
if (m_sposSet && m_startRef)
m_distanceToWall = m_navMesh->findDistanceToWall(m_startRef, m_spos, 100.0f, m_hitPos, m_hitNormal);
}
else if (m_toolMode == TOOLMODE_FIND_POLYS_AROUND)
{
if (m_sposSet && m_startRef && m_eposSet)
{
const float dx = m_epos[0] - m_spos[0];
const float dz = m_epos[2] - m_spos[2];
float dist = sqrtf(dx*dx + dz*dz);
m_npolys = m_navMesh->findPolysAround(m_startRef, m_spos, dist, m_polys, 0, 0, 0, MAX_POLYS);
}
}
}
void BuilderStatMesh::toolRender(int flags)
{
if (!m_navMesh)
return;
static const float startCol[4] = { 0.5f, 0.1f, 0.0f, 0.75f };
static const float endCol[4] = { 0.2f, 0.4f, 0.0f, 0.75f };
static const float pathCol[4] = {0,0,0,0.25f};
glDepthMask(GL_FALSE);
if (flags & NAVMESH_POLYS)
dtDebugDrawStatNavMesh(m_navMesh);
if (flags & NAVMESH_BVTREE)
dtDebugDrawStatNavMeshBVTree(m_navMesh);
if (flags & NAVMESH_TOOLS)
{
if (m_toolMode == TOOLMODE_PATHFIND)
{
dtDebugDrawStatNavMeshPoly(m_navMesh, m_startRef, startCol);
dtDebugDrawStatNavMeshPoly(m_navMesh, m_endRef, endCol);
if (m_npolys)
{
for (int i = 1; i < m_npolys-1; ++i)
dtDebugDrawStatNavMeshPoly(m_navMesh, m_polys[i], pathCol);
}
if (m_nstraightPath)
{
glColor4ub(128,16,0,220);
glLineWidth(3.0f);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < m_nstraightPath; ++i)
glVertex3f(m_straightPath[i*3], m_straightPath[i*3+1]+0.4f, m_straightPath[i*3+2]);
glEnd();
glLineWidth(1.0f);
glPointSize(4.0f);
glBegin(GL_POINTS);
for (int i = 0; i < m_nstraightPath; ++i)
glVertex3f(m_straightPath[i*3], m_straightPath[i*3+1]+0.4f, m_straightPath[i*3+2]);
glEnd();
glPointSize(1.0f);
}
}
else if (m_toolMode == TOOLMODE_RAYCAST)
{
dtDebugDrawStatNavMeshPoly(m_navMesh, m_startRef, startCol);
if (m_nstraightPath)
{
dtDebugDrawStatNavMeshPoly(m_navMesh, m_polys[0], pathCol);
glColor4ub(128,16,0,220);
glLineWidth(3.0f);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < m_nstraightPath; ++i)
glVertex3f(m_straightPath[i*3], m_straightPath[i*3+1]+0.4f, m_straightPath[i*3+2]);
glEnd();
glLineWidth(1.0f);
glPointSize(4.0f);
glBegin(GL_POINTS);
for (int i = 0; i < m_nstraightPath; ++i)
glVertex3f(m_straightPath[i*3], m_straightPath[i*3+1]+0.4f, m_straightPath[i*3+2]);
glEnd();
glPointSize(1.0f);
}
}
else if (m_toolMode == TOOLMODE_DISTANCE_TO_WALL)
{
dtDebugDrawStatNavMeshPoly(m_navMesh, m_startRef, startCol);
const float col[4] = {1,1,1,0.5f};
rcDebugDrawCylinderWire(m_spos[0]-m_distanceToWall, m_spos[1]+0.02f, m_spos[2]-m_distanceToWall,
m_spos[0]+m_distanceToWall, m_spos[1]+m_agentHeight, m_spos[2]+m_distanceToWall, col);
glLineWidth(3.0f);
glColor4fv(col);
glBegin(GL_LINES);
glVertex3f(m_hitPos[0], m_hitPos[1] + 0.02f, m_hitPos[2]);
glVertex3f(m_hitPos[0], m_hitPos[1] + m_agentHeight, m_hitPos[2]);
glEnd();
glLineWidth(1.0f);
}
else if (m_toolMode == TOOLMODE_FIND_POLYS_AROUND)
{
for (int i = 0; i < m_npolys; ++i)
dtDebugDrawStatNavMeshPoly(m_navMesh, m_polys[i], pathCol);
const float dx = m_epos[0] - m_spos[0];
const float dz = m_epos[2] - m_spos[2];
float dist = sqrtf(dx*dx + dz*dz);
const float col[4] = {1,1,1,0.5f};
rcDebugDrawCylinderWire(m_spos[0]-dist, m_spos[1]+0.02f, m_spos[2]-dist,
m_spos[0]+dist, m_spos[1]+m_agentHeight, m_spos[2]+dist, col);
}
}
glDepthMask(GL_TRUE);
}
void BuilderStatMesh::drawAgent(const float* pos, float r, float h, float c, const float* col)
{
glDepthMask(GL_FALSE);
// Agent dimensions.
glLineWidth(2.0f);
rcDebugDrawCylinderWire(pos[0]-r, pos[1]+0.02f, pos[2]-r, pos[0]+r, pos[1]+h, pos[2]+r, col);
glLineWidth(1.0f);
glColor4ub(0,0,0,196);
glBegin(GL_LINES);
glVertex3f(pos[0], pos[1]-c, pos[2]);
glVertex3f(pos[0], pos[1]+c, pos[2]);
glVertex3f(pos[0]-r/2, pos[1]+0.02f, pos[2]);
glVertex3f(pos[0]+r/2, pos[1]+0.02f, pos[2]);
glVertex3f(pos[0], pos[1]+0.02f, pos[2]-r/2);
glVertex3f(pos[0], pos[1]+0.02f, pos[2]+r/2);
glEnd();
glDepthMask(GL_TRUE);
}

View File

@ -0,0 +1,502 @@
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "SDL.h"
#include "SDL_Opengl.h"
#include "imgui.h"
#include "Builder.h"
#include "BuilderStatMeshSimple.h"
#include "Recast.h"
#include "RecastTimer.h"
#include "RecastDebugDraw.h"
#include "DetourStatNavMesh.h"
#include "DetourStatNavMeshBuilder.h"
#include "DetourDebugDraw.h"
#ifdef WIN32
# define snprintf _snprintf
#endif
BuilderStatMeshSimple::BuilderStatMeshSimple() :
m_keepInterResults(false),
m_triflags(0),
m_solid(0),
m_chf(0),
m_cset(0),
m_polyMesh(0),
m_drawMode(DRAWMODE_NAVMESH)
{
}
BuilderStatMeshSimple::~BuilderStatMeshSimple()
{
cleanup();
}
void BuilderStatMeshSimple::cleanup()
{
delete [] m_triflags;
m_triflags = 0;
delete m_solid;
m_solid = 0;
delete m_chf;
m_chf = 0;
delete m_cset;
m_cset = 0;
delete m_polyMesh;
m_polyMesh = 0;
toolCleanup();
}
void BuilderStatMeshSimple::handleSettings()
{
Builder::handleCommonSettings();
if (imguiCheck(GENID, "Keep Itermediate Results", m_keepInterResults))
m_keepInterResults = !m_keepInterResults;
imguiSeparator();
}
void BuilderStatMeshSimple::handleDebugMode()
{
// Check which modes are valid.
bool valid[MAX_DRAWMODE];
for (int i = 0; i < MAX_DRAWMODE; ++i)
valid[i] = false;
if (m_verts && m_tris)
{
valid[DRAWMODE_NAVMESH] = m_navMesh != 0;
valid[DRAWMODE_NAVMESH_TRANS] = m_navMesh != 0;
valid[DRAWMODE_NAVMESH_BVTREE] = m_navMesh != 0;
valid[DRAWMODE_NAVMESH_INVIS] = m_navMesh != 0;
valid[DRAWMODE_MESH] = true;
valid[DRAWMODE_VOXELS] = m_solid != 0;
valid[DRAWMODE_VOXELS_WALKABLE] = m_solid != 0;
valid[DRAWMODE_COMPACT] = m_chf != 0;
valid[DRAWMODE_COMPACT_DISTANCE] = m_chf != 0;
valid[DRAWMODE_COMPACT_REGIONS] = m_chf != 0;
valid[DRAWMODE_REGION_CONNECTIONS] = m_cset != 0;
valid[DRAWMODE_RAW_CONTOURS] = m_cset != 0;
valid[DRAWMODE_BOTH_CONTOURS] = m_cset != 0;
valid[DRAWMODE_CONTOURS] = m_cset != 0;
valid[DRAWMODE_POLYMESH] = m_polyMesh != 0;
}
if (!valid[m_drawMode])
m_drawMode = DRAWMODE_MESH;
int unavail = 0;
for (int i = 0; i < MAX_DRAWMODE; ++i)
if (!valid[i]) unavail++;
if (unavail == MAX_DRAWMODE)
return;
imguiLabel(GENID, "Draw");
if (valid[DRAWMODE_MESH] && imguiCheck(GENID, "Input Mesh", m_drawMode == DRAWMODE_MESH))
m_drawMode = DRAWMODE_MESH;
if (valid[DRAWMODE_NAVMESH] && imguiCheck(GENID, "Navmesh", m_drawMode == DRAWMODE_NAVMESH))
m_drawMode = DRAWMODE_NAVMESH;
if (valid[DRAWMODE_NAVMESH_INVIS] && imguiCheck(GENID, "Navmesh Invis", m_drawMode == DRAWMODE_NAVMESH_INVIS))
m_drawMode = DRAWMODE_NAVMESH_INVIS;
if (valid[DRAWMODE_NAVMESH_TRANS] && imguiCheck(GENID, "Navmesh Trans", m_drawMode == DRAWMODE_NAVMESH_TRANS))
m_drawMode = DRAWMODE_NAVMESH_TRANS;
if (valid[DRAWMODE_NAVMESH_BVTREE] && imguiCheck(GENID, "Navmesh BVTree", m_drawMode == DRAWMODE_NAVMESH_BVTREE))
m_drawMode = DRAWMODE_NAVMESH_BVTREE;
if (valid[DRAWMODE_VOXELS] && imguiCheck(GENID, "Voxels", m_drawMode == DRAWMODE_VOXELS))
m_drawMode = DRAWMODE_VOXELS;
if (valid[DRAWMODE_VOXELS_WALKABLE] && imguiCheck(GENID, "Walkable Voxels", m_drawMode == DRAWMODE_VOXELS_WALKABLE))
m_drawMode = DRAWMODE_VOXELS_WALKABLE;
if (valid[DRAWMODE_COMPACT] && imguiCheck(GENID, "Compact", m_drawMode == DRAWMODE_COMPACT))
m_drawMode = DRAWMODE_COMPACT;
if (valid[DRAWMODE_COMPACT_DISTANCE] && imguiCheck(GENID, "Compact Distance", m_drawMode == DRAWMODE_COMPACT_DISTANCE))
m_drawMode = DRAWMODE_COMPACT_DISTANCE;
if (valid[DRAWMODE_COMPACT_REGIONS] && imguiCheck(GENID, "Compact Regions", m_drawMode == DRAWMODE_COMPACT_REGIONS))
m_drawMode = DRAWMODE_COMPACT_REGIONS;
if (valid[DRAWMODE_REGION_CONNECTIONS] && imguiCheck(GENID, "Region Connections", m_drawMode == DRAWMODE_REGION_CONNECTIONS))
m_drawMode = DRAWMODE_REGION_CONNECTIONS;
if (valid[DRAWMODE_RAW_CONTOURS] && imguiCheck(GENID, "Raw Contours", m_drawMode == DRAWMODE_RAW_CONTOURS))
m_drawMode = DRAWMODE_RAW_CONTOURS;
if (valid[DRAWMODE_BOTH_CONTOURS] && imguiCheck(GENID, "Both Contours", m_drawMode == DRAWMODE_BOTH_CONTOURS))
m_drawMode = DRAWMODE_BOTH_CONTOURS;
if (valid[DRAWMODE_CONTOURS] && imguiCheck(GENID, "Contours", m_drawMode == DRAWMODE_CONTOURS))
m_drawMode = DRAWMODE_CONTOURS;
if (valid[DRAWMODE_POLYMESH] && imguiCheck(GENID, "Poly Mesh", m_drawMode == DRAWMODE_POLYMESH))
m_drawMode = DRAWMODE_POLYMESH;
if (unavail)
{
imguiValue(GENID, "Tick 'Keep Itermediate Results'");
imguiValue(GENID, "to see more debug mode options.");
}
}
void BuilderStatMeshSimple::handleRender()
{
if (!m_verts || !m_tris || !m_trinorms)
return;
float col[4];
glEnable(GL_FOG);
glDepthMask(GL_TRUE);
if (m_drawMode == DRAWMODE_MESH)
{
// Draw mesh
rcDebugDrawMeshSlope(m_verts, m_nverts, m_tris, m_trinorms, m_ntris, m_agentMaxSlope);
}
else if (m_drawMode != DRAWMODE_NAVMESH_TRANS)
{
// Draw mesh
rcDebugDrawMesh(m_verts, m_nverts, m_tris, m_trinorms, m_ntris, 0);
}
glDisable(GL_FOG);
glDepthMask(GL_FALSE);
// Draw bounds
col[0] = 1; col[1] = 1; col[2] = 1; col[3] = 0.5f;
rcDebugDrawBoxWire(m_bmin[0],m_bmin[1],m_bmin[2], m_bmax[0],m_bmax[1],m_bmax[2], col);
if (m_navMesh &&
(m_drawMode == DRAWMODE_NAVMESH ||
m_drawMode == DRAWMODE_NAVMESH_TRANS ||
m_drawMode == DRAWMODE_NAVMESH_BVTREE ||
m_drawMode == DRAWMODE_NAVMESH_INVIS))
{
int flags = NAVMESH_TOOLS;
if (m_drawMode != DRAWMODE_NAVMESH_INVIS)
flags |= NAVMESH_POLYS;
if (m_drawMode == DRAWMODE_NAVMESH_BVTREE)
flags |= NAVMESH_BVTREE;
toolRender(flags);
}
glDepthMask(GL_TRUE);
if (m_chf && m_drawMode == DRAWMODE_COMPACT)
rcDebugDrawCompactHeightfieldSolid(*m_chf);
if (m_chf && m_drawMode == DRAWMODE_COMPACT_DISTANCE)
rcDebugDrawCompactHeightfieldDistance(*m_chf);
if (m_chf && m_drawMode == DRAWMODE_COMPACT_REGIONS)
rcDebugDrawCompactHeightfieldRegions(*m_chf);
if (m_solid && m_drawMode == DRAWMODE_VOXELS)
{
glEnable(GL_FOG);
rcDebugDrawHeightfieldSolid(*m_solid);
glDisable(GL_FOG);
}
if (m_solid && m_drawMode == DRAWMODE_VOXELS_WALKABLE)
{
glEnable(GL_FOG);
rcDebugDrawHeightfieldWalkable(*m_solid);
glDisable(GL_FOG);
}
if (m_cset && m_drawMode == DRAWMODE_RAW_CONTOURS)
{
glDepthMask(GL_FALSE);
rcDebugDrawRawContours(*m_cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch);
glDepthMask(GL_TRUE);
}
if (m_cset && m_drawMode == DRAWMODE_BOTH_CONTOURS)
{
glDepthMask(GL_FALSE);
rcDebugDrawRawContours(*m_cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch, 0.5f);
rcDebugDrawContours(*m_cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch);
glDepthMask(GL_TRUE);
}
if (m_cset && m_drawMode == DRAWMODE_CONTOURS)
{
glDepthMask(GL_FALSE);
rcDebugDrawContours(*m_cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch);
glDepthMask(GL_TRUE);
}
if (m_chf && m_cset && m_drawMode == DRAWMODE_REGION_CONNECTIONS)
{
rcDebugDrawCompactHeightfieldRegions(*m_chf);
glDepthMask(GL_FALSE);
rcDebugDrawRegionConnections(*m_cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch);
glDepthMask(GL_TRUE);
}
if (m_polyMesh && m_drawMode == DRAWMODE_POLYMESH)
{
glDepthMask(GL_FALSE);
rcDebugDrawPolyMesh(*m_polyMesh);
glDepthMask(GL_TRUE);
}
static const float startCol[4] = { 0.5f, 0.1f, 0.0f, 0.75f };
static const float endCol[4] = { 0.2f, 0.4f, 0.0f, 0.75f };
if (m_sposSet)
drawAgent(m_spos, m_agentRadius, m_agentHeight, m_agentMaxClimb, startCol);
if (m_eposSet)
drawAgent(m_epos, m_agentRadius, m_agentHeight, m_agentMaxClimb, endCol);
}
void BuilderStatMeshSimple::handleMeshChanged(const float* verts, int nverts,
const int* tris, const float* trinorms, int ntris,
const float* bmin, const float* bmax)
{
Builder::handleMeshChanged(verts, nverts, tris, trinorms, ntris, bmin, bmax);
toolCleanup();
toolReset();
}
bool BuilderStatMeshSimple::handleBuild()
{
if (!m_verts || ! m_tris)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Input mesh is not specified.");
return false;
}
cleanup();
toolCleanup();
// Init build configuration from GUI
memset(&m_cfg, 0, sizeof(m_cfg));
m_cfg.cs = m_cellSize;
m_cfg.ch = m_cellHeight;
m_cfg.walkableSlopeAngle = m_agentMaxSlope;
m_cfg.walkableHeight = (int)ceilf(m_agentHeight / m_cfg.ch);
m_cfg.walkableClimb = (int)ceilf(m_agentMaxClimb / m_cfg.ch);
m_cfg.walkableRadius = (int)ceilf(m_agentRadius / m_cfg.cs);
m_cfg.maxEdgeLen = (int)(m_edgeMaxLen / m_cellSize);
m_cfg.maxSimplificationError = m_edgeMaxError;
m_cfg.minRegionSize = (int)rcSqr(m_regionMinSize);
m_cfg.mergeRegionSize = (int)rcSqr(m_regionMergeSize);
m_cfg.maxVertsPerPoly = (int)m_vertsPerPoly;
if (m_cfg.maxVertsPerPoly == DT_VERTS_PER_POLYGON)
m_drawMode = DRAWMODE_NAVMESH;
else
m_drawMode = DRAWMODE_POLYMESH;
// Set the area where the navigation will be build.
// Here the bounds of the input mesh are used, but the
// area could be specified by an user defined box, etc.
vcopy(m_cfg.bmin, m_bmin);
vcopy(m_cfg.bmax, m_bmax);
rcCalcGridSize(m_cfg.bmin, m_cfg.bmax, m_cfg.cs, &m_cfg.width, &m_cfg.height);
// Reset build times gathering.
memset(&m_buildTimes, 0, sizeof(m_buildTimes));
rcSetBuildTimes(&m_buildTimes);
// Start the build process.
rcTimeVal totStartTime = rcGetPerformanceTimer();
if (rcGetLog())
{
rcGetLog()->log(RC_LOG_PROGRESS, "Building navigation:");
rcGetLog()->log(RC_LOG_PROGRESS, " - %d x %d cells", m_cfg.width, m_cfg.height);
rcGetLog()->log(RC_LOG_PROGRESS, " - %.1fK verts, %.1fK tris", m_nverts/1000.0f, m_ntris/1000.0f);
}
// Allocate voxel heighfield where we rasterize our input data to.
m_solid = new rcHeightfield;
if (!m_solid)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'solid'.");
return false;
}
if (!rcCreateHeightfield(*m_solid, m_cfg.width, m_cfg.height, m_cfg.bmin, m_cfg.bmax, m_cfg.cs, m_cfg.ch))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not create solid heightfield.");
return false;
}
// Allocate array that can hold triangle flags.
// If you have multiple meshes you need to process, allocate
// and array which can hold the max number of triangles you need to process.
m_triflags = new unsigned char[m_ntris];
if (!m_triflags)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'triangleFlags' (%d).", m_ntris);
return false;
}
// Find triangles which are walkable based on their slope and rasterize them.
// If your input data is multiple meshes, you can transform them here, calculate
// the flags for each of the meshes and rasterize them.
memset(m_triflags, 0, m_ntris*sizeof(unsigned char));
rcMarkWalkableTriangles(m_cfg.walkableSlopeAngle, m_verts, m_nverts, m_tris, m_ntris, m_triflags);
rcRasterizeTriangles(m_verts, m_nverts, m_tris, m_triflags, m_ntris, *m_solid);
if (!m_keepInterResults)
{
delete [] m_triflags;
m_triflags = 0;
}
// Once all geoemtry is rasterized, we do initial pass of filtering to
// remove unwanted overhangs caused by the conservative rasterization
// as well as filter spans where the character cannot possibly stand.
rcFilterLedgeSpans(m_cfg.walkableHeight, m_cfg.walkableClimb, *m_solid);
rcFilterWalkableLowHeightSpans(m_cfg.walkableHeight, *m_solid);
// Compact the heightfield so that it is faster to handle from now on.
// This will result more cache coherent data as well as the neighbours
// between walkable cells will be calculated.
m_chf = new rcCompactHeightfield;
if (!m_chf)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'chf'.");
return false;
}
if (!rcBuildCompactHeightfield(m_cfg.walkableHeight, m_cfg.walkableClimb, RC_WALKABLE, *m_solid, *m_chf))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not build compact data.");
return false;
}
if (!m_keepInterResults)
{
delete m_solid;
m_solid = 0;
}
// Prepare for region partitioning, by calculating distance field along the walkable surface.
if (!rcBuildDistanceField(*m_chf))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not build distance field.");
return false;
}
// Partition the walkable surface into simple regions without holes.
if (!rcBuildRegions(*m_chf, m_cfg.walkableRadius, m_cfg.borderSize, m_cfg.minRegionSize, m_cfg.mergeRegionSize))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not build regions.");
}
// Create contours.
m_cset = new rcContourSet;
if (!m_cset)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'cset'.");
return false;
}
if (!rcBuildContours(*m_chf, m_cfg.maxSimplificationError, m_cfg.maxEdgeLen, *m_cset))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not create contours.");
return false;
}
if (!m_keepInterResults)
{
delete m_chf;
m_chf = 0;
}
// Build polygon navmesh from the contours.
m_polyMesh = new rcPolyMesh;
if (!m_polyMesh)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'polyMesh'.");
return false;
}
if (!rcBuildPolyMesh(*m_cset, m_cfg.bmin, m_cfg.bmax, m_cfg.cs, m_cfg.ch, m_cfg.maxVertsPerPoly, *m_polyMesh))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Could not triangulate contours.");
return false;
}
if (!m_keepInterResults)
{
delete m_cset;
m_cset = 0;
}
if (m_cfg.maxVertsPerPoly == DT_VERTS_PER_POLYGON)
{
unsigned char* navData = 0;
int navDataSize = 0;
if (!dtCreateNavMeshData(m_polyMesh->verts, m_polyMesh->nverts,
m_polyMesh->polys, m_polyMesh->npolys, m_polyMesh->nvp,
m_cfg.bmin, m_cfg.bmax, m_cfg.cs, m_cfg.ch, &navData, &navDataSize))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "Could not build Detour navmesh.");
return false;
}
m_navMesh = new dtStatNavMesh;
if (!m_navMesh)
{
delete [] navData;
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "Could not create Detour navmesh");
return false;
}
if (!m_navMesh->init(navData, navDataSize, true))
{
delete [] navData;
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "Could not init Detour navmesh");
return false;
}
}
rcTimeVal totEndTime = rcGetPerformanceTimer();
// Show performance stats.
if (rcGetLog())
{
const float pc = 100.0f / rcGetDeltaTimeUsec(totStartTime, totEndTime);
rcGetLog()->log(RC_LOG_PROGRESS, "Rasterize: %.1fms (%.1f%%)", m_buildTimes.rasterizeTriangles/1000.0f, m_buildTimes.rasterizeTriangles*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Build Compact: %.1fms (%.1f%%)", m_buildTimes.buildCompact/1000.0f, m_buildTimes.buildCompact*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Filter Border: %.1fms (%.1f%%)", m_buildTimes.filterBorder/1000.0f, m_buildTimes.filterBorder*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Filter Walkable: %.1fms (%.1f%%)", m_buildTimes.filterWalkable/1000.0f, m_buildTimes.filterWalkable*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Filter Reachable: %.1fms (%.1f%%)", m_buildTimes.filterMarkReachable/1000.0f, m_buildTimes.filterMarkReachable*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Build Distancefield: %.1fms (%.1f%%)", m_buildTimes.buildDistanceField/1000.0f, m_buildTimes.buildDistanceField*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - distance: %.1fms (%.1f%%)", m_buildTimes.buildDistanceFieldDist/1000.0f, m_buildTimes.buildDistanceFieldDist*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - blur: %.1fms (%.1f%%)", m_buildTimes.buildDistanceFieldBlur/1000.0f, m_buildTimes.buildDistanceFieldBlur*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Build Regions: %.1fms (%.1f%%)", m_buildTimes.buildRegions/1000.0f, m_buildTimes.buildRegions*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - watershed: %.1fms (%.1f%%)", m_buildTimes.buildRegionsReg/1000.0f, m_buildTimes.buildRegionsReg*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - expand: %.1fms (%.1f%%)", m_buildTimes.buildRegionsExp/1000.0f, m_buildTimes.buildRegionsExp*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - find catchment basins: %.1fms (%.1f%%)", m_buildTimes.buildRegionsFlood/1000.0f, m_buildTimes.buildRegionsFlood*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - filter: %.1fms (%.1f%%)", m_buildTimes.buildRegionsFilter/1000.0f, m_buildTimes.buildRegionsFilter*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Build Contours: %.1fms (%.1f%%)", m_buildTimes.buildContours/1000.0f, m_buildTimes.buildContours*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - trace: %.1fms (%.1f%%)", m_buildTimes.buildContoursTrace/1000.0f, m_buildTimes.buildContoursTrace*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - simplify: %.1fms (%.1f%%)", m_buildTimes.buildContoursSimplify/1000.0f, m_buildTimes.buildContoursSimplify*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Fixup contours: %.1fms (%.1f%%)", m_buildTimes.fixupContours/1000.0f, m_buildTimes.fixupContours*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Build Polymesh: %.1fms (%.1f%%)", m_buildTimes.buildPolymesh/1000.0f, m_buildTimes.buildPolymesh*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Polymesh: Verts:%d Polys:%d", m_polyMesh->nverts, m_polyMesh->npolys);
rcGetLog()->log(RC_LOG_PROGRESS, "TOTAL: %.1fms", rcGetDeltaTimeUsec(totStartTime, totEndTime)/1000.0f);
}
toolRecalc();
return true;
}

View File

@ -0,0 +1,790 @@
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "SDL.h"
#include "SDL_Opengl.h"
#include "imgui.h"
#include "Builder.h"
#include "BuilderStatMeshTiling.h"
#include "Recast.h"
#include "RecastTimer.h"
#include "RecastDebugDraw.h"
#include "DetourStatNavMesh.h"
#include "DetourStatNavMeshBuilder.h"
#include "DetourDebugDraw.h"
#ifdef WIN32
# define snprintf _snprintf
#endif
BuilderStatMeshTiling::BuilderStatMeshTiling() :
m_keepInterResults(false),
m_tileSize(64),
m_chunkyMesh(0),
m_tileSet(0),
m_polyMesh(0),
m_drawMode(DRAWMODE_NAVMESH)
{
}
BuilderStatMeshTiling::~BuilderStatMeshTiling()
{
cleanup();
}
void BuilderStatMeshTiling::cleanup()
{
delete m_chunkyMesh;
m_chunkyMesh = 0;
delete m_tileSet;
m_tileSet = 0;
delete m_polyMesh;
m_polyMesh = 0;
toolCleanup();
}
void BuilderStatMeshTiling::handleSettings()
{
Builder::handleCommonSettings();
imguiLabel(GENID, "Tiling");
imguiSlider(GENID, "TileSize", &m_tileSize, 16.0f, 1024.0f, 16.0f);
char text[64];
int gw = 0, gh = 0;
rcCalcGridSize(m_bmin, m_bmax, m_cellSize, &gw, &gh);
const int ts = (int)m_tileSize;
const int tw = (gw + ts-1) / ts;
const int th = (gh + ts-1) / ts;
snprintf(text, 64, "Tiles %d x %d", tw, th);
imguiValue(GENID, text);
imguiSeparator();
if (imguiCheck(GENID, "Keep Itermediate Results", m_keepInterResults))
m_keepInterResults = !m_keepInterResults;
imguiSeparator();
}
void BuilderStatMeshTiling::handleDebugMode()
{
// Check which modes are valid.
bool valid[MAX_DRAWMODE];
for (int i = 0; i < MAX_DRAWMODE; ++i)
valid[i] = false;
bool hasChf = false;
bool hasSolid = false;
bool hasCset = false;
if (m_tileSet)
{
for (int i = 0; i < m_tileSet->width*m_tileSet->height; ++i)
{
if (m_tileSet->tiles[i].solid) hasSolid = true;
if (m_tileSet->tiles[i].chf) hasChf = true;
if (m_tileSet->tiles[i].cset) hasCset = true;
}
}
if (m_verts && m_tris)
{
valid[DRAWMODE_NAVMESH] = m_navMesh != 0;
valid[DRAWMODE_NAVMESH_TRANS] = m_navMesh != 0;
valid[DRAWMODE_NAVMESH_BVTREE] = m_navMesh != 0;
valid[DRAWMODE_NAVMESH_INVIS] = m_navMesh != 0;
valid[DRAWMODE_MESH] = true;
valid[DRAWMODE_VOXELS] = hasSolid;
valid[DRAWMODE_VOXELS_WALKABLE] = hasSolid;
valid[DRAWMODE_COMPACT] = hasChf;
valid[DRAWMODE_COMPACT_DISTANCE] = hasChf;
valid[DRAWMODE_COMPACT_REGIONS] = hasChf;
valid[DRAWMODE_REGION_CONNECTIONS] = hasCset;
valid[DRAWMODE_RAW_CONTOURS] = hasCset;
valid[DRAWMODE_BOTH_CONTOURS] = hasCset;
valid[DRAWMODE_CONTOURS] = hasCset;
valid[DRAWMODE_POLYMESH] = m_polyMesh != 0;
}
if (!valid[m_drawMode])
m_drawMode = DRAWMODE_MESH;
int unavail = 0;
for (int i = 0; i < MAX_DRAWMODE; ++i)
if (!valid[i]) unavail++;
if (unavail == MAX_DRAWMODE)
return;
imguiLabel(GENID, "Draw");
if (valid[DRAWMODE_MESH] && imguiCheck(GENID, "Input Mesh", m_drawMode == DRAWMODE_MESH))
m_drawMode = DRAWMODE_MESH;
if (valid[DRAWMODE_NAVMESH] && imguiCheck(GENID, "Navmesh", m_drawMode == DRAWMODE_NAVMESH))
m_drawMode = DRAWMODE_NAVMESH;
if (valid[DRAWMODE_NAVMESH_INVIS] && imguiCheck(GENID, "Navmesh Invis", m_drawMode == DRAWMODE_NAVMESH_INVIS))
m_drawMode = DRAWMODE_NAVMESH_INVIS;
if (valid[DRAWMODE_NAVMESH_TRANS] && imguiCheck(GENID, "Navmesh Trans", m_drawMode == DRAWMODE_NAVMESH_TRANS))
m_drawMode = DRAWMODE_NAVMESH_TRANS;
if (valid[DRAWMODE_NAVMESH_BVTREE] && imguiCheck(GENID, "Navmesh BVTree", m_drawMode == DRAWMODE_NAVMESH_BVTREE))
m_drawMode = DRAWMODE_NAVMESH_BVTREE;
if (valid[DRAWMODE_VOXELS] && imguiCheck(GENID, "Voxels", m_drawMode == DRAWMODE_VOXELS))
m_drawMode = DRAWMODE_VOXELS;
if (valid[DRAWMODE_VOXELS_WALKABLE] && imguiCheck(GENID, "Walkable Voxels", m_drawMode == DRAWMODE_VOXELS_WALKABLE))
m_drawMode = DRAWMODE_VOXELS_WALKABLE;
if (valid[DRAWMODE_COMPACT] && imguiCheck(GENID, "Compact", m_drawMode == DRAWMODE_COMPACT))
m_drawMode = DRAWMODE_COMPACT;
if (valid[DRAWMODE_COMPACT_DISTANCE] && imguiCheck(GENID, "Compact Distance", m_drawMode == DRAWMODE_COMPACT_DISTANCE))
m_drawMode = DRAWMODE_COMPACT_DISTANCE;
if (valid[DRAWMODE_COMPACT_REGIONS] && imguiCheck(GENID, "Compact Regions", m_drawMode == DRAWMODE_COMPACT_REGIONS))
m_drawMode = DRAWMODE_COMPACT_REGIONS;
if (valid[DRAWMODE_REGION_CONNECTIONS] && imguiCheck(GENID, "Region Connections", m_drawMode == DRAWMODE_REGION_CONNECTIONS))
m_drawMode = DRAWMODE_REGION_CONNECTIONS;
if (valid[DRAWMODE_RAW_CONTOURS] && imguiCheck(GENID, "Raw Contours", m_drawMode == DRAWMODE_RAW_CONTOURS))
m_drawMode = DRAWMODE_RAW_CONTOURS;
if (valid[DRAWMODE_BOTH_CONTOURS] && imguiCheck(GENID, "Both Contours", m_drawMode == DRAWMODE_BOTH_CONTOURS))
m_drawMode = DRAWMODE_BOTH_CONTOURS;
if (valid[DRAWMODE_CONTOURS] && imguiCheck(GENID, "Contours", m_drawMode == DRAWMODE_CONTOURS))
m_drawMode = DRAWMODE_CONTOURS;
if (valid[DRAWMODE_POLYMESH] && imguiCheck(GENID, "Poly Mesh", m_drawMode == DRAWMODE_POLYMESH))
m_drawMode = DRAWMODE_POLYMESH;
if (unavail)
{
imguiValue(GENID, "Tick 'Keep Itermediate Results'");
imguiValue(GENID, "to see more debug mode options.");
}
}
void BuilderStatMeshTiling::handleRender()
{
if (!m_verts || !m_tris || !m_trinorms)
return;
float col[4];
glEnable(GL_FOG);
glDepthMask(GL_TRUE);
if (m_drawMode == DRAWMODE_MESH)
{
// Draw mesh
rcDebugDrawMeshSlope(m_verts, m_nverts, m_tris, m_trinorms, m_ntris, m_agentMaxSlope);
}
else if (m_drawMode != DRAWMODE_NAVMESH_TRANS)
{
// Draw mesh
rcDebugDrawMesh(m_verts, m_nverts, m_tris, m_trinorms, m_ntris, 0);
}
glDisable(GL_FOG);
glDepthMask(GL_FALSE);
// Draw bounds
col[0] = 1; col[1] = 1; col[2] = 1; col[3] = 0.5f;
rcDebugDrawBoxWire(m_bmin[0],m_bmin[1],m_bmin[2], m_bmax[0],m_bmax[1],m_bmax[2], col);
// Tiling grid.
const int ts = (int)m_tileSize;
int gw = 0, gh = 0;
rcCalcGridSize(m_bmin, m_bmax, m_cellSize, &gw, &gh);
int tw = (gw + ts-1) / ts;
int th = (gh + ts-1) / ts;
const float s = ts*m_cellSize;
glBegin(GL_LINES);
glColor4ub(0,0,0,64);
for (int y = 0; y < th; ++y)
{
for (int x = 0; x < tw; ++x)
{
float fx, fy, fz;
fx = m_bmin[0] + x*s;
fy = m_bmin[1];
fz = m_bmin[2] + y*s;
glVertex3f(fx,fy,fz);
glVertex3f(fx+s,fy,fz);
glVertex3f(fx,fy,fz);
glVertex3f(fx,fy,fz+s);
if (x+1 >= tw)
{
glVertex3f(fx+s,fy,fz);
glVertex3f(fx+s,fy,fz+s);
}
if (y+1 >= th)
{
glVertex3f(fx,fy,fz+s);
glVertex3f(fx+s,fy,fz+s);
}
}
}
glEnd();
if (m_navMesh &&
(m_drawMode == DRAWMODE_NAVMESH ||
m_drawMode == DRAWMODE_NAVMESH_TRANS ||
m_drawMode == DRAWMODE_NAVMESH_BVTREE ||
m_drawMode == DRAWMODE_NAVMESH_INVIS))
{
int flags = NAVMESH_TOOLS;
if (m_drawMode != DRAWMODE_NAVMESH_INVIS)
flags |= NAVMESH_POLYS;
if (m_drawMode == DRAWMODE_NAVMESH_BVTREE)
flags |= NAVMESH_BVTREE;
toolRender(flags);
}
glDepthMask(GL_TRUE);
if (m_tileSet)
{
if (m_drawMode == DRAWMODE_COMPACT)
{
for (int i = 0; i < m_tileSet->width*m_tileSet->height; ++i)
{
if (m_tileSet->tiles[i].chf)
rcDebugDrawCompactHeightfieldSolid(*m_tileSet->tiles[i].chf);
}
}
if (m_drawMode == DRAWMODE_COMPACT_DISTANCE)
{
for (int i = 0; i < m_tileSet->width*m_tileSet->height; ++i)
{
if (m_tileSet->tiles[i].chf)
rcDebugDrawCompactHeightfieldDistance(*m_tileSet->tiles[i].chf);
}
}
if (m_drawMode == DRAWMODE_COMPACT_REGIONS)
{
for (int i = 0; i < m_tileSet->width*m_tileSet->height; ++i)
{
if (m_tileSet->tiles[i].chf)
rcDebugDrawCompactHeightfieldRegions(*m_tileSet->tiles[i].chf);
}
}
if (m_drawMode == DRAWMODE_VOXELS)
{
glEnable(GL_FOG);
for (int i = 0; i < m_tileSet->width*m_tileSet->height; ++i)
{
if (m_tileSet->tiles[i].solid)
rcDebugDrawHeightfieldSolid(*m_tileSet->tiles[i].solid);
}
glDisable(GL_FOG);
}
if (m_drawMode == DRAWMODE_VOXELS_WALKABLE)
{
glEnable(GL_FOG);
for (int i = 0; i < m_tileSet->width*m_tileSet->height; ++i)
{
if (m_tileSet->tiles[i].solid)
rcDebugDrawHeightfieldWalkable(*m_tileSet->tiles[i].solid);
}
glDisable(GL_FOG);
}
if (m_drawMode == DRAWMODE_RAW_CONTOURS)
{
glDepthMask(GL_FALSE);
for (int i = 0; i < m_tileSet->width*m_tileSet->height; ++i)
{
if (m_tileSet->tiles[i].cset)
rcDebugDrawRawContours(*m_tileSet->tiles[i].cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch);
}
glDepthMask(GL_TRUE);
}
if (m_drawMode == DRAWMODE_BOTH_CONTOURS)
{
glDepthMask(GL_FALSE);
for (int i = 0; i < m_tileSet->width*m_tileSet->height; ++i)
{
if (m_tileSet->tiles[i].cset)
{
rcDebugDrawRawContours(*m_tileSet->tiles[i].cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch, 0.5f);
rcDebugDrawContours(*m_tileSet->tiles[i].cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch);
}
}
glDepthMask(GL_TRUE);
}
if (m_drawMode == DRAWMODE_CONTOURS)
{
glDepthMask(GL_FALSE);
for (int i = 0; i < m_tileSet->width*m_tileSet->height; ++i)
{
if (m_tileSet->tiles[i].cset)
rcDebugDrawContours(*m_tileSet->tiles[i].cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch);
}
glDepthMask(GL_TRUE);
}
if (m_drawMode == DRAWMODE_REGION_CONNECTIONS)
{
for (int i = 0; i < m_tileSet->width*m_tileSet->height; ++i)
{
if (m_tileSet->tiles[i].chf)
rcDebugDrawCompactHeightfieldRegions(*m_tileSet->tiles[i].chf);
}
glDepthMask(GL_FALSE);
for (int i = 0; i < m_tileSet->width*m_tileSet->height; ++i)
{
if (m_tileSet->tiles[i].cset)
rcDebugDrawRegionConnections(*m_tileSet->tiles[i].cset, m_cfg.bmin, m_cfg.cs, m_cfg.ch);
}
glDepthMask(GL_TRUE);
}
if (m_polyMesh && m_drawMode == DRAWMODE_POLYMESH)
{
glDepthMask(GL_FALSE);
rcDebugDrawPolyMesh(*m_polyMesh);
glDepthMask(GL_TRUE);
}
}
static const float startCol[4] = { 0.5f, 0.1f, 0.0f, 0.75f };
static const float endCol[4] = { 0.2f, 0.4f, 0.0f, 0.75f };
if (m_sposSet)
drawAgent(m_spos, m_agentRadius, m_agentHeight, m_agentMaxClimb, startCol);
if (m_eposSet)
drawAgent(m_epos, m_agentRadius, m_agentHeight, m_agentMaxClimb, endCol);
}
void BuilderStatMeshTiling::handleMeshChanged(const float* verts, int nverts,
const int* tris, const float* trinorms, int ntris,
const float* bmin, const float* bmax)
{
Builder::handleMeshChanged(verts, nverts, tris, trinorms, ntris, bmin, bmax);
toolCleanup();
toolReset();
}
bool BuilderStatMeshTiling::handleBuild()
{
if (!m_verts || ! m_tris)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Input mesh is not specified.");
return false;
}
cleanup();
toolCleanup();
toolReset();
// Init build configuration from GUI
memset(&m_cfg, 0, sizeof(m_cfg));
m_cfg.cs = m_cellSize;
m_cfg.ch = m_cellHeight;
m_cfg.walkableSlopeAngle = m_agentMaxSlope;
m_cfg.walkableHeight = (int)ceilf(m_agentHeight / m_cfg.ch);
m_cfg.walkableClimb = (int)ceilf(m_agentMaxClimb / m_cfg.ch);
m_cfg.walkableRadius = (int)ceilf(m_agentRadius / m_cfg.cs);
m_cfg.maxEdgeLen = (int)(m_edgeMaxLen / m_cellSize);
m_cfg.maxSimplificationError = m_edgeMaxError;
m_cfg.minRegionSize = (int)rcSqr(m_regionMinSize);
m_cfg.mergeRegionSize = (int)rcSqr(m_regionMergeSize);
m_cfg.maxVertsPerPoly = (int)m_vertsPerPoly;
m_cfg.tileSize = (int)m_tileSize;
m_cfg.borderSize = m_cfg.walkableRadius*2 + 2; // Reserve enough padding.
if (m_cfg.maxVertsPerPoly == DT_VERTS_PER_POLYGON)
m_drawMode = DRAWMODE_NAVMESH;
else
m_drawMode = DRAWMODE_POLYMESH;
// Set the area where the navigation will be build.
// Here the bounds of the input mesh are used, but the
// area could be specified by an user defined box, etc.
vcopy(m_cfg.bmin, m_bmin);
vcopy(m_cfg.bmax, m_bmax);
rcCalcGridSize(m_cfg.bmin, m_cfg.bmax, m_cfg.cs, &m_cfg.width, &m_cfg.height);
// Reset build times gathering.
memset(&m_buildTimes, 0, sizeof(m_buildTimes));
rcSetBuildTimes(&m_buildTimes);
// Start the build process.
rcTimeVal totStartTime = rcGetPerformanceTimer();
// Calculate the number of tiles in the output and initialize tiles.
m_tileSet = new TileSet;
if (!m_tileSet)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: Out of memory 'tileSet'.");
return false;
}
vcopy(m_tileSet->bmin, m_cfg.bmin);
vcopy(m_tileSet->bmax, m_cfg.bmax);
m_tileSet->cs = m_cfg.cs;
m_tileSet->ch = m_cfg.ch;
m_tileSet->width = (m_cfg.width + m_cfg.tileSize-1) / m_cfg.tileSize;
m_tileSet->height = (m_cfg.height + m_cfg.tileSize-1) / m_cfg.tileSize;
m_tileSet->tiles = new Tile[m_tileSet->height * m_tileSet->width];
if (!m_tileSet->tiles)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: Out of memory 'tileSet->tiles' (%d).", m_tileSet->height * m_tileSet->width);
return false;
}
// Build chunky trimesh for local polygon queries.
rcTimeVal chunkyStartTime = rcGetPerformanceTimer();
m_chunkyMesh = new rcChunkyTriMesh;
if (!m_chunkyMesh)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: Out of memory 'm_chunkyMesh'.");
return false;
}
if (!rcCreateChunkyTriMesh(m_verts, m_tris, m_ntris, 256, m_chunkyMesh))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: Could not build chunky mesh.");
return false;
}
rcTimeVal chunkyEndTime = rcGetPerformanceTimer();
if (rcGetLog())
{
rcGetLog()->log(RC_LOG_PROGRESS, "Building navigation:");
rcGetLog()->log(RC_LOG_PROGRESS, " - %d x %d cells", m_cfg.width, m_cfg.height);
rcGetLog()->log(RC_LOG_PROGRESS, " - %d x %d tiles", m_tileSet->width, m_tileSet->height);
rcGetLog()->log(RC_LOG_PROGRESS, " - %.1f verts, %.1f tris", m_nverts/1000.0f, m_ntris/1000.0f);
}
// Initialize per tile config.
rcConfig tileCfg;
memcpy(&tileCfg, &m_cfg, sizeof(rcConfig));
tileCfg.width = m_cfg.tileSize + m_cfg.borderSize*2;
tileCfg.height = m_cfg.tileSize + m_cfg.borderSize*2;
// Allocate array that can hold triangle flags for all geom chunks.
unsigned char* triangleFlags = new unsigned char[m_chunkyMesh->maxTrisPerChunk];
if (!triangleFlags)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: Out of memory 'triangleFlags' (%d).", m_chunkyMesh->maxTrisPerChunk);
return false;
}
rcHeightfield* solid = 0;
rcCompactHeightfield* chf = 0;
rcContourSet* cset = 0;
for (int y = 0; y < m_tileSet->height; ++y)
{
for (int x = 0; x < m_tileSet->width; ++x)
{
rcTimeVal startTime = rcGetPerformanceTimer();
Tile& tile = m_tileSet->tiles[x + y*m_tileSet->width];
// Calculate the per tile bounding box.
tileCfg.bmin[0] = m_cfg.bmin[0] + (x*m_cfg.tileSize - m_cfg.borderSize)*m_cfg.cs;
tileCfg.bmin[2] = m_cfg.bmin[2] + (y*m_cfg.tileSize - m_cfg.borderSize)*m_cfg.cs;
tileCfg.bmax[0] = m_cfg.bmin[0] + ((x+1)*m_cfg.tileSize + m_cfg.borderSize)*m_cfg.cs;
tileCfg.bmax[2] = m_cfg.bmin[2] + ((y+1)*m_cfg.tileSize + m_cfg.borderSize)*m_cfg.cs;
delete solid;
delete chf;
solid = 0;
chf = 0;
float tbmin[2], tbmax[2];
tbmin[0] = tileCfg.bmin[0];
tbmin[1] = tileCfg.bmin[2];
tbmax[0] = tileCfg.bmax[0];
tbmax[1] = tileCfg.bmax[2];
int cid[256];// TODO: Make grow when returning too many items.
const int ncid = rcGetChunksInRect(m_chunkyMesh, tbmin, tbmax, cid, 256);
if (!ncid)
continue;
solid = new rcHeightfield;
if (!solid)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: [%d,%d] Out of memory 'solid'.", x, y);
continue;
}
if (!rcCreateHeightfield(*solid, tileCfg.width, tileCfg.height, tileCfg.bmin, tileCfg.bmax, tileCfg.cs, tileCfg.ch))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: [%d,%d] Could not create solid heightfield.", x, y);
continue;
}
for (int i = 0; i < ncid; ++i)
{
const rcChunkyTriMeshNode& node = m_chunkyMesh->nodes[cid[i]];
const int* tris = &m_chunkyMesh->tris[node.i*3];
const int ntris = node.n;
memset(triangleFlags, 0, ntris*sizeof(unsigned char));
rcMarkWalkableTriangles(tileCfg.walkableSlopeAngle,
m_verts, m_nverts, tris, ntris, triangleFlags);
rcRasterizeTriangles(m_verts, m_nverts, tris, triangleFlags, ntris, *solid);
}
rcFilterLedgeSpans(tileCfg.walkableHeight, tileCfg.walkableClimb, *solid);
rcFilterWalkableLowHeightSpans(tileCfg.walkableHeight, *solid);
chf = new rcCompactHeightfield;
if (!chf)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: [%d,%d] Out of memory 'chf'.", x, y);
continue;
}
if (!rcBuildCompactHeightfield(tileCfg.walkableHeight, tileCfg.walkableClimb,
RC_WALKABLE, *solid, *chf))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: [%d,%d] Could not build compact data.", x, y);
continue;
}
if (!rcBuildDistanceField(*chf))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: [%d,%d] Could not build distance fields.", x, y);
continue;
}
if (!rcBuildRegions(*chf, tileCfg.walkableRadius, tileCfg.borderSize, tileCfg.minRegionSize, tileCfg.mergeRegionSize))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: [%d,%d] Could not build regions.", x, y);
continue;
}
cset = new rcContourSet;
if (!cset)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: [%d,%d] Out of memory 'cset'.", x, y);
continue;
}
if (!rcBuildContours(*chf, tileCfg.maxSimplificationError, tileCfg.maxEdgeLen, *cset))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: [%d,%d] Could not create contours.", x, y);
continue;
}
if (m_keepInterResults)
{
tile.solid = solid;
solid = 0;
tile.chf = chf;
chf = 0;
}
if (!cset->nconts)
{
delete cset;
cset = 0;
continue;
}
tile.cset = cset;
// Offset the vertices in the cset.
rcTranslateContours(tile.cset, x*tileCfg.tileSize - tileCfg.borderSize, 0, y*tileCfg.tileSize - tileCfg.borderSize);
rcTimeVal endTime = rcGetPerformanceTimer();
tile.buildTime += rcGetDeltaTimeUsec(startTime, endTime);
}
}
delete [] triangleFlags;
delete solid;
delete chf;
// Make sure that the vertices along the tile edges match,
// so that they can be later properly stitched together.
for (int y = 0; y < m_tileSet->height; ++y)
{
for (int x = 0; x < m_tileSet->width; ++x)
{
rcTimeVal startTime = rcGetPerformanceTimer();
if ((x+1) < m_tileSet->width)
{
if (!rcFixupAdjacentContours(m_tileSet->tiles[x + y*m_tileSet->width].cset,
m_tileSet->tiles[x+1 + y*m_tileSet->width].cset,
m_cfg.walkableClimb, (x+1)*m_cfg.tileSize, -1))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: [%d,%d] Could not fixup x+1.", x, y);
return false;
}
}
if ((y+1) < m_tileSet->height)
{
if (!rcFixupAdjacentContours(m_tileSet->tiles[x + y*m_tileSet->width].cset,
m_tileSet->tiles[x + (y+1)*m_tileSet->width].cset,
m_cfg.walkableClimb, -1, (y+1)*m_cfg.tileSize))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: [%d,%d] Could not fixup y+1.", x, y);
return false;
}
}
rcTimeVal endTime = rcGetPerformanceTimer();
m_tileSet->tiles[x+y*m_tileSet->width].buildTime += rcGetDeltaTimeUsec(startTime, endTime);
}
}
// Combine contours.
rcContourSet combSet;
combSet.nconts = 0;
for (int y = 0; y < m_tileSet->height; ++y)
{
for (int x = 0; x < m_tileSet->width; ++x)
{
Tile& tile = m_tileSet->tiles[x + y*m_tileSet->width];
if (!tile.cset) continue;
combSet.nconts += tile.cset->nconts;
}
}
combSet.conts = new rcContour[combSet.nconts];
if (!combSet.conts)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: Out of memory 'combSet.conts' (%d).", combSet.nconts);
return false;
}
int n = 0;
for (int y = 0; y < m_tileSet->height; ++y)
{
for (int x = 0; x < m_tileSet->width; ++x)
{
Tile& tile = m_tileSet->tiles[x + y*m_tileSet->width];
if (!tile.cset) continue;
for (int i = 0; i < tile.cset->nconts; ++i)
{
combSet.conts[n].verts = tile.cset->conts[i].verts;
combSet.conts[n].nverts = tile.cset->conts[i].nverts;
combSet.conts[n].reg = tile.cset->conts[i].reg;
n++;
}
}
}
m_polyMesh = new rcPolyMesh;
if (!m_polyMesh)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'polyMesh'.");
return false;
}
bool polyRes = rcBuildPolyMesh(combSet, m_cfg.bmin, m_cfg.bmax, m_cfg.cs, m_cfg.ch, m_cfg.maxVertsPerPoly, *m_polyMesh);
// Remove vertex binding to avoid double deletion.
for (int i = 0; i < combSet.nconts; ++i)
{
combSet.conts[i].verts = 0;
combSet.conts[i].nverts = 0;
}
if (!polyRes)
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "buildTiledNavigation: Could not triangulate contours.");
return false;
}
if (!m_keepInterResults)
{
for (int y = 0; y < m_tileSet->height; ++y)
{
for (int x = 0; x < m_tileSet->width; ++x)
{
Tile& tile = m_tileSet->tiles[x + y*m_tileSet->width];
delete tile.cset;
tile.cset = 0;
}
}
}
if (m_cfg.maxVertsPerPoly == DT_VERTS_PER_POLYGON)
{
unsigned char* navData = 0;
int navDataSize = 0;
if (!dtCreateNavMeshData(m_polyMesh->verts, m_polyMesh->nverts,
m_polyMesh->polys, m_polyMesh->npolys, m_polyMesh->nvp,
m_cfg.bmin, m_cfg.bmax, m_cfg.cs, m_cfg.ch, &navData, &navDataSize))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "Could not build Detour navmesh.");
return false;
}
m_navMesh = new dtStatNavMesh;
if (!m_navMesh)
{
delete [] navData;
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "Could not create Detour navmesh");
return false;
}
if (!m_navMesh->init(navData, navDataSize, true))
{
if (rcGetLog())
rcGetLog()->log(RC_LOG_ERROR, "Could not init Detour navmesh");
return false;
}
}
rcTimeVal totEndTime = rcGetPerformanceTimer();
if (rcGetLog())
{
const float pc = 100.0f / rcGetDeltaTimeUsec(totStartTime, totEndTime);
rcGetLog()->log(RC_LOG_PROGRESS, "Chunky Mesh: %.1fms (%.1f%%)", rcGetDeltaTimeUsec(chunkyStartTime, chunkyEndTime)/1000.0f, rcGetDeltaTimeUsec(chunkyStartTime, chunkyEndTime)*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Rasterize: %.1fms (%.1f%%)", m_buildTimes.rasterizeTriangles/1000.0f, m_buildTimes.rasterizeTriangles*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Build Compact: %.1fms (%.1f%%)", m_buildTimes.buildCompact/1000.0f, m_buildTimes.buildCompact*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Filter Border: %.1fms (%.1f%%)", m_buildTimes.filterBorder/1000.0f, m_buildTimes.filterBorder*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Filter Walkable: %.1fms (%.1f%%)", m_buildTimes.filterWalkable/1000.0f, m_buildTimes.filterWalkable*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Filter Reachable: %.1fms (%.1f%%)", m_buildTimes.filterMarkReachable/1000.0f, m_buildTimes.filterMarkReachable*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Build Distancefield: %.1fms (%.1f%%)", m_buildTimes.buildDistanceField/1000.0f, m_buildTimes.buildDistanceField*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - distance: %.1fms (%.1f%%)", m_buildTimes.buildDistanceFieldDist/1000.0f, m_buildTimes.buildDistanceFieldDist*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - blur: %.1fms (%.1f%%)", m_buildTimes.buildDistanceFieldBlur/1000.0f, m_buildTimes.buildDistanceFieldBlur*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Build Regions: %.1fms (%.1f%%)", m_buildTimes.buildRegions/1000.0f, m_buildTimes.buildRegions*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - watershed: %.1fms (%.1f%%)", m_buildTimes.buildRegionsReg/1000.0f, m_buildTimes.buildRegionsReg*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - expand: %.1fms (%.1f%%)", m_buildTimes.buildRegionsExp/1000.0f, m_buildTimes.buildRegionsExp*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - find catchment basins: %.1fms (%.1f%%)", m_buildTimes.buildRegionsFlood/1000.0f, m_buildTimes.buildRegionsFlood*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - filter: %.1fms (%.1f%%)", m_buildTimes.buildRegionsFilter/1000.0f, m_buildTimes.buildRegionsFilter*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Build Contours: %.1fms (%.1f%%)", m_buildTimes.buildContours/1000.0f, m_buildTimes.buildContours*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - trace: %.1fms (%.1f%%)", m_buildTimes.buildContoursTrace/1000.0f, m_buildTimes.buildContoursTrace*pc);
rcGetLog()->log(RC_LOG_PROGRESS, " - simplify: %.1fms (%.1f%%)", m_buildTimes.buildContoursSimplify/1000.0f, m_buildTimes.buildContoursSimplify*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Fixup contours: %.1fms (%.1f%%)", m_buildTimes.fixupContours/1000.0f, m_buildTimes.fixupContours*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Build Polymesh: %.1fms (%.1f%%)", m_buildTimes.buildPolymesh/1000.0f, m_buildTimes.buildPolymesh*pc);
rcGetLog()->log(RC_LOG_PROGRESS, "Polymesh: Verts:%d Polys:%d", m_polyMesh->nverts, m_polyMesh->npolys);
rcGetLog()->log(RC_LOG_PROGRESS, "TOTAL: %.1fms", rcGetDeltaTimeUsec(totStartTime, totEndTime)/1000.0f);
}
toolRecalc();
return true;
}

File diff suppressed because it is too large Load Diff

View File

@ -425,7 +425,8 @@ void addGfxCmdText(int x, int y, int dir, const char* text, unsigned int color)
struct GuiState
{
GuiState() :
mbutPressed(false), mbutReleased(false), mbut(false), mx(-1), my(-1),
leftPressed(false), leftReleased(false), left(false), mx(-1), my(-1),
upPressed(false), downPressed(false), up(false), down(false),
isHot(false), isActive(false), wentActive(false),
dragX(0), dragY(0), dragOrig(0),
widgetX(0), widgetY(0), widgetW(100),
@ -433,8 +434,9 @@ struct GuiState
{
}
bool mbutPressed, mbutReleased;
bool mbut;
bool left, up, down;
bool upPressed, downPressed;
bool leftPressed, leftReleased;
int mx,my;
unsigned int active;
unsigned int hot;
@ -471,8 +473,10 @@ inline bool inRect(int x, int y, int w, int h)
void clearInput()
{
g_state.mbutPressed = false;
g_state.mbutReleased = false;
g_state.leftPressed = false;
g_state.leftReleased = false;
g_state.upPressed = false;
g_state.downPressed = false;
}
void clearActive(void)
@ -502,7 +506,7 @@ bool buttonLogic(unsigned int id, bool over)
{
if (over)
setHot(id);
if (isHot(id) && g_state.mbutPressed)
if (isHot(id) && g_state.leftPressed)
setActive(id);
}
@ -512,7 +516,7 @@ bool buttonLogic(unsigned int id, bool over)
g_state.isActive = true;
if (over)
setHot(id);
if (g_state.mbutReleased)
if (g_state.leftReleased)
{
if (isHot(id))
res = true;
@ -526,24 +530,27 @@ bool buttonLogic(unsigned int id, bool over)
return res;
}
static void updateInput()
static void updateInput(int mx, int my, unsigned char mbut)
{
int mx, my;
Uint8 state = SDL_GetMouseState(&mx, &my);
bool mbut = (state & SDL_BUTTON_LMASK) != 0;
SDL_Surface* screen = SDL_GetVideoSurface();
my = screen->h-1 - my;
bool left = (mbut & IMGUI_MBUT_LEFT) != 0;
bool up = (mbut & IMGUI_MBUT_UP) != 0;
bool down = (mbut & IMGUI_MBUT_DOWN) != 0;
g_state.mx = mx;
g_state.my = my;
g_state.mbutPressed = !g_state.mbut && mbut;
g_state.mbutReleased = g_state.mbut && !mbut;
g_state.mbut = mbut;
g_state.leftPressed = !g_state.left && left;
g_state.leftReleased = g_state.left && !left;
g_state.left = left;
g_state.upPressed = !g_state.up && up;
g_state.downPressed = !g_state.down && down;
g_state.up = up;
g_state.down = down;
}
void imguiBeginFrame()
void imguiBeginFrame(int mx, int my, unsigned char mbut)
{
updateInput();
updateInput(mx,my,mbut);
g_state.hot = g_state.hotToBe;
g_state.hotToBe = 0;
@ -588,7 +595,8 @@ static int g_scrollAreaTop = 0;
static int* g_scrollVal = 0;
static int g_focusTop = 0;
static int g_focusBottom = 0;
static unsigned int g_scrollId = 0;
static unsigned int g_scrollId = 0;
static bool g_insideScrollArea = false;
bool imguiBeginScrollArea(unsigned int id, const char* name, int x, int y, int w, int h, int* scroll)
{
@ -613,7 +621,9 @@ bool imguiBeginScrollArea(unsigned int id, const char* name, int x, int y, int w
addGfxCmdScissor(x+SCROLL_AREA_PADDING, y+SCROLL_AREA_PADDING, w-SCROLL_AREA_PADDING*4, h-AREA_HEADER-SCROLL_AREA_PADDING);
return inRect(x, y, w, h);
g_insideScrollArea = inRect(x, y, w, h);
return g_insideScrollArea;
}
void imguiEndScrollArea()
@ -673,6 +683,22 @@ void imguiEndScrollArea()
addGfxCmdRoundedRect(hx, hy, hw, hh, w/2-1, RGBA(255,196,0,196));
else
addGfxCmdRoundedRect(hx, hy, hw, hh, w/2-1, isHot(hid) ? RGBA(255,196,0,96) : RGBA(255,255,255,64));
// Handle mouse scrolling.
if (g_insideScrollArea) // && !anyActive())
{
if (g_state.upPressed)
{
*g_scrollVal -= 20;
if (*g_scrollVal < 0) *g_scrollVal = 0;
}
else if (g_state.downPressed)
{
*g_scrollVal += 20;
if (*g_scrollVal > (sh - h)) *g_scrollVal = (sh - h);
}
}
}
}

665
RecastDemo/Source/main.cpp Normal file
View File

@ -0,0 +1,665 @@
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#ifdef WIN32
# include <io.h>
#else
# include <dirent.h>
#endif
#include "SDL.h"
#include "SDL_Opengl.h"
#include "GLFont.h"
#include "imgui.h"
#include "Recast.h"
#include "RecastDebugDraw.h"
#include "MeshLoaderObj.h"
#include "BuilderStatMeshSimple.h"
#include "BuilderStatMeshTiling.h"
GLFont g_font;
void drawText(int x, int y, int dir, const char* text, unsigned int col)
{
if (dir < 0)
g_font.drawText((float)x - g_font.getTextLength(text), (float)y, text, col);
else
g_font.drawText((float)x, (float)y, text, col);
}
struct FileList
{
static const int MAX_FILES = 256;
inline FileList() : size(0) {}
inline ~FileList()
{
clear();
}
void clear()
{
for (int i = 0; i < size; ++i)
delete [] files[i];
size = 0;
}
void add(const char* path)
{
if (size >= MAX_FILES)
return;
int n = strlen(path);
files[size] = new char[n+1];
strcpy(files[size], path);
size++;
}
static int cmp(const void* a, const void* b)
{
return strcmp(*(const char**)a, *(const char**)b);
}
void sort()
{
if (size > 1)
qsort(files, size, sizeof(char*), cmp);
}
char* files[MAX_FILES];
int size;
};
static void scanDirectory(const char* path, const char* ext, FileList& list)
{
list.clear();
#ifdef WIN32
_finddata_t dir;
char pathWithExt[MAX_PATH];
long fh;
strcpy(pathWithExt, path);
strcat(pathWithExt, "/*");
strcat(pathWithExt, ext);
fh = _findfirst(pathWithExt, &dir);
if (fh == -1L)
return;
do
{
list.add(dir.name);
}
while (_findnext(fh, &dir) == 0);
_findclose(fh);
#else
dirent* current = 0;
DIR* dp = opendir(path);
if (!dp)
return;
while ((current = readdir(dp)) != 0)
{
int len = strlen(current->d_name);
if (len > 4 && strncmp(current->d_name+len-4, ext, 4) == 0)
{
list.add(current->d_name);
}
}
closedir(dp);
#endif
list.sort();
}
bool intersectSegmentTriangle(const float* sp, const float* sq,
const float* a, const float* b, const float* c,
float &t)
{
float v, w;
float ab[3], ac[3], qp[3], ap[3], norm[3], e[3];
vsub(ab, b, a);
vsub(ac, c, a);
vsub(qp, sp, sq);
// Compute triangle normal. Can be precalculated or cached if
// intersecting multiple segments against the same triangle
vcross(norm, ab, ac);
// Compute denominator d. If d <= 0, segment is parallel to or points
// away from triangle, so exit early
float d = vdot(qp, norm);
if (d <= 0.0f) return false;
// Compute intersection t value of pq with plane of triangle. A ray
// intersects iff 0 <= t. Segment intersects iff 0 <= t <= 1. Delay
// dividing by d until intersection has been found to pierce triangle
vsub(ap, sp, a);
t = vdot(ap, norm);
if (t < 0.0f) return false;
if (t > d) return false; // For segment; exclude this code line for a ray test
// Compute barycentric coordinate components and test if within bounds
vcross(e, qp, ap);
v = vdot(ac, e);
if (v < 0.0f || v > d) return false;
w = -vdot(ab, e);
if (w < 0.0f || v + w > d) return false;
// Segment/ray intersects triangle. Perform delayed division
t /= d;
return true;
}
static bool raycast(rcMeshLoaderObj& mesh, float* src, float* dst, float& tmin)
{
float dir[3];
vsub(dir, dst, src);
int nt = mesh.getTriCount();
const float* verts = mesh.getVerts();
const float* normals = mesh.getNormals();
const int* tris = mesh.getTris();
tmin = 1.0f;
bool hit = false;
for (int i = 0; i < nt*3; i += 3)
{
const float* n = &normals[i];
if (vdot(dir, n) > 0)
continue;
float t = 1;
if (intersectSegmentTriangle(src, dst,
&verts[tris[i]*3],
&verts[tris[i+1]*3],
&verts[tris[i+2]*3], t))
{
if (t < tmin)
tmin = t;
hit = true;
}
}
return hit;
}
int main(int argc, char *argv[])
{
// Init SDL
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
printf("Could not initialise SDL\n");
return -1;
}
// Init OpenGL
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
int width = 1200;
int height = 700;
SDL_Surface* screen = SDL_SetVideoMode(width, height, 0, SDL_OPENGL);
if (!screen)
{
printf("Could not initialise SDL opengl\n");
return -1;
}
SDL_WM_SetCaption("Recast Demo", 0);
if(!g_font.create("font.cfnt"))
{
printf("Could not load font.\n");
SDL_Quit();
return -1;
}
float t = 0.0f;
Uint32 lastTime = SDL_GetTicks();
int mx = 0, my = 0;
float rx = 45;
float ry = -45;
float moveW = 0, moveS = 0, moveA = 0, moveD = 0;
float camx = 0, camy = 0, camz = 0, camr=10;
float origrx, origry;
int origx, origy;
bool rotate = false;
float rays[3], raye[3];
bool mouseOverMenu = false;
bool showLog = false;
bool showDebugMode = true;
bool showTools = true;
bool showLevels = false;
bool showBuilder = false;
int propScroll = 0;
int logScroll = 0;
int toolsScroll = 0;
int debugScroll = 0;
char builderName[64] = "Choose Builder...";
FileList meshFiles;
char meshName[128] = "Choose Mesh...";
rcMeshLoaderObj* mesh = 0;
float meshBMin[3], meshBMax[3];
Builder* builder = 0;
rcLog log;
log.clear();
rcSetLog(&log);
glEnable(GL_CULL_FACE);
float fogCol[4] = { 0.32f,0.25f,0.25f,1 };
glEnable(GL_FOG);
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogf(GL_FOG_START, 0);
glFogf(GL_FOG_END, 10);
glFogfv(GL_FOG_COLOR, fogCol);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
bool done = false;
while(!done)
{
// Handle input events.
unsigned char mbut = 0;
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
// Handle any key presses here.
if (event.key.keysym.sym == SDLK_ESCAPE)
{
done = true;
}
break;
case SDL_MOUSEBUTTONDOWN:
// Handle mouse clicks here.
if (!mouseOverMenu)
{
if (event.button.button == SDL_BUTTON_RIGHT)
{
// Rotate view
rotate = true;
origx = mx;
origy = my;
origrx = rx;
origry = ry;
}
else if (event.button.button == SDL_BUTTON_LEFT)
{
// Hit test mesh.
if (mesh && builder)
{
// Hit test mesh.
float t;
if (raycast(*mesh, rays, raye, t))
{
float pos[3];
pos[0] = rays[0] + (raye[0] - rays[0])*t;
pos[1] = rays[1] + (raye[1] - rays[1])*t;
pos[2] = rays[2] + (raye[2] - rays[2])*t;
if (SDL_GetModState() & KMOD_SHIFT)
builder->setToolStartPos(pos);
else
builder->setToolEndPos(pos);
}
}
}
}
if (event.button.button == SDL_BUTTON_WHEELUP)
mbut |= IMGUI_MBUT_UP;
if (event.button.button == SDL_BUTTON_WHEELDOWN)
mbut |= IMGUI_MBUT_DOWN;
break;
case SDL_MOUSEBUTTONUP:
// Handle mouse clicks here.
if(event.button.button == SDL_BUTTON_RIGHT)
{
rotate = false;
}
break;
case SDL_MOUSEMOTION:
mx = event.motion.x;
my = height-1 - event.motion.y;
if (rotate)
{
int dx = mx - origx;
int dy = my - origy;
rx = origrx - dy*0.25f;
ry = origry + dx*0.25f;
}
break;
case SDL_QUIT:
done = true;
break;
default:
break;
}
}
if (SDL_GetMouseState(0,0) & SDL_BUTTON_LMASK)
mbut |= IMGUI_MBUT_LEFT;
if (SDL_GetMouseState(0,0) & SDL_BUTTON_RMASK)
mbut |= IMGUI_MBUT_RIGHT;
Uint32 time = SDL_GetTicks();
float dt = (time - lastTime) / 1000.0f;
lastTime = time;
t += dt;
// Update and render
glViewport(0, 0, width, height);
glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_TEXTURE_2D);
// Render 3d
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50.0f, (float)width/(float)height, 1.0f, camr);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(rx,1,0,0);
glRotatef(ry,0,1,0);
glTranslatef(-camx, -camy, -camz);
// Get hit ray position and direction.
GLdouble proj[16];
GLdouble model[16];
GLint view[4];
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetIntegerv(GL_VIEWPORT, view);
GLdouble x, y, z;
gluUnProject(mx, my, 0.0f, model, proj, view, &x, &y, &z);
rays[0] = (float)x; rays[1] = (float)y; rays[2] = (float)z;
gluUnProject(mx, my, 1.0f, model, proj, view, &x, &y, &z);
raye[0] = (float)x; raye[1] = (float)y; raye[2] = (float)z;
// Handle keyboard movement.
Uint8* keystate = SDL_GetKeyState(NULL);
moveW = rcClamp(moveW + dt * 4 * (keystate[SDLK_w] ? 1 : -1), 0.0f, 1.0f);
moveS = rcClamp(moveS + dt * 4 * (keystate[SDLK_s] ? 1 : -1), 0.0f, 1.0f);
moveA = rcClamp(moveA + dt * 4 * (keystate[SDLK_a] ? 1 : -1), 0.0f, 1.0f);
moveD = rcClamp(moveD + dt * 4 * (keystate[SDLK_d] ? 1 : -1), 0.0f, 1.0f);
float keybSpeed = 22.0f;
if (SDL_GetModState() & KMOD_SHIFT)
keybSpeed *= 4.0f;
float movex = (moveD - moveA) * keybSpeed * dt;
float movey = (moveS - moveW) * keybSpeed * dt;
camx += movex * (float)model[0];
camy += movex * (float)model[4];
camz += movex * (float)model[8];
camx += movey * (float)model[2];
camy += movey * (float)model[6];
camz += movey * (float)model[10];
glEnable(GL_FOG);
if (builder)
builder->handleRender();
glDisable(GL_FOG);
// Render GUI
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
imguiBeginFrame(mx,my,mbut);
mouseOverMenu = false;
int propDiv = showDebugMode ? height*0.6f : height;
if (imguiBeginScrollArea(GENID, "Properties",
width-250-10, 10+height-propDiv, 250, propDiv-20, &propScroll))
mouseOverMenu = true;
if (imguiCheck(GENID, "Show Log", showLog))
showLog = !showLog;
if (imguiCheck(GENID, "Show Tools", showTools))
showTools = !showTools;
if (imguiCheck(GENID, "Show Debug Mode", showDebugMode))
showDebugMode = !showDebugMode;
imguiSeparator();
imguiLabel(GENID, "Builder");
if (imguiButton(GENID, builderName))
{
if (showBuilder)
{
showBuilder = false;
}
else
{
showBuilder = true;
showLevels = false;
}
}
if (builder)
{
imguiSeparator();
imguiLabel(GENID, "Input Mesh");
if (imguiButton(GENID, meshName))
{
if (showLevels)
{
showLevels = false;
}
else
{
showBuilder = false;
showLevels = true;
scanDirectory("meshes", ".obj", meshFiles);
}
}
if (mesh)
{
char text[64];
snprintf(text, 64, "Verts: %.1fk Tris: %.1fk", mesh->getVertCount()/1000.0f, mesh->getTriCount()/1000.0f);
imguiValue(GENID, text);
}
imguiSeparator();
}
if (mesh && builder)
{
builder->handleSettings();
if (imguiButton(GENID, "Build"))
{
log.clear();
if (!builder->handleBuild())
{
showLog = true;
logScroll = 0;
}
}
imguiSeparator();
}
imguiEndScrollArea();
if (showDebugMode)
{
if (imguiBeginScrollArea(GENID, "Debug Mode",
width-250-10, 10,
250, height-propDiv-10, &debugScroll))
mouseOverMenu = true;
if (builder)
builder->handleDebugMode();
imguiEndScrollArea();
}
// Builder selection dialog.
if (showBuilder)
{
static int levelScroll = 0;
if (imguiBeginScrollArea(GENID, "Choose Level", width-10-250-10-200, height-10-250, 200, 250, &levelScroll))
mouseOverMenu = true;
Builder* newBuilder = 0;
if (imguiItem(GENID, "Simple Static Mesh"))
{
newBuilder = new BuilderStatMeshSimple();
if (newBuilder) strcpy(builderName, "Simple Static Mesh");
}
if (imguiItem(GENID, "Tiling Static Mesh"))
{
newBuilder = new BuilderStatMeshTiling();
if (newBuilder) strcpy(builderName, "Tiling Static Mesh");
}
if (newBuilder)
{
delete builder;
builder = newBuilder;
if (mesh && builder)
{
builder->handleMeshChanged(mesh->getVerts(), mesh->getVertCount(),
mesh->getTris(), mesh->getNormals(), mesh->getTriCount(),
meshBMin, meshBMax);
}
showBuilder = false;
}
imguiEndScrollArea();
}
// Level selection dialog.
if (showLevels)
{
static int levelScroll = 0;
if (imguiBeginScrollArea(GENID, "Choose Level", width-10-250-10-200, height-10-250, 200, 250, &levelScroll))
mouseOverMenu = true;
int levelToLoad = -1;
for (int i = 0; i < meshFiles.size; ++i)
{
if (imguiItem(GENID1(i), meshFiles.files[i]))
levelToLoad = i;
}
if (levelToLoad != -1)
{
strncpy(meshName, meshFiles.files[levelToLoad], sizeof(meshName));
meshName[sizeof(meshName)-1] = '\0';
showLevels = false;
delete mesh;
mesh = 0;
char path[256];
strcpy(path, "meshes/");
strcat(path, meshName);
mesh = new rcMeshLoaderObj;
if (!mesh || !mesh->load(path))
{
delete mesh;
mesh = 0;
}
if (mesh)
rcCalcBounds(mesh->getVerts(), mesh->getVertCount(), meshBMin, meshBMax);
if (builder)
{
builder->handleMeshChanged(mesh->getVerts(), mesh->getVertCount(),
mesh->getTris(), mesh->getNormals(), mesh->getTriCount(),
meshBMin, meshBMax);
}
// Reset camera and fog to match the mesh bounds.
camr = sqrtf(rcSqr(meshBMax[0]-meshBMin[0]) +
rcSqr(meshBMax[1]-meshBMin[1]) +
rcSqr(meshBMax[2]-meshBMin[2])) / 2;
camx = (meshBMax[0] + meshBMin[0]) / 2 + camr;
camy = (meshBMax[1] + meshBMin[1]) / 2 + camr;
camz = (meshBMax[2] + meshBMin[2]) / 2 + camr;
camr *= 3;
rx = 45;
ry = -45;
glFogf(GL_FOG_START, camr*0.2f);
glFogf(GL_FOG_END, camr*1.25f);
}
imguiEndScrollArea();
}
// Log
if (showLog)
{
if (imguiBeginScrollArea(GENID, "Log", 10, 10, width - 300, 200, &logScroll))
mouseOverMenu = true;
for (int i = 0; i < log.getMessageCount(); ++i)
imguiLabel(GENID1(i), log.getMessageText(i));
imguiEndScrollArea();
}
// Tools
if (showTools && mesh && builder)
{
if (imguiBeginScrollArea(GENID, "Tools", 10, height - 10 - 200, 150, 200, &toolsScroll))
mouseOverMenu = true;
builder->handleTools();
imguiEndScrollArea();
}
// Help text.
const char msg[] = "W/S/A/D: Move RMB: Rotate LMB: Place Start LMB+SHIFT: Place End";
const float len = g_font.getTextLength(msg);
g_font.drawText(width/2-len/2, (float)height-20.0f, msg, GLFont::RGBA(255,255,255,128));
glDisable(GL_TEXTURE_2D);
imguiEndFrame();
imguiRender(&drawText);
glEnable(GL_DEPTH_TEST);
SDL_GL_SwapBuffers();
}
SDL_Quit();
delete builder;
delete mesh;
return 0;
}