refactor sdlrender example a bit(still w.i.p.)

This commit is contained in:
Syoyo Fujita
2024-07-01 04:52:24 +09:00
parent 7f3ae22884
commit ed64e343c3
4 changed files with 34 additions and 49 deletions

View File

@@ -3,6 +3,10 @@
This viewer uses NanoRT(SW ray tracer) to render the model and display it using SDL2
(So no OpenGL dependency)
## Status
W.I.P.
## Requirements
* C++14 compiler

View File

@@ -15,6 +15,12 @@
#include <emscripten/html5.h>
#endif
// To avoid some C define symbol conflict with SDL(e.g. 'Bool')
// tinyusdz headers muet be included before SDL
#include "tinyusdz.hh"
#include "tydra/render-data.hh"
// ../common/SDL2
#include <SDL.h>
@@ -33,12 +39,11 @@
#include "imnodes.h"
#include "roboto_mono_embed.inc.h"
#include "virtualGizmo3D/vGizmo.h"
#include "trackball.h"
//
#include "gui.hh"
#include "simple-render.hh"
#include "tinyusdz.hh"
#include "trackball.h"
#if defined(USDVIEW_USE_NATIVEFILEDIALOG)
#include "nfd.h"
@@ -89,7 +94,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::RTRenderScene rt_render_scene;
example::Camera camera;
@@ -296,47 +301,21 @@ bool LoadModel(const std::string& filename, 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()) {
std::cerr << "WARN : " << warn << "\n";
}
if (!tinyusdz::IsUSD(filename)) {
std::cerr << "ERR: file not found or file is not USD format : " << filename << "\n";
return false;
}
bool ret = tinyusdz::LoadUSDFromFile(filename, stage, &warn, &err);
if (warn.size()) {
std::cerr << "WARN : " << warn << "\n";
}
if (!ret) {
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 (!ret) {
std::cerr << "Failed to load USDC file: " << filename << "\n";
return false;
}
return false;
}
return true;
@@ -387,7 +366,7 @@ void RenderThread(GUIContext* ctx) {
continue;
}
example::Render(ctx->render_scene, ctx->camera, &ctx->aov);
example::Render(ctx->rt_render_scene, ctx->camera, &ctx->aov);
ctx->update_texture = true;
@@ -842,7 +821,7 @@ int main(int argc, char** argv) {
//}
// Setup render mesh
if (!gui_ctx.render_scene.Setup()) {
if (!gui_ctx.rt_render_scene.Setup()) {
std::cerr << "Failed to setup render mesh.\n";
exit(-1);
}

View File

@@ -489,7 +489,7 @@ void BuildCameraFrame(float3* origin, float3* corner, float3* u, float3* v,
}
}
bool Render(const RenderScene& scene, const Camera& cam, AOV* output) {
bool Render(const RTRenderScene& scene, const Camera& cam, AOV* output) {
int width = output->width;
int height = output->height;
@@ -680,7 +680,7 @@ bool Render(const RenderScene& scene, const Camera& cam, AOV* output) {
return true;
}
bool RenderLines(int start_y, int end_y, const RenderScene& scene,
bool RenderLines(int start_y, int end_y, const RTRenderScene& scene,
const Camera& cam, AOV* output) {
int width = output->width;
int height = output->height;
@@ -835,7 +835,7 @@ bool RenderLines(int start_y, int end_y, const RenderScene& scene,
return true;
}
bool RenderScene::Setup() {
bool RTRenderScene::Setup() {
//
// Construct scene
//

View File

@@ -4,7 +4,9 @@
#include "nanort.h"
#include "nanosg.h"
//
#include "tinyusdz.hh"
#include "tydra/render-data.hh"
namespace example {
@@ -201,7 +203,7 @@ struct Image {
int32_t channels{-1}; // e.g. 3 for RGB.
};
class RenderScene {
class RTRenderScene {
public:
std::vector<DrawGeomMesh> draw_meshes;
std::vector<Material> materials;
@@ -215,11 +217,11 @@ class RenderScene {
bool Setup();
};
bool Render(const RenderScene &scene, const Camera &cam, AOV *output);
bool Render(const RTRenderScene &scene, const Camera &cam, AOV *output);
// Render images for lines [start_y, end_y]
// single-threaded. for webassembly.
bool RenderLines(int start_y, int end_y, const RenderScene &scene,
bool RenderLines(int start_y, int end_y, const RTRenderScene &scene,
const Camera &cam, AOV *output);
} // namespace example