Skip to content

Commit d559763

Browse files
committed
Merge branch 'main' into public
2 parents a2bba34 + cb96376 commit d559763

File tree

12 files changed

+327
-527
lines changed

12 files changed

+327
-527
lines changed

demos/vinyl/data/vinyl.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,10 +707,9 @@
707707
{
708708
"Type": "nap::Snapshot",
709709
"mID": "Snapshot",
710-
"Width": 1920,
711-
"Height": 1080,
712-
"MaxCellWidth": 1920,
713-
"MaxCellHeight": 1080,
710+
"Width": 3840,
711+
"Height": 2160,
712+
"Divisor": 4,
714713
"OutputDirectory": "snapshots",
715714
"ImageFileFormat": "PNG",
716715
"TextureFormat": "RGBA8",

modules/napparametergui/src/parametergui.cpp

Lines changed: 83 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ namespace nap
2525
{
2626
if (ImGui::BeginPopupModal("Load", nullptr, ImGuiWindowFlags_AlwaysAutoResize))
2727
{
28-
mPresets = mParameterService.getPresets(*mParameterGroup);
2928
if (!mPresets.empty() && mSelectedPresetIndex == -1)
3029
mSelectedPresetIndex = 0;
3130

@@ -36,27 +35,32 @@ namespace nap
3635
return true;
3736
}, &mPresets, mPresets.size());
3837

39-
if (ImGui::Button("OK"))
38+
if (!mPresets.empty())
4039
{
41-
utility::ErrorState errorState;
42-
if (mParameterService.loadPreset(*mParameterGroup, mPresets[mSelectedPresetIndex], errorState))
43-
ImGui::CloseCurrentPopup();
44-
else
45-
ImGui::OpenPopup("Failed to load preset");
46-
47-
if (ImGui::BeginPopupModal("Failed to load preset"))
40+
if (ImGui::Button("OK"))
4841
{
49-
ImGui::Text(errorState.toString().c_str());
50-
if (ImGui::Button("OK"))
42+
utility::ErrorState errorState;
43+
if (mParameterService.loadPreset(*mParameterGroup, mPresets[mSelectedPresetIndex], errorState))
5144
{
5245
ImGui::CloseCurrentPopup();
5346
}
47+
else
48+
ImGui::OpenPopup("Failed to load preset");
5449

55-
ImGui::EndPopup();
50+
if (ImGui::BeginPopupModal("Failed to load preset"))
51+
{
52+
ImGui::Text(errorState.toString().c_str());
53+
if (ImGui::Button("OK"))
54+
{
55+
ImGui::CloseCurrentPopup();
56+
}
57+
58+
ImGui::EndPopup();
59+
}
5660
}
61+
ImGui::SameLine();
5762
}
5863

59-
ImGui::SameLine();
6064
if (ImGui::Button("Cancel"))
6165
{
6266
restorePresetState();
@@ -123,36 +127,39 @@ namespace nap
123127

124128
if (ImGui::Button("OK"))
125129
{
126-
utility::ErrorState errorState;
127-
if (mParameterService.savePreset(*mParameterGroup, mPresets[mSelectedPresetIndex], errorState))
130+
if (mSelectedPresetIndex != -1)
128131
{
129-
ImGui::CloseCurrentPopup();
130-
std::string previous_selection = mPresets[mSelectedPresetIndex];
131-
132-
// After we have retrieved the filenames from the service, the list may be in a different order,
133-
// so we search for the item in the list to find the selected index.
134-
mPresets = mParameterService.getPresets(*mParameterGroup);
135-
for (int index = 0; index < mPresets.size(); ++index)
132+
utility::ErrorState errorState;
133+
if (mParameterService.savePreset(*mParameterGroup, mPresets[mSelectedPresetIndex], errorState))
136134
{
137-
if (mPresets[index] == previous_selection)
135+
ImGui::CloseCurrentPopup();
136+
std::string previous_selection = mPresets[mSelectedPresetIndex];
137+
138+
// After we have retrieved the filenames from the service, the list may be in a different order,
139+
// so we search for the item in the list to find the selected index.
140+
mPresets = mParameterService.getPresets(*mParameterGroup);
141+
for (int index = 0; index < mPresets.size(); ++index)
138142
{
139-
mSelectedPresetIndex = index;
140-
break;
143+
if (mPresets[index] == previous_selection)
144+
{
145+
mSelectedPresetIndex = index;
146+
break;
147+
}
141148
}
142-
}
143149

144-
}
145-
else
146-
{
147-
ImGui::OpenPopup("Failed to save preset");
148-
}
150+
}
151+
else
152+
{
153+
ImGui::OpenPopup("Failed to save preset");
154+
}
149155

150-
if (ImGui::BeginPopupModal("Failed to save preset"))
151-
{
152-
ImGui::Text(errorState.toString().c_str());
153-
if (ImGui::Button("OK"))
154-
ImGui::CloseCurrentPopup();
155-
ImGui::EndPopup();
156+
if (ImGui::BeginPopupModal("Failed to save preset"))
157+
{
158+
ImGui::Text(errorState.toString().c_str());
159+
if (ImGui::Button("OK"))
160+
ImGui::CloseCurrentPopup();
161+
ImGui::EndPopup();
162+
}
156163
}
157164
}
158165

@@ -181,7 +188,7 @@ namespace nap
181188
}
182189

