Skip to content

Commit

Permalink
Merge pull request GPUOpen-LibrariesAndSDKs#198 from AvKhokhlov/avkho…
Browse files Browse the repository at this point in the history
…khlov/light_for_stanalone

Avkhokhlov/light for stanalone
  • Loading branch information
yozhijk committed Oct 1, 2018
2 parents 371fb99 + 3561ef8 commit d621d05
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 47 deletions.
2 changes: 2 additions & 0 deletions BaikalStandalone/Application/app_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ namespace Baikal

s.image_file_format = m_cmd_parser.GetOption("-iff", s.image_file_format);

s.light_file = m_cmd_parser.GetOption("-lights", s.light_file);

if (m_cmd_parser.OptionExists("-ct"))
{
auto camera_type = m_cmd_parser.GetOption("-ct");
Expand Down
3 changes: 3 additions & 0 deletions BaikalStandalone/Application/app_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ namespace Baikal
std::string base_image_file_name;
std::string image_file_format;

//light file
std::string light_file;

//unused
int num_shadow_rays;
int samplecount;
Expand Down
50 changes: 5 additions & 45 deletions BaikalStandalone/Application/cl_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ THE SOFTWARE.
#include <thread>
#include <chrono>

#include "Application/scene_load_utils.h"

#ifdef ENABLE_DENOISER
#include "PostEffects/wavelet_denoiser.h"
#endif
Expand All @@ -50,7 +52,7 @@ namespace Baikal
AppClRender::AppClRender(AppSettings& settings, GLuint tex) : m_tex(tex), m_output_type(Renderer::OutputType::kColor)
{
InitCl(settings, m_tex);
LoadScene(settings);
InitScene(settings);
}

void AppClRender::InitCl(AppSettings& settings, GLuint tex)
Expand Down Expand Up @@ -143,53 +145,11 @@ namespace Baikal
}


