Skip to content

Commit 158bc7a

Browse files
author
jonathan hoffstadt
committed
feat: add model loader extension
1 parent 2488335 commit 158bc7a

10 files changed

+1214
-1000
lines changed

.github/workflows/build.yml

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ jobs:
5555
if not exist ../out/pl_job_ext.dll exit 1
5656
if not exist ../out/pl_gpu_allocators_ext.dll exit 1
5757
if not exist ../out/pl_resource_ext.dll exit 1
58+
if not exist ../out/pl_model_loader_ext.dll exit 1
5859
if not exist ../out/pl_ecs_ext.dll exit 1
5960
if not exist ../out/pl_ref_renderer_ext.dll exit 1
6061
if not exist ../out/pilotlight.lib exit 1
@@ -106,6 +107,7 @@ jobs:
106107
test -f ./out/pl_graphics_ext.dylib || exit 1
107108
test -f ./out/pl_job_ext.dylib || exit 1
108109
test -f ./out/pl_gpu_allocators_ext.dylib || exit 1
110+
test -f ./out/pl_model_loader_ext.dylib || exit 1
109111
test -f ./out/pl_resource_ext.dylib || exit 1
110112
test -f ./out/pl_ecs_ext.dylib || exit 1
111113
test -f ./out/pl_ref_renderer_ext.dylib || exit 1
@@ -171,6 +173,7 @@ jobs:
171173
test -f ./out/pl_debug_ext.so || exit 1
172174
test -f ./out/pl_job_ext.so || exit 1
173175
test -f ./out/pl_gpu_allocators_ext.so || exit 1
176+
test -f ./out/pl_model_loader_ext.so || exit 1
174177
test -f ./out/pl_resource_ext.so || exit 1
175178
test -f ./out/pl_ecs_ext.so || exit 1
176179
test -f ./out/pl_ref_renderer_ext.so || exit 1

apps/app.c

+79-52
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Index of this file:
3434
#include "pl_debug_ext.h"
3535
#include "pl_ecs_ext.h"
3636
#include "pl_resource_ext.h"
37+
#include "pl_model_loader_ext.h"
3738
#include "pl_ref_renderer_ext.h"
3839
#include "pl_job_ext.h"
3940

@@ -106,6 +107,7 @@ const plEcsI* gptEcs = NULL;
106107
const plCameraI* gptCamera = NULL;
107108
const plResourceI* gptResource = NULL;
108109
const plRefRendererI* gptRenderer = NULL;
110+
const plModelLoaderI* gptModelLoader = NULL;
109111
const plJobI* gptJobs = NULL;
110112

111113
//-----------------------------------------------------------------------------
@@ -126,20 +128,21 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
126128
pl_set_profile_context(gptDataRegistry->get_data("profile"));
127129

128130
// reload global apis
129-
gptWindows = ptApiRegistry->first(PL_API_WINDOW);
130-
gptThreads = ptApiRegistry->first(PL_API_THREADS);
131-
gptStats = ptApiRegistry->first(PL_API_STATS);
132-
gptFile = ptApiRegistry->first(PL_API_FILE);
133-
gptGfx = ptApiRegistry->first(PL_API_GRAPHICS);
134-
gptDevice = ptApiRegistry->first(PL_API_DEVICE);
135-
gptDebug = ptApiRegistry->first(PL_API_DEBUG);
136-
gptImage = ptApiRegistry->first(PL_API_IMAGE);
137-
gptStream = ptApiRegistry->first(PL_API_DRAW_STREAM);
138-
gptEcs = ptApiRegistry->first(PL_API_ECS);
139-
gptCamera = ptApiRegistry->first(PL_API_CAMERA);
140-
gptResource = ptApiRegistry->first(PL_API_RESOURCE);
141-
gptRenderer = ptApiRegistry->first(PL_API_REF_RENDERER);
142-
gptJobs = ptApiRegistry->first(PL_API_JOB);
131+
gptWindows = ptApiRegistry->first(PL_API_WINDOW);
132+
gptThreads = ptApiRegistry->first(PL_API_THREADS);
133+
gptStats = ptApiRegistry->first(PL_API_STATS);
134+
gptFile = ptApiRegistry->first(PL_API_FILE);
135+
gptGfx = ptApiRegistry->first(PL_API_GRAPHICS);
136+
gptDevice = ptApiRegistry->first(PL_API_DEVICE);
137+
gptDebug = ptApiRegistry->first(PL_API_DEBUG);
138+
gptImage = ptApiRegistry->first(PL_API_IMAGE);
139+
gptStream = ptApiRegistry->first(PL_API_DRAW_STREAM);
140+
gptEcs = ptApiRegistry->first(PL_API_ECS);
141+
gptCamera = ptApiRegistry->first(PL_API_CAMERA);
142+
gptResource = ptApiRegistry->first(PL_API_RESOURCE);
143+
gptRenderer = ptApiRegistry->first(PL_API_REF_RENDERER);
144+
gptJobs = ptApiRegistry->first(PL_API_JOB);
145+
gptModelLoader = ptApiRegistry->first(PL_API_MODEL_LOADER);
143146

