examples/code/lua/lua.cpp

examples/code/lua/lua.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: LUA
The purpose of this sample is to demonstrate how to create a LUA pass,
render with it and retrieve the results.
*/
#define _USE_MATH_DEFINES // for M_PI
#include <math.h>
#include "primitives.h"
#include "textures.h"
#include "xmlwriter.h"
#include <sstream>
#include <fstream>
#include <iostream>
const unsigned int SPHERES = 3;
const float SPHERE_RADIUS = 3.0f;
int main(char argc, char** argv) {
try {
// Route errors to stderr
#if defined(WIN32)
// Route general info to debug output (i.e output window in
// visual studio)
#else
// Route general info to std output
#endif
// Setup our beast manager
bex::apiCall(ILBCreateManager("../../../temp/lua", ILB_CS_LOCAL, bex::getLicenseKey().c_str(), &bmh));
// Set the path to the Beast binaries
bex::apiCall(ILBSetBeastPath(bmh, "../../../bin"));
// Waste the cache from previous runs if present
bex::apiCall(ILBClearCache(bmh));
// Create ball and a plane meshes
std::string sphereMatName = "SphereMaterial";
std::string boxMatName = "boxMaterial";
ILBMeshHandle sphereMesh = bex::createSphere(bmh, "Sphere", sphereMatName, 32, 32);
ILBMeshHandle cornellMesh = bex::createCornellBox(bmh, "Cornell", boxMatName);
// Create a scene
bex::apiCall(ILBBeginScene(bmh, "BakingScene", &scene));
// Create an instance of the Cornell Box
ILBInstanceHandle cornellInstance;
bex::Matrix4x4 cornellTrans = bex::scaleTranslation(bex::Vec3(10.0f, 10.0f, 10.0f),
bex::Vec3(0.0f, 0.0f, 0.0f));
bex::apiCall(ILBCreateInstance(scene, cornellMesh, "CornellInstance", &cornellTrans, &cornellInstance));
// Create an area light
bex::Vec3 pos = bex::Vec3(0.0f, 9.0f, 0.0f);
bex::Vec3 lookAt(0.0f, 0.0f, 0.0f);
bex::Matrix4x4 matrix = bex::setAreaLightMatrix(pos,
lookAt - pos,
bex::Vec3(1.0f, 0.0f, 0.0f),
bex::Vec2(2.0f, 2.0f));
bex::apiCall(ILBCreateAreaLight(scene,
_T("Light"),
&matrix,
&bex::ColorRGB(1.0f, 1.0f, 1.0f),
&light));
bex::apiCall(ILBSetCastShadows(light, true));
bex::apiCall(ILBSetShadowSamples(light, 48));
// Create the spheres on the plane
const float spherePosRadius = 10.0f - 2.0f*SPHERE_RADIUS;
for(int i = 0; i < SPHERES; ++i) {
float x = (bex::frand() - 0.5f) * spherePosRadius * 2.0f;
float z = (bex::frand() - 0.5f) * spherePosRadius * 2.0f;
bex::Matrix4x4 trans = bex::scaleTranslation(bex::Vec3(SPHERE_RADIUS, SPHERE_RADIUS, SPHERE_RADIUS),
bex::Vec3(x, SPHERE_RADIUS-10.0f, z));
ILBInstanceHandle tempInstance;
std::stringstream sphereName;
sphereName << "SphereInstance_" << i;
bex::apiCall(ILBCreateInstance(scene, sphereMesh, sphereName.str().c_str(), &trans, &tempInstance));
bex::apiCall(ILBSetRenderStats(tempInstance, ILB_RS_SHADOW_BIAS, ILB_RSOP_ENABLE));
}
// Create the floor material
bex::apiCall(ILBCreateMaterial(scene, boxMatName.c_str(), &boxMat));
//bex::apiCall(ILBSetMaterialColor(boxMat, ILB_CC_DIFFUSE, &bex::ColorRGBA(.9f, .2f, .2f, 1.0f)));
// Create the sphere material
ILBMaterialHandle sphereMat;
bex::apiCall(ILBCreateMaterial(scene, sphereMatName.c_str(), &sphereMat));
bex::apiCall(ILBSetMaterialColor(sphereMat, ILB_CC_DIFFUSE, &bex::ColorRGBA(.9f, .9f, .9f, 1.0f)));
// Setup a camera to render from
bex::Vec3 camPos(0.0f, 0.0f, -20.0f);
bex::Vec3 camLookAt(0.0f, -3.0f, 0.0f);
bex::apiCall(ILBCreatePerspectiveCamera(scene,
_T("Camera"),
&bex::setCameraMatrix(camPos, camLookAt-camPos, bex::Vec3(0.0f, 1.0f, 0.0f)),
&camera));
// Finalize the scene
bex::apiCall(ILBEndScene(scene));
std::string xmlFileName = "../../data/lua.xml";
// Create settings xml file
{
using namespace bex;
std::ofstream ofs(xmlFileName.c_str(), std::ios_base::out | std::ios_base::trunc);
XMLWriter xml(ofs);
{ScopedTag _x(xml, "ILConfig");
{ScopedTag _x(xml, "AASettings");
xml.data("minSampleRate", 0);
xml.data("maxSampleRate", 2);
}
{ScopedTag _x(xml, "RenderSettings");
xml.data("bias", 0.00001f);
}
}
}
bex::apiCall(ILBCreateJob(bmh, _T("TestJob"), scene, xmlFileName.c_str(), &job));
// Create passes
bex::apiCall(ILBCreateLuaPass(job, _T("LUA"), "../../data/rnm_3.lua", &luaPass));
bex::apiCall(ILBCreateLuaPass(job, _T("LUA2"), "../../data/rnm_3.lua", &luaPass));
// Create Targets
ILBTargetHandle textureTarget;
bex::apiCall(ILBCreateTextureTarget(job, _T("textureTarget"), 256, 256, &textureTarget));
bex::apiCall(ILBAddBakeInstance(textureTarget, cornellInstance, &entity));
// Add pass to targets
bex::apiCall(ILBAddPassToTarget(textureTarget, luaPass));
// Finally render the scene
if(!bex::renderJob(job, std::cout, true, false, ILB_RD_FORCE_LOCAL)) {
return 1;
}
bex::apiCall(ILBGetFramebuffer(textureTarget, luaPass, 0, &fb));
int32 channelCount;
bex::apiCall(ILBGetChannelCount(fb, &channelCount));
std::cout << "Channel count: " << channelCount << std::endl;
for (int i = 0; i < channelCount; i++) {
bex::apiCall(ILBGetChannelName(fb, i, &sth));
std::cout << i << ": " << bex::convertStringHandle(sth) << std::endl;
}
bex::apiCall(ILBDestroyJob(job));
return 0;
} 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;
}
}