Skip to content

Commit 51eb283

Browse files
committed
fix: multiview rendering issues
1 parent a9e5034 commit 51eb283

11 files changed

Lines changed: 187 additions & 28 deletions

editor/app.c

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ typedef struct _plAppData
149149
bool bResize;
150150

151151
// ui options
152+
bool bSecondaryViewActive;
152153
bool bContinuousBVH;
153154
bool bFrustumCulling;
154155
bool bShowSkybox;
@@ -167,10 +168,12 @@ typedef struct _plAppData
167168

168169
// scene
169170
plEntity tMainCamera;
171+
plEntity tSecondaryCamera;
170172

171173
// scenes/views
172174
plScene* ptScene;
173175
plView* ptView;
176+
plView* ptSecondaryView;
174177

175178
// drawing
176179
plDrawLayer2D* ptDrawLayer;
@@ -400,6 +403,7 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
400403
};
401404
ptAppData->ptScene = gptRenderer->create_scene(tSceneInit);
402405
ptAppData->ptView = gptRenderer->create_view(ptAppData->ptScene, ptIO->tMainViewportSize);
406+
ptAppData->ptSecondaryView = gptRenderer->create_view(ptAppData->ptScene, (plVec2){500.0f, 500.0f});
403407

404408
// create main camera
405409
plCamera* ptMainCamera = NULL;
@@ -408,6 +412,12 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
408412
gptCamera->update(ptMainCamera);
409413
gptEcs->attach_script(ptAppData->ptComponentLibrary, "pl_script_camera", PL_SCRIPT_FLAG_PLAYING | PL_SCRIPT_FLAG_RELOADABLE, ptAppData->tMainCamera, NULL);
410414

415+
// create secondary camera
416+
plCamera* ptSecondaryCamera = NULL;
417+
ptAppData->tSecondaryCamera = gptCamera->create_perspective(ptAppData->ptComponentLibrary, "secondary camera", pl_create_vec3(-4.012f, 2.984f, -1.109f), PL_PI_3, 1.0f, 0.1f, 20.0f, true, &ptSecondaryCamera);
418+
gptCamera->set_pitch_yaw(ptSecondaryCamera, -0.465f, 1.341f);
419+
gptCamera->update(ptSecondaryCamera);
420+
411421
// create lights
412422
plLightComponent* ptLight = NULL;
413423
gptRenderer->create_directional_light(ptAppData->ptComponentLibrary, "direction light", pl_create_vec3(0.0f, -1.0f, -0.085f), &ptLight);
@@ -520,6 +530,7 @@ pl_app_shutdown(plAppData* ptAppData)
520530
gptConsole->cleanup();
521531

522532
gptRenderer->cleanup_view(ptAppData->ptView);
533+
gptRenderer->cleanup_view(ptAppData->ptSecondaryView);
523534
gptRenderer->cleanup_scene(ptAppData->ptScene);
524535

525536
gptEcs->cleanup();
@@ -576,6 +587,9 @@ pl_app_update(plAppData* ptAppData)
576587
gptShaderVariant->update_stats();
577588

578589
plCamera* ptCamera = (plCamera*)gptEcs->get_component(ptAppData->ptComponentLibrary, gptCamera->get_ecs_type_key(), ptAppData->tMainCamera);
590+
plCamera* ptSecondaryCamera = (plCamera*)gptEcs->get_component(ptAppData->ptComponentLibrary, gptCamera->get_ecs_type_key(), ptAppData->tSecondaryCamera);
591+
592+
gptCamera->update(ptSecondaryCamera);
579593

580594
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~selection stuff~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
581595

@@ -686,24 +700,42 @@ pl_app_update(plAppData* ptAppData)
686700
plLightComponent* ptLights = NULL;
687701
const uint32_t uLightCount = gptEcs->get_components(ptAppData->ptComponentLibrary, gptRenderer->get_ecs_type_key_light(), (void**)&ptLights, NULL);
688702
gptRenderer->debug_draw_lights(ptAppData->ptView, ptLights, uLightCount);
703+
gptRenderer->debug_draw_lights(ptAppData->ptSecondaryView, ptLights, uLightCount);
689704
}
690705

691706
if(ptAppData->bDrawAllBoundingBoxes)
707+
{
692708
gptRenderer->debug_draw_all_bound_boxes(ptAppData->ptView);
709+
gptRenderer->debug_draw_all_bound_boxes(ptAppData->ptSecondaryView);
710+
}
693711

694712
if(ptAppData->bShowSkybox)
713+
{
695714
gptRenderer->show_skybox(ptAppData->ptView);
715+
gptRenderer->show_skybox(ptAppData->ptSecondaryView);
716+
}
696717

697718
if(ptAppData->bShowBVH)
719+
{
698720
gptRenderer->debug_draw_bvh(ptAppData->ptView);
721+
gptRenderer->debug_draw_bvh(ptAppData->ptSecondaryView);
722+
}
699723