144147
return ptAppData;
145148
}
@@ -170,23 +173,28 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
170173
ptExtensionRegistry->load("pl_debug_ext", NULL, NULL, true);
171174
ptExtensionRegistry->load("pl_ecs_ext", NULL, NULL, false);
172175
ptExtensionRegistry->load("pl_resource_ext", NULL, NULL, false);
176+
ptExtensionRegistry->load("pl_model_loader_ext", NULL, NULL, false);
173177
ptExtensionRegistry->load("pl_ref_renderer_ext", NULL, NULL, true);
174178

175179
// load apis
176-
gptWindows = ptApiRegistry->first(PL_API_WINDOW);
177-
gptThreads = ptApiRegistry->first(PL_API_THREADS);
178-
gptStats = ptApiRegistry->first(PL_API_STATS);
179-
gptFile = ptApiRegistry->first(PL_API_FILE);
180-
gptGfx = ptApiRegistry->first(PL_API_GRAPHICS);
181-
gptDevice = ptApiRegistry->first(PL_API_DEVICE);
182-
gptDebug = ptApiRegistry->first(PL_API_DEBUG);
183-
gptImage = ptApiRegistry->first(PL_API_IMAGE);
184-
gptStream = ptApiRegistry->first(PL_API_DRAW_STREAM);
185-
gptEcs = ptApiRegistry->first(PL_API_ECS);
186-
gptCamera = ptApiRegistry->first(PL_API_CAMERA);
187-
gptResource = ptApiRegistry->first(PL_API_RESOURCE);
188-
gptRenderer = ptApiRegistry->first(PL_API_REF_RENDERER);
189-
gptJobs = ptApiRegistry->first(PL_API_JOB);
180+
gptWindows = ptApiRegistry->first(PL_API_WINDOW);
181+
gptThreads = ptApiRegistry->first(PL_API_THREADS);
182+
gptStats = ptApiRegistry->first(PL_API_STATS);
183+
gptFile = ptApiRegistry->first(PL_API_FILE);
184+
gptGfx = ptApiRegistry->first(PL_API_GRAPHICS);
185+
gptDevice = ptApiRegistry->first(PL_API_DEVICE);
186+
gptDebug = ptApiRegistry->first(PL_API_DEBUG);
187+
gptImage = ptApiRegistry->first(PL_API_IMAGE);
188+
gptStream = ptApiRegistry->first(PL_API_DRAW_STREAM);
189+
gptEcs = ptApiRegistry->first(PL_API_ECS);
190+
gptCamera = ptApiRegistry->first(PL_API_CAMERA);
191+
gptResource = ptApiRegistry->first(PL_API_RESOURCE);
192+
gptRenderer = ptApiRegistry->first(PL_API_REF_RENDERER);
193+
gptJobs = ptApiRegistry->first(PL_API_JOB);
194+
gptModelLoader = ptApiRegistry->first(PL_API_MODEL_LOADER);
195+
196+
// initialize job system
197+
gptJobs->initialize(0);
190198

191199
const plWindowDesc tWindowDesc = {
192200
.pcName = "Pilot Light Example",
@@ -224,36 +232,52 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
224232
ptAppData->ptDrawLayer = pl_request_layer(pl_get_draw_list(NULL), "draw layer");
225233

226234
// create main camera
227-
plComponentLibrary* ptComponentLibrary = gptRenderer->get_component_library();
228-
ptAppData->tMainCamera = gptEcs->create_perspective_camera(ptComponentLibrary, "main camera", (plVec3){0, 0, 5.0f}, PL_PI_3, ptIO->afMainViewportSize[0] / ptIO->afMainViewportSize[1], 0.01f, 400.0f);
229-
gptCamera->set_pitch_yaw(gptEcs->get_component(ptComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera), 0.0f, PL_PI);
230-
gptCamera->update(gptEcs->get_component(ptComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera));
235+
plComponentLibrary* ptMainComponentLibrary = gptRenderer->get_component_library(ptAppData->uSceneHandle0);
236+
ptAppData->tMainCamera = gptEcs->create_perspective_camera(ptMainComponentLibrary, "main camera", (plVec3){0, 0, 5.0f}, PL_PI_3, ptIO->afMainViewportSize[0] / ptIO->afMainViewportSize[1], 0.01f, 400.0f);
237+
gptCamera->set_pitch_yaw(gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera), 0.0f, PL_PI);
238+
gptCamera->update(gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera));
231239

