diff --git a/Makefile b/Makefile index 3f4a0a1..a96e775 100644 --- a/Makefile +++ b/Makefile @@ -167,7 +167,7 @@ out/l2/allie_cpp/glue.o: src/l2/allie_cpp/glue.c $(HEADERS_src_l2) $(xdg_shell_c full_allie_cpp_obj := out/l2/allie_cpp/glue.o $(xdg_shell_private_o) # todo: add a call to a function that obtains all .hpp files from l2/allie_cpp -out/l3/allie_cpp/r4c: src/l3/r4/r4c.cpp src/l1/allie_cpp/anne/utils.hpp \ +out/l3/allie_cpp/r4c: src/l3/r4/r4c.cpp src/l1/allie_cpp/utils.hpp \ src/l2/allie_cpp/alice.hpp $(full_allie_cpp_obj) mkdir -p out/l3/allie_cpp $(cxx_cpp) $(cxxflags_cpp) -o $@ $< $(full_allie_cpp_obj) \ diff --git a/src/l1/allie_cpp/anne/utils.hpp b/src/l1/allie_cpp/utils.hpp similarity index 87% rename from src/l1/allie_cpp/anne/utils.hpp rename to src/l1/allie_cpp/utils.hpp index fac6234..805993a 100644 --- a/src/l1/allie_cpp/anne/utils.hpp +++ b/src/l1/allie_cpp/utils.hpp @@ -27,6 +27,12 @@ struct allie_SpanU8 { explicit allie_SpanU8(std::string_view cpp_span): data(reinterpret_cast(cpp_span.data())), len(cpp_span.size()){} }; +struct allie_VecU32 { + U32* buf; + U64 len; + U64 capacity; +}; + struct allie_VecU8 { const U8* buf; U64 len; @@ -94,4 +100,11 @@ void allie_form_a_cvec_of_non_primitive_type(VecDstT& dst, const std::vector +void allie_form_cpp_vector_of_matching_types(std::vector& dst, const VecT& src) { + dst.resize(src.len); + memcpy(dst.data(), src.buf, src.len * sizeof(T)); } \ No newline at end of file diff --git a/src/l2/alice/assets.h b/src/l2/alice/assets.h index a30c9fc..1b58067 100644 --- a/src/l2/alice/assets.h +++ b/src/l2/alice/assets.h @@ -56,7 +56,7 @@ typedef struct { typedef struct { vec3 pos; - vec3 color; + cvec3 color; } ShinyMeshVertexInc; typedef struct { @@ -102,6 +102,8 @@ typedef struct { float anim_time; } Pipeline1PushRangeFragment; + +// todo: rename to AliceSpotlight typedef struct { vec3 pos; char _padding_0[4]; @@ -113,6 +115,7 @@ typedef struct { char _padding_3[12]; } Pipeline0Spotlight; +// todo: rename to AlicePointLight typedef struct { vec3 pos; char _padding_0[4]; @@ -120,9 +123,6 @@ typedef struct { char _padding_1[4]; } Pipeline0PointLight; -#define pipeline_0_ubo_point_light_max_count 120 -#define pipeline_0_ubo_spotlight_max_count 20 - /* This structure will be coupled with point_light and spotlight arrays */ typedef struct { S32 point_light_count; diff --git a/src/l2/alice/camera.h b/src/l2/alice/camera.h index 9c4942e..9acdece 100644 --- a/src/l2/alice/camera.h +++ b/src/l2/alice/camera.h @@ -29,4 +29,12 @@ void AliceCamVerticalControl_update_direction( -self->pitch_cap, self->pitch_cap ); self->cam.cam_basis = marie_simple_camera_rot_m_basis_in_cols(yaw, pitch, 0); +} + +mat4 AliceCam_get_t_mat(const AliceCam* self, ivec2 win_size) { + mat4 projection_matrix = marie_perspective_projection_fov_mat4( + (float)win_size.x, (float)win_size.y, self->fov, 0.01f, 1000); + mat4 camera_rotation_matrix = marie_mat3_to_mat4_transposed(self->cam_basis); + mat4 camera_translation_matrix = marie_translation_mat4(vec3_minus(self->pos)); + return mat4_mul_mat4(projection_matrix, mat4_mul_mat4(camera_rotation_matrix, camera_translation_matrix)); } \ No newline at end of file diff --git a/src/l2/alice/engine.h b/src/l2/alice/engine.h index 71766ef..91805f0 100644 --- a/src/l2/alice/engine.h +++ b/src/l2/alice/engine.h @@ -361,7 +361,7 @@ AlicePipeline0b create_graphics_pipeline_0b( }, { .location = 1, .binding = 0, - .format = VK_FORMAT_R32G32B32_SFLOAT, + .format = VK_FORMAT_R8G8B8_UNORM, .offset = offsetof(ShinyMeshVertexInc, color), }, { @@ -1879,9 +1879,9 @@ void AliceAcknGenericMesh_set_inst(AliceAcknGenericMesh self, U64 instance, Gene tr_inv.x.z, tr_inv.y.z, tr_inv.z.z ); } -void AliceAcknShinyMesh_set_inst(AliceShinyMeshHand* self, U64 instance, ShinyMeshInstanceInc uncomp){ - assert(instance < self->instance_attr.count); - ShinyMeshInstance* staging = (ShinyMeshInstance*)MargaretSubbuf_get_mapped(&self->instance_attr.staging); +void AliceAcknShinyMesh_set_inst(AliceAcknShinyMesh self, U64 instance, ShinyMeshInstanceInc uncomp){ + assert(instance < self->el.instance_attr.count); + ShinyMeshInstance* staging = (ShinyMeshInstance*)MargaretSubbuf_get_mapped(&self->el.instance_attr.staging); staging[instance].base = uncomp; mat4 tr_inv = mat4_transpose(mat4_inverse(uncomp.model_t)); staging[instance].normal_t = mat3_new( @@ -1890,7 +1890,7 @@ void AliceAcknShinyMesh_set_inst(AliceShinyMeshHand* self, U64 instance, ShinyMe tr_inv.x.z, tr_inv.y.z, tr_inv.z.z ); } -void Alice_set_point_light_count(Alice* alice, U32 new_count){ +void Alice_resize_point_light_arr(Alice* alice, U32 new_count){ check(new_count < 1000000); Pipeline0UBO* ubo = (Pipeline0UBO*)MargaretSubbuf_get_mapped(&alice->pipeline0_light_conf.num_ubo_staging); ubo->point_light_count = (S32)new_count; @@ -1989,8 +1989,11 @@ ivec2 Alice_get_wl_confirmed_win_sz(Alice* alice) { return (ivec2){alice->wl.width_confirmed, alice->wl.height_confirmed}; } -void Alice_set_camera_info(Alice* alice, mat4 projection_matrix, vec3 cam_pos) { - printf("DEBUG: we set camera info to something sane\n"); - alice->cam_proj_t_mat = projection_matrix; +void Alice_set_camera_info(Alice* alice, mat4 t_mat, vec3 cam_pos) { + alice->cam_proj_t_mat = t_mat; alice->camera_pos = cam_pos; } + +bool Alice_is_wl_key_pressed(Alice* alice, xkb_keysym_t keysym) { + return keysym < 0x80 ? alice->wl.first_0x80_keys[keysym] : false; +} diff --git a/src/l2/alice/model_file.h b/src/l2/alice/model_file.h index 2cc620f..602bf35 100644 --- a/src/l2/alice/model_file.h +++ b/src/l2/alice/model_file.h @@ -10,7 +10,7 @@ static_assert(sizeof(float) == 4, "..."); static_assert(sizeof(GenericMeshVertexInc) == 4 * (3 + 2), "..."); static_assert(alignof(GenericMeshVertexInc) == 4, "..."); -static_assert(sizeof(ShinyMeshVertexInc) == 4 * (3 + 3), "..."); +static_assert(sizeof(ShinyMeshVertexInc) == 4 * 3 + 3 + 1, "..."); static_assert(alignof(ShinyMeshVertexInc) == 4, "..."); /* Yes, at this point I really started thinking that maybe I should have written Alice as a template */ diff --git a/src/l2/allie_cpp/alice.hpp b/src/l2/allie_cpp/alice.hpp index 49d6115..36248db 100644 --- a/src/l2/allie_cpp/alice.hpp +++ b/src/l2/allie_cpp/alice.hpp @@ -1,14 +1,16 @@ -#include "../../l1/allie_cpp/anne/utils.hpp" - +#include "../../l1/allie_cpp/utils.hpp" #include "../../../gen/l1/allie_cpp/geom.hpp" #include "assets.hpp" #include #include +#include +#include typedef void* allie_LucyFaceFixedSize; typedef void* allie_LucyFace; typedef void* allie_AliceAcknGenericMesh; +typedef void* allie_AliceAcknShinyMesh; typedef void* allie_Alice; /* Actually it is RBTreeNodeLucyFaceFixedSize */ @@ -55,6 +57,8 @@ struct LucyFace { extern "C" { void allie_AliceAcknGenericMesh_resize_instance_arr(allie_AliceAcknGenericMesh self, U64 new_count); void allie_AliceAcknGenericMesh_set_inst(allie_AliceAcknGenericMesh self, U64 index, GenericMeshInstanceInc uncomp); + void allie_AliceAcknShinyMesh_resize_instance_arr(allie_AliceAcknShinyMesh self, U64 new_count); + void allie_AliceAcknShinyMesh_set_inst(allie_AliceAcknShinyMesh self, U64 index, AliceShinyMeshInstanceInc uncomp); } struct AliceAcknGenericMesh { @@ -74,20 +78,36 @@ struct AliceAcknGenericMesh { } }; +struct AliceAcknShinyMesh { + ALLIE_CPP_OPAQUE(AliceAcknShinyMesh); + explicit AliceAcknShinyMesh(void* opa): opa(opa){} + // todo :implement (and fire in the right time) + ~AliceAcknShinyMesh() = default; + + void resize_instance_arr(U64 new_count) noexcept { + allie_AliceAcknShinyMesh_resize_instance_arr(opa, new_count); + } + + void set_inst(U64 index, AliceShinyMeshInstanceInc uncomp) noexcept { + allie_AliceAcknShinyMesh_set_inst(opa, index, uncomp); + } +}; + struct Alice; -struct AliceCallbacks { +struct allie_AliceCallbacks { void* guest; /* guest data, button, button action (wl_pointer_button_state) */ void (*on_wl_pointer_button)(void*, U32, U32); /* guest data, keysym, key action (wl_keyboard_key_state) */ void (*on_wl_keyboard_key)(void*, U32, U32); void (*on_another_frame)(void*, float); + void (*on_wl_pointer_motion)(void*, vec2); }; extern "C" { allie_Alice allie_Alice_new(); - void allie_Alice_mainloop(allie_Alice, const AliceCallbacks*); + void allie_Alice_mainloop(allie_Alice, const allie_AliceCallbacks*); allie_LucyFace allie_Alice_new_LucyFace(allie_Alice alice, allie_SpanU8 path); void allie_Alice_lucy_cache_add_glyphs(allie_Alice alice, allie_VecLucyGlyphCachingRequest req); void allie_Alice_lucy_renderer_add_simple_label(allie_Alice alice, void* ffs, @@ -95,7 +115,39 @@ extern "C" { U32 allie_Alice_load_r8g8b8a8_texture(allie_Alice alice, allie_SpanU8 file_path); U32 allie_Alice_load_r8_texture(allie_Alice alice, allie_SpanU8 file_path); allie_AliceAcknGenericMesh allie_Alice_add_generic_mesh(allie_Alice alice, - const allie_AliceGenericMeshTopology* topology, U32 diffuse_tex, U32 normal_tex, U32 specular_tex); + const allie_AliceGenericMeshTopology* topology, U32 diffuse_tex, U32 normal_tex, U32 specular_tex); + allie_AliceAcknShinyMesh allie_Alice_add_shiny_mesh(allie_Alice alice, const allie_AliceShinyMeshTopology* top); + ivec2 allie_Alice_get_wl_confirmed_win_sz(allie_Alice alice); + void allie_Alice_set_camera_info(allie_Alice alice, mat4 t_mat, vec3 cam_pos); + bool allie_Alice_is_wl_key_pressed(allie_Alice alice, xkb_keysym_t keysym); + void allie_Alice_resize_point_light_arr(allie_Alice alice, U32 new_count); + void allie_Alice_set_point_light(allie_Alice alice, U32 index, AlicePointLight data); +} + +struct AliceCallbacks { + std::function on_wl_pointer_button; + std::function on_wl_keyboard_key; + std::function on_another_frame; + std::function on_wl_pointer_motion; +}; + +/* A glue function for callbacks AliceCallbacks <-> allie_AliceCallbacks. + * Here guest is C++ object AliceCallbacks, yet we have to mask C++ std::function as + * C functions in allie_AliceCallbacks */ +void allie_AliceCallbacks_on_wl_pointer_button(void* guest, U32 button, U32 act) { + static_cast(guest)->on_wl_pointer_button(button, act); +} + +void allie_AliceCallbacks_on_wl_keyboard_key(void* guest, U32 keysym, U32 key) { + static_cast(guest)->on_wl_keyboard_key(keysym, key); +} + +void allie_AliceCallbacks_on_another_frame(void* guest, float fl) { + static_cast(guest)->on_another_frame(fl); +} + +void allie_AliceCallbacks_on_wl_pointer_motion(void* guest, vec2 pos) { + static_cast(guest)->on_wl_pointer_motion(pos); } struct AliceTextureSlot { @@ -106,12 +158,17 @@ struct AliceTextureSlot { struct Alice { ALLIE_CPP_OPAQUE(Alice) Alice() noexcept: opa(allie_Alice_new()){} - ~Alice() { - // todo: do something about it - } + ~Alice() = default; // todo: do something about it - void mainloop(const AliceCallbacks& callbacks) noexcept { - allie_Alice_mainloop(opa, &callbacks); + void mainloop(AliceCallbacks callbacks) noexcept { + allie_AliceCallbacks a_cb{ + .guest = &callbacks, + .on_wl_pointer_button = allie_AliceCallbacks_on_wl_pointer_button, + .on_wl_keyboard_key = allie_AliceCallbacks_on_wl_keyboard_key, + .on_another_frame = allie_AliceCallbacks_on_another_frame, + .on_wl_pointer_motion = allie_AliceCallbacks_on_wl_pointer_motion, + }; + allie_Alice_mainloop(opa, &a_cb); } LucyFace new_lucy_face (std::string_view path) noexcept { @@ -120,16 +177,12 @@ struct Alice { void lucy_cache_add_glyphs(std::vector&& reqs) { allie_VecLucyGlyphCachingRequest a_reqs{}; - a_reqs.len = a_reqs.capacity = reqs.size(); - a_reqs.buf = static_cast( safe_malloc(reqs.size() * sizeof(allie_LucyGlyphCachingRequest) )); - for (U64 i = 0; i < reqs.size(); i++) { - allie_LucyGlyphCachingRequest& a_r = a_reqs.buf[i]; - LucyGlyphCachingRequest& r = reqs[i]; - a_r.sized_face = r.sized_face.opa; - a_r.codepoint_ranges.len = a_r.codepoint_ranges.capacity = r.codepoint_ranges.size(); - a_r.codepoint_ranges.buf = static_cast(safe_malloc( r.codepoint_ranges.size() * sizeof(U32Segment) )); - memcpy(a_r.codepoint_ranges.buf, r.codepoint_ranges.data(), r.codepoint_ranges.size() * sizeof(U32Segment) ); - } + allie_form_a_cvec_of_non_primitive_type( + a_reqs, reqs, [](allie_LucyGlyphCachingRequest& a_r, const LucyGlyphCachingRequest& r) { + a_r.sized_face = r.sized_face.opa; + allie_form_a_cvec_of_matching_types( + a_r.codepoint_ranges, r.codepoint_ranges); + }); allie_Alice_lucy_cache_add_glyphs(opa, a_reqs); } @@ -140,17 +193,17 @@ struct Alice { color, additional_y_advance, allie_SpanU8(text), start_pos); } - AliceTextureSlot load_r8g8b8a8_texture(std::string_view path) { + AliceTextureSlot load_r8g8b8a8_texture(std::string_view path) noexcept { return {.id = allie_Alice_load_r8g8b8a8_texture(opa, allie_SpanU8(path))}; } - AliceTextureSlot load_r8_texture(std::string_view path) { + AliceTextureSlot load_r8_texture(std::string_view path) noexcept { return {.id = allie_Alice_load_r8_texture(opa, allie_SpanU8(path))}; } AliceAcknGenericMesh add_generic_mesh( const AliceGenericMeshTopology& top, - AliceTextureSlot& diffuse_tex, AliceTextureSlot& normal_tex, AliceTextureSlot& specular_tex) { + AliceTextureSlot& diffuse_tex, AliceTextureSlot& normal_tex, AliceTextureSlot& specular_tex) noexcept { allie_AliceGenericMeshTopology a_top{}; allie_form_a_cvec_of_matching_types(a_top.vertices, top.vertices); @@ -163,4 +216,34 @@ struct Alice { free(a_top.indexes.buf); return AliceAcknGenericMesh(hand); } + + AliceAcknShinyMesh add_shiny_mesh(const AliceShinyMeshTopology& top) noexcept { + allie_AliceShinyMeshTopology a_top{}; + allie_form_a_cvec_of_matching_types(a_top.vertices, top.vertices); + allie_form_a_cvec_of_matching_types(a_top.indexes, top.indexes); + allie_AliceAcknShinyMesh hand = allie_Alice_add_shiny_mesh(opa, &a_top); + free(a_top.vertices.buf); + free(a_top.indexes.buf); + return AliceAcknShinyMesh(hand); + } + + ivec2 get_wl_confirmed_win_sz() noexcept { + return allie_Alice_get_wl_confirmed_win_sz(opa); + } + + bool is_wl_pressed(xkb_keysym_t keysym) noexcept { + return allie_Alice_is_wl_key_pressed(opa, keysym); + } + + void set_camera_info(mat4 t_mat, vec3 cam_pos) noexcept { + allie_Alice_set_camera_info(opa, t_mat, cam_pos); + } + + void resize_point_light_arr(U32 new_count) { + allie_Alice_resize_point_light_arr(opa, new_count); + } + + void set_point_light(U32 index, AlicePointLight light) { + allie_Alice_set_point_light(opa, index, light); + } }; diff --git a/src/l2/allie_cpp/assets.hpp b/src/l2/allie_cpp/assets.hpp index f46f276..db725bb 100644 --- a/src/l2/allie_cpp/assets.hpp +++ b/src/l2/allie_cpp/assets.hpp @@ -1,9 +1,12 @@ #pragma once #include "../../../gen/l1/allie_cpp/geom.hpp" +#include "../../l1/allie_cpp/utils.hpp" #include #include +/* ============= Generic mesh asset ============================ */ + struct AliceGenericMeshVertexInc { vec3 pos; vec2 tex; @@ -15,12 +18,6 @@ struct allie_VecAliceGenericMeshVertexInc { U64 capacity; }; -struct allie_VecU32 { - U32* buf; - U64 len; - U64 capacity; -}; - struct allie_AliceGenericMeshTopology { allie_VecAliceGenericMeshVertexInc vertices; allie_VecU32 indexes; @@ -38,11 +35,10 @@ struct AliceGenericMeshTopology { AliceGenericMeshTopology alice_expect_read_generic_mesh_from_file(std::string_view file_path) { allie_AliceGenericMeshTopology a_top = allie_alice_expect_read_generic_mesh_from_file(allie_SpanU8(file_path)); AliceGenericMeshTopology top{}; - top.vertices.resize(a_top.vertices.len); - memcpy(top.vertices.data(), a_top.vertices.buf, a_top.vertices.len * sizeof(AliceGenericMeshVertexInc)); - - top.indexes.resize(a_top.indexes.len); - memcpy(top.indexes.data(), a_top.indexes.buf, a_top.indexes.len * sizeof(U32)); + allie_form_cpp_vector_of_matching_types(top.vertices, a_top.vertices); + allie_form_cpp_vector_of_matching_types(top.indexes, a_top.indexes); + free(a_top.vertices.buf); + free(a_top.indexes.buf); return top; } @@ -50,8 +46,64 @@ struct GenericMeshInstanceInc { mat4 model_t; }; +/* ============== Shiny mesh asset ===================== */ -// struct AliceShinyMeshVertexInc { -// vec3 pos; -// vec3 color; -// }; +struct AliceShinyMeshVertexInc { + vec3 pos; + cvec3 color; +}; + +struct allie_VecAliceShinyMeshVertexInc { + AliceShinyMeshVertexInc* buf; + U64 len; + U64 capacity; +}; + +struct allie_AliceShinyMeshTopology { + allie_VecAliceShinyMeshVertexInc vertices; + allie_VecU32 indexes; +}; + +extern "C" { + allie_AliceShinyMeshTopology allie_alice_expect_read_shiny_mesh_from_file(allie_SpanU8 file_path); +} + +struct AliceShinyMeshTopology { + std::vector vertices; + std::vector indexes; +}; + +AliceShinyMeshTopology alice_expect_read_shiny_mesh_from_file(std::string_view file_path) { + allie_AliceShinyMeshTopology a_top = allie_alice_expect_read_shiny_mesh_from_file(allie_SpanU8(file_path)); + AliceShinyMeshTopology top; + allie_form_cpp_vector_of_matching_types(top.vertices, a_top.vertices); + allie_form_cpp_vector_of_matching_types(top.indexes, a_top.indexes); + free(a_top.vertices.buf); + free(a_top.indexes.buf); + return top; +} + +struct AliceShinyMeshInstanceInc { + mat4 model_t; + vec3 color_on; +}; + +/* =========== Point light, spotlight assets ==========*/ + +struct AliceSpotlight { + vec3 pos; + char _padding_0[4]; + vec3 dir; + char _padding_1[4]; + vec3 color; + char _padding_2[4]; + float d; + char _padding_3[12]; +}; + +struct AlicePointLight { + vec3 pos; + char _padding_0[4]; + vec3 color; + char _padding_1[4]; +}; diff --git a/src/l2/allie_cpp/camera.hpp b/src/l2/allie_cpp/camera.hpp new file mode 100644 index 0000000..cb241eb --- /dev/null +++ b/src/l2/allie_cpp/camera.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include "graphics_geom.hpp" + +#include + +struct AliceCam { + float fov; + mat3 cam_basis; + vec3 pos; + + AliceCam(): + fov(1.5f), cam_basis(marie_simple_camera_rot_m_basis_in_cols(0, 0, 0)), pos({0, 0, 0}){} + + mat4 get_t_mat(ivec2 win_sz) { + mat4 projection_matrix = marie_perspective_projection_fov_mat4( + static_cast(win_sz.x), static_cast(win_sz.y), + fov, 0.01f, 1000); + mat4 camera_rotation_matrix = marie_mat3_to_mat4_transposed(cam_basis); + mat4 camera_translation_matrix = marie_translation_mat4(-pos); + return projection_matrix * camera_rotation_matrix * camera_translation_matrix; + } +}; + +struct AliceCamVerticalControl{ + AliceCam cam; + float sensitivity; + float pitch_cap; + + AliceCamVerticalControl(): cam(), sensitivity(0.5f * M_PIf / 180), pitch_cap(M_PIf * 0.49f) {} + + void update_direction(int win_width, int win_height, float pointer_x, float pointer_y) { + float yaw = (static_cast(win_width) / 2 - pointer_x) * sensitivity; + float pitch = std::clamp( + (static_cast(win_height) / 2 - pointer_y) * sensitivity, -pitch_cap, pitch_cap + ); + cam.cam_basis = marie_simple_camera_rot_m_basis_in_cols(yaw, pitch, 0); + } +}; \ No newline at end of file diff --git a/src/l2/allie_cpp/glue.c b/src/l2/allie_cpp/glue.c index cfd3ece..95e2169 100644 --- a/src/l2/allie_cpp/glue.c +++ b/src/l2/allie_cpp/glue.c @@ -36,6 +36,10 @@ GenericMeshTopology allie_alice_expect_read_generic_mesh_from_file(SpanU8 file_p return alice_expect_read_generic_mesh_from_file(VecU8_from_span(file_path)); } +ShinyMeshTopology allie_alice_expect_read_shiny_mesh_from_file(SpanU8 file_path) { + return alice_expect_read_shiny_mesh_from_file(VecU8_from_span(file_path)); +} + U32 allie_Alice_load_r8g8b8a8_texture(Alice* alice, SpanU8 file_path) { return Alice_load_r8g8b8a8_texture(alice, VecU8_from_span(file_path)); } @@ -57,3 +61,35 @@ void allie_AliceAcknGenericMesh_resize_instance_arr(AliceAcknGenericMesh self, U void allie_AliceAcknGenericMesh_set_inst(AliceAcknGenericMesh self, U64 index, GenericMeshInstanceInc uncomp) { AliceAcknGenericMesh_set_inst(self, index, uncomp); } + +AliceAcknShinyMesh allie_Alice_add_shiny_mesh(Alice* alice, const ShinyMeshTopology* top) { + return Alice_add_shiny_mesh(alice, top); +} + +void allie_AliceAcknShinyMesh_resize_instance_arr(AliceAcknShinyMesh self, U64 new_count) { + AliceAcknShinyMesh_resize_instance_arr(self, new_count); +} + +void allie_AliceAcknShinyMesh_set_inst(AliceAcknShinyMesh self, U64 index, ShinyMeshInstanceInc uncomp) { + AliceAcknShinyMesh_set_inst(self, index, uncomp); +} + +ivec2 allie_Alice_get_wl_confirmed_win_sz(Alice* alice) { + return Alice_get_wl_confirmed_win_sz(alice); +} + +void allie_Alice_set_camera_info(Alice* alice, mat4 projection_matrix, vec3 cam_pos) { + return Alice_set_camera_info(alice, projection_matrix, cam_pos); +} + +bool allie_Alice_is_wl_key_pressed(Alice* alice, xkb_keysym_t keysym) { + return Alice_is_wl_key_pressed(alice, keysym); +} + +void allie_Alice_resize_point_light_arr(Alice* alice, U32 new_count) { + Alice_resize_point_light_arr(alice, new_count); +} + +void allie_Alice_set_point_light(Alice* alice, U32 index, Pipeline0PointLight data) { + Alice_set_point_light(alice, index, data); +} \ No newline at end of file diff --git a/src/l2/anne/r4.h b/src/l2/anne/r4.h index 856a15d..55e9b2f 100644 --- a/src/l2/anne/r4.h +++ b/src/l2/anne/r4.h @@ -582,7 +582,7 @@ void r4_asset_gen_generic_mesh_one_fourth_of_a_cylinder(float s_resol, float w, U32 quad_to_triangles_conv_arr[6] = {0, 1, 2, 0, 2, 3}; -ShinyMeshTopology generate_shiny_cube(vec3 color) { +ShinyMeshTopology generate_shiny_cube(cvec3 color) { ShinyMeshVertexInc vert[24] = { {{+1, +1, +1}, color}, {{+1, -1, +1}, color}, @@ -790,7 +790,7 @@ void r4_asset_gen_generic_mesh_quad(float width, float length, VecU8 path_to_sav /* a is r at bottom, b is r on top. y is in [0, height]. Shape is symmetrical from Oy */ ShinyMeshTopology generate_shiny_lamp(float height, float a, float b){ - vec3 d_clr = {0.1f, 0.1f, 0.2f}; + cvec3 d_clr = {25, 25, 50}; ShinyMeshVertexInc vert[24] = { {{+b, height, +b}, d_clr}, {{+b, 0, +b}, d_clr}, @@ -896,7 +896,7 @@ int gen_assets_for_r4() { mkdir_nofail("l2/textures"); mkdir_nofail("l2/textures/r4"); r4_asset_gen_generic_mesh_one_fourth_of_a_cylinder_2(10, 2, 6); - alice_write_shiny_mesh_to_file(generate_shiny_cube((vec3){0.6f, 0.6f, 0.7f}), vcstr("l2/models/cube.AliceShinyMesh")); + alice_write_shiny_mesh_to_file(generate_shiny_cube((cvec3){150, 150, 180}), vcstr("l2/models/cube.AliceShinyMesh")); alice_write_shiny_mesh_to_file(generate_shiny_lamp(0.3f, 0.13f, 0.19f), vcstr("l2/models/lamp.AliceShinyMesh")); r4_asset_gen_generic_mesh_quad(10, 10, vcstr("l2/models/quad.AliceGenericMesh")); r4_asset_gen_generic_mesh_cylinder(200, 0.4f, 0.17f, 30, vcstr("l2/models/puck.AliceGenericMesh"), diff --git a/src/l3/r4/r4.c b/src/l3/r4/r4.c index b567aab..3f283c6 100644 --- a/src/l3/r4/r4.c +++ b/src/l3/r4/r4.c @@ -17,7 +17,6 @@ float quad_form3_mul_vec(quad_form3_t Q, vec3 M) { 2 * (M.x * M.y * Q.x.y + M.x * M.z * Q.x.z + M.y * M.x * Q.y.z); } - typedef struct{ vec3 pos; vec3 color; @@ -237,14 +236,8 @@ void main_h_on_wl_pointer_motion(void* data, vec2 pos) { AliceCamVerticalControl_update_direction(&st->cam, win_sz.x, win_sz.y, pos.x, pos.y); } -// todo: fuck, return this to Alice. Frustum config is needed in uh, later it will be needed mat4 get_t_mat(R4BetaState* st) { - ivec2 win_sz = Alice_get_wl_confirmed_win_sz(st->alice); - mat4 projection_matrix = marie_perspective_projection_fov_mat4( - (float)win_sz.x, (float)win_sz.y, st->cam.cam.fov, 0.01f, 1000); - mat4 camera_rotation_matrix = marie_mat3_to_mat4_transposed(st->cam.cam.cam_basis); - mat4 camera_translation_matrix = marie_translation_mat4(vec3_minus(st->cam.cam.pos)); - return mat4_mul_mat4(projection_matrix, mat4_mul_mat4(camera_rotation_matrix, camera_translation_matrix)); + return AliceCam_get_t_mat(&st->cam.cam, Alice_get_wl_confirmed_win_sz(st->alice)); } void main_h_on_another_frame(void* data, float fl){ @@ -253,22 +246,22 @@ void main_h_on_another_frame(void* data, float fl){ vec3 proj_back = project_dir_onto_plane_xz(st->cam.cam.cam_basis.z); vec3 proj_right = project_dir_onto_plane_xz(st->cam.cam.cam_basis.x); const float max_speed = 10.f; - if (alice->wl.first_0x80_keys['w']) { + if (Alice_is_wl_key_pressed(alice, XKB_KEY_w)) { st->hero_pos = vec3_minus_vec3(st->hero_pos, vec3_mul_scal(proj_back, fl * max_speed)); } - if (alice->wl.first_0x80_keys['s']) { + if (Alice_is_wl_key_pressed(alice, XKB_KEY_s)) { st->hero_pos = vec3_add_vec3(st->hero_pos, vec3_mul_scal(proj_back, fl * max_speed)); } - if (alice->wl.first_0x80_keys['a']) { + if (Alice_is_wl_key_pressed(alice, XKB_KEY_a)) { st->hero_pos = vec3_minus_vec3(st->hero_pos, vec3_mul_scal(proj_right, fl * max_speed)); } - if (alice->wl.first_0x80_keys['d']) { + if (Alice_is_wl_key_pressed(alice, XKB_KEY_d)) { st->hero_pos = vec3_add_vec3(st->hero_pos, vec3_mul_scal(proj_right, fl * max_speed)); } - if (alice->wl.first_0x80_keys['e']) { + if (Alice_is_wl_key_pressed(alice, XKB_KEY_e)) { st->hero_pos = vec3_add_vec3(st->hero_pos, (vec3){0, max_speed * fl, 0}); } - if (alice->wl.first_0x80_keys['q']) { + if (Alice_is_wl_key_pressed(alice, XKB_KEY_q)) { st->hero_pos = vec3_add_vec3(st->hero_pos, (vec3){0, -max_speed * fl, 0}); } st->cam.cam.pos = st->hero_pos; @@ -283,7 +276,7 @@ void main_h_on_another_frame(void* data, float fl){ // assert(pipeline_0_ubo_point_light_max_count >= st->LS_state.len); if (st->LS_state.len > alice->pipeline0_light_conf.point_lights.count) { - Alice_set_point_light_count(alice, st->LS_state.len); + Alice_resize_point_light_arr(alice, st->LS_state.len); } if (st->LS_state.len + st->bullets_stuck_on_ROA.len > st->LS_mesh->el.instance_attr.count) { AliceAcknShinyMesh_resize_instance_arr(st->LS_mesh, st->LS_state.len + st->bullets_stuck_on_ROA.len); diff --git a/src/l3/r4/r4c.cpp b/src/l3/r4/r4c.cpp index 1d48e77..1ead836 100644 --- a/src/l3/r4/r4c.cpp +++ b/src/l3/r4/r4c.cpp @@ -2,7 +2,7 @@ #include "../../../gen/l1/allie_cpp/geom.hpp" #include #include -#include "../../l2/allie_cpp/graphics_geom.hpp" +#include "../../l2/allie_cpp/camera.hpp" template using unique = std::unique_ptr; @@ -15,6 +15,8 @@ struct R4C_State { unique alice; unique face; unique sized_face; + AliceCamVerticalControl cam; + vec3 hero_pos; AppMem app; }; @@ -23,19 +25,35 @@ vec4 funky_color(float time) { return vec4{(sinf(0.95f * time + 0.6f) + 1) / 2, (sinf(0.233f * time + 0.2f) + 1) / 2, (sinf(1.433f * time) + 1) / 2, 1}; } -void main_h_on_another_frame(void* data, float fl) { - R4C_State& st = *(R4C_State*)data; +vec3 project_dir_onto_plane_xz(vec3 v){ + vec2 xz = normalize(vec2(v.x, v.z)); + return (vec3){xz.x, 0, xz.y}; +} + +void update_state(R4C_State& st, float fl) { Alice& alice = *st.alice; - LucyFaceFixedSize& ffs = *st.sized_face; - st.app.gr += fl; - alice.lucy_renderer_add_simple_label(ffs, funky_color(st.app.gr), 0, "Hello from C++", ivec2(500, 50)); -} - -void main_h_on_wl_keyboard_key(void* data, U32 keysym, U32 act) { - -} - -void main_h_on_wl_pointer_button(void* data, U32 button, U32 act) { + vec3 proj_back = project_dir_onto_plane_xz(st.cam.cam.cam_basis.z); + vec3 proj_right = project_dir_onto_plane_xz(st.cam.cam.cam_basis.x); + const float max_speed = 10.f; + if (alice.is_wl_pressed(XKB_KEY_w)) { + st.hero_pos = st.hero_pos - proj_back * fl * max_speed; + } + if (alice.is_wl_pressed(XKB_KEY_s)) { + st.hero_pos = st.hero_pos + proj_back * fl * max_speed; + } + if (alice.is_wl_pressed(XKB_KEY_a)) { + st.hero_pos = st.hero_pos - proj_right * fl * max_speed; + } + if (alice.is_wl_pressed(XKB_KEY_d)) { + st.hero_pos = st.hero_pos + proj_right * fl * max_speed; + } + if (alice.is_wl_pressed(XKB_KEY_e)) { + st.hero_pos = st.hero_pos + vec3(0, fl * max_speed, 0); + } + if (alice.is_wl_pressed(XKB_KEY_q)) { + st.hero_pos = st.hero_pos - vec3(0, fl * max_speed, 0); + } + st.cam.cam.pos = st.hero_pos; } @@ -52,11 +70,57 @@ int main() { AliceAcknGenericMesh ROA_mesh = st.alice->add_generic_mesh(ROA_topology, ROA_diffuse_tex_slot, ROA_normal_tex_slot, ROA_specular_tex_slot); ROA_mesh.resize_instance_arr(1); ROA_mesh.set_inst(0, {.model_t = marie_translation_mat4({0.2, -0.4, 0.6})}); - st.alice->mainloop({ - .guest = &st, - .on_wl_pointer_button = main_h_on_wl_pointer_button, - .on_wl_keyboard_key = main_h_on_wl_keyboard_key, - .on_another_frame = main_h_on_another_frame, + AliceShinyMeshTopology CUBE_topology = alice_expect_read_shiny_mesh_from_file("./gen/l2/models/cube.AliceShinyMesh"); + AliceAcknShinyMesh CUBE_mesh = st.alice->add_shiny_mesh(CUBE_topology); + CUBE_mesh.resize_instance_arr(50); + st.alice->resize_point_light_arr(50); + + for (U32 i = 0; i < 50; i++) { + vec4 clr = funky_color((float)i); + vec3 color = {clr.x, clr.y, clr.z}; + vec3 pos = {sinf((float)i * 1.4f) * 4, sinf((float)i * 0.5444f) * 2, sinf((float)i * 0.977f) * 5}; + CUBE_mesh.set_inst(i, AliceShinyMeshInstanceInc{ + .model_t = marie_translation_mat4(pos) * marie_3d_scal_mat4(0.1f), + .color_on = color, + }); + st.alice->set_point_light(i, AlicePointLight{ + .pos = pos, + .color = color, + }); + } + + st.hero_pos = vec3(0, 2, 0); + + + auto on_wl_pointer_button = [&](U32 button, U32 act) { + + }; + + auto on_wl_keyboard_key = [&](U32 keysym, U32 act) { + + }; + + auto on_another_frame = [&](float fl) { + Alice& alice = *st.alice; + + update_state(st, fl); + alice.set_camera_info(st.cam.cam.get_t_mat(alice.get_wl_confirmed_win_sz()), st.cam.cam.pos); + + LucyFaceFixedSize& ffs = *st.sized_face; + st.app.gr += fl; + alice.lucy_renderer_add_simple_label(ffs, funky_color(st.app.gr), 0, "Hello from C++", ivec2(500, 50)); + }; + + auto on_wl_pointer_motion = [&](vec2 pos) { + ivec2 win_sz = st.alice->get_wl_confirmed_win_sz(); + st.cam.update_direction(win_sz.x, win_sz.y, pos.x, pos.y); + }; + + st.alice->mainloop(AliceCallbacks{ + .on_wl_pointer_button = on_wl_pointer_button, + .on_wl_keyboard_key = on_wl_keyboard_key, + .on_another_frame = on_another_frame, + .on_wl_pointer_motion = on_wl_pointer_motion, }); return 0; } \ No newline at end of file