From e31535abfff4c3335dd465e3745a69d3102253a6 Mon Sep 17 00:00:00 2001 From: quou Date: Sat, 28 Dec 2024 18:27:30 +1100 Subject: converting, loading and rendering GLTF models --- c2.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) (limited to 'c2.cpp') diff --git a/c2.cpp b/c2.cpp index ba07606..9327727 100644 --- a/c2.cpp +++ b/c2.cpp @@ -1,4 +1,5 @@ #include "app.hpp" +#include "model.hpp" #include "ui.hpp" #include "video.hpp" extern "C" { @@ -65,14 +66,21 @@ struct Config_Buffer { float offset[2]; }; +struct Config_Buffer2 { + m4f transform; + m4f projection; +}; + extern "C" int entrypoint() { Arena video_arena, asset_arena, ui_arena; + Model_Loader model_loader; Asset_Arena assets; Device* dev; Shader* shader, * ui_shader; Texture* texture; Texture* texture2; - Buffer_Id vbo, cbuf; + Model* monkey; + Buffer_Id vbo, cbuf, cbuf2; Sampler_Id clamped_linear; UI* ui; C2* app = App::create("c2"); @@ -96,13 +104,21 @@ extern "C" int entrypoint() { ); assets.init(&asset_arena, "pack"); dev = Device::create(&video_arena, app); + model_loader.init(dev, &assets); + register_asset_loader("MODL", &model_loader); + monkey = (Model*)assets.load("monkey.mdl"); app->dev = dev; shader = (Shader*)assets.load("triangle.csh"); ui_shader = (Shader*)assets.load("ui.csh"); texture = (Texture*)assets.load("22.tex"); texture2 = (Texture*)assets.load("kita.tex"); cbuf = dev->create_buffer( - sizeof verts, + sizeof(Config_Buffer), + Buffer_Flags::constant_buffer | + Buffer_Flags::cpu_readwrite + ); + cbuf2 = dev->create_buffer( + sizeof(Config_Buffer2), Buffer_Flags::constant_buffer | Buffer_Flags::cpu_readwrite ); @@ -115,6 +131,8 @@ extern "C" int entrypoint() { ui->create_label("Hello, world", 10, 15); assert(per_frame != 0); uint8_t r = 0; + float rot = 0.0f; + v3f raxis(0.0f, 1.0f, 0.0); vbo = upload_verts(dev); while (app->running) { Arena frame_arena; @@ -134,6 +152,28 @@ extern "C" int entrypoint() { dev->unmap_buffer(cbuf); } + { + Texture& bb = dev->get_texture(dev->get_backbuffer()); + void* mem; + mem = dev->map_buffer(cbuf2, 0, sizeof(Config_Buffer2)); + Config_Buffer2* c = (Config_Buffer2*)mem; + c->projection = m4f::pers( + 70.0f, + (float)bb.w / (float)bb.h, + 0.01f, + 100.0f + ); + c->transform = (m4f::translate( + m4f::identity(), + v3f(0.0f, 0.0f, -5.0f) + ) * m4f::rotate( + m4f::identity(), + rot, + raxis + )); + dev->unmap_buffer(cbuf2); + } + Pipeline_Builder pb(&frame_arena); pb.begin_rp(); pb.rp_target(dev->get_backbuffer(), { r, 0x00, 0xff, 0xff }); @@ -166,9 +206,15 @@ extern "C" int entrypoint() { dev->get_ctx().submit(draw, pip, pass); + pb.begin_rp(); + pb.rp_target(dev->get_backbuffer(), Clear_Mode::restore); + Render_Pass& pass2 = pb.build_rp(); + monkey->render(dev, &frame_arena, pass2, cbuf2); + ui->render(&frame_arena, dev->get_backbuffer()); r += 10; + rot += 0.05f; frame++; dev->present(); app->end(); @@ -178,6 +224,7 @@ extern "C" int entrypoint() { dev->destroy_sampler(clamped_linear); dev->destroy_buffer(vbo); dev->destroy_buffer(cbuf); + dev->destroy_buffer(cbuf2); dev->destroy(); app->destroy(); return 0; -- cgit v1.2.3-54-g00ecf