void AppClRender::LoadScene(AppSettings& settings)
void AppClRender::InitScene(AppSettings& settings)
{
rand_init();

// Load obj file
std::string basepath = settings.path;
basepath += "/";
std::string filename = basepath + settings.modelname;

{
m_scene = Baikal::SceneIo::LoadScene(filename, basepath);

{
#ifdef WIN32
#undef LoadImage
#endif
auto image_io(ImageIo::CreateImageIo());
auto ibl_texture = image_io->LoadImage(settings.envmapname);

auto ibl = ImageBasedLight::Create();
ibl->SetTexture(ibl_texture);
m_scene->AttachLight(ibl);
}

// Enable this to generate new materal mapping for a model
#if 0
auto material_io{Baikal::MaterialIo::CreateMaterialIoXML()};
material_io->SaveMaterialsFromScene(basepath + "materials.xml", *m_scene);
material_io->SaveIdentityMapping(basepath + "mapping.xml", *m_scene);
#endif

// Check it we have material remapping
std::ifstream in_materials(basepath + "materials.xml");
std::ifstream in_mapping(basepath + "mapping.xml");

if (in_materials && in_mapping)
{
in_materials.close();
in_mapping.close();

auto material_io = Baikal::MaterialIo::CreateMaterialIoXML();
auto mats = material_io->LoadMaterials(basepath + "materials.xml");
auto mapping = material_io->LoadMaterialMapping(basepath + "mapping.xml");

material_io->ReplaceSceneMaterials(*m_scene, *mats, mapping);
}
}
m_scene = LoadScene(settings);

switch (settings.camera_type)
{
Expand Down
2 changes: 1 addition & 1 deletion BaikalStandalone/Application/cl_render.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ namespace Baikal
#endif
private:
void InitCl(AppSettings& settings, GLuint tex);
void LoadScene(AppSettings& settings);
void InitScene(AppSettings& settings);
void RenderThread(ControlData& cd);

Baikal::Scene1::Ptr m_scene;
Expand Down
148 changes: 148 additions & 0 deletions BaikalStandalone/Application/scene_load_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**********************************************************************
Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
********************************************************************/

#include "scene_load_utils.h"
#include <SceneGraph/scene1.h>
#include <scene_io.h>
#include <image_io.h>
#include <material_io.h>
#include "XML/tinyxml2.h"

using namespace Baikal;

namespace
{
void LoadMaterials(std::string const& basepath, Scene1::Ptr scene)
{
// Check it we have material remapping
std::ifstream in_materials(basepath + "materials.xml");
std::ifstream in_mapping(basepath + "mapping.xml");

if (!in_materials || !in_mapping)
return;

auto material_io = Baikal::MaterialIo::CreateMaterialIoXML();
auto mats = material_io->LoadMaterials(basepath + "materials.xml");
auto mapping = material_io->LoadMaterialMapping(basepath + "mapping.xml");
material_io->ReplaceSceneMaterials(*scene, *mats, mapping);
}

void LoadLights(std::string const& light_file, Scene1::Ptr scene)
{
if (light_file.empty())
{
return;
}

tinyxml2::XMLDocument doc;
doc.LoadFile(light_file.c_str());

auto root = doc.FirstChildElement("light_list");

if (!root)
{
throw std::runtime_error("Failed to open lights set file.");
}

auto elem = root->FirstChildElement("light");

while (elem)
{
Light::Ptr light;
std::string type = elem->Attribute("type");

if (type == "point")
{
light = PointLight::Create();
}
else if (type == "spot")
{
auto spot = SpotLight::Create();
RadeonRays::float2 cone_shape(elem->FloatAttribute("csx"),
elem->FloatAttribute("csy"));
spot->SetConeShape(cone_shape);
light = spot;
}
else if (type == "direct")
{
light = DirectionalLight::Create();
}
else if (type == "ibl")
{
auto image_io = ImageIo::CreateImageIo();
auto tex = image_io->LoadImage(elem->Attribute("tex"));
auto ibl = ImageBasedLight::Create();
ibl->SetTexture(tex);
ibl->SetMultiplier(elem->FloatAttribute("mul"));
light = ibl;
}
else
{
throw std::runtime_error(std::string("Unknown light type: ") + type);
}

RadeonRays::float3 rad;

light->SetPosition({
elem->FloatAttribute("posx"),
elem->FloatAttribute("posy"),
elem->FloatAttribute("posz")});

light->SetDirection({
elem->FloatAttribute("dirx"),
elem->FloatAttribute("diry"),
elem->FloatAttribute("dirz")});

light->SetEmittedRadiance({
elem->FloatAttribute("radx"),
elem->FloatAttribute("rady"),
elem->FloatAttribute("radz")});

scene->AttachLight(light);

elem = elem->NextSiblingElement("light");
}
}
}


Scene1::Ptr LoadScene(Baikal::AppSettings const& settings)
{
#ifdef WIN32
std::string basepath = settings.path + "\\";
#else
std::string basepath = settings.path + "/";
#endif

std::string filename = basepath + settings.modelname;

auto scene = Baikal::SceneIo::LoadScene(filename, basepath);

if (scene == nullptr)
{
throw std::runtime_error("LoadScene(...): cannot create scene");
}

LoadMaterials(basepath, scene);
LoadLights(settings.light_file, scene);
return scene;
}
34 changes: 34 additions & 0 deletions BaikalStandalone/Application/scene_load_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

/**********************************************************************
Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
********************************************************************/

#pragma once

#include <memory>
#include "app_utils.h"

namespace Baikal
{
class Scene1;
}

std::shared_ptr<Baikal::Scene1> LoadScene(Baikal::AppSettings const& settings);
4 changes: 3 additions & 1 deletion BaikalStandalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ set(APPLICATION_SOURCES
Application/graph_scheme.h
Application/graph_scheme.cpp
Application/material_explorer.h
Application/material_explorer.cpp)
Application/material_explorer.cpp
Application/scene_load_utils.h
Application/scene_load_utils.cpp)

set(IMGUI_SORUCES
ImGUI/imconfig.h
Expand Down

0 comments on commit d621d05

Please sign in to comment.