finished wasm impl

This commit is contained in:
Victor Vobis 2026-02-06 19:27:20 +01:00
parent faa5316836
commit 29ab44ba76
20 changed files with 107 additions and 95 deletions

5
.gitignore vendored
View File

@ -7,6 +7,11 @@
*.obj
*.elf
*.html
*.js
*.wasm
*.data
# Linker output
*.ilk
*.map

View File

@ -26,6 +26,13 @@ PLATFORM ?= PLATFORM_DESKTOP
LIBRAYLIB := libraylib.a
WEB_OUTPUT := miniRT
WEB_OUTPUT_HTML := $(WEB_OUTPUT).html
WEB_OUTPUT_JS := $(WEB_OUTPUT).js
WEB_OUTPUT_WASM := $(WEB_OUTPUT).wasm
WEB_OUTPUT_DATA := $(WEB_OUTPUT).data
ifeq ($(PLATFORM), PLATFORM_WEB)
LIBRAYLIB := libraylib.web.a
NAME := $(NAME).html
@ -133,7 +140,7 @@ clean:
make clean -C raylib/src
fclean: clean
rm -rf $(NAME) $(MAP)
rm -rf $(NAME) $(MAP) $(WEB_OUTPUT_JS) $(WEB_OUTPUT_HTML) $(WEB_OUTPUT_WASM) $(WEB_OUTPUT_DATA)
make fclean -C libft
make fclean -C memory
make fclean -C libft/printf

BIN
miniRT

Binary file not shown.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -494,14 +494,16 @@ typedef struct s_data
t_mouse mouse;
t_container *menu;
t_container explorer;
void *param;
void (*func_ptr)(void *, void *);
void *current_menu;
void *func_ptr_param;
void (*func_ptr)(void *data, void *param);
bool should_rerender;
bool go;
#ifndef __EMSCRIPTEN__
struct s_thread *threads;
int thread_count;
pthread_barrier_t barrier;
pthread_rwlock_t rwlock;
bool go;
#endif
} t_data;
@ -752,7 +754,7 @@ bool key_move_light(int keycode, t_scene *scene);
uint key_misc_function(int keycode, t_scene *scene, t_data *data);
/* Menu */
void container_draw(void *menu, void *pixel);
void container_draw(void *data, void *menu);
void container_item_get_by_id(t_container *container, \
uint id, uint x, uint y);
void help_menu_draw(void *data_ptr, void *null);

BIN
raylib/src/strbrREj Normal file

Binary file not shown.

View File

@ -1,8 +1,8 @@
C -0.80287,-2.14970,-500 1.80997,-0.62646,9.81486 40
L 5.0,0.0,3.0 1.0 255,255,255
L 0.0,5.0,3.0 1.0 255,255,255
L -5.0,0.0,3.0 1.0 255,255,255
L 0.0,-5.0,3.0 1.0 255,255,255
C -1.44594,0.62854,-11.608 0.17775,-0.11375,0.97747 40
L 5.70700,0.60600,3.90900 1.0 255,255,255
L 0.30300,5.10100,3.16500 1.0 255,255,255
L -4.97998,0.13200,3.90900 1.0 255,255,255
L 0.14300,-4.97998,3.12100 1.0 255,255,255
A 0.50000 254,254,254
sp -1.0,-0.10000,5.0 2.0 28,121,241
sp 2.0,-0.10000,5.0 2.0 32,30,32

View File

@ -1,3 +1,3 @@
C 0,0,-5 0,0,5 90
C 0,0,-5 0,0,1 90
L 0,2,-2 1 255,255,200
A 0.5 0,0,0

View File

@ -42,7 +42,8 @@ void container_item_get_by_id(t_container *cont, uint id, uint x, uint y)
if (cont->item[i].type == ITEM_BUTTON \
&& cont->item[i].button.func_ptr)
{
cont->item[i].button.func_ptr(cont->data, &cont->item[i].param);
cont->data->func_ptr_param = &cont->item[i].param;
cont->data->func_ptr = cont->item[i].button.func_ptr;
if (cont->param.type == PARAM_CONE \
|| cont->param.type == PARAM_CYL)
cont->param.func(cont->param.param);

View File

@ -68,8 +68,7 @@ void explorer_read_dir(void *data, void *param)
explorer_read_dir_loop(dir, param, cwd, &explorer);
closedir(dir);
container_item_desc_sort(&explorer);
((t_data *)data)->func_ptr = container_draw;
((t_data *)data)->param = &explorer;
((t_data *)data)->current_menu = &explorer;
return ;
}

