#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>
#include <map>
const int planeRes = 32;
const int sphereRes = 16;
const int lightmapRes = 128;
class SampleScene {
public:
m_scene = 0;
}
bex::Matrix4x4 floorTrans = bex::scaleTranslation(bex::Vec3(10.0f, 1.0f, 10.0f),
bex::Vec3(0.0f, -5.0f, 0.0f));
bex::apiCall(
ILBCreateInstance(m_scene, plane,
"FloorInstance", &floorTrans, &m_floorInstance));
bex::Matrix4x4 trans = bex::scaleTranslation(bex::Vec3(SPHERE_RADIUS, SPHERE_RADIUS, SPHERE_RADIUS),
bex::Vec3(0.0f, SPHERE_RADIUS - 5.0f + 0.1f, 0.0f));
bex::apiCall(
ILBCreateInstance(m_scene, sphere,
"SphereInstance", &trans, &m_sphereInstance));
}
void createLight() {
"Sun",
&bex::directionalLightOrientation(bex::Vec3(1.0, -1.0f, -1.0f)),
&bex::ColorRGB(1.0f, 1.0f, .8f),
&light));
"SkyLight",
&bex::identity(),
&bex::ColorRGB(1.0f, 0.0f, 0.0f),
&skyLight));
}
void createDiffuseMaterial(const std::string& name, const bex::ColorRGBA& diffuseCol) {
}
void createEmissiveMaterial(
const std::string& name,
ILBTextureHandle texture) {
}
void createVertexColorMaterial(const std::string& name) {
}
void createCamera() {
}
void finalize() {
}
return m_scene;
}
return m_floorInstance;
}
return m_sphereInstance;
}
return m_camera;
}
private:
const static float SPHERE_RADIUS;
};
class SampleJob {
public:
SampleJob(
ILBManagerHandle bmh,
const std::string& name, SampleScene& scene,
const std::string& xmlFile) : m_bmh(bmh) {
bex::apiCall(
ILBCreateJob(bmh, name.c_str(), scene.get(), xmlFile.c_str(), &m_job));
createFullShadingPass();
}
void createCameraTarget(SampleScene& scene, unsigned int xres, unsigned int yres) {
m_targets.push_back(target);
}
void createTextureTarget(
const std::string& name,
unsigned int xres,
unsigned int yres,
ILBInstanceHandle instance) {
m_targets.push_back(target);
m_textureTargets[name] = target;
}
m_targets.push_back(target);
m_vertexTargets[name] = std::pair<ILBTargetHandle,ILBTargetEntityHandle>(target,entity);
}
int run(bool returnWhenComplete, bool destroyJob) {
addPassToTargets();
if(!bex::renderJob(m_job, std::cout, returnWhenComplete, destroyJob)) {
return 1;
}
return 0;
}
TextureTargetMap::iterator it = m_textureTargets.find(name);
if (it == m_textureTargets.end()) {
std::cout << "Could not find target " << name << std::endl;
return 0;
}
return bex::copyFrameBuffer(m_bmh, (*it).second, m_pass, name, true);
}
void readVertexColors(const std::string& name, std::vector<float>& colors) {
VertexTargetMap::iterator it = m_vertexTargets.find(name);
if (it == m_vertexTargets.end()) {
std::cout << "Could not find target " << name << std::endl;
return;
}
bex::copyVertexBuffer((*it).second.first, m_pass, (*it).second.second, colors);
}
private:
void addPassToTargets() {
for (size_t i = 0; i < m_targets.size(); i++) {
}
}
void createFullShadingPass() {
}
typedef std::map<std::string, ILBTargetHandle> TextureTargetMap;
typedef std::map<std::string, std::pair<ILBTargetHandle,ILBTargetEntityHandle> > VertexTargetMap;
std::vector<ILBTargetHandle> m_targets;
TextureTargetMap m_textureTargets;
VertexTargetMap m_vertexTargets;
};
const float SampleScene::SPHERE_RADIUS = 4.0f;
void createXML(const std::string& filename, bool gi) {
{
using namespace bex;
std::ofstream ofs(filename.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);
}
if (gi) {
{ScopedTag _x(xml, "GISettings");
xml.data("enableGI", true);
xml.data("fgRays", 1000);
xml.data("fgContrastThreshold", 0.1);
xml.data("fgInterpolationPoints", 15);
xml.data("primaryIntegrator", "FinalGather");
xml.data("secondaryIntegrator", "None");
}
}
}
}
}
const std::string& floorMatName,
const std::string& sphereMatName,
const std::string& xmlFile,
bool lightmaps) {
std::string suffix = lightmaps?"Lightmap":"Vertexcolors";
SampleScene scene(bmh, "RenderScene"+suffix);
scene.createInstances(floorMesh, sphereMesh);
scene.createCamera();
if (lightmaps){
scene.createEmissiveMaterial(floorMatName, floorLightmap);
scene.createEmissiveMaterial(sphereMatName, sphereLightmap);
} else {
scene.createVertexColorMaterial(floorMatName);
scene.createVertexColorMaterial(sphereMatName);
}
scene.finalize();
SampleJob job(bmh, "RenderJob"+suffix, scene, xmlFile);
job.createCameraTarget(scene, 640, 480);
job.run(false, true);
}
int main(char argc, char** argv) {
try {
#if defined(WIN32)
#else
#endif
const std::string sphereMatName = "SphereMaterial";
const std::string floorMatName = "FloorMaterial";
ILBMeshHandle sphereMesh = bex::createSphere(bmh,
"Sphere", sphereMatName, sphereRes, sphereRes);
ILBMeshHandle floorMesh = bex::createPlane(bmh,
"Floor", floorMatName, planeRes, planeRes);
SampleScene bakingScene(bmh, "BakingScene");
bakingScene.createInstances(floorMesh, sphereMesh);
bakingScene.createLight();
bakingScene.createCamera();
bakingScene.createDiffuseMaterial(floorMatName, bex::ColorRGBA(0.7f, 0.7f, 0.7f, 1.0f));
bakingScene.createDiffuseMaterial(sphereMatName, bex::ColorRGBA(.9f, .9f, .9f, 1.0f));
bakingScene.finalize();
std::string xmlFileNameGI = "../../data/bakingGI.xml";
createXML(xmlFileNameGI, true);
SampleJob bakeJob(bmh, "BakeJob", bakingScene, xmlFileNameGI);
bakeJob.createTextureTarget("floorTextureTarget", lightmapRes, lightmapRes, bakingScene.getFloorInstance());
bakeJob.createTextureTarget("sphereTextureTarget", lightmapRes, lightmapRes, bakingScene.getSphereInstance());
bakeJob.createVertexTarget("floorVertexTarget", bakingScene.getFloorInstance());
bakeJob.createVertexTarget("sphereVertexTarget", bakingScene.getSphereInstance());
if (bakeJob.run(true, false)) {
return 1;
}
ILBTextureHandle lightmapFloor = bakeJob.createTextureFromTarget(
"floorTextureTarget");
ILBTextureHandle lightmapSphere = bakeJob.createTextureFromTarget(
"sphereTextureTarget");
std::vector<float> floorVertexColors;
bakeJob.readVertexColors("floorVertexTarget", floorVertexColors);
std::vector<float> sphereVertexColors;
bakeJob.readVertexColors("sphereVertexTarget", sphereVertexColors);
ILBMeshHandle floorMeshColors = bex::createPlane(bmh,
"FloorColor", floorMatName, planeRes, planeRes, &floorVertexColors);
ILBMeshHandle sphereMeshColors = bex::createSphere(bmh,
"SphereColor", sphereMatName, sphereRes, sphereRes, &sphereVertexColors);
std::string xmlFileNameNoGI = "../../data/bakingNoGI.xml";
createXML(xmlFileNameNoGI, false);
displayResults(bmh, floorMeshColors, floorMatName, lightmapFloor,
sphereMeshColors, sphereMatName, lightmapSphere, xmlFileNameNoGI, true);
displayResults(bmh, floorMeshColors, floorMatName, lightmapFloor,
sphereMeshColors, sphereMatName, lightmapSphere, xmlFileNameNoGI, false);
return 0;
} catch(bex::Exception& ex) {
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;
}
}