Skip to content

Commit

Permalink
w.i.p.
Browse files Browse the repository at this point in the history
  • Loading branch information
syoyo committed Jul 6, 2024
1 parent 7f3ae22 commit f89a18e
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 53 deletions.
81 changes: 32 additions & 49 deletions examples/sdlviewer/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
//
#include "gui.hh"
#include "simple-render.hh"
#include "tinyusdz.hh"
#include "trackball.h"
//
#include "tinyusdz.hh"
#include "tydra/render-data.hh" // To convert USD Stage to OpenGL/Vulkan friendly data structure.

#if defined(USDVIEW_USE_NATIVEFILEDIALOG)
#include "nfd.h"
Expand Down Expand Up @@ -89,7 +91,7 @@ struct GUIContext {
// std::array<float, 3> lookat = {0.0f, 0.0f, 0.0f};
// std::array<float, 3> up = {0.0f, 1.0f, 0.0f};

example::RenderScene render_scene;
//example::RenderScene render_scene;

example::Camera camera;

Expand All @@ -102,11 +104,22 @@ struct GUIContext {
int render_width = 512;
int render_height = 512;

// scene reload
tinyusdz::Stage stage;
std::atomic<bool> request_reload{false};
std::string filename;

// RenderScene: Scene graph object which is suited for GL/Vulkan renderer.
// Constructed from `tinyusdz::Stage`
tinyusdz::tydra::RenderScene render_scene;

// scene reload
std::atomic<bool> request_reload{false};

tinyusdz::AssetResolutionResolver arr;

// USDZ specific
// Byte range info for assets(textures, audio, etc)
tinyusdz::USDZAsset usdz_asset;

#if __EMSCRIPTEN__ || defined(EMULATE_EMSCRIPTEN)
bool render_finished{false};
int current_render_line = 0;
Expand Down Expand Up @@ -290,53 +303,23 @@ static void ScreenActivate(SDL_Window* window) {
#endif
}

bool LoadModel(const std::string& filename, tinyusdz::Stage* stage) {
std::string ext = str_tolower(GetFileExtension(filename));
bool LoadModel(const std::string& filename, /* out */tinyusdz::Stage* stage) {

std::string warn;
std::string err;

if (ext.compare("usdz") == 0) {
std::cout << "usdz\n";
bool ret = tinyusdz::LoadUSDZFromFile(filename, stage, &warn, &err);
if (!warn.empty()) {
bool ret = tinyusdz::LoadUSDFromFile(filename, stage, &warn, &err);
if (!warn.empty()) {
std::cerr << "WARN : " << warn << "\n";
}
if (!err.empty()) {
std::cerr << "ERR : " << err << "\n";
}

if (!ret) {
std::cerr << "Failed to load USDZ file: " << filename << "\n";
return false;
}
} else if (ext.compare("usda") == 0) {
std::cout << "usda\n";
bool ret = tinyusdz::LoadUSDAFromFile(filename, stage, &warn, &err);
if (!warn.empty()) {
std::cerr << "WARN : " << warn << "\n";
}
if (!err.empty()) {
std::cerr << "ERR : " << err << "\n";
}
}

if (!ret) {
std::cerr << "Failed to load USDA file: " << filename << "\n";
return false;
}
} else { // assume usdc
bool ret = tinyusdz::LoadUSDCFromFile(filename, stage, &warn, &err);
if (!warn.empty()) {
std::cerr << "WARN : " << warn << "\n";
}
if (!err.empty()) {
std::cerr << "ERR : " << err << "\n";
}
if (!err.empty()) {
std::cerr << "ERR : " << err << "\n";
}

if (!ret) {
std::cerr << "Failed to load USDC file: " << filename << "\n";
return false;
}
if (!ret) {
std::cerr << "Failed to load USD file: " << filename << "\n";
return false;
}

return true;
Expand Down Expand Up @@ -387,7 +370,7 @@ void RenderThread(GUIContext* ctx) {
continue;
}

example::Render(ctx->render_scene, ctx->camera, &ctx->aov);
//example::Render(ctx->render_scene, ctx->camera, &ctx->aov);

ctx->update_texture = true;

Expand Down Expand Up @@ -842,10 +825,10 @@ int main(int argc, char** argv) {
//}

// Setup render mesh
if (!gui_ctx.render_scene.Setup()) {
std::cerr << "Failed to setup render mesh.\n";
exit(-1);
}
//if (!gui_ctx.render_scene.Setup()) {
// std::cerr << "Failed to setup render mesh.\n";
// exit(-1);
//}
std::cout << "Setup render mesh\n";
}

Expand Down
68 changes: 66 additions & 2 deletions examples/sdlviewer/simple-render.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#include "simple-render.hh"

#include <atomic>
#include <cassert>
#include <thread>

// tinyusdz Tydra
#include "tydra/render-data.hh"

//
#include "simple-render.hh"

#include "nanort.h"
#include "nanosg.h"

Expand Down Expand Up @@ -835,7 +840,66 @@ bool RenderLines(int start_y, int end_y, const RenderScene& scene,
return true;
}

bool RenderScene::Setup() {
bool RenderScene::SetupFromUSDFile(const std::string &usd_filename) {

// Convert USD Scene(Stage) to Vulkan-friendly scene data using TinyUSDZ Tydra
tinyusdz::tydra::RenderScene render_scene;
tinyusdz::tydra::RenderSceneConverter converter;
tinyusdz::tydra::RenderSceneConverterEnv env(stage);

std::string warn, err;

bool is_usdz = tinyusdz::IsUSDZ(usd_filename);

// In default, RenderSceneConverter triangulate meshes and build single vertex ind ex.
// You can explicitly enable triangulation and vertex-indices build by
//env.mesh_config.triangulate = true;
//env.mesh_config.build_vertex_indices = true;

// Load textures as stored representaion(e.g. 8bit sRGB texture is read as 8bit sR GB)
env.material_config.linearize_color_space = false;
env.material_config.preserve_texel_bitdepth = true;

std::string usd_basedir = tinyusdz::io::GetBaseDir(usd_filename);

tinyusdz::USDZAsset usdz_asset;

if (is_usdz) {
// Setup AssetResolutionResolver to read a asset(file) from memory.
if (!tinyusdz::ReadUSDZAssetInfoFromFile(usd_filename, &usdz_asset, &warn, &err )) {
std::cerr << "Failed to read USDZ assetInfo from file: " << err << "\n";
return;
}

if (warn.size()) {
std::cout << warn << "\n";
}

tinyusdz::AssetResolutionResolver arr;

// NOTE: Pointer address of usdz_asset must be valid until the call of RenderSce neConverter::ConvertToRenderScene.
if (!tinyusdz::SetupUSDZAssetResolution(arr, &usdz_asset)) {
std::cerr << "Failed to setup AssetResolution for USDZ asset\n";
return;
};

env.asset_resolver = arr;

} else {
env.set_search_paths({usd_basedir});
}

env.timecode = tinyusdz::value::TimeCode::Default();
bool ret = converter.ConvertToRenderScene(env, &render_scene);
if (!ret) {
std::cerr << "Failed to convert USD Stage to RenderScene: \n" << converter.GetError() << "\n";
return;
}

if (converter.GetWarning().size()) {
std::cout << "ConvertToRenderScene warn: " << converter.GetWarning() << "\n";
}

//
// Construct scene
//
Expand Down
4 changes: 2 additions & 2 deletions examples/sdlviewer/simple-render.hh
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ class RenderScene {
std::vector<nanosg::Node<float, DrawGeomMesh>> nodes;
nanosg::Scene<float, DrawGeomMesh> scene;

// Convert meshes and build BVH
bool Setup();
// Convert scene and build BVH
bool SetupFromStage(const tinyusdz::Stage &stage);
};

bool Render(const RenderScene &scene, const Camera &cam, AOV *output);
Expand Down

0 comments on commit f89a18e

Please sign in to comment.