View File

@ -17,9 +17,8 @@ uint key_misc_function(int keycode, t_scene *scene, t_data *data)
if (keycode == KEY_P)
scene_save(scene);
else if (keycode == KEY_M)
return (data->func_ptr = container_draw, \
data->param = &data->menu[MENU_MAIN], true);
else if (keycode == KEY_H)
return (data->current_menu = &data->menu[MENU_MAIN], true);
else if (keycode == KEY_Y)
return (data->func_ptr = help_menu_draw, true);
else if (keycode == KEY_DELETE)
{

View File

@ -69,6 +69,7 @@ void rotate_camera(float rotation_angle, t_vector perpendicular_normal, \
normalize_vector(&rotation_axis);
rodrigues_rotation(&camera->normal, rotation_axis, \
rotation_angle * M_PI / 180);
normalize_vector(&camera->normal);
calc_camera_tilt(camera);
calc_camera_space(camera);
}

View File

@ -41,19 +41,24 @@ void help_menu_draw(void *data_ptr, void *pixel)
t_rect background;
t_data *data;
background = (t_rect){.x = 5, .y = 5, .width = 400, .height = 220, };
(void)pixel;
uint x = WI / 4;
uint y = HI / 4;
data = data_ptr;
background = (t_rect){x, y, .width = WI / 2, .height = HI / 2, };
(void)pixel;
rt_draw_rect_blend(background, data->pixel, 0, HELP_MENU_BG);
glyph_print(10, 10, \
"controls\nmove: wasdqe\n" \
"rotate: arrow keys\n" \
"move light: hjklio\t" \
"next light: n\n\n" \
"drag object: lmouse\nchange object: rmouse + use menu\n\n" \
"resolution and anti-aliasing:\nnumpad + or -; Enter to reset\n" \
"backsp: reset_cam\n" \
"p: save file\nesc: exit\n", \
data->pixel);
data->func_ptr = NULL;
glyph_print(x + 2, y + 2, \
"controls\n" \
"main menu: m\n" \
"movement: \n" \
"move: wasdqe\n" \
"rotate: arrow keys\n" \
"move light: hjklio\n" \
"select next light: n\n\n" \
"drag object: lmouse\nchange object: rmouse + use menu\n\n" \
"resolution and anti-aliasing:\n\nnumpad + or -; Enter to reset\n" \
"backsp: reset_cam\n" \
"p: save file\nesc: exit\n", \
data->pixel);
}

View File

