| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993 |
- #include "BsDrawHelper.h"
- #include "BsMesh.h"
- #include "BsAABox.h"
- #include "BsSphere.h"
- #include "BsVertexDataDesc.h"
- #include "BsMeshHeap.h"
- #include "BsShapeMeshes3D.h"
- #include "BsTextData.h"
- #include "BsVector2.h"
- namespace BansheeEngine
- {
- const UINT32 DrawHelper::VERTEX_BUFFER_GROWTH = 4096;
- const UINT32 DrawHelper::INDEX_BUFFER_GROWTH = 4096 * 2;
- DrawHelper::DrawHelper()
- :mLayer(1)
- {
- mTransform = Matrix4::IDENTITY;
- mSolidVertexDesc = bs_shared_ptr_new<VertexDataDesc>();
- mSolidVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
- mSolidVertexDesc->addVertElem(VET_FLOAT3, VES_NORMAL);
- mSolidVertexDesc->addVertElem(VET_COLOR, VES_COLOR);
- mWireVertexDesc = bs_shared_ptr_new<VertexDataDesc>();
- mWireVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
- mWireVertexDesc->addVertElem(VET_COLOR, VES_COLOR);
- mTextVertexDesc = bs_shared_ptr_new<VertexDataDesc>();
- mTextVertexDesc->addVertElem(VET_FLOAT3, VES_POSITION);
- mTextVertexDesc->addVertElem(VET_FLOAT2, VES_TEXCOORD);
- mTextVertexDesc->addVertElem(VET_COLOR, VES_COLOR);
- mSolidMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mSolidVertexDesc);
- mWireMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mWireVertexDesc);
- mTextMeshHeap = MeshHeap::create(VERTEX_BUFFER_GROWTH, INDEX_BUFFER_GROWTH, mTextVertexDesc);
- }
- DrawHelper::~DrawHelper()
- {
- clearMeshes();
- }
- void DrawHelper::setColor(const Color& color)
- {
- mColor = color;
- }
- void DrawHelper::setTransform(const Matrix4& transform)
- {
- mTransform = transform;
- }
- void DrawHelper::setLayer(UINT64 layer)
- {
- mLayer = layer;
- }
- void DrawHelper::cube(const Vector3& position, const Vector3& extents)
- {
- mSolidCubeData.push_back(CubeData());
- CubeData& cubeData = mSolidCubeData.back();
- cubeData.position = position;
- cubeData.extents = extents;
- cubeData.color = mColor;
- cubeData.transform = mTransform;
- cubeData.layer = mLayer;
- cubeData.center = mTransform.multiplyAffine(position);
- }
- void DrawHelper::sphere(const Vector3& position, float radius, UINT32 quality)
- {
- mSolidSphereData.push_back(SphereData());
- SphereData& sphereData = mSolidSphereData.back();
- sphereData.position = position;
- sphereData.radius = radius;
- sphereData.quality = quality;
- sphereData.color = mColor;
- sphereData.transform = mTransform;
- sphereData.layer = mLayer;
- sphereData.center = mTransform.multiplyAffine(position);
- }
- void DrawHelper::wireCube(const Vector3& position, const Vector3& extents)
- {
- mWireCubeData.push_back(CubeData());
- CubeData& cubeData = mWireCubeData.back();
- cubeData.position = position;
- cubeData.extents = extents;
- cubeData.color = mColor;
- cubeData.transform = mTransform;
- cubeData.layer = mLayer;
- cubeData.center = mTransform.multiplyAffine(position);
- }
- void DrawHelper::wireSphere(const Vector3& position, float radius, UINT32 quality)
- {
- mWireSphereData.push_back(SphereData());
- SphereData& sphereData = mWireSphereData.back();
- sphereData.position = position;
- sphereData.radius = radius;
- sphereData.quality = quality;
- sphereData.color = mColor;
- sphereData.transform = mTransform;
- sphereData.layer = mLayer;
- sphereData.center = mTransform.multiplyAffine(position);
- }
- void DrawHelper::line(const Vector3& start, const Vector3& end)
- {
- mLineData.push_back(LineData());
- LineData& lineData = mLineData.back();
- lineData.start = start;
- lineData.end = end;
- lineData.color = mColor;
- lineData.transform = mTransform;
- lineData.layer = mLayer;
- lineData.center = mTransform.multiplyAffine((start + end) * 0.5f);
- }
- void DrawHelper::frustum(const Vector3& position, float aspect, Degree FOV, float near, float far)
- {
- mFrustumData.push_back(FrustumData());
- FrustumData& frustumData = mFrustumData.back();
- frustumData.position = position;
- frustumData.aspect = aspect;
- frustumData.FOV = FOV;
- frustumData.near = near;
- frustumData.far = far;
- frustumData.color = mColor;
- frustumData.transform = mTransform;
- frustumData.layer = mLayer;
- frustumData.center = mTransform.multiplyAffine(position);
- }
- void DrawHelper::cone(const Vector3& base, const Vector3& normal, float height, float radius, UINT32 quality)
- {
- mConeData.push_back(ConeData());
- ConeData& coneData = mConeData.back();
- coneData.base = base;
- coneData.normal = normal;
- coneData.height = height;
- coneData.radius = radius;
- coneData.quality = quality;
- coneData.color = mColor;
- coneData.transform = mTransform;
- coneData.layer = mLayer;
- coneData.center = mTransform.multiplyAffine(base + normal * height * 0.5f);
- }
- void DrawHelper::disc(const Vector3& position, const Vector3& normal, float radius, UINT32 quality)
- {
- mDiscData.push_back(DiscData());
- DiscData& discData = mDiscData.back();
- discData.position = position;
- discData.normal = normal;
- discData.radius = radius;
- discData.quality = quality;
- discData.color = mColor;
- discData.transform = mTransform;
- discData.layer = mLayer;
- discData.center = mTransform.multiplyAffine(position);
- }
- void DrawHelper::wireDisc(const Vector3& position, const Vector3& normal, float radius, UINT32 quality)
- {
- mWireDiscData.push_back(DiscData());
- DiscData& discData = mWireDiscData.back();
- discData.position = position;
- discData.normal = normal;
- discData.radius = radius;
- discData.quality = quality;
- discData.color = mColor;
- discData.transform = mTransform;
- discData.layer = mLayer;
- discData.center = mTransform.multiplyAffine(position);
- }
- void DrawHelper::arc(const Vector3& position, const Vector3& normal, float radius,
- Degree startAngle, Degree amountAngle, UINT32 quality)
- {
- mArcData.push_back(ArcData());
- ArcData& arcData = mArcData.back();
- arcData.position = position;
- arcData.normal = normal;
- arcData.radius = radius;
- arcData.startAngle = startAngle;
- arcData.amountAngle = amountAngle;
- arcData.quality = quality;
- arcData.color = mColor;
- arcData.transform = mTransform;
- arcData.layer = mLayer;
- arcData.center = mTransform.multiplyAffine(position);
- }
- void DrawHelper::wireArc(const Vector3& position, const Vector3& normal, float radius,
- Degree startAngle, Degree amountAngle, UINT32 quality)
- {
- mWireArcData.push_back(ArcData());
- ArcData& arcData = mWireArcData.back();
- arcData.position = position;
- arcData.normal = normal;
- arcData.radius = radius;
- arcData.startAngle = startAngle;
- arcData.amountAngle = amountAngle;
- arcData.quality = quality;
- arcData.color = mColor;
- arcData.transform = mTransform;
- arcData.layer = mLayer;
- arcData.center = mTransform.multiplyAffine(position);
- }
- void DrawHelper::rectangle(const Rect3& area)
- {
- mRect3Data.push_back(Rect3Data());
- Rect3Data& rectData = mRect3Data.back();
- rectData.area = area;
- rectData.color = mColor;
- rectData.transform = mTransform;
- rectData.layer = mLayer;
- rectData.center = mTransform.multiplyAffine(area.getCenter());
- }
- void DrawHelper::text(const Vector3& position, const WString& text, const HFont& font, UINT32 size)
- {
- if (!font.isLoaded() || text.empty())
- return;
- mText2DData.push_back(Text2DData());
- Text2DData& textData = mText2DData.back();
- textData.position = position;
- textData.color = mColor;
- textData.transform = mTransform;
- textData.layer = mLayer;
- textData.center = mTransform.multiplyAffine(position);
- textData.text = text;
- textData.font = font;
- textData.size = size;
- }
- void DrawHelper::clear()
- {
- mSolidCubeData.clear();
- mWireCubeData.clear();
- mSolidSphereData.clear();
- mWireSphereData.clear();
- mLineData.clear();
- mRect3Data.clear();
- mFrustumData.clear();
- mFrustumData.clear();
- mDiscData.clear();
- mWireDiscData.clear();
- mArcData.clear();
- mWireArcData.clear();
- mConeData.clear();
- mText2DData.clear();
- }
- void DrawHelper::buildMeshes(SortType sorting, const Vector3& reference, UINT64 layers)
- {
- clearMeshes();
- enum class ShapeType
- {
- Cube, Sphere, WireCube, WireSphere, Line, Frustum,
- Cone, Disc, WireDisc, Arc, WireArc, Rectangle, Text
- };
- struct RawData
- {
- ShapeType shapeType;
- MeshType meshType;
- UINT32 idx;
- UINT32 textIdx;
- float distance;
- UINT32 numVertices;
- UINT32 numIndices;
- };
- /************************************************************************/
- /* Sort everything according to specified sorting rule */
- /************************************************************************/
- UINT32 idx = 0;
- Vector<RawData> allShapes;
- UINT32 localIdx = 0;
- for (auto& shapeData : mSolidCubeData)
- {
- if ((shapeData.layer & layers) == 0)
- {
- localIdx++;
- continue;
- }
- allShapes.push_back(RawData());
- RawData& rawData = allShapes.back();
- rawData.idx = localIdx++;
- rawData.textIdx = 0;
- rawData.meshType = MeshType::Solid;
- rawData.shapeType = ShapeType::Cube;
- rawData.distance = shapeData.center.distance(reference);
- ShapeMeshes3D::getNumElementsAABox(rawData.numVertices, rawData.numIndices);
- }
- localIdx = 0;
- for (auto& shapeData : mSolidSphereData)
- {
- if ((shapeData.layer & layers) == 0)
- {
- localIdx++;
- continue;
- }
- allShapes.push_back(RawData());
- RawData& rawData = allShapes.back();
- rawData.idx = localIdx++;
- rawData.textIdx = 0;
- rawData.meshType = MeshType::Solid;
- rawData.shapeType = ShapeType::Sphere;
- rawData.distance = shapeData.center.distance(reference);
- ShapeMeshes3D::getNumElementsSphere(shapeData.quality,
- rawData.numVertices, rawData.numIndices);
- }
- localIdx = 0;
- for (auto& shapeData : mConeData)
- {
- if ((shapeData.layer & layers) == 0)
- {
- localIdx++;
- continue;
- }
- allShapes.push_back(RawData());
- RawData& rawData = allShapes.back();
- rawData.idx = localIdx++;
- rawData.textIdx = 0;
- rawData.meshType = MeshType::Solid;
- rawData.shapeType = ShapeType::Cone;
- rawData.distance = shapeData.center.distance(reference);
- ShapeMeshes3D::getNumElementsCone(shapeData.quality,
- rawData.numVertices, rawData.numIndices);
- }
- localIdx = 0;
- for (auto& shapeData : mDiscData)
- {
- if ((shapeData.layer & layers) == 0)
- {
- localIdx++;
- continue;
- }
- allShapes.push_back(RawData());
- RawData& rawData = allShapes.back();
- rawData.idx = localIdx++;
- rawData.textIdx = 0;
- rawData.meshType = MeshType::Solid;
- rawData.shapeType = ShapeType::Disc;
- rawData.distance = shapeData.center.distance(reference);
- ShapeMeshes3D::getNumElementsDisc(shapeData.quality,
- rawData.numVertices, rawData.numIndices);
- }
- localIdx = 0;
- for (auto& shapeData : mArcData)
- {
- if ((shapeData.layer & layers) == 0)
- {
- localIdx++;
- continue;
- }
- allShapes.push_back(RawData());
- RawData& rawData = allShapes.back();
- rawData.idx = localIdx++;
- rawData.textIdx = 0;
- rawData.meshType = MeshType::Solid;
- rawData.shapeType = ShapeType::Arc;
- rawData.distance = shapeData.center.distance(reference);
- ShapeMeshes3D::getNumElementsArc(shapeData.quality,
- rawData.numVertices, rawData.numIndices);
- }
- localIdx = 0;
- for (auto& shapeData : mRect3Data)
- {
- if ((shapeData.layer & layers) == 0)
- {
- localIdx++;
- continue;
- }
- allShapes.push_back(RawData());
- RawData& rawData = allShapes.back();
- rawData.idx = localIdx++;
- rawData.textIdx = 0;
- rawData.meshType = MeshType::Solid;
- rawData.shapeType = ShapeType::Rectangle;
- rawData.distance = shapeData.center.distance(reference);
- ShapeMeshes3D::getNumElementsQuad(rawData.numVertices, rawData.numIndices);
- }
- localIdx = 0;
- for (auto& shapeData : mWireCubeData)
- {
- if ((shapeData.layer & layers) == 0)
- {
- localIdx++;
- continue;
- }
- allShapes.push_back(RawData());
- RawData& rawData = allShapes.back();
- rawData.idx = localIdx++;
- rawData.textIdx = 0;
- rawData.meshType = MeshType::Wire;
- rawData.shapeType = ShapeType::WireCube;
- rawData.distance = shapeData.center.distance(reference);
- ShapeMeshes3D::getNumElementsWireAABox(rawData.numVertices, rawData.numIndices);
- }
- localIdx = 0;
- for (auto& shapeData : mWireSphereData)
- {
- if ((shapeData.layer & layers) == 0)
- {
- localIdx++;
- continue;
- }
- allShapes.push_back(RawData());
- RawData& rawData = allShapes.back();
- rawData.idx = localIdx++;
- rawData.textIdx = 0;
- rawData.meshType = MeshType::Wire;
- rawData.shapeType = ShapeType::WireSphere;
- rawData.distance = shapeData.center.distance(reference);
- ShapeMeshes3D::getNumElementsWireSphere(shapeData.quality,
- rawData.numVertices, rawData.numIndices);
- }
- localIdx = 0;
- for (auto& shapeData : mLineData)
- {
- if ((shapeData.layer & layers) == 0)
- {
- localIdx++;
- continue;
- }
- allShapes.push_back(RawData());
- RawData& rawData = allShapes.back();
- rawData.idx = localIdx++;
- rawData.textIdx = 0;
- rawData.meshType = MeshType::Wire;
- rawData.shapeType = ShapeType::Line;
- rawData.distance = shapeData.center.distance(reference);
- rawData.numVertices = 2;
- rawData.numIndices = 2;
- }
- localIdx = 0;
- for (auto& shapeData : mFrustumData)
- {
- if ((shapeData.layer & layers) == 0)
- {
- localIdx++;
- continue;
- }
- allShapes.push_back(RawData());
- RawData& rawData = allShapes.back();
- rawData.idx = localIdx++;
- rawData.textIdx = 0;
- rawData.meshType = MeshType::Wire;
- rawData.shapeType = ShapeType::Frustum;
- rawData.distance = shapeData.center.distance(reference);
- ShapeMeshes3D::getNumElementsFrustum(rawData.numVertices, rawData.numIndices);
- }
- localIdx = 0;
- for (auto& shapeData : mWireDiscData)
- {
- if ((shapeData.layer & layers) == 0)
- {
- localIdx++;
- continue;
- }
- allShapes.push_back(RawData());
- RawData& rawData = allShapes.back();
- rawData.idx = localIdx++;
- rawData.textIdx = 0;
- rawData.meshType = MeshType::Wire;
- rawData.shapeType = ShapeType::WireDisc;
- rawData.distance = shapeData.center.distance(reference);
- ShapeMeshes3D::getNumElementsWireDisc(shapeData.quality,
- rawData.numVertices, rawData.numIndices);
- }
- localIdx = 0;
- for (auto& shapeData : mWireArcData)
- {
- if ((shapeData.layer & layers) == 0)
- {
- localIdx++;
- continue;
- }
- allShapes.push_back(RawData());
- RawData& rawData = allShapes.back();
- rawData.idx = localIdx++;
- rawData.textIdx = 0;
- rawData.meshType = MeshType::Wire;
- rawData.shapeType = ShapeType::WireArc;
- rawData.distance = shapeData.center.distance(reference);
- ShapeMeshes3D::getNumElementsWireArc(shapeData.quality,
- rawData.numVertices, rawData.numIndices);
- }
- struct TextRenderData
- {
- UINT32 page;
- SPtr<TextData<>> textData;
- };
- UnorderedMap<UINT32, TextRenderData> textRenderData;
- UINT32 textIdx = 0;
- localIdx = 0;
- for (auto& shapeData : mText2DData)
- {
- if ((shapeData.layer & layers) == 0)
- {
- localIdx++;
- continue;
- }
- SPtr<TextData<>> textData = bs_shared_ptr_new<TextData<>>(shapeData.text, shapeData.font, shapeData.size);
- UINT32 numPages = textData->getNumPages();
- for (UINT32 j = 0; j < numPages; j++)
- {
- UINT32 numQuads = textData->getNumQuadsForPage(j);
- allShapes.push_back(RawData());
- RawData& rawData = allShapes.back();
- rawData.idx = localIdx;
- rawData.textIdx = textIdx;
- rawData.meshType = MeshType::Text;
- rawData.shapeType = ShapeType::Text;
- rawData.distance = shapeData.center.distance(reference);
- rawData.numVertices = numQuads * 4;
- rawData.numIndices = numQuads * 6;
- TextRenderData& renderData = textRenderData[textIdx];
- renderData.page = j;
- renderData.textData = textData;
- textIdx++;
- idx++;
- }
- localIdx++;
- }
- if (sorting == SortType::FrontToBack)
- {
- std::sort(begin(allShapes), end(allShapes),
- [&](const RawData& x, const RawData& y)
- {
- return x.distance < y.distance;
- });
- }
- else if (sorting == SortType::BackToFront)
- {
- std::sort(begin(allShapes), end(allShapes),
- [&](const RawData& x, const RawData& y)
- {
- return y.distance < x.distance;
- });
- }
- /************************************************************************/
- /* Create batches */
- /************************************************************************/
- struct Batch
- {
- MeshType type;
- HTexture texture;
- UINT32 startIdx;
- UINT32 endIdx;
- UINT32 numVertices;
- UINT32 numIndices;
- };
- UINT32 numShapes = (UINT32)allShapes.size();
- Vector<Batch> batches;
- if (numShapes > 0)
- {
- batches.push_back(Batch());
- {
- Batch& currentBatch = batches.back();
- currentBatch.startIdx = 0;
- currentBatch.type = allShapes[0].meshType;
- currentBatch.numVertices = allShapes[0].numVertices;
- currentBatch.numIndices = allShapes[0].numIndices;
- if (allShapes[0].meshType == MeshType::Text)
- {
- TextRenderData& renderData = textRenderData[allShapes[0].textIdx];
- currentBatch.texture = renderData.textData->getTextureForPage(renderData.page);
- }
- }
- for (UINT32 i = 1; i < numShapes; i++)
- {
- Batch& currentBatch = batches.back();
- HTexture texture;
- bool startNewBatch = false;
- if(allShapes[i].meshType != currentBatch.type)
- {
- startNewBatch = true;
- }
- else
- {
- if(allShapes[i].meshType == MeshType::Text)
- {
- TextRenderData& renderData = textRenderData[allShapes[i].textIdx];
- texture = renderData.textData->getTextureForPage(renderData.page);
- if (texture != currentBatch.texture)
- startNewBatch = true;
- }
- }
- if (startNewBatch)
- {
- currentBatch.endIdx = i - 1;
- batches.push_back(Batch());
- Batch& newBatch = batches.back();
- newBatch.startIdx = i;
- newBatch.type = allShapes[i].meshType;
- newBatch.numVertices = allShapes[i].numVertices;
- newBatch.numIndices = allShapes[i].numIndices;
- newBatch.texture = texture;
- }
- else
- {
- currentBatch.endIdx = i;
- currentBatch.numVertices += allShapes[i].numVertices;
- currentBatch.numIndices += allShapes[i].numIndices;
- }
- }
- {
- Batch& currentBatch = batches.back();
- currentBatch.endIdx = numShapes - 1;
- }
- }
- /************************************************************************/
- /* Generate geometry for each batch */
- /************************************************************************/
- for (auto& batch : batches)
- {
- if (batch.type == MeshType::Solid)
- {
- MeshDataPtr meshData = bs_shared_ptr_new<MeshData>(batch.numVertices, batch.numIndices, mSolidVertexDesc);
- UINT32 curVertexOffset = 0;
- UINT32 curIndexOffet = 0;
- auto positionIter = meshData->getVec3DataIter(VES_POSITION);
- auto normalIter = meshData->getVec3DataIter(VES_NORMAL);
- auto colorIter = meshData->getDWORDDataIter(VES_COLOR);
- for (UINT32 i = batch.startIdx; i <= batch.endIdx; i++)
- {
- RawData& shapeData = allShapes[i];
- Matrix4* transform = nullptr;
- RGBA color = 0;
- switch (shapeData.shapeType)
- {
- case ShapeType::Cube:
- {
- CubeData& cubeData = mSolidCubeData[shapeData.idx];
- AABox box(cubeData.position - cubeData.extents, cubeData.position + cubeData.extents);
- ShapeMeshes3D::solidAABox(box, meshData, curVertexOffset, curIndexOffet);
- transform = &cubeData.transform;
- color = cubeData.color.getAsRGBA();
- }
- break;
- case ShapeType::Sphere:
- {
- SphereData& sphereData = mSolidSphereData[shapeData.idx];
- Sphere sphere(sphereData.position, sphereData.radius);
- ShapeMeshes3D::solidSphere(sphere, meshData, curVertexOffset, curIndexOffet, sphereData.quality);
- transform = &sphereData.transform;
- color = sphereData.color.getAsRGBA();
- }
- break;
- case ShapeType::Cone:
- {
- ConeData& coneData = mConeData[shapeData.idx];
- ShapeMeshes3D::solidCone(coneData.base, coneData.normal, coneData.height, coneData.radius,
- meshData, curVertexOffset, curIndexOffet, coneData.quality);
- transform = &coneData.transform;
- color = coneData.color.getAsRGBA();
- }
- break;
- case ShapeType::Disc:
- {
- DiscData& discData = mDiscData[shapeData.idx];
- ShapeMeshes3D::solidDisc(discData.position, discData.radius, discData.normal,
- meshData, curVertexOffset, curIndexOffet, discData.quality);
- transform = &discData.transform;
- color = discData.color.getAsRGBA();
- }
- break;
- case ShapeType::Arc:
- {
- ArcData& arcData = mArcData[shapeData.idx];
- ShapeMeshes3D::solidArc(arcData.position, arcData.radius, arcData.normal,
- arcData.startAngle, arcData.amountAngle, meshData, curVertexOffset, curIndexOffet, arcData.quality);
- transform = &arcData.transform;
- color = arcData.color.getAsRGBA();
- }
- break;
- case ShapeType::Rectangle:
- {
- Rect3Data rectData = mRect3Data[shapeData.idx];
- ShapeMeshes3D::solidQuad(rectData.area, meshData, curVertexOffset, curIndexOffet);
- transform = &rectData.transform;
- color = rectData.color.getAsRGBA();
- }
- break;
- }
- Matrix4 transformIT = transform->inverseAffine().transpose();
- for (UINT32 i = 0; i < shapeData.numVertices; i++)
- {
- Vector3 worldPos = transform->multiplyAffine(positionIter.getValue());
- Vector3 worldNormal = transformIT.multiplyAffine(normalIter.getValue());
- positionIter.addValue(worldPos);
- normalIter.addValue(worldNormal);
- colorIter.addValue(color);
- }
- curVertexOffset += shapeData.numVertices;
- curIndexOffet += shapeData.numIndices;
- }
- mMeshes.push_back(ShapeMeshData());
- ShapeMeshData& newMesh = mMeshes.back();
- newMesh.mesh = mSolidMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
- newMesh.type = MeshType::Solid;
- }
- else if(batch.type == MeshType::Wire)
- {
- MeshDataPtr meshData = bs_shared_ptr_new<MeshData>(batch.numVertices,
- batch.numIndices, mWireVertexDesc);
- UINT32 curVertexOffset = 0;
- UINT32 curIndexOffet = 0;
- auto positionIter = meshData->getVec3DataIter(VES_POSITION);
- auto colorIter = meshData->getDWORDDataIter(VES_COLOR);
- for (UINT32 i = batch.startIdx; i <= batch.endIdx; i++)
- {
- RawData& shapeData = allShapes[i];
- Matrix4* transform = nullptr;
- RGBA color = 0;
- switch (shapeData.shapeType)
- {
- case ShapeType::WireCube:
- {
- CubeData& cubeData = mWireCubeData[shapeData.idx];
- AABox box(cubeData.position - cubeData.extents, cubeData.position + cubeData.extents);
- ShapeMeshes3D::wireAABox(box, meshData, curVertexOffset, curIndexOffet);
- transform = &cubeData.transform;
- color = cubeData.color.getAsRGBA();
- }
- break;
- case ShapeType::WireSphere:
- {
- SphereData& sphereData = mWireSphereData[shapeData.idx];
- Sphere sphere(sphereData.position, sphereData.radius);
- ShapeMeshes3D::wireSphere(sphere, meshData, curVertexOffset, curIndexOffet, sphereData.quality);
- transform = &sphereData.transform;
- color = sphereData.color.getAsRGBA();
- }
- break;
- case ShapeType::Line:
- {
- LineData& lineData = mLineData[shapeData.idx];
- ShapeMeshes3D::pixelLine(lineData.start, lineData.end, meshData, curVertexOffset, curIndexOffet);
- transform = &lineData.transform;
- color = lineData.color.getAsRGBA();
- }
- break;
- case ShapeType::Frustum:
- {
- FrustumData& frustumData = mFrustumData[shapeData.idx];
- ShapeMeshes3D::wireFrustum(frustumData.position, frustumData.aspect, frustumData.FOV, frustumData.near,
- frustumData.far, meshData, curVertexOffset, curIndexOffet);
- transform = &frustumData.transform;
- color = frustumData.color.getAsRGBA();
- }
- break;
- case ShapeType::WireDisc:
- {
- DiscData& discData = mWireDiscData[shapeData.idx];
- ShapeMeshes3D::wireDisc(discData.position, discData.radius, discData.normal,
- meshData, curVertexOffset, curIndexOffet, discData.quality);
- transform = &discData.transform;
- color = discData.color.getAsRGBA();
- }
- break;
- case ShapeType::WireArc:
- {
- ArcData& arcData = mWireArcData[shapeData.idx];
- ShapeMeshes3D::wireArc(arcData.position, arcData.radius, arcData.normal,
- arcData.startAngle, arcData.amountAngle, meshData, curVertexOffset, curIndexOffet, arcData.quality);
- transform = &arcData.transform;
- color = arcData.color.getAsRGBA();
- }
- break;
- }
- for (UINT32 i = 0; i < shapeData.numVertices; i++)
- {
- Vector3 worldPos = transform->multiplyAffine(positionIter.getValue());
- positionIter.addValue(worldPos);
- colorIter.addValue(color);
- }
- curVertexOffset += shapeData.numVertices;
- curIndexOffet += shapeData.numIndices;
- }
- mMeshes.push_back(ShapeMeshData());
- ShapeMeshData& newMesh = mMeshes.back();
- newMesh.mesh = mWireMeshHeap->alloc(meshData, DOT_LINE_LIST);
- newMesh.type = MeshType::Wire;
- }
- else // Text
- {
- MeshDataPtr meshData = bs_shared_ptr_new<MeshData>(batch.numVertices,
- batch.numIndices, mTextVertexDesc);
- UINT32 curVertexOffset = 0;
- UINT32 curIndexOffet = 0;
- auto positionIter = meshData->getVec3DataIter(VES_POSITION);
- auto uvIter = meshData->getVec2DataIter(VES_TEXCOORD);
- auto colorIter = meshData->getDWORDDataIter(VES_COLOR);
- for (UINT32 i = batch.startIdx; i <= batch.endIdx; i++)
- {
- RawData& shapeData = allShapes[i];
- Text2DData& text2DData = mText2DData[shapeData.idx];
- TextRenderData& renderData = textRenderData[shapeData.textIdx];
- UINT32 numQuads = renderData.textData->getNumQuadsForPage(renderData.page);
- UINT32* indices = meshData->getIndices32();
- // Note: Need temporary buffers because TextLine doesn't support arbitrary vertex stride. Eventually
- // that should be supported (should be almost trivial to implement)
- Vector2* tempVertices = bs_stack_alloc<Vector2>(shapeData.numVertices);
- Vector2* tempUVs = bs_stack_alloc<Vector2>(shapeData.numVertices);
- UINT32 numLines = renderData.textData->getNumLines();
- UINT32 quadOffset = 0;
- for (UINT32 j = 0; j < numLines; j++)
- {
- const TextDataBase::TextLine& line = renderData.textData->getLine(j);
- UINT32 writtenQuads = line.fillBuffer(renderData.page, tempVertices, tempUVs, indices, quadOffset, numQuads);
- quadOffset += writtenQuads;
- }
- for (UINT32 j = 0; j < shapeData.numVertices; j++)
- {
- Vector3 localPos(tempVertices[j].x, tempVertices[j].y, 0.0f);
- Vector3 worldPos = text2DData.transform.multiplyAffine(localPos);
- positionIter.addValue(worldPos);
- uvIter.addValue(tempUVs[j]);
- colorIter.addValue(text2DData.color.getAsRGBA());
- }
- bs_stack_free(tempVertices);
- bs_stack_free(tempUVs);
- curVertexOffset += shapeData.numVertices;
- curIndexOffet += shapeData.numIndices;
- }
- mMeshes.push_back(ShapeMeshData());
- ShapeMeshData& newMesh = mMeshes.back();
- newMesh.mesh = mTextMeshHeap->alloc(meshData, DOT_TRIANGLE_LIST);
- newMesh.type = MeshType::Text;
- }
- }
- }
- void DrawHelper::clearMeshes()
- {
- for (auto meshData : mMeshes)
- {
- if (meshData.type == MeshType::Solid)
- mSolidMeshHeap->dealloc(meshData.mesh);
- else if (meshData.type == MeshType::Wire)
- mWireMeshHeap->dealloc(meshData.mesh);
- else // Text
- mTextMeshHeap->dealloc(meshData.mesh);
- }
- mMeshes.clear();
- }
- }
|