Skip to content

Commit

Permalink
refactor sdlrender example a bit(still w.i.p.)
Browse files Browse the repository at this point in the history
  • Loading branch information
syoyo committed Jun 30, 2024
1 parent 7f3ae22 commit ed64e34
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 47 deletions.
4 changes: 4 additions & 0 deletions examples/sdlviewer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 20 additions & 41 deletions examples/sdlviewer/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -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"
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 (!err.empty()) {
std::cerr << "ERR : " << err << "\n";
}
if (!tinyusdz::IsUSD(filename)) {
std::cerr << "ERR: file not found or file is not USD format : " << filename << "\n";
return false;
}

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";
}
bool ret = tinyusdz::LoadUSDFromFile(filename, stage, &warn, &err);
if (warn.size()) {
std::cerr << "WARN : " << warn << "\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 (!ret) {
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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/sdlviewer/simple-render.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
//
Expand Down
8 changes: 5 additions & 3 deletions examples/sdlviewer/simple-render.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

#include "nanort.h"
#include "nanosg.h"
//
#include "tinyusdz.hh"
#include "tydra/render-data.hh"

namespace example {

Expand Down Expand Up @@ -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;
Expand All @@ -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

0 comments on commit ed64e34

Please sign in to comment.