700724
if(ptAppData->bShowGrid)
725+
{
701726
gptRenderer->show_grid(ptAppData->ptView);
727+
gptRenderer->show_grid(ptAppData->ptSecondaryView);
728+
}
702729

703730
// render scene
704731
gptRenderer->prepare_scene(ptAppData->ptScene);
705732
gptRenderer->prepare_view(ptAppData->ptView, ptCamera);
706733
gptRenderer->render_view(ptAppData->ptView, ptCamera, ptAppData->bFrustumCulling ? ptCamera : NULL);
734+
if(ptAppData->bSecondaryViewActive)
735+
{
736+
gptRenderer->prepare_view(ptAppData->ptSecondaryView, ptSecondaryCamera);
737+
gptRenderer->render_view(ptAppData->ptSecondaryView, ptSecondaryCamera, ptSecondaryCamera);
738+
}
707739

708740
// main "editor" debug window
709741
if(ptAppData->bShowPilotLightTool)
@@ -712,12 +744,31 @@ pl_app_update(plAppData* ptAppData)
712744
// add full screen quad for offscreen render
713745
if(ptAppData->ptScene)
714746
{
715-
plVec2 tStartPos = {0};
716-
plVec2 tEndPos = ptIO->tMainViewportSize;
717-
gptDraw->add_image(ptAppData->ptDrawLayer,
718-
gptRenderer->get_view_color_texture(ptAppData->ptView).uData,
719-
tStartPos,
720-
tEndPos);
747+
{
748+
plVec2 tStartPos = {0};
749+
plVec2 tEndPos = ptIO->tMainViewportSize;
750+
gptDraw->add_image_ex(ptAppData->ptDrawLayer,
751+
gptRenderer->get_view_color_texture(ptAppData->ptView).uData,
752+
tStartPos,
753+
tEndPos,
754+
(plVec2){0},
755+
gptRenderer->get_view_color_texture_max_uv(ptAppData->ptView),
756+
PL_COLOR_32_WHITE);
757+
}
758+
759+
if(ptAppData->bSecondaryViewActive)
760+
{
761+
plVec2 tStartPos = { 0.75f * ptIO->tMainViewportSize.x, 0.0f};
762+
plVec2 tEndPos = {ptIO->tMainViewportSize.x, 0.25f * ptIO->tMainViewportSize.y};
763+
gptDraw->add_image_ex(ptAppData->ptDrawLayer,
764+
gptRenderer->get_view_color_texture(ptAppData->ptSecondaryView).uData,
765+
tStartPos,
766+
tEndPos,
767+
(plVec2){0},
768+
gptRenderer->get_view_color_texture_max_uv(ptAppData->ptSecondaryView),
769+
PL_COLOR_32_WHITE);
770+
gptCamera->set_aspect(ptSecondaryCamera, ptIO->tMainViewportSize.x / ptIO->tMainViewportSize.y);
771+
}
721772
}
722773

723774
gptDraw->submit_2d_layer(ptAppData->ptDrawLayer);
@@ -788,6 +839,7 @@ pl__show_editor_window(plAppData* ptAppData)
788839
gptUI->checkbox("Editor Attached", &ptAppData->bEditorAttached);
789840
gptUI->checkbox("Show Debug Lights", &ptAppData->bShowDebugLights);
790841
gptUI->checkbox("Show Bounding Boxes", &ptAppData->bDrawAllBoundingBoxes);
842+
gptUI->checkbox("Secondary View", &ptAppData->bSecondaryViewActive);
791843

792844
gptUI->vertical_spacing();
793845

editor/editor.cpp

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ pl_app_shutdown(plAppData* ptAppData)
350350
gptScreenLog->cleanup();
351351
if(ptAppData->ptView)
352352
gptRenderer->cleanup_view(ptAppData->ptView);
353+
if(ptAppData->ptSecondaryView)
354+
gptRenderer->cleanup_view(ptAppData->ptSecondaryView);
353355
if(ptAppData->ptScene)
354356
gptRenderer->cleanup_scene(ptAppData->ptScene);
355357
gptEcs->cleanup();
@@ -411,7 +413,9 @@ pl_app_update(plAppData* ptAppData)
411413

412414
plCamera* ptCamera = (plCamera*)gptEcs->get_component(ptAppData->ptCompLibrary, gptCamera->get_ecs_type_key(), ptAppData->tMainCamera);
413415
plCamera* ptCullCamera = (plCamera*)gptEcs->get_component(ptAppData->ptCompLibrary, gptCamera->get_ecs_type_key(), ptAppData->tCullCamera);
416+
plCamera* ptSecondaryCamera = (plCamera*)gptEcs->get_component(ptAppData->ptCompLibrary, gptCamera->get_ecs_type_key(), ptAppData->tSecondaryCamera);
414417
gptCamera->update(ptCullCamera);
418+
gptCamera->update(ptSecondaryCamera);
415419

