examples-physical/code/baking-live/baking-live.cpp

examples-physical/code/baking-live/baking-live.cpp
/*
Copyright 2014 Autodesk, Inc. All rights reserved.
Use of this software is subject to the terms of the Autodesk license agreement
provided at the time of installation or download, or which otherwise
accompanies this software in either electronic or hard copy form.
*/
/*
Beast API Sample: Live bake with physical materials
The purpose of this sample is to demonstrate how to:
1. Bake different combinations of passes and lighting modes with a physical material
*/
#include "samplescommon.h"
// Make sure we have a unicode safe cout define
#ifdef UNICODE
#define tcout std::wcout
#else
#define tcout std::cout
#endif
// An animator that changes the resolution of the texture target entity once every 8 second
class ResizeAnimator : public bex::SceneUpdater {
public:
ResizeAnimator(ILBTargetEntityHandle targetEntity) :
m_targetEntity(targetEntity),
m_timer(0.0f),
m_even(true)
{
}
virtual void update(float deltaTime) {
const float timerInterval = 10.0f;
m_timer += deltaTime;
if (m_timer >= timerInterval) {
m_timer -= timerInterval;
if (m_even) {
ILBSetBakeResolution(m_targetEntity, 512, 256);
} else {
ILBSetBakeResolution(m_targetEntity, 256, 64);
}
m_even = !m_even;
}
}
private:
ILBTargetEntityHandle m_targetEntity;
float m_timer;
bool m_even;
};
int main(char argc, char** argv) {
try {
// Setup the beast manager
ILBManagerHandle bmh = bex::setupBeastManager(_T("baking-live-physical"));
// Create an empty scene
bex::apiCall(ILBBeginPhysicalScene(bmh, "PhysicalFinalScene", &scene));
bex::setupCamera(scene);
bex::apiCall(ILBEndScene(scene));
// Create a live Ernst job
bex::apiCall(ILBCreateLiveErnstJob(bmh, _T("TestJob"), scene, &job));
// Create target
// NOTE: Must be created before the job is started. One target can be used for all baked entities so you never need more than one when doing live
ILBTargetHandle textureTarget;
bex::apiCall(ILBCreateTextureTarget(job, _T("TextureTarget0"), 1, 1, &textureTarget)); // Resolution here is irrelevant and it is overridden below
// Start the job
// Create the entire scene with meshes, textures, materials, light sources and instances live
bex::InstanceMap instances;
bex::setupTestScene(bmh, scene, instances);
// Add bake instances to the target (live)
ILBTargetEntityHandle textureTargetEntity;
bex::apiCall(ILBAddBakeInstance(textureTarget, instances[_T("FloorInstance")], &textureTargetEntity));
bex::apiCall(ILBSetBakeResolution(textureTargetEntity, 512, 256));
// Set Ernst to bake whole texture and not just parts visible from the camera
// We use an animator to show that it is possible to change bake resolution while running
ResizeAnimator animator(textureTargetEntity);
// Render and display the result
int returnCode = 0;
if (!bex::displayLiveJob(job, tcout, textureTarget, &animator)) {
returnCode = 1;
}
// Destroy the job
bex::apiCall(ILBDestroyJob(job));
return returnCode;
} catch(bex::Exception& ex) {
ILBStringHandle errorString;
ILBStringHandle extendedError;
ILBErrorToString(ex.status, &errorString);
std::cout << "Beast API error" << std::endl;
std::cout << "Error: " << bex::convertStringHandle(errorString) << std::endl;
std::cout << "Info: " << bex::convertStringHandle(extendedError) << std::endl;
return 1;
} catch(std::exception& ex) {
std::cout << "Standard exception" << std::endl;
std::cout << "Error: " << ex.what() << std::endl;;
return 1;
}
}