@ -70,7 +70,8 @@ void mouse_move_body(Vector2 mouse_delta, int id_group, t_mouse *mouse)
void decrease_resolution(t_scene *scene)
{
if (scene->resolution_x == 1 && scene->resolution_y == 1)
if (scene->resolution_x == SCENE_MAX_RESOLUTION_X
&& scene->resolution_y == SCENE_MAX_RESOLUTION_Y)
{
scene->anti_aliasing = false;
scene->resolution_x = RESOLUTION_SCALE_X;
@ -83,6 +84,10 @@ void mouse_left_move(int x, t_mouse *mouse, t_scene *scene)
uint id_group;
Vector2 mouse_delta = GetMouseDelta();
if (mouse_delta.x == 0 && mouse_delta.y == 0) return;
mouse->data->should_rerender = true;
if (mouse->grabbed == NULL && !mouse->slider)
{
scene->camera.position.x -= mouse_delta.x / 10;

View File

@ -21,19 +21,21 @@ void mouse_click_left(int x, int y, t_scene *scene, t_mouse *mouse)
mouse->prev_y = y;
mouse->left_is_pressed = true;
mouse->data->scene.body_focus = NULL;
mouse->data->should_rerender = true;
id_group = id_group_get(scene->pixel[y * WI + x].id);
mouse->data->func_ptr = NULL;
if (id_group == ID_GROUP_ITEM)
if (id_group == ID_GROUP_ITEM && mouse->data->current_menu)
{
container_item_get_by_id(mouse->data->param, \
container_item_get_by_id(mouse->data->current_menu, \
scene->pixel[y * WI + x].id, x, y);
mouse->data->func_ptr = container_draw;
}
else if (id_group > 0 && id_group < ID_GROUP_ITEM)
{
body = body_get_by_id(scene->pixel[y * WI + x].id, scene);
scene->body_focus = NULL;
mouse_grab(mouse, body);
} else {
mouse->data->current_menu = NULL;
}
}
@ -45,6 +47,7 @@ void mouse_click_right(int x, int y, t_data *data, t_mouse *mouse)
id_group = id_group_get(data->pixel[y * WI + x].id);
data->scene.body_focus = NULL;
mouse->right_is_pressed = true;
data->should_rerender = true;
if (id_group >= ID_GROUP_SPHERE && id_group <= ID_GROUP_CONE)
{
body = body_get_by_id(data->pixel[y * WI + x].id, &data->scene);
@ -63,8 +66,7 @@ void mouse_click_right(int x, int y, t_data *data, t_mouse *mouse)
}
else
mouse->grabbed = NULL;
data->param = &data->menu[id_group];
data->func_ptr = container_draw;
data->current_menu = &data->menu[id_group];
}
void menu_scroll_items(t_container *param, uint scaler)
@ -86,24 +88,26 @@ int mouse_press(int x, int y, t_data *data)
if (x < 0 || x >= WI || y < 0 || y >= HI)
return (0);
id_group = id_group_get(data->scene.pixel[y * WI + x].id);
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && !data->mouse.left_is_pressed)
mouse_click_left(x, y, &data->scene, &data->mouse);
else if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
else if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) && !data->mouse.right_is_pressed)
mouse_click_right(x, y, data, &data->mouse);
float mouse_wheel = GetMouseWheelMove();
if (mouse_wheel > 0)
{
data->should_rerender = true;
if (id_group == ID_GROUP_MENU_MAIN || id_group == ID_GROUP_ITEM)
menu_scroll_items(data->param, SCROLL_UP_DISTANCE);
menu_scroll_items(data->current_menu, SCROLL_UP_DISTANCE);
else
key_change_fov(KEY_SLASH, &data->scene.camera);
}
else if (mouse_wheel < 0)
{
data->should_rerender = true;
if (id_group == ID_GROUP_MENU_MAIN || id_group == ID_GROUP_ITEM)
menu_scroll_items(data->param, SCROLL_DOWN_DISTANCE);
menu_scroll_items(data->current_menu, SCROLL_DOWN_DISTANCE);
else
key_change_fov(KEY_PERIOD, &data->scene.camera);
}
@ -112,7 +116,7 @@ int mouse_press(int x, int y, t_data *data)
int mouse_release(int x, int y, t_data *data)
{
if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT))
if (data->mouse.left_is_pressed && !IsMouseButtonDown(MOUSE_BUTTON_LEFT))
{
data->mouse.left_is_pressed = false;
data->scene.resolution_x = SCENE_MAX_RESOLUTION_X, data->scene.resolution_y = SCENE_MAX_RESOLUTION_Y;
@ -124,10 +128,12 @@ int mouse_release(int x, int y, t_data *data)
}
data->mouse.grabbed = NULL;
data->mouse.slider = NULL;
data->should_rerender = true;
}
else if (!IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
if (data->mouse.right_is_pressed && !IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
{
data->mouse.right_is_pressed = false;
data->should_rerender = true;
}
return (0);
}

View File

@ -97,6 +97,7 @@ void initialize_data(t_data *data, char *path)
Image img = GenImageColor(WI, HI, BLACK);
data->pixel_colors = img.data;
data->texture = LoadTextureFromImage(img);
data->should_rerender = true;
pixels_image_syncronize(data->pixel_colors, data->pixel);
}
@ -168,6 +169,11 @@ void get_key_code(int keys[]) {
keys[c++] = KEY_EQUAL;
if (c == 3) return ;
}
if (IsKeyPressed(KEY_Y)) {
keys[c++] = KEY_Y;
if (c == 3) return ;
}
if (IsKeyPressed(KEY_MINUS)) {
keys[c++] = KEY_MINUS;
if (c == 3) return ;
@ -206,9 +212,12 @@ void get_key_code(int keys[]) {
}
}
bool key_was_pressed = false;
void main_loop() {
int keys[3] = { 0 };
Vector2 mouse_pos = GetMousePosition();
if (data.mouse.left_is_pressed || data.mouse.right_is_pressed)
{
@ -223,16 +232,25 @@ void main_loop() {
{
if (keys[i])
{
key_was_pressed = true;
data.should_rerender = true;
data.scene.resolution_x = RESOLUTION_SCALE_X;
data.scene.resolution_y = RESOLUTION_SCALE_Y;
key_press(keys[i], &data);
}
}
rendering_loop(&data);
if (keys[0] && keys[0] != KEY_EQUAL) {
data.scene.resolution_x = SCENE_MAX_RESOLUTION_X;
data.scene.resolution_y = SCENE_MAX_RESOLUTION_Y;
}
if (key_was_pressed) key_was_pressed = false;
else {
data.should_rerender = false;
data.func_ptr = NULL;
}
}
int main(int argc, char **argv)
@ -242,56 +260,18 @@ int main(int argc, char **argv)
t_thread thread[THREAD_COUNT];
#endif /* ifndef __EMSCRIPTEN__ */
#ifdef __EMSCRIPTEN__
EM_ASM(
console.log(FS.readdir('/'));
console.log(FS.readdir('/scenes'));
console.log("Starting Program");
);
#endif
if (argc == 1)
path = "scenes/multilight.rt";
else
path = validate_file_extension(argc, argv);
#ifdef __EMSCRIPTEN__
EM_ASM(
console.log("Before Init Data");
);
#endif
initialize_data(&data, path);
#ifdef __EMSCRIPTEN__
EM_ASM(
console.log("After Init Data");
);
#endif
lst_memory(&data, data_destroy_func, ADD);
#ifdef __EMSCRIPTEN__
EM_ASM(
console.log("Initialized Data");
);
#endif
#ifndef __EMSCRIPTEN__
threads_init(thread, &data);
#endif
rendering_loop(&data);
#ifndef __EMSCRIPTEN__
data.threads = thread;
#endif
#ifdef __EMSCRIPTEN__
EM_ASM(
console.log("Starting main loop");
);
#endif
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(main_loop, 0, 1);
#else

View File

@ -36,7 +36,7 @@ uint color_blend(uint color1, uint color2)
int g;
int b;
r = ((color1 >> 16) & 0xff) - ((color2 >> 16) & 0xff);
r = ((color1) & 0xff) - ((color2) & 0xff);
if (r < 0)
r = 0;
else if (r > 255)
@ -46,7 +46,7 @@ uint color_blend(uint color1, uint color2)
g = 0;
else if (g > 255)
g = 255;
b = ((color1) & 0xff) - ((color2) & 0xff);
b = ((color1 >> 16) & 0xff) - ((color2 >> 16) & 0xff);
if (b < 0)
b = 0;
else if (b > 255)

View File

@ -88,24 +88,28 @@ void ray_check_bodys(t_pixel *pixel, t_vector ray, t_scene *scene)
uint rendering_loop(t_data *data)
{
if (data->should_rerender) {
ft_printf("Rendering\n");
#ifndef __EMSCRIPTEN__
pthread_rwlock_unlock(&data->rwlock);
pthread_barrier_wait(&data->barrier);
pthread_rwlock_wrlock(&data->rwlock);
pthread_barrier_wait(&data->barrier);
pthread_rwlock_unlock(&data->rwlock);
pthread_barrier_wait(&data->barrier);
pthread_rwlock_wrlock(&data->rwlock);
pthread_barrier_wait(&data->barrier);
#else
define_camera_rays(data->pixel, &data->scene.camera, &data->scene);
define_camera_rays(data->pixel, &data->scene.camera, &data->scene);
#endif /* ifndef __EMSCRITPEN */
if (data->func_ptr)
data->func_ptr(data, data->param);
glyph_print(1, 1, "Press 'y' for help", data->pixel);
if (data->current_menu)
container_draw(data, data->current_menu);
if (data->func_ptr) {
data->func_ptr(data, data->func_ptr_param);
}
}
UpdateTexture(data->texture, data->pixel_colors);
BeginDrawing();
DrawTexture(data->texture, 0, 0, RAYWHITE);
EndDrawing();
// mlx_put_image_to_window(data->mlx, data->win, &data->image, 0, 0);
// mlx_do_sync(data->mlx);
return (0);
}