Tutorial_Generation_swappable.cpp

Tutorial_Generation_swappable.cpp
/*
* Copyright 2016 Autodesk, Inc. All rights reserved.
* Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
* or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
*/
#include "common/basesystemenv.h"
#include "common/basesystemenv.h"
namespace
{
// This tutorial demonstrates how to use exclusive GUID to generate "Swappable" sectors.
// Swappable sectors are two or more geometrical instances of the same object.
// For example: a building before and after it is partially destroyed by a scripted explosion.
// In this situation, all the instances share the same location but will not be loaded together at runtime.
// The generator needs to be configured to avoid generation of overlap data between the multiple
// instances of the object. This is achieved using the exclusive GUIDs.
// A simple producer that creates one main sector (a plane) and two instances of swappable sectors (two orthogonal vertical walls).
class SwappableProducer : public Kaim::GeneratorInputProducer
{
public:
SwappableProducer() {}
virtual KyResult Produce(const Kaim::GeneratorSector& sector, Kaim::ClientInputConsumer& inputConsumer)
{
Kaim::String sectorName(sector.GetName());
Kaim::String base_sector_name("Base_Sector");
Kaim::String swapable_sector1_Name("Swap_Instance_1");
Kaim::String swapable_sector2_Name("Swap_Instance_2");
// Both name and GUID can be used to identify which sector the InputConsumer is requesting.
if (sectorName == base_sector_name)
{
Kaim::Vec3f aTriangle1[3] = { Kaim::Vec3f(0.0f, 0.0f, 0.0f), Kaim::Vec3f(50.0f, 50.0f, 0.0f), Kaim::Vec3f(50.0f, 0.0f, 0.0f) };
Kaim::Vec3f aTriangle2[3] = { Kaim::Vec3f(0.0f, 0.0f, 0.0f), Kaim::Vec3f(0.0f, 50.0f, 0.0f), Kaim::Vec3f(50.0f, 50.0f, 0.0f) };
Kaim::DynamicNavTag aNavTag;
aNavTag.m_blindDataArray.PushBack(42);
inputConsumer.ConsumeTriangle(aTriangle1[0], aTriangle1[1], aTriangle1[2], aNavTag);
inputConsumer.ConsumeTriangle(aTriangle2[0], aTriangle2[1], aTriangle2[2], aNavTag);
return KY_SUCCESS;
}
if (sectorName == swapable_sector1_Name)
{
// A vertical wall along the Y-Axis.
Kaim::Vec3f aTriangle1[3] = { Kaim::Vec3f(25.0f, 0.0f, 0.0f), Kaim::Vec3f(25.0f, 50.0f, 0.0f), Kaim::Vec3f(25.0f, 0.0f, 25.0f) };
Kaim::Vec3f aTriangle2[3] = { Kaim::Vec3f(25.0f, 50.0f, 0.0f), Kaim::Vec3f(25.0f, 50.0f, 25.0f), Kaim::Vec3f(25.0f, 0.0f, 25.0f) };
Kaim::DynamicNavTag bNavTag;
bNavTag.m_blindDataArray.PushBack(13);
inputConsumer.ConsumeTriangle(aTriangle1[0], aTriangle1[1], aTriangle1[2], bNavTag);
inputConsumer.ConsumeTriangle(aTriangle2[0], aTriangle2[1], aTriangle2[2], bNavTag);
}
else if (sectorName == swapable_sector2_Name)
{
// A vertical wall along the X-Axis.
Kaim::Vec3f aTriangle1[3] = { Kaim::Vec3f(0.0f, 25.0f, 0.0f), Kaim::Vec3f(50.0f, 25.0f, 0.0f), Kaim::Vec3f(0.0f, 25.0f, 25.0f) };
Kaim::Vec3f aTriangle2[3] = { Kaim::Vec3f(50.0f, 25.0f, 0.0f), Kaim::Vec3f(50.0f, 25.0f, 25.0f), Kaim::Vec3f(0.0f, 25.0f, 25.0f) };
Kaim::DynamicNavTag bNavTag;
bNavTag.m_blindDataArray.PushBack(13);
inputConsumer.ConsumeTriangle(aTriangle1[0], aTriangle1[1], aTriangle1[2], bNavTag);
inputConsumer.ConsumeTriangle(aTriangle2[0], aTriangle2[1], aTriangle2[2], bNavTag);
}
return KY_SUCCESS;
}
};
class Tutorial_Generation_Swappable : public GeneratorEnv
{
public:
bool GenerateSwappable()
{
// Create the producer using the class coded above.
Kaim::Ptr<SwappableProducer> producer = *KY_NEW SwappableProducer;
// Set up a generator using the Producer and no Glue;
// parallel processing will be disabled.
Kaim::Generator generator(producer);
Kaim::GeneratorInputOutput generatorInputOutput;
generatorInputOutput.m_runOptions.m_doLogReport = true;
generatorInputOutput.m_runOptions.m_doLogReportDetails = true;
// Set the Directory where the navdata and clientInput files
// will be saved.
generator.SetOutputDirectory(RootDir().c_str(), RelativeOutputDir().c_str());
// Add the main sector and the two swappable instances.
Kaim::KyGuid mainGuid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA");
Kaim::KyGuid instance1Guid("BBBBBBBB-BBBB-BBBB-1111-BBBBBBBBBBBB");
Kaim::KyGuid instance2Guid("BBBBBBBB-BBBB-BBBB-2222-BBBBBBBBBBBB");
Kaim::Ptr<Kaim::GeneratorSector> mainSector = *KY_NEW Kaim::GeneratorSector(mainGuid, "Base_Sector");
Kaim::Ptr<Kaim::GeneratorSector> i1Sector = *KY_NEW Kaim::GeneratorSector(instance1Guid, "Swap_Instance_1");
Kaim::Ptr<Kaim::GeneratorSector> i2Sector = *KY_NEW Kaim::GeneratorSector(instance2Guid, "Swap_Instance_2");
generatorInputOutput.AddSector(mainSector);
generatorInputOutput.AddSector(i1Sector);
generatorInputOutput.AddSector(i2Sector);
// Set instances 1 and 2 as "Exclusive", meaning that they will NEVER be loaded together
// even though they share the same location.
Kaim::KyGuid exclusiveGuids[2] = { instance1Guid, instance2Guid };
generatorInputOutput.AddExclusiveGuids(exclusiveGuids, 2);
// This stage is complete, the generator can Generate both sectors;
// no overlap will be generated between instance 1 and 2.
if (generator.Generate(generatorInputOutput) == KY_ERROR)
{
return false;
}
return true;
}
};
#define KT_TEST_ENV_CLASS Tutorial_Generation_Swappable
KT_TEST_ENV {}
KT_TUTORIAL { KT_ASSERT(KT_ENV.GenerateSwappable()) }
}