From 9b70ca7b3a1b7bfd3bf434d8b84bde42f5572ca0 Mon Sep 17 00:00:00 2001 From: vimene Date: Tue, 5 Dec 2023 10:42:35 +0100 Subject: added renderer, improved various things - Added renderer to unify frame buffers - Added FrameBuffer class as a parent for frame buffers' classes - Improved formatting in Makefile.am - Added const modifier in Matrix4's methods --- src/engine.cpp | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'src/engine.cpp') diff --git a/src/engine.cpp b/src/engine.cpp index aeae21c..91c0b80 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include #ifdef ENABLE_NCURSES @@ -27,6 +29,7 @@ #include "o3d/camera.h" #include "math/vector.h" #include "math/mat4.h" +#include "renderer.h" #define FPS 60 @@ -50,8 +53,7 @@ static void usage_error_exit() { std::exit(EXIT_FAILURE); } -template -static void scene_main(FB& fb, std::function update_frame) { +static void scene_main(engine::Renderer& renderer, engine::math::Matrix4 final_transform_mat, std::function update_frame) { float dist = 4.f; float rad = 5.f; bool cont = true; @@ -76,7 +78,7 @@ static void scene_main(FB& fb, std::function update_frame) { scene.camera.rot_y += .0065f; scene.camera.rot_z += .0080f; - fb.clear(); + renderer.clear(); auto transform_mat = engine::math::Matrix4::translate(-scene.camera.loc) * engine::math::Matrix4::rot_x(-scene.camera.rot_x) @@ -86,15 +88,16 @@ static void scene_main(FB& fb, std::function update_frame) { transform_mat * engine::math::Matrix4::translate(engine::math::Vector3{-.5f * rad, -.5f * rad, -.5f * rad}) * scale_mat, transform_mat * engine::math::Matrix4::translate(engine::math::Vector3{+.5f * rad, +.5f * rad, +.5f * rad}) * scale_mat, }}; - auto projection_mat = engine::math::Matrix4::projection(static_cast(fb.height()) / static_cast(fb.width()), 2.f, 50.f); + auto pre_final_mat = final_transform_mat + * engine::math::Matrix4::projection(static_cast(renderer.height()) / static_cast(renderer.width()), 2.f, 50.f); for (int i = 0; i < 2; i++) { - auto final_mat = projection_mat * mats[i]; + auto final_mat = pre_final_mat * mats[i]; const auto& mesh = scene.objs[i].mesh; std::vector pts; for (const auto& vert : mesh.pts) pts.push_back({ final_mat * engine::math::Vector4{vert.point}, vert.data }); for (auto face : mesh.faces) - fb.draw_triangle({pts[face[0]], pts[face[1]], pts[face[2]]}); + renderer.draw_triangle({pts[face[0]], pts[face[1]], pts[face[2]]}); } cont = update_frame(); } @@ -121,10 +124,13 @@ static int main_term() { int w, h; getmaxyx(stdscr, h, w); - engine::fb::CharacterFrameBuffer cfb{static_cast(w), static_cast(h)}; + engine::Renderer renderer{ + std::make_unique(static_cast(w), static_cast(h))}; - scene_main(cfb, [&]() { - mvaddnstr(0, 0, cfb.chars(), cfb.width() * cfb.height()); + scene_main(renderer, engine::math::Matrix4::scale(engine::math::Vector3(2.f, 1.f, 1.f)), [&]() { + mvaddnstr(0, 0, + static_cast(renderer.fb.get())->chars(), + renderer.width() * renderer.height()); bool cont = true; //timeout(1000 / FPS); @@ -156,7 +162,7 @@ static int main_term() { } getmaxyx(stdscr, h, w); - cfb.resize(static_cast(w), static_cast(h)); + renderer.resize(static_cast(w), static_cast(h)); return cont; }); @@ -183,17 +189,21 @@ static int main_graphical() { } window = SDL_CreateWindow("Engine", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (window == NULL) { + SDL_Quit(); std::cerr << "Error: SDL_CreateWindow error: " << SDL_GetError() << std::endl; return EXIT_FAILURE; } renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT); - engine::fb::PixelFrameBuffer pfb{SCREEN_WIDTH, SCREEN_HEIGHT}; + engine::Renderer engine_renderer{ + std::make_unique(SCREEN_WIDTH, SCREEN_HEIGHT)}; SDL_Event e; - scene_main(pfb, [&]() { - SDL_UpdateTexture(texture, NULL, pfb.pixels(), SCREEN_WIDTH * 4); + scene_main(engine_renderer, engine::math::Matrix4::idty(), [&]() { + SDL_UpdateTexture(texture, NULL, + static_cast(engine_renderer.fb.get())->pixels(), + SCREEN_WIDTH * 4); SDL_RenderClear(renderer); SDL_RenderCopy(renderer, texture, NULL, NULL); SDL_RenderPresent(renderer); -- cgit v1.2.3