232240
// create cull camera
233-
ptAppData->tCullCamera = gptEcs->create_perspective_camera(ptComponentLibrary, "cull camera", (plVec3){0, 0, 5.0f}, PL_PI_3, ptIO->afMainViewportSize[0] / ptIO->afMainViewportSize[1], 0.1f, 106.0f);
234-
gptCamera->set_pitch_yaw(gptEcs->get_component(ptComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tCullCamera), 0.0f, PL_PI);
235-
gptCamera->update(gptEcs->get_component(ptComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tCullCamera));
241+
ptAppData->tCullCamera = gptEcs->create_perspective_camera(ptMainComponentLibrary, "cull camera", (plVec3){0, 0, 5.0f}, PL_PI_3, ptIO->afMainViewportSize[0] / ptIO->afMainViewportSize[1], 0.1f, 106.0f);
242+
gptCamera->set_pitch_yaw(gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tCullCamera), 0.0f, PL_PI);
243+
gptCamera->update(gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tCullCamera));
236244

237-
ptAppData->tMainCamera2 = gptEcs->create_perspective_camera(ptComponentLibrary, "secondary camera", (plVec3){-3.265f, 2.967f, 0.311f}, PL_PI_3, 1.0f, 0.01f, 400.0f);
238-
gptCamera->set_pitch_yaw(gptEcs->get_component(ptComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera2), -0.535f, 1.737f);
239-
gptCamera->update(gptEcs->get_component(ptComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera2));
245+
plComponentLibrary* ptSecondaryComponentLibrary = gptRenderer->get_component_library(ptAppData->uSceneHandle1);
246+
ptAppData->tMainCamera2 = gptEcs->create_perspective_camera(ptSecondaryComponentLibrary, "secondary camera", (plVec3){-3.265f, 2.967f, 0.311f}, PL_PI_3, 1.0f, 0.01f, 400.0f);
247+
gptCamera->set_pitch_yaw(gptEcs->get_component(ptSecondaryComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera2), -0.535f, 1.737f);
248+
gptCamera->update(gptEcs->get_component(ptSecondaryComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera2));
240249

241250
// load models
242251
pl_begin_profile_frame();
243-
pl_begin_profile_sample("load models");
252+
253+
pl_begin_profile_sample("load skyboxes");
244254
const plMat4 tTransform0 = pl_mat4_translate_xyz(2.0f, 1.0f, 0.0f);
245255
gptRenderer->load_skybox_from_panorama(ptAppData->uSceneHandle0, "../data/glTF-Sample-Environments-main/ennis.jpg", 1024);
246256
gptRenderer->load_skybox_from_panorama(ptAppData->uSceneHandle1, "../data/glTF-Sample-Environments-main/ennis.jpg", 1024);
247-
gptRenderer->load_gltf(ptAppData->uSceneHandle0, "../data/glTF-Sample-Assets-main/Models/Sponza/glTF/Sponza.gltf", NULL);
248-
gptRenderer->load_gltf(ptAppData->uSceneHandle0, "../data/glTF-Sample-Assets-main/Models/CesiumMan/glTF/CesiumMan.gltf", NULL);
249-
gptRenderer->load_stl(ptAppData->uSceneHandle0, "../data/pilotlight-assets-master/meshes/monkey.stl", (plVec4){1.0f, 1.0f, 0.0f, 0.80f}, &tTransform0);
257+
pl_end_profile_sample();
250258

251-
gptRenderer->load_gltf(ptAppData->uSceneHandle1, "../data/glTF-Sample-Assets-main/Models/CesiumMan/glTF/CesiumMan.gltf", NULL);
252-
gptRenderer->load_stl(ptAppData->uSceneHandle1, "../data/pilotlight-assets-master/meshes/monkey.stl", (plVec4){1.0f, 0.0f, 0.0f, 0.80f}, &tTransform0);
259+
plModelLoaderData tLoaderData0 = {0};
260+
261+
pl_begin_profile_sample("load models 0");
262+
gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/glTF-Sample-Assets-main/Models/Sponza/glTF/Sponza.gltf", NULL, &tLoaderData0);
263+
gptModelLoader->load_gltf(ptMainComponentLibrary, "../data/glTF-Sample-Assets-main/Models/CesiumMan/glTF/CesiumMan.gltf", NULL, &tLoaderData0);
264+
gptModelLoader->load_stl(ptMainComponentLibrary, "../data/pilotlight-assets-master/meshes/monkey.stl", (plVec4){1.0f, 1.0f, 0.0f, 0.80f}, &tTransform0, &tLoaderData0);
265+
gptRenderer->add_drawable_objects_to_scene(ptAppData->uSceneHandle0, tLoaderData0.uOpaqueCount, tLoaderData0.atOpaqueObjects, tLoaderData0.uTransparentCount, tLoaderData0.atTransparentObjects);
266+
gptModelLoader->free_data(&tLoaderData0);
253267
pl_end_profile_sample();
254268

