| 1 | // |
| 2 | // Copyright (c) 2009-2010 Mikko Mononen [email protected] |
| 3 | // |
| 4 | // This software is provided 'as-is', without any express or implied |
| 5 | // warranty. In no event will the authors be held liable for any damages |
| 6 | // arising from the use of this software. |
| 7 | // Permission is granted to anyone to use this software for any purpose, |
| 8 | // including commercial applications, and to alter it and redistribute it |
| 9 | // freely, subject to the following restrictions: |
| 10 | // 1. The origin of this software must not be misrepresented; you must not |
| 11 | // claim that you wrote the original software. If you use this software |
| 12 | // in a product, an acknowledgment in the product documentation would be |
| 13 | // appreciated but is not required. |
| 14 | // 2. Altered source versions must be plainly marked as such, and must not be |
| 15 | // misrepresented as being the original software. |
| 16 | // 3. This notice may not be removed or altered from any source distribution. |
| 17 | // |
| 18 | |
| 19 | #include <math.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <float.h> |
| 23 | #include "SDL.h" |
| 24 | #include "SDL_opengl.h" |
| 25 | #include "imgui.h" |
| 26 | #include "ConvexVolumeTool.h" |
| 27 | #include "InputGeom.h" |
| 28 | #include "Sample.h" |
| 29 | #include "Recast.h" |
| 30 | #include "RecastDebugDraw.h" |
| 31 | #include "DetourDebugDraw.h" |
| 32 | |
| 33 | #ifdef WIN32 |
| 34 | # define snprintf _snprintf |
| 35 | #endif |
| 36 | |
| 37 | // Quick and dirty convex hull. |
| 38 | |
| 39 | // Returns true if 'c' is left of line 'a'-'b'. |
| 40 | inline bool left(const float* a, const float* b, const float* c) |
| 41 | { |
| 42 | const float u1 = b[0] - a[0]; |
| 43 | const float v1 = b[2] - a[2]; |
| 44 | const float u2 = c[0] - a[0]; |
| 45 | const float v2 = c[2] - a[2]; |
| 46 | return u1 * v2 - v1 * u2 < 0; |
| 47 | } |
| 48 | |
| 49 | // Returns true if 'a' is more lower-left than 'b'. |
| 50 | inline bool cmppt(const float* a, const float* b) |
| 51 | { |
| 52 | if (a[0] < b[0]) return true; |
| 53 | if (a[0] > b[0]) return false; |
| 54 | if (a[2] < b[2]) return true; |
| 55 | if (a[2] > b[2]) return false; |
| 56 | return false; |
| 57 | } |
| 58 | // Calculates convex hull on xz-plane of points on 'pts', |
| 59 | // stores the indices of the resulting hull in 'out' and |
| 60 | // returns number of points on hull. |
| 61 | static int convexhull(const float* pts, int npts, int* out) |
| 62 | { |
| 63 | // Find lower-leftmost point. |
| 64 | int hull = 0; |
| 65 | for (int i = 1; i < npts; ++i) |
| 66 | if (cmppt(&pts[i*3], &pts[hull*3])) |
| 67 | hull = i; |
| 68 | // Gift wrap hull. |
| 69 | int endpt = 0; |
| 70 | int i = 0; |
| 71 | do |
| 72 | { |
| 73 | out[i++] = hull; |
| 74 | endpt = 0; |
| 75 | for (int j = 1; j < npts; ++j) |
| 76 | if (hull == endpt || left(&pts[hull*3], &pts[endpt*3], &pts[j*3])) |
| 77 | endpt = j; |
| 78 | hull = endpt; |
| 79 | } |
| 80 | while (endpt != out[0]); |
| 81 | |
| 82 | return i; |
| 83 | } |
| 84 | |
| 85 | static int pointInPoly(int nvert, const float* verts, const float* p) |
| 86 | { |
| 87 | int i, j, c = 0; |
| 88 | for (i = 0, j = nvert-1; i < nvert; j = i++) |
| 89 | { |
| 90 | const float* vi = &verts[i*3]; |
| 91 | const float* vj = &verts[j*3]; |
| 92 | if (((vi[2] > p[2]) != (vj[2] > p[2])) && |
| 93 | (p[0] < (vj[0]-vi[0]) * (p[2]-vi[2]) / (vj[2]-vi[2]) + vi[0]) ) |
| 94 | c = !c; |
| 95 | } |
| 96 | return c; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | ConvexVolumeTool::ConvexVolumeTool() : |
| 101 | m_sample(0), |
| 102 | m_areaType(SAMPLE_POLYAREA_GRASS), |
| 103 | m_polyOffset(0.0f), |
| 104 | m_boxHeight(6.0f), |
| 105 | m_boxDescent(1.0f), |
| 106 | m_npts(0), |
| 107 | m_nhull(0) |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | void ConvexVolumeTool::init(Sample* sample) |
| 112 | { |
| 113 | m_sample = sample; |
| 114 | } |
| 115 | |
| 116 | void ConvexVolumeTool::reset() |
| 117 | { |
| 118 | m_npts = 0; |
| 119 | m_nhull = 0; |
| 120 | } |
| 121 | |
| 122 | void ConvexVolumeTool::handleMenu() |
| 123 | { |
| 124 | imguiSlider("Shape Height", &m_boxHeight, 0.1f, 20.0f, 0.1f); |
| 125 | imguiSlider("Shape Descent", &m_boxDescent, 0.1f, 20.0f, 0.1f); |
| 126 | imguiSlider("Poly Offset", &m_polyOffset, 0.0f, 10.0f, 0.1f); |
| 127 | |
| 128 | imguiSeparator(); |
| 129 | |
| 130 | imguiLabel("Area Type"); |
| 131 | imguiIndent(); |
| 132 | if (imguiCheck("Ground", m_areaType == SAMPLE_POLYAREA_GROUND)) |
| 133 | m_areaType = SAMPLE_POLYAREA_GROUND; |
| 134 | if (imguiCheck("Water", m_areaType == SAMPLE_POLYAREA_WATER)) |
| 135 | m_areaType = SAMPLE_POLYAREA_WATER; |
| 136 | if (imguiCheck("Road", m_areaType == SAMPLE_POLYAREA_ROAD)) |
| 137 | m_areaType = SAMPLE_POLYAREA_ROAD; |
| 138 | if (imguiCheck("Door", m_areaType == SAMPLE_POLYAREA_DOOR)) |
| 139 | m_areaType = SAMPLE_POLYAREA_DOOR; |
| 140 | if (imguiCheck("Grass", m_areaType == SAMPLE_POLYAREA_GRASS)) |
| 141 | m_areaType = SAMPLE_POLYAREA_GRASS; |
| 142 | if (imguiCheck("Jump", m_areaType == SAMPLE_POLYAREA_JUMP)) |
| 143 | m_areaType = SAMPLE_POLYAREA_JUMP; |
| 144 | imguiUnindent(); |
| 145 | |
| 146 | imguiSeparator(); |
| 147 | |
| 148 | if (imguiButton("Clear Shape")) |
| 149 | { |
| 150 | m_npts = 0; |
| 151 | m_nhull = 0; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void ConvexVolumeTool::handleClick(const float* /*s*/, const float* p, bool shift) |
| 156 | { |
| 157 | if (!m_sample) return; |
| 158 | InputGeom* geom = m_sample->getInputGeom(); |
| 159 | if (!geom) return; |
| 160 | |
| 161 | if (shift) |
| 162 | { |
| 163 | // Delete |
| 164 | int nearestIndex = -1; |
| 165 | const ConvexVolume* vols = geom->getConvexVolumes(); |
| 166 | for (int i = 0; i < geom->getConvexVolumeCount(); ++i) |
| 167 | { |
| 168 | if (pointInPoly(vols[i].nverts, vols[i].verts, p) && |
| 169 | p[1] >= vols[i].hmin && p[1] <= vols[i].hmax) |
| 170 | { |
| 171 | nearestIndex = i; |
| 172 | } |
| 173 | } |
| 174 | // If end point close enough, delete it. |
| 175 | if (nearestIndex != -1) |
| 176 | { |
| 177 | geom->deleteConvexVolume(nearestIndex); |
| 178 | } |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | // Create |
| 183 | |
| 184 | // If clicked on that last pt, create the shape. |
| 185 | if (m_npts && rcVdistSqr(p, &m_pts[(m_npts-1)*3]) < rcSqr(0.2f)) |
| 186 | { |
| 187 | if (m_nhull > 2) |
| 188 | { |
| 189 | // Create shape. |
| 190 | float verts[MAX_PTS*3]; |
| 191 | for (int i = 0; i < m_nhull; ++i) |
| 192 | rcVcopy(&verts[i*3], &m_pts[m_hull[i]*3]); |
| 193 | |
| 194 | float minh = FLT_MAX, maxh = 0; |
| 195 | for (int i = 0; i < m_nhull; ++i) |
| 196 | minh = rcMin(minh, verts[i*3+1]); |
| 197 | minh -= m_boxDescent; |
| 198 | maxh = minh + m_boxHeight; |
| 199 | |
| 200 | if (m_polyOffset > 0.01f) |
| 201 | { |
| 202 | float offset[MAX_PTS*2*3]; |
| 203 | int noffset = rcOffsetPoly(verts, m_nhull, m_polyOffset, offset, MAX_PTS*2); |
| 204 | if (noffset > 0) |
| 205 | geom->addConvexVolume(offset, noffset, minh, maxh, (unsigned char)m_areaType); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | geom->addConvexVolume(verts, m_nhull, minh, maxh, (unsigned char)m_areaType); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | m_npts = 0; |
| 214 | m_nhull = 0; |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | // Add new point |
| 219 | if (m_npts < MAX_PTS) |
| 220 | { |
| 221 | rcVcopy(&m_pts[m_npts*3], p); |
| 222 | m_npts++; |
| 223 | // Update hull. |
| 224 | if (m_npts > 1) |
| 225 | m_nhull = convexhull(m_pts, m_npts, m_hull); |
| 226 | else |
| 227 | m_nhull = 0; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | } |
| 233 | |
| 234 | void ConvexVolumeTool::handleToggle() |
| 235 | { |
| 236 | } |
| 237 | |
| 238 | void ConvexVolumeTool::handleStep() |
| 239 | { |
| 240 | } |
| 241 | |
| 242 | void ConvexVolumeTool::handleUpdate(const float /*dt*/) |
| 243 | { |
| 244 | } |
| 245 | |
| 246 | void ConvexVolumeTool::handleRender() |
| 247 | { |
| 248 | duDebugDraw& dd = m_sample->getDebugDraw(); |
| 249 | |
| 250 | // Find height extent of the shape. |
| 251 | float minh = FLT_MAX, maxh = 0; |
| 252 | for (int i = 0; i < m_npts; ++i) |
| 253 | minh = rcMin(minh, m_pts[i*3+1]); |
| 254 | minh -= m_boxDescent; |
| 255 | maxh = minh + m_boxHeight; |
| 256 | |
| 257 | dd.begin(DU_DRAW_POINTS, 4.0f); |
| 258 | for (int i = 0; i < m_npts; ++i) |
| 259 | { |
| 260 | unsigned int col = duRGBA(255,255,255,255); |
| 261 | if (i == m_npts-1) |
| 262 | col = duRGBA(240,32,16,255); |
| 263 | dd.vertex(m_pts[i*3+0],m_pts[i*3+1]+0.1f,m_pts[i*3+2], col); |
| 264 | } |
| 265 | dd.end(); |
| 266 | |
| 267 | dd.begin(DU_DRAW_LINES, 2.0f); |
| 268 | for (int i = 0, j = m_nhull-1; i < m_nhull; j = i++) |
| 269 | { |
| 270 | const float* vi = &m_pts[m_hull[j]*3]; |
| 271 | const float* vj = &m_pts[m_hull[i]*3]; |
| 272 | dd.vertex(vj[0],minh,vj[2], duRGBA(255,255,255,64)); |
| 273 | dd.vertex(vi[0],minh,vi[2], duRGBA(255,255,255,64)); |
| 274 | dd.vertex(vj[0],maxh,vj[2], duRGBA(255,255,255,64)); |
| 275 | dd.vertex(vi[0],maxh,vi[2], duRGBA(255,255,255,64)); |
| 276 | dd.vertex(vj[0],minh,vj[2], duRGBA(255,255,255,64)); |
| 277 | dd.vertex(vj[0],maxh,vj[2], duRGBA(255,255,255,64)); |
| 278 | } |
| 279 | dd.end(); |
| 280 | } |
| 281 | |
| 282 | void ConvexVolumeTool::handleRenderOverlay(double* /*proj*/, double* /*model*/, int* view) |
| 283 | { |
| 284 | // Tool help |
| 285 | const int h = view[3]; |
| 286 | if (!m_npts) |
| 287 | { |
| 288 | imguiDrawText(280, h-40, IMGUI_ALIGN_LEFT, "LMB: Create new shape. SHIFT+LMB: Delete existing shape (click inside a shape).", imguiRGBA(255,255,255,192)); |
| 289 | } |
| 290 | else |
| 291 | { |
| 292 | imguiDrawText(280, h-40, IMGUI_ALIGN_LEFT, "Click LMB to add new points. Click on the red point to finish the shape.", imguiRGBA(255,255,255,192)); |
| 293 | imguiDrawText(280, h-60, IMGUI_ALIGN_LEFT, "The shape will be convex hull of all added points.", imguiRGBA(255,255,255,192)); |
| 294 | } |
| 295 | |
| 296 | } |