|
| 1 | +// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include <array> |
| 6 | +#include <memory> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +#include "polyscope/types.h" |
| 10 | +#include "polyscope/view.h" |
| 11 | + |
| 12 | +namespace polyscope { |
| 13 | + |
| 14 | +// Forward declarations |
| 15 | +namespace render { |
| 16 | +class FrameBuffer; |
| 17 | +class TextureBuffer; |
| 18 | +class ShaderProgram; |
| 19 | +} // namespace render |
| 20 | + |
| 21 | +// A snapshot of the global view state, used for push/pop when rendering viewports |
| 22 | +struct ViewStateSnapshot { |
| 23 | + glm::mat4x4 viewMat; |
| 24 | + float fov; |
| 25 | + glm::vec3 viewCenter; |
| 26 | + NavigateStyle navigateStyle; |
| 27 | + UpDir upDir; |
| 28 | + FrontDir frontDir; |
| 29 | + ProjectionMode projectionMode; |
| 30 | + float nearClip, farClip; |
| 31 | + float moveScale; |
| 32 | + ViewRelativeMode viewRelativeMode; |
| 33 | + std::array<float, 4> bgColor; |
| 34 | + int bufferWidth, bufferHeight; |
| 35 | + int windowWidth, windowHeight; |
| 36 | + |
| 37 | + // Flight state |
| 38 | + bool midflight; |
| 39 | + float flightStartTime, flightEndTime; |
| 40 | + glm::dualquat flightTargetViewR, flightInitialViewR; |
| 41 | + glm::vec3 flightTargetViewT, flightInitialViewT; |
| 42 | + float flightTargetFov, flightInitialFov; |
| 43 | +}; |
| 44 | + |
| 45 | +// Save/restore the current global view state |
| 46 | +ViewStateSnapshot saveViewState(); |
| 47 | +void restoreViewState(const ViewStateSnapshot& snapshot); |
| 48 | + |
| 49 | + |
| 50 | +class Viewport { |
| 51 | +public: |
| 52 | + Viewport(std::string name, int gridRow, int gridCol); |
| 53 | + ~Viewport(); |
| 54 | + |
| 55 | + // === Identity |
| 56 | + std::string name; |
| 57 | + int gridRow, gridCol; |
| 58 | + |
| 59 | + // === Camera state (independent per viewport) |
| 60 | + glm::mat4x4 viewMat; |
| 61 | + float fov; |
| 62 | + glm::vec3 viewCenter; |
| 63 | + NavigateStyle navigateStyle; |
| 64 | + UpDir upDir; |
| 65 | + FrontDir frontDir; |
| 66 | + ProjectionMode projectionMode; |
| 67 | + float nearClip, farClip; |
| 68 | + float moveScale; |
| 69 | + ViewRelativeMode viewRelativeMode; |
| 70 | + std::array<float, 4> bgColor; |
| 71 | + |
| 72 | + // Flight state |
| 73 | + bool midflight; |
| 74 | + float flightStartTime, flightEndTime; |
| 75 | + glm::dualquat flightTargetViewR, flightInitialViewR; |
| 76 | + glm::vec3 flightTargetViewT, flightInitialViewT; |
| 77 | + float flightTargetFov, flightInitialFov; |
| 78 | + |
| 79 | + // === Pixel region (computed from grid layout + window size) |
| 80 | + int pixelX, pixelY; // lower-left corner in buffer coords |
| 81 | + int pixelWidth, pixelHeight; // size in buffer pixels |
| 82 | + int windowX, windowY; // lower-left corner in window coords |
| 83 | + int windowW, windowH; // size in window pixels |
| 84 | + |
| 85 | + // === Framebuffers (per-viewport) |
| 86 | + std::shared_ptr<render::FrameBuffer> sceneBuffer; |
| 87 | + std::shared_ptr<render::FrameBuffer> sceneBufferFinal; |
| 88 | + std::shared_ptr<render::FrameBuffer> sceneDepthMinFrame; |
| 89 | + std::shared_ptr<render::TextureBuffer> sceneColor, sceneColorFinal, sceneDepth, sceneDepthMin; |
| 90 | + std::shared_ptr<render::ShaderProgram> compositePeel; |
| 91 | + |
| 92 | + // === Methods |
| 93 | + |
| 94 | + // Push this viewport's camera state into the global view:: variables |
| 95 | + void pushViewState(); |
| 96 | + |
| 97 | + // Store the current global view state back into this viewport |
| 98 | + void pullViewState(); |
| 99 | + |
| 100 | + // Recompute pixel dimensions from grid layout and window size |
| 101 | + void updateLayout(int totalBufferW, int totalBufferH, int totalWindowW, int totalWindowH, int gridRows, int gridCols); |
| 102 | + |
| 103 | + // Create or resize per-viewport framebuffers |
| 104 | + void ensureBuffersAllocated(); |
| 105 | + void resizeBuffers(); |
| 106 | + |
| 107 | + // Reset camera to the home view for this viewport |
| 108 | + void resetCameraToHomeView(); |
| 109 | + |
| 110 | + // Set navigate style with proper camera adjustment (use instead of setting navigateStyle directly) |
| 111 | + void setNavigateStyle(NavigateStyle newStyle, bool animateFlight = false); |
| 112 | + |
| 113 | + // Set up direction with proper camera adjustment (use instead of setting upDir directly) |
| 114 | + void setUpDir(UpDir newUpDir, bool animateFlight = false); |
| 115 | + |
| 116 | + // Check if screen coordinates (in window space) fall within this viewport |
| 117 | + bool containsScreenCoords(float x, float y) const; |
| 118 | +}; |
| 119 | + |
| 120 | +} // namespace polyscope |
0 commit comments