255-
pl_begin_profile_sample("finalize scene");
269+
pl_begin_profile_sample("load models 1");
270+
gptModelLoader->load_gltf(ptSecondaryComponentLibrary, "../data/glTF-Sample-Assets-main/Models/CesiumMan/glTF/CesiumMan.gltf", NULL, &tLoaderData0);
271+
gptModelLoader->load_stl(ptSecondaryComponentLibrary, "../data/pilotlight-assets-master/meshes/monkey.stl", (plVec4){1.0f, 0.0f, 0.0f, 0.80f}, &tTransform0, &tLoaderData0);
272+
gptRenderer->add_drawable_objects_to_scene(ptAppData->uSceneHandle1, tLoaderData0.uOpaqueCount, tLoaderData0.atOpaqueObjects, tLoaderData0.uTransparentCount, tLoaderData0.atTransparentObjects);
273+
gptModelLoader->free_data(&tLoaderData0);
274+
pl_end_profile_sample();
275+
276+
pl_begin_profile_sample("finalize scene 0");
256277
gptRenderer->finalize_scene(ptAppData->uSceneHandle0);
278+
pl_end_profile_sample();
279+
280+
pl_begin_profile_sample("finalize scene 1");
257281
gptRenderer->finalize_scene(ptAppData->uSceneHandle1);
258282
pl_end_profile_sample();
259283

@@ -276,6 +300,7 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
276300
PL_EXPORT void
277301
pl_app_shutdown(plAppData* ptAppData)
278302
{
303+
gptJobs->cleanup();
279304
gptGfx->destroy_font_atlas(&ptAppData->tFontAtlas); // backend specific cleanup
280305
pl_cleanup_font_atlas(&ptAppData->tFontAtlas);
281306
gptRenderer->cleanup();
@@ -294,7 +319,7 @@ pl_app_resize(plAppData* ptAppData)
294319
{
295320
gptGfx->resize(gptRenderer->get_graphics());
296321
plIO* ptIO = pl_get_io();
297-
gptCamera->set_aspect(gptEcs->get_component(gptRenderer->get_component_library(), PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera), ptIO->afMainViewportSize[0] / ptIO->afMainViewportSize[1]);
322+
gptCamera->set_aspect(gptEcs->get_component(gptRenderer->get_component_library(ptAppData->uSceneHandle0), PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera), ptIO->afMainViewportSize[0] / ptIO->afMainViewportSize[1]);
298323
ptAppData->bResize = true;
299324
}
300325

@@ -349,11 +374,12 @@ pl_app_update(plAppData* ptAppData)
349374
*pdMemoryCounter = (double)pl_get_memory_context()->szMemoryUsage;
350375

351376
// handle input
352-
plComponentLibrary* ptComponentLibrary = gptRenderer->get_component_library();
377+
plComponentLibrary* ptMainComponentLibrary = gptRenderer->get_component_library(ptAppData->uSceneHandle0);
378+
plComponentLibrary* ptSecondaryComponentLibrary = gptRenderer->get_component_library(ptAppData->uSceneHandle1);
353379

354-
plCameraComponent* ptCamera = gptEcs->get_component(ptComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera);
355-
plCameraComponent* ptCamera2 = gptEcs->get_component(ptComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera2);
356-
plCameraComponent* ptCullCamera = gptEcs->get_component(ptComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tCullCamera);
380+
plCameraComponent* ptCamera = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera);
381+
plCameraComponent* ptCamera2 = gptEcs->get_component(ptSecondaryComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tMainCamera2);
382+
plCameraComponent* ptCullCamera = gptEcs->get_component(ptMainComponentLibrary, PL_COMPONENT_TYPE_CAMERA, ptAppData->tCullCamera);
357383

358384
static const float fCameraTravelSpeed = 4.0f;
359385
static const float fCameraRotationSpeed = 0.005f;
@@ -381,7 +407,8 @@ pl_app_update(plAppData* ptAppData)
381407
gptCamera->update(ptCullCamera);
382408