183190

184-
void ParameterGUI::showParameters(ParameterGroup& parameterGroup)
191+
void ParameterGUI::showParameters(ParameterGroup& parameterGroup, int depth)
185192
{
186193
for (auto& parameter : parameterGroup.mParameters)
187194
{
@@ -193,13 +200,24 @@ namespace nap
193200
ImGui::PopID();
194201
}
195202

203+
// Add indentation if depth is higher than zero
204+
if (depth > 0)
205+
{
206+
ImGui::Indent(ImGui::GetStyle().FramePadding.x + 1.0f);
207+
}
196208
for (auto& child : parameterGroup.mChildren)
197209
{
198210
if (ImGui::CollapsingHeader(child->mID.c_str()))
199211
{
200-
showParameters(*child);
212+
// Increment the current depth to keep track of the indentation level
213+
showParameters(*child, depth+1);
201214
}
202215
}
216+
// Undo previously added indentation
217+
if (depth > 0)
218+
{
219+
ImGui::Unindent(ImGui::GetStyle().FramePadding.x + 1.0f);
220+
}
203221
}
204222

205223

@@ -226,7 +244,6 @@ namespace nap
226244
ImGui::CloseCurrentPopup();
227245
ImGui::EndPopup();
228246
}
229-
230247
}
231248
else
232249
{
@@ -249,6 +266,7 @@ namespace nap
249266
{
250267
ImGui::OpenPopup("Load");
251268
savePresetState();
269+
mPresets = mParameterService.getPresets(*mParameterGroup);
252270
}
253271

254272
handleLoadPresetPopup();
@@ -275,10 +293,34 @@ namespace nap
275293
}
276294

277295

296+
bool ParameterGUI::load(std::string preset, utility::ErrorState& errorState)
297+
{
298+
if (!errorState.check(mParameterGroup != nullptr, "No parameter group to reference"))
299+
return false;
300+
301+
mPresets = mParameterService.getPresets(*mParameterGroup);
302+
if (!errorState.check(!mPresets.empty(), "No presets found"))
303+
return false;
304+
305+
const auto it = std::find(mPresets.begin(), mPresets.end(), preset);
306+
if (!errorState.check(it != mPresets.end(), "Preset %s not found", preset.c_str()))
307+
return false;
308+
309+
if (!mParameterService.loadPreset(*mParameterGroup, *it, errorState))
310+
{
311+
errorState.fail("Failed to load preset %s", (*it).c_str());
312+
return false;
313+
}
314+
315+
mSelectedPresetIndex = it - mPresets.begin();
316+
return true;
317+
}
318+
319+
278320
bool ParameterGUI::init(utility::ErrorState& errorState)
279321
{
280322
if (!errorState.check(mParameterGroup != nullptr, "No parameter group to display"))
281323
return false;
282324
return true;
283325
}
284-
}
326+
}

modules/napparametergui/src/parametergui.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ namespace nap
3737
*/
3838
void show(bool newWindow = true);
3939

40+
/**
41+
* Load a preset programmatically
42+
* @param preset the filename of the preset to load
43+
* @param errorState contains the error if the preset load failed
44+
* @return if the preset loaded successfully
45+
*/
46+
bool load(std::string preset, utility::ErrorState& errorState);
47+
4048
/**
4149
* Initializes the parameter GUI
4250
* @param errorState contains the error if initialization failed
@@ -79,9 +87,11 @@ namespace nap
7987
void restorePresetState();
8088

8189
/**
82-
* Display all parameters as GUI elements.
90+
* Display all parameters as GUI elements. This function is recursive.
91+
* @param parameterGroup the parameter group to display
92+
* @param depth the current recursion depth. This is zero by default and is incremented automatically when dealing with nested parameter groups.
8393
*/
84-
void showParameters(ParameterGroup& parameterGroup);
94+
void showParameters(ParameterGroup& parameterGroup, int depth = 0);
8595

8696
private:
8797
ParameterService& mParameterService; ///< The parameter service

modules/naprender/src/perspcameracomponent.cpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <glm/gtc/matrix_transform.hpp>
1010
#include <entity.h>
1111
#include "transformcomponent.h"
12+
#include "mathutils.h"
1213

1314
RTTI_BEGIN_CLASS(nap::PerpCameraProperties)
1415
RTTI_PROPERTY("FieldOfView", &nap::PerpCameraProperties::mFieldOfView, nap::rtti::EPropertyMetaData::Default)
@@ -185,31 +186,18 @@ namespace nap
185186
return projection;
186187
}
187188

