00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <DebugDrawer.h>
00010 #include <gfxConst.h>
00011
00012 #include <OgreSceneManager.h>
00013 #include <OgreRenderQueue.h>
00014 #include <OgreManualObject.h>
00015 #include <OgreAxisAlignedBox.h>
00016
00017 using namespace OgreGFX;
00018
00019 IcoSphere::IcoSphere()
00020 : index(0)
00021 {
00022 }
00023
00024 IcoSphere::~IcoSphere()
00025 {
00026 }
00027
00028 void IcoSphere::create(int recursionLevel)
00029 {
00030 vertices.clear();
00031 lineIndices.clear();
00032 triangleIndices.clear();
00033 faces.clear();
00034 middlePointIndexCache.clear();
00035 index = 0;
00036
00037 float t = (1.0f + Ogre::Math::Sqrt(5.0f)) / 2.0f;
00038
00039 addVertex(Ogre::Vector3(-1.0f, t, 0.0f));
00040 addVertex(Ogre::Vector3( 1.0f, t, 0.0f));
00041 addVertex(Ogre::Vector3(-1.0f, -t, 0.0f));
00042 addVertex(Ogre::Vector3( 1.0f, -t, 0.0f));
00043
00044 addVertex(Ogre::Vector3( 0.0f, -1.0f, t));
00045 addVertex(Ogre::Vector3( 0.0f, 1.0f, t));
00046 addVertex(Ogre::Vector3( 0.0f, -1.0f, -t));
00047 addVertex(Ogre::Vector3( 0.0f, 1.0f, -t));
00048
00049 addVertex(Ogre::Vector3( t, 0.0f, -1.0f));
00050 addVertex(Ogre::Vector3( t, 0.0f, 1.0f));
00051 addVertex(Ogre::Vector3(-t, 0.0f, -1.0f));
00052 addVertex(Ogre::Vector3(-t, 0.0f, 1.0f));
00053
00054 addFace(0, 11, 5);
00055 addFace(0, 5, 1);
00056 addFace(0, 1, 7);
00057 addFace(0, 7, 10);
00058 addFace(0, 10, 11);
00059
00060 addFace(1, 5, 9);
00061 addFace(5, 11, 4);
00062 addFace(11, 10, 2);
00063 addFace(10, 7, 6);
00064 addFace(7, 1, 8);
00065
00066 addFace(3, 9, 4);
00067 addFace(3, 4, 2);
00068 addFace(3, 2, 6);
00069 addFace(3, 6, 8);
00070 addFace(3, 8, 9);
00071
00072 addFace(4, 9, 5);
00073 addFace(2, 4, 11);
00074 addFace(6, 2, 10);
00075 addFace(8, 6, 7);
00076 addFace(9, 8, 1);
00077
00078 addLineIndices(1, 0);
00079 addLineIndices(1, 5);
00080 addLineIndices(1, 7);
00081 addLineIndices(1, 8);
00082 addLineIndices(1, 9);
00083
00084 addLineIndices(2, 3);
00085 addLineIndices(2, 4);
00086 addLineIndices(2, 6);
00087 addLineIndices(2, 10);
00088 addLineIndices(2, 11);
00089
00090 addLineIndices(0, 5);
00091 addLineIndices(5, 9);
00092 addLineIndices(9, 8);
00093 addLineIndices(8, 7);
00094 addLineIndices(7, 0);
00095
00096 addLineIndices(10, 11);
00097 addLineIndices(11, 4);
00098 addLineIndices(4, 3);
00099 addLineIndices(3, 6);
00100 addLineIndices(6, 10);
00101
00102 addLineIndices(0, 11);
00103 addLineIndices(11, 5);
00104 addLineIndices(5, 4);
00105 addLineIndices(4, 9);
00106 addLineIndices(9, 3);
00107 addLineIndices(3, 8);
00108 addLineIndices(8, 6);
00109 addLineIndices(6, 7);
00110 addLineIndices(7, 10);
00111 addLineIndices(10, 0);
00112
00113 for (int i = 0; i < recursionLevel; i++)
00114 {
00115 std::list<TriangleIndices> faces2;
00116
00117 for (std::list<TriangleIndices>::iterator j = faces.begin(); j != faces.end(); j++)
00118 {
00119 TriangleIndices f = *j;
00120 int a = getMiddlePoint(f.v1, f.v2);
00121 int b = getMiddlePoint(f.v2, f.v3);
00122 int c = getMiddlePoint(f.v3, f.v1);
00123
00124 removeLineIndices(f.v1, f.v2);
00125 removeLineIndices(f.v2, f.v3);
00126 removeLineIndices(f.v3, f.v1);
00127
00128 faces2.push_back(TriangleIndices(f.v1, a, c));
00129 faces2.push_back(TriangleIndices(f.v2, b, a));
00130 faces2.push_back(TriangleIndices(f.v3, c, b));
00131 faces2.push_back(TriangleIndices(a, b, c));
00132
00133 addTriangleLines(f.v1, a, c);
00134 addTriangleLines(f.v2, b, a);
00135 addTriangleLines(f.v3, c, b);
00136 }
00137
00138 faces = faces2;
00139 }
00140 }
00141
00142 void IcoSphere::addLineIndices(int index0, int index1)
00143 {
00144 lineIndices.push_back(LineIndices(index0, index1));
00145 }
00146
00147 void IcoSphere::removeLineIndices(int index0, int index1)
00148 {
00149 std::list<LineIndices>::iterator result = std::find(lineIndices.begin(), lineIndices.end(), LineIndices(index0, index1));
00150
00151 if (result != lineIndices.end())
00152 lineIndices.erase(result);
00153 }
00154
00155 void IcoSphere::addTriangleLines(int index0, int index1, int index2)
00156 {
00157 addLineIndices(index0, index1);
00158 addLineIndices(index1, index2);
00159 addLineIndices(index2, index0);
00160 }
00161
00162 int IcoSphere::addVertex(const Ogre::Vector3 &vertex)
00163 {
00164 Ogre::Real length = vertex.length();
00165 vertices.push_back(Ogre::Vector3(vertex.x / length, vertex.y / length, vertex.z / length));
00166 return index++;
00167 }
00168
00169 int IcoSphere::getMiddlePoint(int index0, int index1)
00170 {
00171 bool isFirstSmaller = index0 < index1;
00172 __int64 smallerIndex = isFirstSmaller ? index0 : index1;
00173 __int64 largerIndex = isFirstSmaller ? index1 : index0;
00174 __int64 key = (smallerIndex << 32) | largerIndex;
00175
00176 if (middlePointIndexCache.find(key) != middlePointIndexCache.end())
00177 return middlePointIndexCache[key];
00178
00179 Ogre::Vector3 point1 = vertices[index0];
00180 Ogre::Vector3 point2 = vertices[index1];
00181 Ogre::Vector3 middle = point1.midPoint(point2);
00182
00183 int index = addVertex(middle);
00184 middlePointIndexCache[key] = index;
00185 return index;
00186 }
00187
00188 void IcoSphere::addFace(int index0, int index1, int index2)
00189 {
00190 faces.push_back(TriangleIndices(index0, index1, index2));
00191 }
00192
00193 void IcoSphere::addToLineIndices(int baseIndex, std::list<int> *target)
00194 {
00195 for (std::list<LineIndices>::iterator i = lineIndices.begin(); i != lineIndices.end(); i++)
00196 {
00197 target->push_back(baseIndex + (*i).v1);
00198 target->push_back(baseIndex + (*i).v2);
00199 }
00200 }
00201
00202 void IcoSphere::addToTriangleIndices(int baseIndex, std::list<int> *target)
00203 {
00204 for (std::list<TriangleIndices>::iterator i = faces.begin(); i != faces.end(); i++)
00205 {
00206 target->push_back(baseIndex + (*i).v1);
00207 target->push_back(baseIndex + (*i).v2);
00208 target->push_back(baseIndex + (*i).v3);
00209 }
00210 }
00211
00212 int IcoSphere::addToVertices(std::list<VertexPair> *target, const Ogre::Vector3 &position, const Ogre::ColourValue &colour, float scale)
00213 {
00214 Ogre::Matrix4 transform = Ogre::Matrix4::IDENTITY;
00215 transform.setTrans(position);
00216 transform.setScale(Ogre::Vector3(scale, scale, scale));
00217
00218 for (int i = 0; i < (int)vertices.size(); i++)
00219 target->push_back(VertexPair(transform * vertices[i], colour));
00220
00221 return vertices.size();
00222 }
00223
00224
00225
00226 template<> DebugDrawer* Ogre::Singleton<DebugDrawer>::ms_Singleton = 0;
00227 DebugDrawer* DebugDrawer::getSingletonPtr(void)
00228 {
00229 return ms_Singleton;
00230 }
00231
00232 DebugDrawer& DebugDrawer::getSingleton(void)
00233 {
00234 assert( ms_Singleton ); return ( *ms_Singleton );
00235 }
00236
00237
00238 DebugDrawer::DebugDrawer(Ogre::SceneManager *_sceneManager, float _fillAlpha)
00239 :isEnabled(true), sceneManager(_sceneManager), fillAlpha(_fillAlpha), manualObject(0), linesIndex(0), trianglesIndex(0)
00240 {
00241 initialise();
00242 }
00243
00244 DebugDrawer::~DebugDrawer()
00245 {
00246 shutdown();
00247 }
00248
00249 void DebugDrawer::initialise()
00250 {
00251 manualObject = sceneManager->createManualObject("debug_object");
00252 manualObject->setVisibilityFlags(OgreGFX::ENT_VISIBILITY_FLAG);
00253 sceneManager->getRootSceneNode()->createChildSceneNode("debug_object")->attachObject(manualObject);
00254
00255 manualObject->setDynamic(true);
00256
00257 icoSphere.create(DEFAULT_ICOSPHERE_RECURSION_LEVEL);
00258
00259 manualObject->begin("debug_draw", Ogre::RenderOperation::OT_LINE_LIST);
00260 manualObject->position(Ogre::Vector3::ZERO);
00261 manualObject->colour(Ogre::ColourValue::ZERO);
00262 manualObject->index(0);
00263 manualObject->end();
00264 manualObject->begin("debug_draw", Ogre::RenderOperation::OT_TRIANGLE_LIST);
00265 manualObject->position(Ogre::Vector3::ZERO);
00266 manualObject->colour(Ogre::ColourValue::ZERO);
00267 manualObject->index(0);
00268 manualObject->end();
00269
00270 linesIndex = trianglesIndex = 0;
00271
00272 }
00273
00274 void DebugDrawer::setIcoSphereRecursionLevel(int recursionLevel)
00275 {
00276 icoSphere.create(recursionLevel);
00277 }
00278
00279 void DebugDrawer::shutdown()
00280 {
00281 sceneManager->destroySceneNode("debug_object");
00282 sceneManager->destroyManualObject(manualObject);
00283 }
00284
00285 void DebugDrawer::buildLine(const Ogre::Vector3& start,
00286 const Ogre::Vector3& end,
00287 const Ogre::ColourValue& colour,
00288 float alpha)
00289 {
00290 int i = addLineVertex(start, Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00291 addLineVertex(end, Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00292
00293 addLineIndices(i, i + 1);
00294 }
00295
00296 void DebugDrawer::buildAngleLine(const Ogre::Vector3& start,
00297 const Ogre::Radian& angle,
00298 const float length,
00299 const Ogre::ColourValue& colour,
00300 float alpha)
00301 {
00302 float a = length * cos (angle.valueRadians());
00303 Ogre::Vector3 end = Ogre::Vector3(start.x + a, start.y + 1, start.z + a * -Ogre::Math::Tan(angle.valueRadians(), false));
00304 buildLine(start, end, colour);
00305 }
00306
00307
00308
00309 void DebugDrawer::buildQuad(const Ogre::Vector3 *vertices,
00310 const Ogre::ColourValue& colour,
00311 float alpha)
00312 {
00313 int index = addLineVertex(vertices[0], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00314 addLineVertex(vertices[1], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00315 addLineVertex(vertices[2], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00316 addLineVertex(vertices[3], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00317
00318 for (int i = 0; i < 4; ++i) addLineIndices(index + i, index + ((i + 1) % 4));
00319 }
00320
00321 void DebugDrawer::buildCircle(const Ogre::Vector3 ¢re,
00322 float radius,
00323 int segmentsCount,
00324 const Ogre::ColourValue& colour,
00325 float alpha)
00326 {
00327 int index = linesIndex;
00328 float increment = 2 * Ogre::Math::PI / segmentsCount;
00329 float angle = 0.0f;
00330
00331 for (int i = 0; i < segmentsCount; i++)
00332 {
00333 addLineVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y, centre.z + radius * Ogre::Math::Sin(angle)),
00334 Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00335 angle += increment;
00336 }
00337
00338 for (int i = 0; i < segmentsCount; i++)
00339 addLineIndices(index + i, i + 1 < segmentsCount ? index + i + 1 : index);
00340 }
00341
00342 void DebugDrawer::buildFilledCircle(const Ogre::Vector3 ¢re,
00343 float radius,
00344 int segmentsCount,
00345 const Ogre::ColourValue& colour,
00346 float alpha)
00347 {
00348 int index = trianglesIndex;
00349 float increment = 2 * Ogre::Math::PI / segmentsCount;
00350 float angle = 0.0f;
00351
00352 for (int i = 0; i < segmentsCount; i++)
00353 {
00354 addTriangleVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y, centre.z + radius * Ogre::Math::Sin(angle)),
00355 Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00356 angle += increment;
00357 }
00358
00359 addTriangleVertex(centre, Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00360
00361 for (int i = 0; i < segmentsCount; i++)
00362 addTriangleIndices(i + 1 < segmentsCount ? index + i + 1 : index, index + i, index + segmentsCount);
00363 }
00364
00365 void DebugDrawer::buildCylinder(const Ogre::Vector3 ¢re,
00366 float radius,
00367 int segmentsCount,
00368 float height,
00369 const Ogre::ColourValue& colour,
00370 float alpha)
00371 {
00372 int index = linesIndex;
00373 float increment = 2 * Ogre::Math::PI / segmentsCount;
00374 float angle = 0.0f;
00375
00376
00377 for (int i = 0; i < segmentsCount; i++)
00378 {
00379 addLineVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y + height / 2, centre.z + radius * Ogre::Math::Sin(angle)),
00380 Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00381 angle += increment;
00382 }
00383
00384 angle = 0.0f;
00385
00386
00387 for (int i = 0; i < segmentsCount; i++)
00388 {
00389 addLineVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y - height / 2, centre.z + radius * Ogre::Math::Sin(angle)),
00390 Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00391 angle += increment;
00392 }
00393
00394 for (int i = 0; i < segmentsCount; i++)
00395 {
00396 addLineIndices(index + i, i + 1 < segmentsCount ? index + i + 1 : index);
00397 addLineIndices(segmentsCount + index + i, i + 1 < segmentsCount ? segmentsCount + index + i + 1 : segmentsCount + index);
00398 addLineIndices(index + i, segmentsCount + index + i);
00399 }
00400 }
00401
00402 void DebugDrawer::buildFilledCylinder(const Ogre::Vector3 ¢re,
00403 float radius,
00404 int segmentsCount,
00405 float height,
00406 const Ogre::ColourValue& colour,
00407 float alpha)
00408 {
00409 int index = trianglesIndex;
00410 float increment = 2 * Ogre::Math::PI / segmentsCount;
00411 float angle = 0.0f;
00412
00413
00414 for (int i = 0; i < segmentsCount; i++)
00415 {
00416 addTriangleVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y + height / 2, centre.z + radius * Ogre::Math::Sin(angle)),
00417 Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00418 angle += increment;
00419 }
00420
00421 addTriangleVertex(Ogre::Vector3(centre.x, centre.y + height / 2, centre.z), Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00422
00423 angle = 0.0f;
00424
00425
00426 for (int i = 0; i < segmentsCount; i++)
00427 {
00428 addTriangleVertex(Ogre::Vector3(centre.x + radius * Ogre::Math::Cos(angle), centre.y - height / 2, centre.z + radius * Ogre::Math::Sin(angle)),
00429 Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00430 angle += increment;
00431 }
00432
00433 addTriangleVertex(Ogre::Vector3(centre.x, centre.y - height / 2, centre.z), Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00434
00435 for (int i = 0; i < segmentsCount; i++)
00436 {
00437 addTriangleIndices(i + 1 < segmentsCount ? index + i + 1 : index,
00438 index + i,
00439 index + segmentsCount);
00440
00441 addTriangleIndices(i + 1 < segmentsCount ? (segmentsCount + 1) + index + i + 1 : (segmentsCount + 1) + index,
00442 (segmentsCount + 1) + index + segmentsCount,
00443 (segmentsCount + 1) + index + i);
00444
00445 addQuadIndices(
00446 index + i,
00447 i + 1 < segmentsCount ? index + i + 1 : index,
00448 i + 1 < segmentsCount ? (segmentsCount + 1) + index + i + 1 : (segmentsCount + 1) + index,
00449 (segmentsCount + 1) + index + i);
00450 }
00451 }
00452
00453 void DebugDrawer::buildCuboid(const Ogre::Vector3 *vertices,
00454 const Ogre::ColourValue& colour,
00455 float alpha)
00456 {
00457 int index = addLineVertex(vertices[0], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00458 for (int i = 1; i < 8; ++i) addLineVertex(vertices[i], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00459
00460 for (int i = 0; i < 4; ++i) addLineIndices(index + i, index + ((i + 1) % 4));
00461 for (int i = 4; i < 8; ++i) addLineIndices(index + i, i == 7 ? index + 4 : index + i + 1);
00462 addLineIndices(index + 1, index + 5);
00463 addLineIndices(index + 2, index + 4);
00464 addLineIndices(index, index + 6);
00465 addLineIndices(index + 3, index + 7);
00466 }
00467
00468 void DebugDrawer::buildFilledCuboid(const Ogre::Vector3 *vertices,
00469 const Ogre::ColourValue& colour,
00470 float alpha)
00471 {
00472 int index = addTriangleVertex(vertices[0], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00473 for (int i = 1; i < 8; ++i) addTriangleVertex(vertices[i], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00474
00475 addQuadIndices(index, index + 1, index + 2, index + 3);
00476 addQuadIndices(index + 4, index + 5, index + 6, index + 7);
00477
00478 addQuadIndices(index + 1, index + 5, index + 4, index + 2);
00479 addQuadIndices(index, index + 3, index + 7, index + 6);
00480
00481 addQuadIndices(index + 1, index , index + 6, index + 5);
00482 addQuadIndices(index + 4, index + 7, index + 3, index + 2);
00483 }
00484
00485 void DebugDrawer::buildFilledQuad(const Ogre::Vector3 *vertices,
00486 const Ogre::ColourValue& colour,
00487 float alpha)
00488 {
00489 int index = addTriangleVertex(vertices[0], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00490 addTriangleVertex(vertices[1], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00491 addTriangleVertex(vertices[2], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00492 addTriangleVertex(vertices[3], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00493
00494 addQuadIndices(index, index + 1, index + 2, index + 3);
00495 }
00496
00497 void DebugDrawer::buildFilledTriangle(const Ogre::Vector3 *vertices,
00498 const Ogre::ColourValue& colour,
00499 float alpha)
00500 {
00501 int index = addTriangleVertex(vertices[0], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00502 addTriangleVertex(vertices[1], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00503 addTriangleVertex(vertices[2], Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00504
00505 addTriangleIndices(index, index + 1, index + 2);
00506 }
00507
00508 void DebugDrawer::buildTetrahedron(const Ogre::Vector3 ¢re,
00509 float scale,
00510 const Ogre::ColourValue &colour,
00511 float alpha)
00512 {
00513 int index = linesIndex;
00514
00515
00516 float bottomDistance = scale * 0.2f;
00517 float topDistance = scale * 0.62f;
00518 float frontDistance = scale * 0.289f;
00519 float backDistance = scale * 0.577f;
00520 float leftRightDistance = scale * 0.5f;
00521
00522 addLineVertex(Ogre::Vector3(centre.x, centre.y + topDistance, centre.z),
00523 Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00524 addLineVertex(Ogre::Vector3(centre.x, centre.y - bottomDistance, centre.z + frontDistance),
00525 Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00526 addLineVertex(Ogre::Vector3(centre.x + leftRightDistance, centre.y - bottomDistance, centre.z - backDistance),
00527 Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00528 addLineVertex(Ogre::Vector3(centre.x - leftRightDistance, centre.y - bottomDistance, centre.z - backDistance),
00529 Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00530
00531 addLineIndices(index, index + 1);
00532 addLineIndices(index, index + 2);
00533 addLineIndices(index, index + 3);
00534
00535 addLineIndices(index + 1, index + 2);
00536 addLineIndices(index + 2, index + 3);
00537 addLineIndices(index + 3, index + 1);
00538 }
00539
00540 void DebugDrawer::buildFilledTetrahedron(const Ogre::Vector3 ¢re,
00541 float scale,
00542 const Ogre::ColourValue &colour,
00543 float alpha)
00544 {
00545 int index = trianglesIndex;
00546
00547
00548 float bottomDistance = scale * 0.2f;
00549 float topDistance = scale * 0.62f;
00550 float frontDistance = scale * 0.289f;
00551 float backDistance = scale * 0.577f;
00552 float leftRightDistance = scale * 0.5f;
00553
00554 addTriangleVertex(Ogre::Vector3(centre.x, centre.y + topDistance, centre.z),
00555 Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00556 addTriangleVertex(Ogre::Vector3(centre.x, centre.y - bottomDistance, centre.z + frontDistance),
00557 Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00558 addTriangleVertex(Ogre::Vector3(centre.x + leftRightDistance, centre.y - bottomDistance, centre.z - backDistance),
00559 Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00560 addTriangleVertex(Ogre::Vector3(centre.x - leftRightDistance, centre.y - bottomDistance, centre.z - backDistance),
00561 Ogre::ColourValue(colour.r, colour.g, colour.b, alpha));
00562
00563 addTriangleIndices(index, index + 1, index + 2);
00564 addTriangleIndices(index, index + 2, index + 3);
00565 addTriangleIndices(index, index + 3, index + 1);
00566
00567 addTriangleIndices(index + 1, index + 3, index + 2);
00568 }
00569
00570 void DebugDrawer::drawLine(const Ogre::Vector3& start,
00571 const Ogre::Vector3& end,
00572 const Ogre::ColourValue& colour)
00573 {
00574 buildLine(start, end, colour);
00575 }
00576
00577 void DebugDrawer::drawAngleLine(const Ogre::Vector3& start,
00578 const Ogre::Radian &angle,
00579 const float length,
00580 const Ogre::ColourValue& colour)
00581 {
00582 buildAngleLine(start, angle, length, colour);
00583 }
00584
00585 void DebugDrawer::drawCircle(const Ogre::Vector3 ¢re,
00586 float radius,
00587 int segmentsCount,
00588 const Ogre::ColourValue& colour,
00589 bool isFilled)
00590 {
00591 buildCircle(centre, radius, segmentsCount, colour);
00592 if (isFilled) buildFilledCircle(centre, radius, segmentsCount, colour, fillAlpha);
00593 }
00594
00595 void DebugDrawer::drawCylinder(const Ogre::Vector3 ¢re,
00596 float radius,
00597 int segmentsCount,
00598 float height,
00599 const Ogre::ColourValue& colour,
00600 bool isFilled)
00601 {
00602 buildCylinder(centre, radius, segmentsCount, height, colour);
00603 if (isFilled) buildFilledCylinder(centre, radius, segmentsCount, height, colour, fillAlpha);
00604 }
00605
00606 void DebugDrawer::drawQuad(const Ogre::Vector3 *vertices,
00607 const Ogre::ColourValue& colour,
00608 bool isFilled)
00609 {
00610 buildQuad(vertices, colour);
00611 if (isFilled) buildFilledQuad(vertices, colour, fillAlpha);
00612 }
00613
00614 void DebugDrawer::drawCuboid(const Ogre::Vector3 *vertices,
00615 const Ogre::ColourValue& colour,
00616 bool isFilled)
00617 {
00618 buildCuboid(vertices, colour);
00619 if (isFilled) buildFilledCuboid(vertices, colour, fillAlpha);
00620 }
00621
00622 void DebugDrawer::drawSphere(const Ogre::Vector3 ¢re,
00623 float radius,
00624 const Ogre::ColourValue& colour,
00625 bool isFilled)
00626 {
00627 int baseIndex = linesIndex;
00628 linesIndex += icoSphere.addToVertices(&lineVertices, centre, colour, radius);
00629 icoSphere.addToLineIndices(baseIndex, &lineIndices);
00630
00631 if (isFilled)
00632 {
00633 baseIndex = trianglesIndex;
00634 trianglesIndex += icoSphere.addToVertices(&triangleVertices, centre, Ogre::ColourValue(colour.r, colour.g, colour.b, fillAlpha), radius);
00635 icoSphere.addToTriangleIndices(baseIndex, &triangleIndices);
00636 }
00637 }
00638
00639 void DebugDrawer::drawTetrahedron(const Ogre::Vector3 ¢re,
00640 float scale,
00641 const Ogre::ColourValue& colour,
00642 bool isFilled)
00643 {
00644 buildTetrahedron(centre, scale, colour);
00645 if (isFilled) buildFilledTetrahedron(centre, scale, colour, fillAlpha);
00646 }
00647
00648 void DebugDrawer::build()
00649 {
00650 manualObject->beginUpdate(0);
00651 if (lineVertices.size() > 0 && isEnabled)
00652 {
00653 manualObject->estimateVertexCount(lineVertices.size());
00654 manualObject->estimateIndexCount(lineIndices.size());
00655 for (std::list<VertexPair>::iterator i = lineVertices.begin(); i != lineVertices.end(); i++)
00656 {
00657 manualObject->position(i->first);
00658 manualObject->colour(i->second);
00659 }
00660 for (std::list<int>::iterator i = lineIndices.begin(); i != lineIndices.end(); i++)
00661 manualObject->index(*i);
00662 }
00663 manualObject->end();
00664
00665 manualObject->beginUpdate(1);
00666 if (triangleVertices.size() > 0 && isEnabled)
00667 {
00668 manualObject->estimateVertexCount(triangleVertices.size());
00669 manualObject->estimateIndexCount(triangleIndices.size());
00670 for (std::list<VertexPair>::iterator i = triangleVertices.begin(); i != triangleVertices.end(); i++)
00671 {
00672 manualObject->position(i->first);
00673 manualObject->colour(i->second.r, i->second.g, i->second.b, fillAlpha);
00674 }
00675 for (std::list<int>::iterator i = triangleIndices.begin(); i != triangleIndices.end(); i++)
00676 manualObject->index(*i);
00677 }
00678 manualObject->end();
00679 }
00680
00681 void DebugDrawer::clear()
00682 {
00683 lineVertices.clear();
00684 triangleVertices.clear();
00685 lineIndices.clear();
00686 triangleIndices.clear();
00687 linesIndex = trianglesIndex = 0;
00688 }
00689
00690 int DebugDrawer::addLineVertex(const Ogre::Vector3 &vertex, const Ogre::ColourValue &colour)
00691 {
00692 lineVertices.push_back(VertexPair(vertex, colour));
00693 return linesIndex++;
00694 }
00695
00696 void DebugDrawer::addLineIndices(int index1, int index2)
00697 {
00698 lineIndices.push_back(index1);
00699 lineIndices.push_back(index2);
00700 }
00701
00702 int DebugDrawer::addTriangleVertex(const Ogre::Vector3 &vertex, const Ogre::ColourValue &colour)
00703 {
00704 triangleVertices.push_back(VertexPair(vertex, colour));
00705 return trianglesIndex++;
00706 }
00707
00708 void DebugDrawer::addTriangleIndices(int index1, int index2, int index3)
00709 {
00710 triangleIndices.push_back(index1);
00711 triangleIndices.push_back(index2);
00712 triangleIndices.push_back(index3);
00713 }
00714
00715 void DebugDrawer::addQuadIndices(int index1, int index2, int index3, int index4)
00716 {
00717 triangleIndices.push_back(index1);
00718 triangleIndices.push_back(index2);
00719 triangleIndices.push_back(index3);
00720
00721 triangleIndices.push_back(index1);
00722 triangleIndices.push_back(index3);
00723 triangleIndices.push_back(index4);
00724 }