383409
// run ecs system
384-
gptRenderer->run_ecs();
410+
gptRenderer->run_ecs(ptAppData->uSceneHandle0);
411+
gptRenderer->run_ecs(ptAppData->uSceneHandle1);
385412

386413
// new ui frame
387414
pl_new_frame();
@@ -529,7 +556,7 @@ pl_app_update(plAppData* ptAppData)
529556
gptDebug->show_debug_windows(&ptAppData->tDebugInfo);
530557

531558
if(ptAppData->bShowEntityWindow)
532-
pl_show_ecs_window(gptEcs, gptRenderer->get_component_library(), &ptAppData->bShowEntityWindow);
559+
pl_show_ecs_window(gptEcs, gptRenderer->get_component_library(ptAppData->uSceneHandle0), &ptAppData->bShowEntityWindow);
533560

534561
if(ptAppData->bShowUiDemo)
535562
{

extensions/pl_ecs_ext.h

+18-1
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ typedef int plCameraType;
8686
typedef int plAnimationMode;
8787
typedef int plAnimationPath;
8888
typedef int plAnimationFlags;
89+
typedef int plMeshFormatFlags;
8990

90-
typedef union plEntity
91+
typedef union _plEntity
9192
{
9293
struct
9394
{
@@ -247,6 +248,22 @@ enum _plAnimationFlags
247248
PL_ANIMATION_FLAG_LOOPED = 1 << 1
248249
};
249250

251+
enum _plMeshFormatFlags
252+
{
253+
PL_MESH_FORMAT_FLAG_NONE = 0,
254+
PL_MESH_FORMAT_FLAG_HAS_POSITION = 1 << 0,
255+
PL_MESH_FORMAT_FLAG_HAS_NORMAL = 1 << 1,
256+
PL_MESH_FORMAT_FLAG_HAS_TANGENT = 1 << 2,
257+
PL_MESH_FORMAT_FLAG_HAS_TEXCOORD_0 = 1 << 3,
258+
PL_MESH_FORMAT_FLAG_HAS_TEXCOORD_1 = 1 << 4,
259+
PL_MESH_FORMAT_FLAG_HAS_COLOR_0 = 1 << 5,
260+
PL_MESH_FORMAT_FLAG_HAS_COLOR_1 = 1 << 6,
261+
PL_MESH_FORMAT_FLAG_HAS_JOINTS_0 = 1 << 7,
262+
PL_MESH_FORMAT_FLAG_HAS_JOINTS_1 = 1 << 8,
263+
PL_MESH_FORMAT_FLAG_HAS_WEIGHTS_0 = 1 << 9,
264+
PL_MESH_FORMAT_FLAG_HAS_WEIGHTS_1 = 1 << 10
265+
};
266+
250267
//-----------------------------------------------------------------------------
251268
// [SECTION] structs
252269
//-----------------------------------------------------------------------------

extensions/pl_graphics_ext.h

-16
Original file line numberDiff line numberDiff line change
@@ -1083,22 +1083,6 @@ enum _plLoadOp
10831083
PL_LOAD_OP_DONT_CARE
10841084
};
10851085

1086-
enum _plMeshFormatFlags
1087-
{
1088-
PL_MESH_FORMAT_FLAG_NONE = 0,
1089-
PL_MESH_FORMAT_FLAG_HAS_POSITION = 1 << 0,
1090-
PL_MESH_FORMAT_FLAG_HAS_NORMAL = 1 << 1,
1091-
PL_MESH_FORMAT_FLAG_HAS_TANGENT = 1 << 2,
1092-
PL_MESH_FORMAT_FLAG_HAS_TEXCOORD_0 = 1 << 3,
1093-
PL_MESH_FORMAT_FLAG_HAS_TEXCOORD_1 = 1 << 4,
1094-
PL_MESH_FORMAT_FLAG_HAS_COLOR_0 = 1 << 5,
1095-
PL_MESH_FORMAT_FLAG_HAS_COLOR_1 = 1 << 6,
1096-
PL_MESH_FORMAT_FLAG_HAS_JOINTS_0 = 1 << 7,
1097-
PL_MESH_FORMAT_FLAG_HAS_JOINTS_1 = 1 << 8,
1098-
PL_MESH_FORMAT_FLAG_HAS_WEIGHTS_0 = 1 << 9,
1099-
PL_MESH_FORMAT_FLAG_HAS_WEIGHTS_1 = 1 << 10
1100-
};
1101-
11021086
enum _plMemoryMode
11031087
{
11041088
PL_MEMORY_GPU,

0 commit comments

Comments
 (0)