189+
188190
// Helper function to calculate either left/right or top/bottom camera planes. The output is the physical location of the near plane in camera space.
189191
static void calculateCameraPlanes(float fov, float aspectRatio, float nearPlane, int numDimensions, int location, float& min, float& max)
190192
{
191193
assert(location < numDimensions);
192194
assert(numDimensions > 0);
193195

194-
const float start_angle = -fov * 0.5f;
195-
const float split_angle = fov / (float)numDimensions;
196-
const float min_angle = start_angle + split_angle * (float)location;
197-
const float max_angle = min_angle + split_angle;
198-
min = glm::tan(min_angle) * aspectRatio * nearPlane;
199-
max = glm::tan(max_angle) * aspectRatio * nearPlane;
200-
}
196+
const float angle_extent = fov * 0.5f;
197+
const float near_extent = glm::tan(angle_extent) * aspectRatio * nearPlane;
201198

202-
// Helper function to calculate either left/right or top/bottom camera planes. The output is the physical location of the near plane in camera space.
203-
static void calculateCameraPlanes(float fov, float aspectRatio, float nearPlane, float rectDimMin, float rectDimMax, float& min, float& max)
204-
{
205-
assert(rectDimMin >= 0.f);
206-
assert(rectDimMax >= 0.f);
207-
208-
const float start_angle = -fov * 0.5f;
209-
const float min_angle = start_angle + fov * rectDimMin;
210-
const float max_angle = start_angle + fov * rectDimMax;
211-
min = glm::tan(min_angle) * aspectRatio * nearPlane;
212-
max = glm::tan(max_angle) * aspectRatio * nearPlane;
199+
min = math::lerp(-near_extent, near_extent, location / static_cast<float>(numDimensions));
200+
max = math::lerp(-near_extent, near_extent, (location+1) / static_cast<float>(numDimensions));
213201
}
214202

215203
// Hook up attribute changes
@@ -241,7 +229,7 @@ namespace nap
241229
}
242230

243231

244-
// Use this function to split the projection into a grid of same-sized rectangles.
232+
// Use this function to split the projection into a regular grid
245233
void PerspCameraComponentInstance::setGridDimensions(int numRows, int numColumns)
246234
{
247235
if (numColumns != mProperties.mGridDimensions.x || numRows != mProperties.mGridDimensions.y)
@@ -290,11 +278,10 @@ namespace nap
290278
const float aspect_ratio = ((float)(getRenderTargetSize().x * mProperties.mGridDimensions.x)) / ((float)(getRenderTargetSize().y * mProperties.mGridDimensions.y));
291279

292280
float left, right, top, bottom;
293-
294281
calculateCameraPlanes(fov, aspect_ratio, near_plane, mProperties.mGridDimensions.x, mProperties.mGridLocation.x, left, right);
295282
calculateCameraPlanes(fov, 1.0f, near_plane, mProperties.mGridDimensions.y, mProperties.mGridLocation.y, bottom, top);
296-
297283
mRenderProjectionMatrix = createASymmetricProjection(near_plane, far_plane, left, right, top, bottom);
284+
298285
mProjectionMatrix = glm::perspective(fov, aspect_ratio, near_plane, far_plane);
299286
mDirty = false;
300287
}
@@ -327,4 +314,4 @@ namespace nap
327314
components.push_back(RTTI_OF(TransformComponent));
328315
}
329316

330-
}
317+
}

modules/naprender/src/perspcameracomponent.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ namespace nap
8484
virtual const glm::mat4 getViewMatrix() const override;
8585

8686
/**
87-
* Use this function to split the projection into a grid of squares. This can be used to render to multiple screens
87+
* Use this function to split the projection into a regular grid. This can be used to render to multiple screens
8888
* with a single camera. Use setGridLocation to set the horizontal and vertical index into this grid.
8989
* @param numRows the number of cells in the horizontal
9090
* @param numColumns the number of cells in the vertical direction.
9191
*/
9292
void setGridDimensions(int numRows, int numColumns);
9393

9494
/**
95-
* Sets the horizontal and vertical index into the projection grid as set by setSplitDimensions.
95+
* Sets the horizontal and vertical index into the projection grid as set by setGridDimensions.
9696
*/
9797
void setGridLocation(int row, int column);
9898

@@ -129,10 +129,12 @@ namespace nap
129129
void updateProjectionMatrices() const;
130130

131131
protected:
132-
mutable glm::mat4x4 mProjectionMatrix; // The composed projection matrix
133-
mutable glm::mat4x4 mRenderProjectionMatrix; // The composed projection matrix used by the renderer
134-
mutable bool mDirty = true; // If the projection matrix needs to be recalculated
135-
PerpCameraProperties mProperties; // These properties are copied from the resource to the instance. When these are changed, only the instance is affected
136-
TransformComponentInstance* mTransformComponent; // Cached transform component
132+
mutable glm::mat4x4 mProjectionMatrix; // The composed projection matrix
133+
mutable glm::mat4x4 mRenderProjectionMatrix; // The composed projection matrix used by the renderer
134+
mutable bool mDirty = true; // If the projection matrix needs to be recalculated
135+
PerpCameraProperties mProperties; // These properties are copied from the resource to the instance. When these are changed, only the instance is affected
136+
TransformComponentInstance* mTransformComponent; // Cached transform component
137+
138+
bool mPerpendicularRenderProjection = true; // Whether the render projection matrix should be projected onto a perpendicular surface
137139
};
138140
}

0 commit comments

Comments
 (0)