Skip to content

Commit d002225

Browse files
committed
[EDITOR] move 3d renderer to lmtyn_editor.h
1 parent cd724e1 commit d002225

File tree

2 files changed

+118
-107
lines changed

2 files changed

+118
-107
lines changed

lmtyn_editor.h

Lines changed: 106 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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 */
79
typedef 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+
12221318
LMTYN_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
*/
16691767
LMTYN_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

win32_lmtyn_editor.c

Lines changed: 12 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -24,77 +24,6 @@ LMTYN_API u8 csr_init(csr_context *ctx, u32 width, u32 height)
2424
return 1;
2525
}
2626

27-
LMTYN_API void csr_render_mesh(csr_context *ctx, lmtyn_mesh *mesh, v3 cam_position, v3 model_position, u32 frame)
28-
{
29-
v3 world_up = vm_v3(0.0f, 1.0f, 0.0f);
30-
v3 cam_look_at_pos = vm_v3(0.0f, 0.0f, 0.0f);
31-
f32 cam_fov = 90.0f;
32-
v3 model_rotation_y = vm_v3(0.0f, 1.0f, 0.0);
33-
34-
m4x4 projection = vm_m4x4_perspective(vm_radf(cam_fov), (f32)ctx->width / (f32)ctx->height, 0.1f, 1000.0f);
35-
m4x4 view = vm_m4x4_lookAt(cam_position, cam_look_at_pos, world_up);
36-
m4x4 projection_view = vm_m4x4_mul(projection, view);
37-
m4x4 model_base = vm_m4x4_translate(vm_m4x4_identity, model_position);
38-
m4x4 model_view_projection = vm_m4x4_mul(projection_view, model_base);
39-
40-
/* Render cube */
41-
csr_render(
42-
ctx,
43-
CSR_RENDER_SOLID,
44-
CSR_CULLING_CCW_BACKFACE, 3,
45-
mesh->vertices, mesh->vertices_size,
46-
(int *)mesh->indices, mesh->indices_size,
47-
model_view_projection.e);
48-
}
49-
50-
LMTYN_API void csr_blit_scaled(csr_context *ctx, lmtyn_editor *editor)
51-
{
52-
lmtyn_editor_region *r = &editor->regions[LMTYN_EDITOR_REGION_RENDER];
53-
54-
u32 y;
55-
u32 x;
56-
57-
for (y = 0; y < r->h; ++y)
58-
{
59-
u32 fb_y = r->y + y;
60-
61-
if (fb_y >= editor->framebuffer_height)
62-
{
63-
break;
64-
}
65-
66-
for (x = 0; x < r->w; ++x)
67-
{
68-
u32 fb_x = r->x + x;
69-
70-
/* Sample CSR pixel at pixel center and flip Y */
71-
u32 src_x = (u32)((x + 0.5f) * ctx->width / r->w);
72-
u32 src_y = (u32)((y + 0.5f) * ctx->height / r->h);
73-
74-
csr_color *src;
75-
76-
if (fb_x >= editor->framebuffer_width)
77-
{
78-
break;
79-
}
80-
81-
if (src_x >= ctx->width)
82-
{
83-
src_x = ctx->width - 1;
84-
}
85-
86-
if (src_y >= ctx->height)
87-
{
88-
src_y = ctx->height - 1;
89-
}
90-
91-
src = &ctx->framebuffer[src_y * ctx->width + src_x];
92-
93-
editor->framebuffer[fb_y * editor->framebuffer_width + fb_x] = (src->r << 16) | (src->g << 8) | (src->b);
94-
}
95-
}
96-
}
97-
9827
LMTYN_API void win32_lmtyn_editor_resize_framebuffer(lmtyn_editor *editor, i32 new_w, i32 new_h, BITMAPINFO *bmi, csr_context *ctx)
9928
{
10029
if (new_w <= 0 || new_h <= 0)
@@ -140,7 +69,6 @@ typedef struct win32_lmtyn_editor_state
14069
{
14170
lmtyn_editor *editor;
14271
lmtyn_editor_input *input;
143-
lmtyn_mesh *mesh;
14472
BITMAPINFO *bmi;
14573
csr_context *ctx;
14674

@@ -287,6 +215,12 @@ i32 WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, i32 nShow)
287215
circles[0].radius = 1.0f;
288216
editor.circles_count = 1;
289217

218+
lmtyn_mesh mesh = {0};
219+
mesh.vertices_capacity = sizeof(f32) * 4096;
220+
mesh.indices_capacity = sizeof(u32) * 4096;
221+
mesh.vertices = (f32 *)malloc(sizeof(f32) * mesh.vertices_capacity);
222+
mesh.indices = (u32 *)malloc(sizeof(u32) * mesh.indices_capacity);
223+
290224
win32_lmtyn_editor_resize_framebuffer(&editor, width, height, &bmi, &ctx);
291225

292226
lmtyn_editor_initialize(
@@ -295,18 +229,12 @@ i32 WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, i32 nShow)
295229
width,
296230
height,
297231
circles,
298-
CIRCLES_CAPACITY);
299-
300-
lmtyn_mesh mesh = {0};
301-
mesh.vertices_capacity = sizeof(f32) * 4096;
302-
mesh.indices_capacity = sizeof(u32) * 4096;
303-
mesh.vertices = (f32 *)malloc(sizeof(f32) * mesh.vertices_capacity);
304-
mesh.indices = (u32 *)malloc(sizeof(u32) * mesh.indices_capacity);
232+
CIRCLES_CAPACITY,
233+
&mesh);
305234

306235
win32_lmtyn_editor_state state = {0};
307236
state.editor = &editor;
308237
state.input = &editor_input;
309-
state.mesh = &mesh;
310238
state.bmi = &bmi;
311239
state.ctx = &ctx;
312240

@@ -350,9 +278,6 @@ i32 WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, i32 nShow)
350278

351279
HDC hdc = GetDC(hwnd);
352280

353-
csr_color clear_color = {40, 40, 40};
354-
v3 cam_position = vm_v3(0.0f, 0.0f, 1.0f);
355-
356281
u32 frame;
357282

358283
for (;;)
@@ -368,23 +293,10 @@ i32 WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, i32 nShow)
368293
DispatchMessage(&msg);
369294
}
370295

371-
lmtyn_editor_render(&editor, &editor_input);
372-
373-
/* Draw 3D Model */
374-
if (editor.circles_count > 1)
375-
{
376-
mesh.vertices_size = 0;
377-
mesh.indices_size = 0;
378-
379-
lmtyn_mesh_generate(&mesh, 0, circles, editor.circles_count, 4);
380-
lmtyn_mesh_normalize(&mesh, 0.0f, 0.0f, 0.0f, 1.0f);
381-
382-
csr_render_clear_screen(&ctx, clear_color);
383-
csr_render_mesh(&ctx, &mesh, cam_position, vm_v3_zero, frame);
384-
csr_blit_scaled(&ctx, &editor);
385-
386-
lmtyn_editor_draw_borders(&editor);
387-
}
296+
lmtyn_editor_render(
297+
&editor,
298+
&editor_input,
299+
&ctx);
388300

389301
StretchDIBits(
390302
hdc,

0 commit comments

Comments
 (0)