416420
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~selection stuff~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
417421

@@ -520,27 +524,48 @@ pl_app_update(plAppData* ptAppData)
520524
plLightComponent* ptLights = nullptr;
521525
const uint32_t uLightCount = gptEcs->get_components(ptAppData->ptCompLibrary, gptRenderer->get_ecs_type_key_light(), (void**)&ptLights, nullptr);
522526
gptRenderer->debug_draw_lights(ptAppData->ptView, ptLights, uLightCount);
527+
gptRenderer->debug_draw_lights(ptAppData->ptSecondaryView, ptLights, uLightCount);
523528
}
524529

525530
if(ptAppData->bDrawAllBoundingBoxes)
531+
{
526532
gptRenderer->debug_draw_all_bound_boxes(ptAppData->ptView);
533+
gptRenderer->debug_draw_all_bound_boxes(ptAppData->ptSecondaryView);
534+
}
527535

528536
if(ptAppData->bShowSkybox)
537+
{
529538
gptRenderer->show_skybox(ptAppData->ptView);
539+
gptRenderer->show_skybox(ptAppData->ptSecondaryView);
540+
}
530541

531542
if(ptAppData->bShowGrid)
543+
{
532544
gptRenderer->show_grid(ptAppData->ptView);
545+
gptRenderer->show_grid(ptAppData->ptSecondaryView);
546+
}
533547

534548
if(ptAppData->bShowBVH)
549+
{
535550
gptRenderer->debug_draw_bvh(ptAppData->ptView);
551+
gptRenderer->debug_draw_bvh(ptAppData->ptSecondaryView);
552+
}
536553

537554
// render scene
538555
gptRenderer->prepare_scene(ptAppData->ptScene);
539556
gptRenderer->prepare_view(ptAppData->ptView, ptCamera);
557+
if(ptAppData->bSecondaryViewActive)
558+
gptRenderer->prepare_view(ptAppData->ptSecondaryView, ptSecondaryCamera);
559+
540560
plCamera* ptActiveCullCamera = ptCamera;
541561
if(ptAppData->bFreezeCullCamera)
542562
ptActiveCullCamera = ptCullCamera;
543563
gptRenderer->render_view(ptAppData->ptView, ptCamera, ptAppData->bFrustumCulling ? ptActiveCullCamera : nullptr);
564+
565+
if(ptAppData->bSecondaryViewActive)
566+
{
567+
gptRenderer->render_view(ptAppData->ptSecondaryView, ptSecondaryCamera, ptSecondaryCamera);
568+
}
544569
}
545570

546571
ImGui::DockSpaceOverViewport(0, 0, ImGuiDockNodeFlags_PassthruCentralNode);
@@ -617,7 +642,7 @@ pl_app_update(plAppData* ptAppData)
617642
if(ptAppData->ptScene)
618643
{
619644

620-
plCamera* ptCamera = (plCamera*)gptEcs->get_component(ptAppData->ptCompLibrary, gptCamera->get_ecs_type_key(), ptAppData->tMainCamera);
645+
plCamera* ptCamera = (plCamera*)gptEcs->get_component(ptAppData->ptCompLibrary, gptCamera->get_ecs_type_key(), ptAppData->tMainCamera);
621646
if(ptAppData->bMainViewHovered)
622647
pl__camera_update_imgui(ptCamera);
623648

@@ -626,14 +651,30 @@ pl_app_update(plAppData* ptAppData)
626651
tContextSize.y / ImGui::GetWindowViewport()->Size.y,
627652
};
628653

654+
plVec2 tUvScale = gptRenderer->get_view_color_texture_max_uv(ptAppData->ptView);
629655

630656
ImTextureID tTexture = gptDearImGui->get_texture_id_from_bindgroup(ptAppData->ptDevice, gptRenderer->get_view_color_texture(ptAppData->ptView));
631-
ImGui::Image(tTexture, tContextSize);
657+
ImGui::Image(tTexture, tContextSize, ImVec2(0, 0), ImVec2(tUvScale.x, tUvScale.y));
632658

633659
}
634660
}
635661
ImGui::End();
636662

