diff --git a/samples/material_sandbox.h b/samples/material_sandbox.h index ebe0407b53..7a3193a449 100644 --- a/samples/material_sandbox.h +++ b/samples/material_sandbox.h @@ -17,14 +17,16 @@ #ifndef TNT_FILAMENT_SAMPLES_MATERIAL_SANDBOX_H #define TNT_FILAMENT_SAMPLES_MATERIAL_SANDBOX_H -#include +#include +#include +#include +#include +#include #include #include -#include -#include -#include +#include static constexpr uint8_t MATERIAL_UNLIT_PACKAGE[] = { #include "generated/material/sandboxUnlit.inc" diff --git a/samples/web/filaweb.cpp b/samples/web/filaweb.cpp index c106457b7d..ee0b19071c 100644 --- a/samples/web/filaweb.cpp +++ b/samples/web/filaweb.cpp @@ -19,11 +19,15 @@ #include #include +#include + #define STB_IMAGE_IMPLEMENTATION #define STBI_NO_STDIO #define STBI_ONLY_PNG #include +#include + using namespace filament; using namespace std; @@ -45,6 +49,93 @@ extern "C" void mouse(int x, int y, int wx, int wy, int buttons) { namespace filaweb { +void Application::run(SetupCallback setup, AnimCallback animation, ImGuiCallback imgui) { + mAnimation = animation; + mGuiCallback = imgui; + mEngine = Engine::create(Engine::Backend::OPENGL); + mSwapChain = mEngine->createSwapChain(nullptr); + mScene = mEngine->createScene(); + mRenderer = mEngine->createRenderer(); + mView = mEngine->createView(); + mView->setScene(mScene); + mGuiCam = mEngine->createCamera(); + mGuiView = mEngine->createView(); + mGuiView->setClearTargets(false, false, false); + mGuiView->setRenderTarget(View::TargetBufferFlags::DEPTH_AND_STENCIL); + mGuiView->setPostProcessingEnabled(false); + mGuiView->setShadowsEnabled(false); + mGuiView->setCamera(mGuiCam); + mGuiHelper = new filagui::ImGuiHelper(mEngine, mGuiView, ""); + setup(mEngine, mView, mScene); + + // File I/O in WebAssembly does not exist, so tell ImGui to not bother with the ini file. + ImGui::GetIO().IniFilename = nullptr; +} + +void Application::resize(uint32_t width, uint32_t height, double pixelRatio) { + mPixelRatio = pixelRatio; + mView->setViewport({0, 0, width, height}); + mGuiView->setViewport({0, 0, width, height}); + mManipulator.setViewport(width, height); + mGuiCam->setProjection(filament::Camera::Projection::ORTHO, + 0.0, width / pixelRatio, + height / pixelRatio, 0.0, + 0.0, 1.0); + mGuiHelper->setDisplaySize(width / pixelRatio, height / pixelRatio, pixelRatio, pixelRatio); +} + +void Application::mouse(uint32_t x, uint32_t y, int32_t wx, int32_t wy, uint16_t buttons) { + // First, pass the current pointer state to ImGui. + auto& io = ImGui::GetIO(); + if (wx > 0) io.MouseWheelH += 1; + if (wx < 0) io.MouseWheelH -= 1; + if (wy > 0) io.MouseWheel += 1; + if (wy < 0) io.MouseWheel -= 1; + io.MousePos.x = x; + io.MousePos.y = y; + io.MouseDown[0] = buttons & 1; + io.MouseDown[1] = buttons & 2; + io.MouseDown[2] = buttons & 4; + + // Negate Y before pushing values to the manipulator. + y = -y; + wy = -wy; + + // Pass values to the camera manipulator to enable dolly and rotate. + // We do not call call track() because two-button mouse usage is less useful on web. + using namespace math; + static double2 previousMousePosition = double2(x, y); + static uint16_t previousMouseButtons = buttons; + double2 delta = double2(x, y) - previousMousePosition; + previousMousePosition = double2(x, y); + mManipulator.dolly(wy); + if (!io.WantCaptureMouse && buttons == 1 && buttons == previousMouseButtons) { + mManipulator.rotate(delta); + } + previousMouseButtons = buttons; +} + +void Application::render() { + using namespace std::chrono; + + mManipulator.updateCameraTransform(); + + auto milliseconds_since_epoch = system_clock::now().time_since_epoch() / milliseconds(1); + mAnimation(mEngine, mView, milliseconds_since_epoch / 1000.0); + + double now = milliseconds_since_epoch / 1000.0; + static double previous = now; + mGuiHelper->render(now - previous, mGuiCallback); + previous = now; + + if (mRenderer->beginFrame(mSwapChain)) { + mRenderer->render(mView); + mRenderer->render(mGuiView); + mRenderer->endFrame(); + } + mEngine->execute(); +} + Asset getRawFile(const char* name) { // Obtain size from JavaScript. uint32_t nbytes; diff --git a/samples/web/filaweb.h b/samples/web/filaweb.h index aa6c04da67..58e40ea492 100644 --- a/samples/web/filaweb.h +++ b/samples/web/filaweb.h @@ -17,6 +17,8 @@ #ifndef TNT_FILAMENT_SAMPLES_FILAWEB_H #define TNT_FILAMENT_SAMPLES_FILAWEB_H +#include "../app/CameraManipulator.h" + #include #include @@ -29,15 +31,7 @@ #include #include -#include #include -#include - -#include - -#include - -#include "../app/CameraManipulator.h" namespace filaweb { @@ -82,92 +76,10 @@ public: return &app; } - void run(SetupCallback setup, AnimCallback animation, ImGuiCallback imgui = NoopCallback) { - mAnimation = animation; - mGuiCallback = imgui; - mEngine = Engine::create(Engine::Backend::OPENGL); - mSwapChain = mEngine->createSwapChain(nullptr); - mScene = mEngine->createScene(); - mRenderer = mEngine->createRenderer(); - mView = mEngine->createView(); - mView->setScene(mScene); - mGuiCam = mEngine->createCamera(); - mGuiView = mEngine->createView(); - mGuiView->setClearTargets(false, false, false); - mGuiView->setRenderTarget(View::TargetBufferFlags::DEPTH_AND_STENCIL); - mGuiView->setPostProcessingEnabled(false); - mGuiView->setShadowsEnabled(false); - mGuiView->setCamera(mGuiCam); - mGuiHelper = new filagui::ImGuiHelper(mEngine, mGuiView, ""); - setup(mEngine, mView, mScene); - - // File I/O in WebAssembly does not exist, so tell ImGui to not bother with the ini file. - ImGui::GetIO().IniFilename = nullptr; - } - - void resize(uint32_t width, uint32_t height, double pixelRatio) { - mPixelRatio = pixelRatio; - mView->setViewport({0, 0, width, height}); - mGuiView->setViewport({0, 0, width, height}); - mManipulator.setViewport(width, height); - mGuiCam->setProjection(filament::Camera::Projection::ORTHO, - 0.0, width / pixelRatio, - height / pixelRatio, 0.0, - 0.0, 1.0); - mGuiHelper->setDisplaySize(width / pixelRatio, height / pixelRatio, pixelRatio, pixelRatio); - } - - void mouse(uint32_t x, uint32_t y, int32_t wx, int32_t wy, uint16_t buttons) { - // First, pass the current pointer state to ImGui. - auto& io = ImGui::GetIO(); - if (wx > 0) io.MouseWheelH += 1; - if (wx < 0) io.MouseWheelH -= 1; - if (wy > 0) io.MouseWheel += 1; - if (wy < 0) io.MouseWheel -= 1; - io.MousePos.x = x; - io.MousePos.y = y; - io.MouseDown[0] = buttons & 1; - io.MouseDown[1] = buttons & 2; - io.MouseDown[2] = buttons & 4; - - // Negate Y before pushing values to the manipulator. - y = -y; - wy = -wy; - - // Pass values to the camera manipulator to enable dolly and rotate. - // We do not call call track() because two-button mouse usage is less useful on web. - using namespace math; - static double2 previousMousePosition = double2(x, y); - static uint16_t previousMouseButtons = buttons; - double2 delta = double2(x, y) - previousMousePosition; - previousMousePosition = double2(x, y); - mManipulator.dolly(wy); - if (!io.WantCaptureMouse && buttons == 1 && buttons == previousMouseButtons) { - mManipulator.rotate(delta); - } - previousMouseButtons = buttons; - } - - void render() { - using namespace std::chrono; - - mManipulator.updateCameraTransform(); - - auto milliseconds_since_epoch = system_clock::now().time_since_epoch() / milliseconds(1); - mAnimation(mEngine, mView, milliseconds_since_epoch / 1000.0); - - double now = milliseconds_since_epoch / 1000.0; - static double previous = now; - mGuiHelper->render(now - previous, mGuiCallback); - previous = now; - - if (mRenderer->beginFrame(mSwapChain)) { - mRenderer->render(mView); - mRenderer->render(mGuiView); - mRenderer->endFrame(); - } - mEngine->execute(); - } + void run(SetupCallback setup, AnimCallback animation, ImGuiCallback imgui = NoopCallback); + void resize(uint32_t width, uint32_t height, double pixelRatio); + void mouse(uint32_t x, uint32_t y, int32_t wx, int32_t wy, uint16_t buttons); + void render(); CameraManipulator& getManipulator() { return mManipulator; } diff --git a/samples/web/sandbox.cpp b/samples/web/sandbox.cpp index 4b9f98a79f..0c7cb869e0 100644 --- a/samples/web/sandbox.cpp +++ b/samples/web/sandbox.cpp @@ -14,16 +14,16 @@ * limitations under the License. */ -#include +#include "../material_sandbox.h" + +#include "filamesh.h" +#include "filaweb.h" + #include -#include -#include #include #include #include #include -#include -#include #include #include @@ -32,9 +32,7 @@ #include -#include "filamesh.h" -#include "filaweb.h" -#include "../material_sandbox.h" +#include using namespace filament; using namespace filagui; diff --git a/samples/web/suzanne.cpp b/samples/web/suzanne.cpp index 6099aebea8..4dcd45d028 100644 --- a/samples/web/suzanne.cpp +++ b/samples/web/suzanne.cpp @@ -14,6 +14,9 @@ * limitations under the License. */ +#include "filamesh.h" +#include "filaweb.h" + #include #include #include @@ -25,13 +28,10 @@ #include #include -#include - #include #include -#include "filamesh.h" -#include "filaweb.h" +#include using namespace filament; using namespace math; diff --git a/samples/web/triangle.cpp b/samples/web/triangle.cpp index 33ce6eb6f9..e6d5cb55b6 100644 --- a/samples/web/triangle.cpp +++ b/samples/web/triangle.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "filaweb.h" + #include #include #include @@ -24,8 +26,6 @@ #include #include -#include "filaweb.h" - #include using namespace filament;