From 8bc8bdf0c5e842482fd02be79541aca1dafdcdc7 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 4 Aug 2023 14:32:05 +0900 Subject: [PATCH] Test using a textured cube --- Makefile | 2 +- res/test_cube.obj | 44 ++++++++++++++++++++++++++++++++ res/test_cube_texture.png | Bin 0 -> 1197 bytes src/screen_trunner.cc | 51 ++++++++++++++++++++++++++++++++------ src/screen_trunner.h | 12 ++++++--- wasm_build/Makefile | 3 ++- 6 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 res/test_cube.obj create mode 100644 res/test_cube_texture.png diff --git a/Makefile b/Makefile index c9e49f8..b1ea627 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ else COMMON_FLAGS = -DNDEBUG -O3 endif -CXX_FLAGS = -Wall -Wextra -Wpedantic -Weffc++ ${COMMON_FLAGS} +CXX_FLAGS = -Wall -Wextra -Wpedantic -Weffc++ ${COMMON_FLAGS} -std=c++20 LINKER_FLAGS = -lraylib OBJDIR = objdir diff --git a/res/test_cube.obj b/res/test_cube.obj new file mode 100644 index 0000000..953752c --- /dev/null +++ b/res/test_cube.obj @@ -0,0 +1,44 @@ +# Blender 3.6.0 +# www.blender.org +o Cube +v -1.000000 1.000000 1.000000 +v -1.000000 -1.000000 1.000000 +v -1.000000 1.000000 -1.000000 +v -1.000000 -1.000000 -1.000000 +v 1.000000 1.000000 1.000000 +v 1.000000 -1.000000 1.000000 +v 1.000000 1.000000 -1.000000 +v 1.000000 -1.000000 -1.000000 +vn -0.0000 1.0000 -0.0000 +vn -0.0000 -0.0000 -1.0000 +vn 1.0000 -0.0000 -0.0000 +vn -0.0000 -1.0000 -0.0000 +vn -1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 1.0000 +vt 0.875000 0.500000 +vt 0.625000 0.750000 +vt 0.625000 0.500000 +vt 0.375000 1.000000 +vt 0.375000 0.750000 +vt 0.625000 0.000000 +vt 0.375000 0.250000 +vt 0.375000 0.000000 +vt 0.375000 0.500000 +vt 0.125000 0.750000 +vt 0.125000 0.500000 +vt 0.625000 0.250000 +vt 0.875000 0.750000 +vt 0.625000 1.000000 +s 0 +f 5/1/1 3/2/1 1/3/1 +f 3/2/2 8/4/2 4/5/2 +f 7/6/3 6/7/3 8/8/3 +f 2/9/4 8/10/4 6/11/4 +f 1/3/5 4/5/5 2/9/5 +f 5/12/6 2/9/6 6/7/6 +f 5/1/1 7/13/1 3/2/1 +f 3/2/2 7/14/2 8/4/2 +f 7/6/3 5/12/3 6/7/3 +f 2/9/4 4/5/4 8/10/4 +f 1/3/5 3/2/5 4/5/5 +f 5/12/6 1/3/6 2/9/6 diff --git a/res/test_cube_texture.png b/res/test_cube_texture.png new file mode 100644 index 0000000000000000000000000000000000000000..d4121d41487e437b39d30f9c2341083e18a7960e GIT binary patch literal 1197 zcmV;e1XBBnP)_010nKMsolF000000002mDz?r5000SaNLh0L01FZT01FZU(%pXi000CCNkl#-cSIW7L{OV;MxDrkqax|F zR}Cd{CUj|Z47hf)u!=D7>1p8Wp29PYkv@RAIOw)g6Ka0^6!35kLQa#tl0JZq>wr+% zLjAY@1s+VnCf_A{C4B&mPXe!ZEYz<+1udB5wPdfP4}eDS(E%V_UTUNDS)i&i1EBQ9 z(ldPkN?*$+;PC#;7sD`+UQ>TL+NE9Ylig@R3^;_@odjlbsjlW{Qb3g6P~`Bz zxwF93To89qgX^frY%m1bbQu$`euclRXr>lqrL(_77{(acjTQhY2NUKN9}?kZ{$fW=cO^^hV)PF`)X{8Q?B! zP6}n1LKX$#E?bC67zsOsh!w^s?^d8KY+@TWqlNr4`cX-MyU>9*@dnnAUCAvl^3PP@ zJ#a*l`3LOdago!XJF;~{LY}Bf$P2BG{M0AnP%ckNu9zgp$ToQ?GSo{~WJ^6%OKiG^ zu2(f-Ipn$pk5s4(o3jw(3Ppq5@u%4TuQWeWNmjrg0L;KPPcYqvsl{OAcd#Agn9-|A zMX~OErLY1BMz}zwRvHfaIRD$bf+9&@0C*Hq1=C`l6p-2Zna;o$0E6efiM_O=TyAm` z8rZzw0g3_o%YuxC6x|->=pVKw0#L_bp#Wt>Jp3#JKwi7bVtvE;)xNeMznCuI1=YWC zrN?I)@>IMBBzxK4W;j02_8hh8XQ8@b>yyL#!Sp_n@d~z8A`|?NlIW$LrK_&1AzxdN z8sM?~9luyz7DzHpZ(Lx{BU_hz0kD6knd1K*41>UWhN6!R^##D<4crJ7zEv3yb#d stack) camera{Vector3{0.0F, 0.0F, 0.0F}, Vector3{0.0F, 0.0F, -1.0F}, Vector3{0.0F, 1.0F, 0.0F}, 80.0F, CAMERA_PERSPECTIVE}, flags(), - TEMP_cube(GenMeshCube(2.0F, 2.0F, 2.0F)), - TEMP_default_material(LoadMaterialDefault()), + TEMP_cube_model(LoadModel("res/test_cube.obj")), + TEMP_cube_texture(LoadTexture("res/test_cube_texture.png")), TEMP_matrix(get_identity_matrix()), - TEMP_offset_matrix(translate_matrix_z(-4.0F)), - TEMP_value(0.0F) {} + TEMP_permanent_matrix(get_identity_matrix()), + TEMP_value(0.0F), + TEMP_current_state(0) { + TEMP_cube_model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = + TEMP_cube_texture; +} TRunnerScreen::~TRunnerScreen() { - UnloadMesh(TEMP_cube); - UnloadMaterial(TEMP_default_material); + UnloadTexture(TEMP_cube_texture); + UnloadModel(TEMP_cube_model); } bool TRunnerScreen::update(float dt) { @@ -30,10 +34,31 @@ bool TRunnerScreen::update(float dt) { if (TEMP_value < PI / 2.0F) { TEMP_matrix = get_rotation_matrix_about_z(TEMP_value); } else if (TEMP_value < PI) { + if (TEMP_current_state == 0) { + TEMP_current_state = 1; + TEMP_permanent_matrix = + TEMP_permanent_matrix * get_rotation_matrix_about_z(PI / 2.0F); + } else if (TEMP_current_state != 1) { + assert(!"unreachable"); + } TEMP_matrix = get_rotation_matrix_about_y(TEMP_value - PI / 2.0F); } else if (TEMP_value < PI * 3.0F / 2.0F) { + if (TEMP_current_state == 1) { + TEMP_current_state = 2; + TEMP_permanent_matrix = + TEMP_permanent_matrix * get_rotation_matrix_about_y(PI / 2.0F); + } else if (TEMP_current_state != 2) { + assert(!"unreachable"); + } TEMP_matrix = get_rotation_matrix_about_x(TEMP_value - PI); } else { + if (TEMP_current_state == 2) { + TEMP_current_state = 0; + TEMP_permanent_matrix = + TEMP_permanent_matrix * get_rotation_matrix_about_x(PI / 2.0F); + } else { + assert(!"unreachable"); + } TEMP_value -= PI * 3.0F / 2.0F; TEMP_matrix = get_rotation_matrix_about_z(TEMP_value); } @@ -45,7 +70,19 @@ bool TRunnerScreen::draw() { ClearBackground(PixelToColor(Pixel::PIXEL_SKY)); BeginMode3D(camera); - DrawMesh(TEMP_cube, TEMP_default_material, TEMP_matrix * TEMP_offset_matrix); + // DrawMesh(TEMP_cube, TEMP_default_material, TEMP_matrix * + // TEMP_offset_matrix); + DrawModel(Model{.transform = TEMP_cube_model.transform * + TEMP_permanent_matrix * TEMP_matrix, + .meshCount = TEMP_cube_model.meshCount, + .materialCount = TEMP_cube_model.materialCount, + .meshes = TEMP_cube_model.meshes, + .materials = TEMP_cube_model.materials, + .meshMaterial = TEMP_cube_model.meshMaterial, + .boneCount = TEMP_cube_model.boneCount, + .bones = TEMP_cube_model.bones, + .bindPose = TEMP_cube_model.bindPose}, + Vector3{0.0F, 0.0F, -4.0F}, 1.0F, WHITE); EndMode3D(); EndDrawing(); diff --git a/src/screen_trunner.h b/src/screen_trunner.h index 0bea606..93f6a75 100644 --- a/src/screen_trunner.h +++ b/src/screen_trunner.h @@ -41,11 +41,17 @@ class TRunnerScreen : public Screen { const Camera3D camera; std::bitset<64> flags; - Mesh TEMP_cube; - Material TEMP_default_material; + Model TEMP_cube_model; + Texture2D TEMP_cube_texture; Matrix TEMP_matrix; - Matrix TEMP_offset_matrix; + Matrix TEMP_permanent_matrix; float TEMP_value; + /* + * 0 - rotating about z + * 1 - rotating about y + * 2 - rotating about x + */ + int TEMP_current_state; }; #endif diff --git a/wasm_build/Makefile b/wasm_build/Makefile index af00916..2634161 100644 --- a/wasm_build/Makefile +++ b/wasm_build/Makefile @@ -27,11 +27,12 @@ CXX = source ${HOME}/git/emsdk/emsdk_env.sh && em++ all: | format jumpartifact.com_demo_0.html jumpartifact.com_demo_0.html: ${SOURCES} ${HEADERS} - ${CXX} -o jumpartifact.com_demo_0.html \ + ${CXX} -std=c++20 -o jumpartifact.com_demo_0.html \ -s USE_GLFW=3 -I../wasm_include -L../wasm_lib -lraylib \ --shell-file custom_shell.html \ -sEXPORTED_FUNCTIONS=_main \ -sEXPORTED_RUNTIME_METHODS=ccall \ + --preload-file ../res \ ${OTHER_FLAGS} \ ${SOURCES}