11#ifndef LMTYN_EDITOR_H
22#define LMTYN_EDITOR_H
33
4- #include "lmtyn.h"
4+ #include "lmtyn.h" /* The main modelling algorithm */
5+ #include "deps/csr.h" /* 3D Software Renderer */
6+ #include "deps/vm.h" /* 3D Linear Algebra / Vector Math */
57
68/* contains some general fields relevant for all ui elements */
79typedef struct lmtyn_editor_ui_header
@@ -99,6 +101,7 @@ typedef struct lmtyn_editor
99101
100102 lmtyn_editor_selection_mode selection_mode ;
101103
104+ lmtyn_mesh * mesh ;
102105 lmtyn_shape_circle * circles ;
103106 u32 circles_capacity ;
104107 u32 circles_count ;
@@ -1219,6 +1222,99 @@ LMTYN_API void lmtyn_editor_draw_region_labels(lmtyn_editor *editor)
12191222 }
12201223}
12211224
1225+ LMTYN_API void lmtyn_editor_draw_3d_framebuffer (lmtyn_editor * editor , csr_context * ctx )
1226+ {
1227+ lmtyn_editor_region * r = & editor -> regions [LMTYN_EDITOR_REGION_RENDER ];
1228+
1229+ u32 y ;
1230+ u32 x ;
1231+
1232+ for (y = 0 ; y < r -> h ; ++ y )
1233+ {
1234+ u32 fb_y = r -> y + y ;
1235+
1236+ if (fb_y >= editor -> framebuffer_height )
1237+ {
1238+ break ;
1239+ }
1240+
1241+ for (x = 0 ; x < r -> w ; ++ x )
1242+ {
1243+ u32 fb_x = r -> x + x ;
1244+
1245+ /* Sample CSR pixel at pixel center and flip Y */
1246+ u32 src_x = (u32 )((x + 0.5f ) * ctx -> width / r -> w );
1247+ u32 src_y = (u32 )((y + 0.5f ) * ctx -> height / r -> h );
1248+
1249+ csr_color * src ;
1250+
1251+ if (fb_x >= editor -> framebuffer_width )
1252+ {
1253+ break ;
1254+ }
1255+
1256+ if (src_x >= ctx -> width )
1257+ {
1258+ src_x = ctx -> width - 1 ;
1259+ }
1260+
1261+ if (src_y >= ctx -> height )
1262+ {
1263+ src_y = ctx -> height - 1 ;
1264+ }
1265+
1266+ src = & ctx -> framebuffer [src_y * ctx -> width + src_x ];
1267+
1268+ editor -> framebuffer [fb_y * editor -> framebuffer_width + fb_x ] = (src -> r << 16 ) | (src -> g << 8 ) | (src -> b );
1269+ }
1270+ }
1271+ }
1272+
1273+ LMTYN_API void lmtyn_editor_draw_3d_model (
1274+ lmtyn_editor * editor ,
1275+ csr_context * ctx )
1276+ {
1277+ csr_color clear_color = {40 , 40 , 40 };
1278+ v3 cam_position = vm_v3 (0.0f , 0.0f , 1.0f );
1279+ v3 world_up = vm_v3 (0.0f , 1.0f , 0.0f );
1280+ v3 cam_look_at_pos = vm_v3 (0.0f , 0.0f , 0.0f );
1281+ f32 cam_fov = 90.0f ;
1282+ v3 model_rotation_y = vm_v3 (0.0f , 1.0f , 0.0 );
1283+ v3 model_position = vm_v3_zero ;
1284+
1285+ m4x4 projection = vm_m4x4_perspective (vm_radf (cam_fov ), (f32 )ctx -> width / (f32 )ctx -> height , 0.1f , 1000.0f );
1286+ m4x4 view = vm_m4x4_lookAt (cam_position , cam_look_at_pos , world_up );
1287+ m4x4 projection_view = vm_m4x4_mul (projection , view );
1288+ m4x4 model_base = vm_m4x4_translate (vm_m4x4_identity , model_position );
1289+ m4x4 model_view_projection = vm_m4x4_mul (projection_view , model_base );
1290+
1291+ if (editor -> circles_count < 2 )
1292+ {
1293+ return ;
1294+ }
1295+
1296+ /* Reset Mesh vertices/indices */
1297+ editor -> mesh -> vertices_size = 0 ;
1298+ editor -> mesh -> indices_size = 0 ;
1299+
1300+ /* Generate Mesh */
1301+ lmtyn_mesh_generate (editor -> mesh , 0 , editor -> circles , editor -> circles_count , 4 );
1302+ lmtyn_mesh_normalize (editor -> mesh , 0.0f , 0.0f , 0.0f , 1.0f );
1303+
1304+ /* Draw Mesh to CSR Framebuffer */
1305+ csr_render_clear_screen (ctx , clear_color );
1306+ csr_render (
1307+ ctx ,
1308+ CSR_RENDER_SOLID ,
1309+ CSR_CULLING_CCW_BACKFACE , 3 ,
1310+ editor -> mesh -> vertices , editor -> mesh -> vertices_size ,
1311+ (i32 * )editor -> mesh -> indices , editor -> mesh -> indices_size ,
1312+ model_view_projection .e );
1313+
1314+ /* Draw CSR Framebuffer to Editor Framebuffer */
1315+ lmtyn_editor_draw_3d_framebuffer (editor , ctx );
1316+ }
1317+
12221318LMTYN_API void lmtyn_editor_regions_update (
12231319 lmtyn_editor * editor )
12241320{
@@ -1288,7 +1384,8 @@ LMTYN_API u8 lmtyn_editor_initialize(
12881384 u32 framebuffer_width ,
12891385 u32 framebuffer_height ,
12901386 lmtyn_shape_circle * circles ,
1291- u32 circles_capacity )
1387+ u32 circles_capacity ,
1388+ lmtyn_mesh * mesh )
12921389{
12931390 if (!editor || !framebuffer || !circles || framebuffer_width < 1 || framebuffer_height < 1 || circles_capacity < 1 )
12941391 {
@@ -1301,7 +1398,7 @@ LMTYN_API u8 lmtyn_editor_initialize(
13011398
13021399 editor -> grid_scale = 10.0f ;
13031400 editor -> grid_cell_size = 1.0f ;
1304- editor -> grid_scroll_speed = 1.0f ;
1401+ editor -> grid_scroll_speed = 0.5f ;
13051402 editor -> grid_color = 0x20404040 ;
13061403 editor -> grid_color_axis = 0x00666666 ;
13071404
@@ -1321,10 +1418,11 @@ LMTYN_API u8 lmtyn_editor_initialize(
13211418 editor -> regions_toolbar_size_y = 30 ;
13221419
13231420 editor -> snap_enabled = 1 ;
1324- editor -> snap_interval = 1.0f ;
1421+ editor -> snap_interval = 0.5f ;
13251422
13261423 editor -> selection_mode = LMTYN_EDITOR_MODE_CIRCLE_PLACEMENT ;
13271424
1425+ editor -> mesh = mesh ;
13281426 editor -> circles = circles ;
13291427 editor -> circles_capacity = circles_capacity ;
13301428 editor -> circles_color = 0x00FFCE1B ;
@@ -1668,7 +1766,8 @@ LMTYN_API void lmtyn_editor_ui_update(
16681766 */
16691767LMTYN_API void lmtyn_editor_render (
16701768 lmtyn_editor * editor ,
1671- lmtyn_editor_input * input )
1769+ lmtyn_editor_input * input ,
1770+ csr_context * ctx )
16721771{
16731772 lmtyn_editor_input_update (editor , input );
16741773
@@ -1683,9 +1782,9 @@ LMTYN_API void lmtyn_editor_render(
16831782 lmtyn_editor_draw_grid (editor , LMTYN_EDITOR_REGION_XY );
16841783 lmtyn_editor_draw_region_labels (editor );
16851784
1686- lmtyn_editor_draw_borders (editor );
1687-
16881785 lmtyn_editor_draw_circles (editor );
1786+ lmtyn_editor_draw_3d_model (editor , ctx );
1787+ lmtyn_editor_draw_borders (editor );
16891788
16901789 lmtyn_editor_ui_update (editor , input );
16911790
0 commit comments