663+
if(ptAppData->bSecondaryViewActive)
664+
{
665+
plVec2 tUvScale = gptRenderer->get_view_color_texture_max_uv(ptAppData->ptSecondaryView);
666+
ImGui::SetNextWindowSizeConstraints(ImVec2(200.0f, 200.0f), ImVec2(10000.0f, 10000.0f));
667+
if(ImGui::Begin("Secondary View", &ptAppData->bSecondaryViewActive, ImGuiWindowFlags_NoDocking))
668+
{
669+
ImVec2 tContextSize = ImGui::GetContentRegionAvail();
670+
gptCamera->set_aspect((plCamera*)gptEcs->get_component(ptAppData->ptCompLibrary, gptCamera->get_ecs_type_key(), ptAppData->tSecondaryCamera), tContextSize.x / tContextSize.y);
671+
672+
ImTextureID tTexture = gptDearImGui->get_texture_id_from_bindgroup(ptAppData->ptDevice, gptRenderer->get_view_color_texture(ptAppData->ptSecondaryView));
673+
ImGui::Image(tTexture, tContextSize, ImVec2(0, 0), ImVec2(tUvScale.x, tUvScale.y));
674+
}
675+
ImGui::End();
676+
}
677+
637678
if(ptAppData->bShowPlotDemo)
638679
ImPlot::ShowDemoWindow(&ptAppData->bShowPlotDemo);
639680

@@ -872,6 +913,7 @@ pl__show_editor_window(plAppData* ptAppData)
872913

873914
ImGui::Checkbox("Show Debug Lights", &ptAppData->bShowDebugLights);
874915
ImGui::Checkbox("Show Bounding Boxes", &ptAppData->bDrawAllBoundingBoxes);
916+
ImGui::Checkbox("Secondary View", &ptAppData->bSecondaryViewActive);
875917

876918
if(ptAppData->ptScene)
877919
{
@@ -892,8 +934,10 @@ pl__show_editor_window(plAppData* ptAppData)
892934
gptPhysics->reset();
893935
gptEcs->reset_library(ptAppData->ptCompLibrary);
894936
gptRenderer->cleanup_view(ptAppData->ptView);
937+
gptRenderer->cleanup_view(ptAppData->ptSecondaryView);
895938
gptRenderer->cleanup_scene(ptAppData->ptScene);
896939
ptAppData->ptView = nullptr;
940+
ptAppData->ptSecondaryView = nullptr;
897941
ptAppData->ptScene = nullptr;
898942
}
899943
}
@@ -996,6 +1040,7 @@ pl__show_editor_window(plAppData* ptAppData)
9961040
plIO* ptIO = gptIO->get_io();
9971041

9981042
ptAppData->ptView = gptRenderer->create_view(ptAppData->ptScene, ptIO->tMainViewportSize);
1043+
ptAppData->ptSecondaryView = gptRenderer->create_view(ptAppData->ptScene, {500.0f, 500.0f});
9991044

10001045
plModelLoaderData tLoaderData0 = {0};
10011046

@@ -1167,6 +1212,14 @@ pl__create_scene(plAppData* ptAppData)
11671212
gptCamera->set_pitch_yaw(ptCullCamera, 0.0f, PL_PI);
11681213
gptCamera->update(ptCullCamera);
11691214

1215+
// create secondary camera
1216+
plCamera* ptSecondaryCamera = nullptr;
1217+
ptAppData->tSecondaryCamera = gptCamera->create_perspective(ptAppData->ptCompLibrary, "secondary camera", pl_create_vec3(-4.7f, 4.2f, -3.256f), PL_PI_3, 1.0f, 0.1f, 20.0f, true, &ptSecondaryCamera);
1218+
gptCamera->set_pitch_yaw(ptSecondaryCamera, -0.1f, 0.911f);
1219+
gptCamera->update(ptSecondaryCamera);
1220+
plTransformComponent* ptSecondaryCameraTransform = (plTransformComponent* )gptEcs->add_component(ptAppData->ptCompLibrary, gptEcs->get_ecs_type_key_transform(), ptAppData->tSecondaryCamera);
1221+
ptSecondaryCameraTransform->tTranslation = pl_create_vec3(-4.7f, 4.2f, -3.256f);
1222+
11701223
// create lights
11711224
plLightComponent* ptLight = nullptr;
11721225
gptRenderer->create_directional_light(ptAppData->ptCompLibrary, "direction light", pl_create_vec3(-0.375f, -1.0f, -0.085f), &ptLight);

editor/editor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ typedef struct _plAppData
176176
bool bVSync;
177177

178178
// ui options
179+
bool bSecondaryViewActive;
179180
bool bShowBVH;
180181
bool bFrustumCulling;
181182
bool bShowImGuiDemo;
@@ -200,12 +201,14 @@ typedef struct _plAppData
200201
bool bFreezeCullCamera;
201202
plEntity tCullCamera;
202203
plEntity tMainCamera;
204+
plEntity tSecondaryCamera;
203205
bool bMainViewHovered;
204206

205207
// scenes/views
206208
plComponentLibrary* ptCompLibrary;
207209
plScene* ptScene;
208210
plView* ptView;
211+
plView* ptSecondaryView;
209212
plVec2 tView0Offset;
210213
plVec2 tView0Scale;
211214

0 commit comments

Comments
 (0)